For the permutations given by N and k , I have a function that finds the k permutation of N in lexicographic order. Also, with perm permutation, I have a function that finds the lexicographic permutation index among all permutations of N For this, I used "factorial decomposition" as suggested in this answer .
Now I want to do the same for whole sections of N For example, for N=7 I want to be able to move between the index (left) and the section (right):
0 ( 7 ) 1 ( 6 1 ) 2 ( 5 2 ) 3 ( 5 1 1 ) 4 ( 4 3 ) 5 ( 4 2 1 ) 6 ( 4 1 1 1 ) 7 ( 3 3 1 ) 8 ( 3 2 2 ) 9 ( 3 2 1 1 ) 10 ( 3 1 1 1 1 ) 11 ( 2 2 2 1 ) 12 ( 2 2 1 1 1 ) 13 ( 2 1 1 1 1 1 ) 14 ( 1 1 1 1 1 1 1 )
I have tried several things. The best I came up with was
sum = 0; for (int i=0; i<length; ++i) sum += part[i]*i; return sum;
which gives the following:
0 0( 7 ) 1 1( 6 1 ) 2 2( 5 2 ) 3 3( 5 1 1 ) 3 4( 4 3 ) 4 5( 4 2 1 ) 6 6( 4 1 1 1 ) 5 7( 3 3 1 ) 6 8( 3 2 2 ) 7 9( 3 2 1 1 ) 10 10( 3 1 1 1 1 ) 9 11( 2 2 2 1 ) 11 12( 2 2 1 1 1 ) 15 13( 2 1 1 1 1 1 ) 21 14( 1 1 1 1 1 1 1 )
This doesn't quite work, but it seems to be on the right track. I came up with this because it calculates how many times I need to move the number down (for example, 6,3,2 goes to 6,3,1,1 ). I canโt figure out how to fix this, because I donโt know how to take into account when things need to be recombined (for example, 6,3,1,1 goes into 6,2,2 ).
c algorithm lexicographic integer-partition
Daniel
source share