mathformulapermutationnon-repetitive

Calculating the index of an element in non-repetitive permutation


The following question is about math. The matter is, how to calculate the index of an element in a non-repetitive permutation. Example,

A={a,b,c} The permutation is then 3!=6 therefore: (a,b,c);(a,c,b);(b,a,c);(b,c,a);(c,a,b);(c,b,a)

I researched for algorithm to get the index of an element in this permutation. In internet there are only repetitive permutation algorithms. The index of (b,c,a) is in this zero-based list, obviously 3. Is there an easy way to calculate the position directly by formula ? I do not need the itertools from python. Because i use very large permutations.(Example 120!) I messed once with python's itertools' permutations function to get the index of an element over the list iterator. But the results were weary. I need a mathematical solution to get the index directly. Thanks for reading.


Solution

  • Some clues:
    You have n! permutations. Note that (n-1)! permutations start from the first element (a), next (n-1)! permutations start from the second element (b) and so on.

    So you can calculate the first term of permutation rank as (n-1)! * Ord(P[0]) where Ord gives ordering number of the first element of permutation in initial sequence (0 for a, 1 for b etc).

    Then continue with the second element using (n-2)! multiplier and so on.

    Don't forget to exclude used elements from order - for your example b is used, so at the second stage c has index 1 rather 0, ad rank is 2!*1 + 1!*1 + 0! * 0 = 3