Question:
Index a permutation - tough problem?? please help...!?
seasnowsky
2007-02-18 21:31:40 UTC
I need to index a permutation of 6 objects, and I need a relatively closed form expression (not a recursive function).
i.e. here is the first few permutations, and their associated index:
Index .....Permutation
1.............123456
2.............123465
3.............123546
4.............123564
5.............124356
etc.
I need a formula or algorithm that for a given index it will return the permutation.
So, in the example, for an index 4, the algorithm/function will return a '1' for the first digit, a '2' for the second, '3' for third, '5' for fourth etc.
Note: it need not follow the above example pattern - it just has to be such that all possible permutations are uniquely indexable.
Three answers:
Phineas Bogg
2007-02-18 22:19:06 UTC
This is a little messy, but I think it works. I will show how to transform a permutation to an index and then describe how to go the other way.



Let's number the positions in the permutation as follows and start the current index at 1:



_ _ _ _ _ _

5 4 3 2 1 0



Current Index = 1

(I am going to start with current index = 1, so the smallest index will be 1. We will have to remember to subtract 1 when we want to convert an index back to a permutation).



(Let's hope Yahoo! does not mess up my formatting).





Now pick a position for the 6 and add (position number)*(6-1)! =

(position number)*120 to the index. For example, let's put 6 in position 3. Now we let's number the remaining places as follows:



_ _ 6 _ _ _

4 3 _ 2 1 0



Current Index = 1 + 360 = 361



Now pick a position for the 5 and add (position number)*(5-1)! =

(position number)*24 to the index. For example, let's put 5 in position 0. Now we let's number the remaining places as follows:



_ _ 6 _ _ 5

3 2 _ 1 0 _



Current Index = 360 + 0*24 = 361



Now pick a position for the 4 and add (position number)*(4-1)! =

(position number)*6 to the index. For example, let's put 4 in position 3. Now we let's number the remaining places as follows:



4 _ 6 _ _ 5

_ 2 _ 1 0 _



Current Index = 361 + 3*6 = 379



Now pick a position for the 3 and add (position number)*(3-1)! =

(position number)*2 to the index. For example, let's put 3 in position 1. Now we let's number the remaining places as follows:



4 _ 6 3 _ 5

_ 1 _ _ 0 _



Current Index = 379 + 1*2 = 381



Now pick a position for the 2 and add (position number)*(2-1)! =

(position number)*1 to the index. For example, let's put 2 in position 1. Now we let's number the remaining places as follows:



4 2 6 3 _ 5

_ _ _ _ 0 _



Current Index = 381 + 1 = 382



Now there is only one position left so 1 goes there and we have the permutation 4 2 6 3 1 5 and index 382.



We can now reverse these steps, by taking the index 382 and subtracting 1 to get 381. Then we divide by 5! to get 3 remainder 21. That tells us to put 6 in position 3 so we have:



_ _ 6 _ _ _



Now we use the remainder, 21, to place the rest of the numbers. Dividing 21 by 4!, we get 0 remainder 21, so 5 goes in spot 0 and we have:



_ _ 6 _ _ 5



Now we use the remainder, 21, to place the rest of the numbers. Dividing 21 by 3!, we get 3 remainder 3, so 4 goes in spot 3 and we have:



4 _ 6 _ _ 5



Now we use the remainder, 3, to place the rest of the numbers. Dividing 4 by 2!, we get 1 remainder 1, so we place 3 in spot 1



4 _ 6 3 _ 5



Finally, we divide 1 by 1 and get 0 remainder, so 2 goes in spot 1 and we place 1 in the last remaining spot and we are have:



4 2 6 3 1 5





I hope that makes sense. Note that it has the neat property that the permuation 1 2 3 4 5 6 corresponds to index 1 and in general the indices of the permutations are in the same order as the permutations themselves (if the permutations are viewes as six digit numbers).
sahsjing
2007-02-18 22:14:56 UTC
You already have an idea: Index the 6! numbers from the least to the largest.



To get the index for any of the 6! numbers, you can write a program to rank the 6! number from the least to the greatest and assign each number with its corresponding index.
doug_donaghue
2007-02-18 21:51:56 UTC
Find a copy of 'Numerical Recipes'. (Mine is loaned out right now so I can't give you a full reference, sorry)



I believe there is a permutation algorithm in it.



And I think there's also one in the LINPAK combinatorics collection.



HTH ☺





Doug


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...