It is based on the general algorithm in this paper by Sawada - for an explanation of how it works, see that paper. The following code prints out the sequences you are after, and does so in lexical order and in constant amortised time. The set of all bracelets allows for the number of A's and B's to vary. What you are after is a subset of 2-ary bracelets (the subset is defined by exactly n of character A and m of character B). Subtract this gain from the length of the permutation and you get the index at which you can start inserting new elements without making duplicates. So if you want to calculate the permutations for N=3 and M=3 and you have the 10 permutations for N=2 and M=3, their gains will be. You can then use Data/Remove Duplicates to generate a list of non-duplicates, and count these. This will list all 11x10x9 990 permutations, including duplicates. Use an algorithm like the VBA provided previously. This number starts at M 1 for the first old permutation and decreases by one for each old permutation until it would become zero, at which point it goes back to M, etc. If you want to count the number of permutations if you select three letters, say, you can do it two ways: 1. I don't know why this works, but you can calculate the number of new possibilities that you'll generate from an old one (I call this 'gain'). You can't just insert everywhere though, because then you'll get duplicates. If you have all permutations for N-1 and M, then you can use them to find the permutations for N and M by inserting e1 into them.Gain = number of new possibilities we'll get for this smaller possibility*Īdd possiblity with e1 inserted in position i Possibilities = genPossibilities(n-1, m, e1, e2) If anyone knows how to do it, and preferably also how to calculate how many possibilities there will be, that would help me a lot! I would prefer an explanation, but code is also fine (I can read C-like languages, Java(Script), Python, Ruby, Lisp/Scheme).įor the interested: Here is the algorithm for getting only unique permutations that I have so far: function genPossibilities(n, m, e1, e2) The second restriction will only cut the number of possiblities in half (in the best case), but I think the third should also be able to really cut down the number of possibilities. I have already done the first restriction (see below) and it cut back the number from 2^K for Matlab's normal permutations function (perms) to K!/N!M!, which is a huge win. Since K will ideally be large, it is not feasible to first generate all permutations and then filter them according to these criteria. If I would be able to do this, the number of possibilities would be decreased drastically. I consider the permutations to be circular, so =.I don't need the reverse of any permutation (i.e. ![]() Real problem: The search space should contain all permutations of two elements (N times el1 and M times el2, where K=M N), with these restrictions: ![]() If a FLORIDA LOTTO player matches three, four, or five winning numbers. For any number K there are normally K! permutations, and generating them all will be hard for numbers higher than ~10. For a chance to win cash prizes in an additional drawing held immediately after. Furthermore, the more elements I can do this for, the better. Since the evaluation actually takes some time I would prefer to generate as little as possible solutions that completely cover my search space. In order to do this, I need to generate and evaluate all possibilities to find out which is best. Now we move on to the next cycle, looking for 5, which doesn't appear.Some background: I'm writing a more or less brute force search algorithm for solving a problem that I have. \) does not contain 1 thus we move on to the second.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |