JOIN
 Revision History
 Select a Forum     Round Tables New Member Discussions News Discussions Algorithm Matches Marathon Matches NASA Tournament Lab TopCoder Cookbook High School Matches Sponsor Discussions Development Forums Design Forums Search My Post History  |  My Watches  |  User Settings Forums TopCoder Cookbook Algorithm Competitions - Rewriting Phase Re: Iterating Over All Permutations of an Array Revision History (2 edits)
 Re: Iterating Over All Permutations of an Array (response to post by Ferlon) Discussion“Objects supporting comparison” may be not only numbers, but characters, strings, etc. – any objects for those operation of comparison is defined.And there are some examples of getting next permutation:3 2 4 3 1 → 3 3 1 2 41 1 2 2 → 1 2 1 2And getting first permutation from the last one:5 4 3 2 1 → 1 2 3 4 5 Using these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with operators >= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one. But you should make sure of you really iterate over all permutations. In particular, when iterating from the first one to the last one it means that you should sort your array lexicographically beforehand. And if somebody in your room misses this point, it may be good time for you to challenge her solution quickly.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and i (with minimal possible aj under these constraints), then swap ai and aj and reorder all elements with indices more than i non-decreasing.And our my_next_permutation does exactly this. First of all, we go from the last element to find the first such element that has greater right neighbor – and this is ai. We see that the sequence of elements with indices greater than i is ordered non-increasing. Then we find the minimal element with index j>i such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j, of course), new sequence of elements with indices greater than i is still ordered non-increasing, so we should just reverse it to obtain non-decreasing sequence.Another question may be: What is the time complexity of algorithm? Answer is T(n)=A(n)*B(n), where T(n) is time complexity of whole algorithm of iterating all permutations, A(n) is an estimate of number of permutations, and B(n) is time complexity of getting next permutation. Of course, if we have an array with all elements distinct, A(n)=n!, and if there are k distinct groups of equal elements with size Si of i-th group, an answer should be n!/(S1!*…*Si!*…*Sk!). And B(n)=O(n), as we can see easily, because there are only three non-nested cycles in function my_next_permutation, and each of them run along the some part of array of length n. So if there are no equal elements in the array, time complexity of whole algorithm is O(n!*n).But, to be honest, that’s not the best asymptotical estimate for this algorithm. If we examine it more deeply, we recognize that two last elements are inspected at every iteration, but the third one – once per 2 times, the fourth one – once per 6 times, and so on. So the time complexity is O(n!+n!+n!/2+n!/6+n!/24+n!/120+...) = O(e*n!) = O(n!).Such an estimation of time complexity shows that this algorithm can be used only for small n, about 10 or 12. It is useful when you want to gather something information about every permutation and you can’t think of method giving this information without straightforward searching over all permutations. So our algorithm is some kind of brute force search. When there is a greater constraint on n, you should use something cleverer. On the other hand, if n is even lesser you can use even simpler recursive algorithm, in which at every recursion step (which is determined by current position, for which you choose an element to put in) you should iterate over all elements which haven’t been used in the prefix of the permutation and process the whole permutation every time you reach the last position.
 Re: Iterating Over All Permutations of an Array (response to post by Ferlon) Discussion“Objects supporting comparison” may be not only numbers, but characters, strings, etc. – any objects for those operation of comparison is defined.And there are some examples of getting next permutation:3 2 4 3 1 → 3 3 1 2 41 1 2 2 → 1 2 1 2And getting first permutation from the last one:5 4 3 2 1 → 1 2 3 4 5 Using these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with operators >= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one. But you should make sure of you really iterate over all permutations. In particular, when iterating from the first one to the last one it means that you should sort your array lexicographically beforehand. And if somebody in your room misses this point, it may be good time for you to challenge her solution quickly.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j
 Re: Iterating Over All Permutations of an Array (response to post by Ferlon) Discussion“Objects supporting comparison” may be not only numbers, but characters, strings, etc. – any objects for those operation of comparison is defined.And there are some examples of getting next permutation:3 2 4 3 1 → 3 3 1 2 41 1 2 2 → 1 2 1 2And getting first permutation from the last one:5 4 3 2 1 → 1 2 3 4 5 Using these tricks, you may not only generate all permutations from the first one to the last one in increasing order. Of course, you may also generate them from the last one to the first one in decreasing order – you should just generate the last permutation by reversing the first one and write function my_prev_permutation, which should be a twin to my_next_permutation with operators >= comparing array elements turned into <=. You can also generate all permutations starting from and stopping at not only first or last, but any arbitrary one. But you should make sure of you really iterate over all permutations. In particular, when iterating from the first one to the last one it means that you should sort your array lexicographically beforehand. And if somebody in your room misses this point, it may be good time for you to challenge her solution quickly.But an interesting question may be: Why does my_next_permutation get exactly lexicographically next permutation from a given one? Let’s examine it intently. To generate the next permutation we should find such an element ai (with maximal possible i) that can be replaced by such an element aj that ai< aj and ii such that aj>ai and swap these two elements. Because old ai was greater than or equal to aj+1 (if j=0 && a[i]>=a[i+1]) --i; if (i<0) { for (j=0,k=n-1; j=a[j]) --j; temp=a[i]; a[i]=a[j]; a[j]=temp; for (j=i+1,k=n-1; j #include #include using namespace std; class TheLuckyString { public: int count (string a) { int i; int ans=0; sort(a.begin(),a.end()); reverse(a.begin(),a.end()); do { for (i=0; i #include #include using namespace std; class MatrixGame { vector ans; vector now; public: vector getMinimal (vector a) { int i,j; vector v; ans=now=a; for (i=0; i and permutate such pairs, or also create an array of indices.