Permutations/Rank of a permutation

From Rosetta Code
Revision as of 06:32, 12 December 2012 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Permutations/Rank of a permutation is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A particular ranking of a permutation associates an integer with a particular ordering of all the permutations of a set of distinct items. For our purposes the ranking will assign integers to an ordering of all the permutations of the integers .

For example, the permutations of the digits zero to 3 arranged lexicographically have the following rank:

  PERMUTATION      RANK
  (0, 1, 2, 3) ->  0
  (0, 1, 3, 2) ->  1
  (0, 2, 1, 3) ->  2
  (0, 2, 3, 1) ->  3
  (0, 3, 1, 2) ->  4
  (0, 3, 2, 1) ->  5
  (1, 0, 2, 3) ->  6
  (1, 0, 3, 2) ->  7
  (1, 2, 0, 3) ->  8
  (1, 2, 3, 0) ->  9
  (1, 3, 0, 2) -> 10
  (1, 3, 2, 0) -> 11
  (2, 0, 1, 3) -> 12
  (2, 0, 3, 1) -> 13
  (2, 1, 0, 3) -> 14
  (2, 1, 3, 0) -> 15
  (2, 3, 0, 1) -> 16
  (2, 3, 1, 0) -> 17
  (3, 0, 1, 2) -> 18
  (3, 0, 2, 1) -> 19
  (3, 1, 0, 2) -> 20
  (3, 1, 2, 0) -> 21
  (3, 2, 0, 1) -> 22
  (3, 2, 1, 0) -> 23

Algorithms exist that can generate a rank from a permutation for some particular ordering of permutations, and that can generate the same rank from the given individual permutation (i.e. given a rank of 17 produce (2, 3, 1, 0) in the example above).

One use of such algorithms could be in generating a small, random, sample of permutations of items without duplicates when the total number of permutations is large. Remember that the total number of permutations of items is given by which grows large very quickly: A 32 bit integer can only hold , a 64 bit integer only . It becomes difficult to take the straight-forward approach of generating all permutations then taking a random sample of them.

A question on the Stack Overflow site asked how to generate one million random and indivudual permutations of 144 items.

This task is to
  1. Create a function to generate a permutation from a rank.
  2. Create the inverse function that given the permutation generates its rank.
  3. Show that for the two functions are indeed inverses of each other.
  4. Compute and show here 4 random, individual, samples of permutations of 12 objects.
Stretch goal
  • State how reasonable it would be to use your program to address the limits of the Stack Overflow question.
References
  1. Ranking and Unranking Permutations in Linear Time by Myrvold & Ruskey. (Also available via Google here).
  2. Ranks on the DevData site.
  3. Another answer on Stack Overflow to a different question that explains its algorithm in detail.

Haskell

Without the random part. <lang haskell>fact n = foldr (*) 1 [1..n]

-- always assume elements are unique rankPerm [] _ = [] rankPerm list n = c:rankPerm (a++b) r where (q,r) = n `divMod` fact (length list - 1) (a,c:b) = splitAt q list

permRank [] = 0 permRank (x:xs) = length(filter (<x) xs) * fact (length xs) + permRank xs

main = mapM_ f [0..23] where f n = print (n, p, permRank p) where p = rankPerm [0..3] n</lang>

Output:

from rank to permutation back to rank:

(0,[0,1,2,3],0)
(1,[0,1,3,2],1)
(2,[0,2,1,3],2)
(3,[0,2,3,1],3)
(4,[0,3,1,2],4)
(5,[0,3,2,1],5)
(6,[1,0,2,3],6)
(7,[1,0,3,2],7)
(8,[1,2,0,3],8)
(9,[1,2,3,0],9)
(10,[1,3,0,2],10)
(11,[1,3,2,0],11)
(12,[2,0,1,3],12)
(13,[2,0,3,1],13)
(14,[2,1,0,3],14)
(15,[2,1,3,0],15)
(16,[2,3,0,1],16)
(17,[2,3,1,0],17)
(18,[3,0,1,2],18)
(19,[3,0,2,1],19)
(20,[3,1,0,2],20)
(21,[3,1,2,0],21)
(22,[3,2,0,1],22)
(23,[3,2,1,0],23)

J

The J primitive A. provides an effective solution to this task. Generating 4 random permutations of 144 items takes about 6 milliseconds so solving the Stack Overflow question should be readily acheivable.

<lang j> A. 2 0 1 NB. return rank of permutation 4

  4 A. i.3                 NB. return permutation of rank 4

2 0 1

  0 1 2 3 4 5 A. i. 3      NB. generate all 6 permutations for 3 items

0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0

  A. 0 1 2 3 4 5 A. i. 3   NB. ranks of each permuation

0 1 2 3 4 5

  ]ranks=: 4 ? !12         NB. 4 random numbers sampled from integers 0 to 12!

315645285 249293994 432230943 23060830

  ranks A. i.12            NB. 4 random samples of 12 items
7 10 11  8  4 0  2 3 9 5  6 1
6  2  8 11 10 0  5 9 7 1  3 4

10 9 1 2 0 3 8 6 7 5 11 4

0  7  4  6 11 5 10 3 9 8  1 2
  (4 ?@$ !144x) A. i.144   NB. 4 random samples of 144 items

117 36 129 85 128 95 27 14 15 119 45 60 21 98 135 106 18 64 132 97 79 84 35 139 101 75 59 13 141 99 86 40 10 140 23 92 125 6 68 41 69 20 56 12 127 65 142 116 71 54 1 5 121 8 78 73 48 30 80 131 111 57 66 100 138 77 37 124 136... 111 65 136 58 92 46 4 83 20 54 21 10 72 110 56 28 13 18 73 133 105 117 63 126 114 43 5 80 45 88 86 108 11 29 0 129 71 141 59 53 113 137 2 102 95 15 35 74 107 61 134 36 32 19 106 100 55 69 76 142 64 49 9 30 47 123 12 97 42...

64 76 139 122  37 127 57 143 32 108 46 17 126   9  51  59  1 74  23  89  42 124 132  19  93 137  70 86  14 112 83  91 63  39 73  18  90 120 53 103 140  87  43  55 131 40 142 102 107 111  80  65  61 34  66  75 88 92 13 138  50 117 97  20  44   7 56  94  41...

139 87 98 118 125 65 35 112 10 43 85 66 58 131 36 30 50 11 136 130 71 100 79 142 40 69 101 84 143 33 95 26 18 94 13 68 8 0 47 70 129 48 107 64 93 16 83 39 29 81 6 105 78 92 104 60 15 55 4 14 7 91 86 12 31 46 20 133 53...</lang>

Java

The basic approach used is to consider the rank a variable-base number, where the symbol set used to encode each digit is the set of all symbols available, minus the symbols already used by digits to the left. Because the symbol used and its numerical value are easily conflated, it may be easier to think of the digits using letters. If n = 5, we may have an ordered symbol set [A, B, C, D, E]. If the leftmost digits of the permutation used B and C, the set is reduced to [A, D, E]. So if E is encountered in the next digit, it has a numerical value of 2 (its index within the symbol set).

Regarding generating a random permutation, the code generates a random rank and converts it to a permutation and back to a rank, to demonstrate that it functions correctly. However, if random permutations were all that was needed, a simpler approach would be to generate random ranks that were already in variable-base form (int[] rather than a large BigInteger). This would reduce the CPU/memory requirements, as it wouldn't need to do arbitrary-precision arithmetic, and could instead do everything with 32-bit ints. The method would simply translate the int[] variable-base rank into an int[] permutation.

Code:

<lang java>import java.math.BigInteger; import java.util.*;

class RankPermutation {

 public static BigInteger getRank(int[] permutation)
 {
   int n = permutation.length;
   BitSet usedDigits = new BitSet();
   BigInteger rank = BigInteger.ZERO;
   for (int i = 0; i < n; i++)
   {
     rank = rank.multiply(BigInteger.valueOf(n - i));
     int digit = 0;
     int v = -1;
     while ((v = usedDigits.nextClearBit(v + 1)) < permutation[i])
       digit++;
     usedDigits.set(v);
     rank = rank.add(BigInteger.valueOf(digit));
   }
   return rank;
 }
 
 public static int[] getPermutation(int n, BigInteger rank)
 {
   int[] digits = new int[n];
   for (int digit = 2; digit <= n; digit++)
   {
     BigInteger divisor = BigInteger.valueOf(digit);
     digits[n - digit] = rank.mod(divisor).intValue();
     if (digit < n)
       rank = rank.divide(divisor);
   }
   BitSet usedDigits = new BitSet();
   int[] permutation = new int[n];
   for (int i = 0; i < n; i++)
   {
     int v = usedDigits.nextClearBit(0);
     for (int j = 0; j < digits[i]; j++)
       v = usedDigits.nextClearBit(v + 1);
     permutation[i] = v;
     usedDigits.set(v);
   }
   return permutation;
 }
 
 public static void main(String[] args)
 {
   for (int i = 0; i < 6; i++)
   {
     int[] permutation = getPermutation(3, BigInteger.valueOf(i));
     System.out.println(String.valueOf(i) + " --> " + Arrays.toString(permutation) + " --> " + getRank(permutation));
   }
   Random rnd = new Random();
   for (int n : new int[] { 12, 144 })
   {
     BigInteger factorial = BigInteger.ONE;
     for (int i = 2; i <= n; i++)
       factorial = factorial.multiply(BigInteger.valueOf(i));
     // Create 5 random samples
     System.out.println("n = " + n);
     for (int i = 0; i < 5; i++)
     {
       BigInteger rank = new BigInteger((factorial.bitLength() + 1) << 1, rnd);
       rank = rank.mod(factorial);
       int[] permutation = getPermutation(n, rank);
       System.out.println("  " + rank + " --> " + Arrays.toString(permutation) + " --> " + getRank(permutation));
     }
   }
 }
 

}</lang>

Output:

0 --> [0, 1, 2] --> 0
1 --> [0, 2, 1] --> 1
2 --> [1, 0, 2] --> 2
3 --> [1, 2, 0] --> 3
4 --> [2, 0, 1] --> 4
5 --> [2, 1, 0] --> 5
n = 12
  459460043 --> [11, 5, 7, 1, 3, 8, 10, 6, 2, 9, 4, 0] --> 459460043
  242238791 --> [6, 0, 9, 5, 11, 2, 8, 4, 10, 7, 3, 1] --> 242238791
  43886594 --> [1, 2, 0, 11, 6, 8, 7, 9, 3, 5, 4, 10] --> 43886594
  356431614 --> [8, 11, 2, 3, 0, 6, 10, 5, 4, 1, 7, 9] --> 356431614
  344630629 --> [8, 6, 11, 7, 3, 0, 5, 10, 4, 1, 9, 2] --> 344630629
n = 144
  4081840330208521662873206235509318516433197707528534675764597914562081781405735597462986112994867493592438058582576097677821221600974033669483476390594977992091821541791590292404824493917612378402336513544253687968370430594214991000754150383016996226 --> [105, 129, 133, 132, 96, 116, 91, 74, 128, 49, 29, 20, 118, 82, 61, 107, 63, 43, 115, 86, 25, 8, 102, 122, 18, 66, 67, 71, 47, 143, 1, 44, 109, 111, 131, 52, 58, 117, 130, 135, 48, 76, 101, 87, 142, 139, 33, 103, 31, 51, 136, 120, 55, 34, 78, 62, 77, 140, 9, 119, 99, 10, 27, 46, 2, 110, 80, 73, 53, 17, 68, 125, 35, 41, 6, 75, 92, 124, 16, 36, 26, 19, 7, 114, 59, 112, 108, 106, 90, 11, 70, 69, 113, 89, 134, 30, 84, 138, 22, 79, 100, 28, 94, 0, 57, 121, 141, 127, 39, 50, 15, 24, 64, 72, 60, 83, 81, 137, 42, 3, 14, 54, 98, 21, 4, 38, 65, 45, 123, 85, 95, 5, 93, 40, 56, 13, 23, 12, 97, 126, 37, 104, 32, 88] --> 4081840330208521662873206235509318516433197707528534675764597914562081781405735597462986112994867493592438058582576097677821221600974033669483476390594977992091821541791590292404824493917612378402336513544253687968370430594214991000754150383016996226
  2303393071343830907186150492848666018688104049718820526188554488347193604795830950201535244280363069858008773693396780501147779890336031158296935978114061601636771344904185209112272057234471902872428445042702453836810958756677409979103815382798559553 --> [59, 109, 108, 98, 87, 75, 50, 8, 51, 44, 78, 63, 19, 127, 140, 45, 115, 128, 35, 26, 6, 39, 2, 7, 94, 14, 66, 5, 9, 31, 36, 68, 142, 138, 124, 130, 71, 129, 97, 58, 12, 132, 40, 80, 139, 143, 118, 79, 46, 137, 116, 111, 117, 136, 103, 100, 43, 24, 65, 70, 73, 67, 76, 106, 102, 17, 134, 49, 54, 33, 0, 77, 89, 92, 56, 47, 42, 1, 57, 32, 30, 62, 25, 83, 29, 15, 126, 13, 55, 131, 4, 10, 85, 84, 91, 18, 121, 88, 104, 28, 95, 60, 72, 120, 114, 21, 133, 105, 37, 38, 3, 11, 122, 61, 74, 52, 16, 113, 96, 81, 99, 23, 101, 141, 69, 93, 110, 82, 34, 27, 123, 135, 86, 125, 90, 112, 48, 41, 53, 22, 64, 107, 119, 20] --> 2303393071343830907186150492848666018688104049718820526188554488347193604795830950201535244280363069858008773693396780501147779890336031158296935978114061601636771344904185209112272057234471902872428445042702453836810958756677409979103815382798559553
  848047014832080341751646538335377058839867620964996131754382188532880389234227596454312311520517451021055848535846150020108831781620073357956660780586648787723957832482692127095007493517434391511473891562806437927741246265623743953254070095131510626 --> [22, 0, 47, 4, 3, 10, 27, 40, 139, 30, 75, 21, 66, 68, 131, 7, 73, 79, 23, 135, 35, 126, 116, 62, 86, 140, 71, 113, 2, 114, 9, 76, 1, 132, 133, 48, 65, 78, 107, 12, 29, 16, 96, 8, 121, 56, 64, 108, 129, 50, 6, 119, 124, 13, 34, 33, 84, 111, 26, 141, 20, 120, 87, 142, 134, 55, 97, 25, 106, 39, 91, 49, 46, 72, 14, 19, 89, 63, 60, 54, 94, 11, 98, 31, 88, 101, 110, 37, 70, 95, 112, 143, 123, 41, 117, 92, 74, 90, 109, 115, 18, 130, 24, 5, 137, 138, 99, 77, 82, 118, 57, 43, 53, 102, 42, 105, 127, 61, 103, 59, 80, 45, 52, 15, 93, 83, 122, 100, 51, 128, 44, 32, 17, 38, 104, 81, 85, 36, 125, 67, 136, 28, 58, 69] --> 848047014832080341751646538335377058839867620964996131754382188532880389234227596454312311520517451021055848535846150020108831781620073357956660780586648787723957832482692127095007493517434391511473891562806437927741246265623743953254070095131510626
  3867119554188057320189830921237689379982446172130048814726708614069209339326961586538911571940960420075292018388619717817588596221122134526830668232334295443338743434633976186486468178683660450737612737202079329785249513121919049677640145391966805396 --> [100, 47, 42, 69, 16, 43, 66, 107, 73, 79, 12, 80, 41, 50, 9, 126, 95, 36, 26, 51, 123, 45, 52, 3, 93, 29, 83, 17, 82, 5, 81, 85, 131, 122, 113, 6, 75, 28, 59, 64, 138, 20, 74, 114, 27, 65, 105, 116, 62, 142, 141, 35, 115, 4, 49, 78, 2, 97, 130, 89, 110, 57, 90, 127, 72, 119, 44, 13, 99, 112, 118, 103, 77, 125, 92, 133, 104, 60, 76, 70, 23, 53, 55, 38, 108, 84, 96, 54, 128, 140, 25, 11, 24, 7, 124, 136, 8, 111, 46, 63, 31, 1, 102, 67, 94, 0, 132, 37, 91, 10, 101, 22, 34, 68, 14, 71, 21, 87, 30, 40, 129, 120, 18, 58, 61, 86, 56, 143, 32, 33, 15, 88, 137, 98, 106, 109, 135, 134, 48, 139, 121, 39, 19, 117] --> 3867119554188057320189830921237689379982446172130048814726708614069209339326961586538911571940960420075292018388619717817588596221122134526830668232334295443338743434633976186486468178683660450737612737202079329785249513121919049677640145391966805396
  4939977610738364532346788397924709243352263476618646232552639571823537393872611379891069437022354024663565745556550101222893478863124970842071831822047445469519025638779057663111504542116305321498448757808078281473228131641155000612722388784688217279 --> [128, 23, 97, 115, 131, 15, 21, 61, 90, 116, 32, 80, 59, 137, 7, 63, 43, 55, 11, 83, 27, 138, 114, 40, 122, 4, 132, 125, 54, 25, 95, 111, 72, 84, 17, 13, 31, 10, 16, 52, 126, 129, 91, 18, 47, 53, 34, 119, 57, 41, 110, 134, 108, 58, 127, 82, 66, 70, 33, 9, 98, 142, 100, 121, 30, 105, 36, 120, 48, 2, 28, 37, 5, 46, 44, 71, 107, 45, 19, 141, 86, 76, 109, 143, 118, 3, 130, 89, 73, 42, 56, 94, 35, 67, 136, 12, 74, 123, 24, 64, 93, 26, 14, 112, 88, 29, 77, 60, 85, 6, 0, 96, 103, 62, 50, 124, 8, 135, 22, 79, 68, 139, 78, 101, 20, 117, 51, 133, 81, 102, 39, 99, 113, 38, 104, 69, 106, 75, 92, 87, 49, 1, 140, 65] --> 4939977610738364532346788397924709243352263476618646232552639571823537393872611379891069437022354024663565745556550101222893478863124970842071831822047445469519025638779057663111504542116305321498448757808078281473228131641155000612722388784688217279

Python

Python: Myrvold & Ruskey

This is based on the work shown in the paper by Myrvold & Ruskey and has algorithms for the two types of ordering of permutations that they show.
Their algorithm is efficient and pythons transparent use of arbitrary precision integers allows the Stack Overflow questions limits to be used. (Chopped at four rather than a million random samples although function get_random_ranks(144, 1e6) doesn't take long).

<lang python>from math import factorial as fact from random import randrange from textwrap import wrap

def identity_perm(n):

   return list(range(n))

def unranker1(n, r, pi):

   while n > 0:
       n1, (rdivn, rmodn) = n-1, divmod(r, n)
       pi[n1], pi[rmodn] = pi[rmodn], pi[n1]
       n = n1
       r = rdivn
   return pi

def init_pi1(n, pi):

   pi1 = [-1] * n
   for i in range(n): 
       pi1[pi[i]] = i
   return pi1

def ranker1(n, pi, pi1):

   if n == 1: 
       return 0
   n1 = n-1
   s = pi[n1]
   pi[n1], pi[pi1[n1]] = pi[pi1[n1]], pi[n1]
   pi1[s], pi1[n1] = pi1[n1], pi1[s]
   return s + n * ranker1(n1, pi, pi1)

def unranker2(n, r, pi):

   while n > 0:
       n1 = n-1
       s, rmodf = divmod(r, fact(n1))
       pi[n1], pi[s] = pi[s], pi[n1]
       n = n1
       r = rmodf
   return pi

def ranker2(n, pi, pi1):

   if n == 1: 
       return 0
   n1 = n-1
   s = pi[n1]
   pi[n1], pi[pi1[n1]] = pi[pi1[n1]], pi[n1]
   pi1[s], pi1[n1] = pi1[n1], pi1[s]
   return s * fact(n1) + ranker2(n1, pi, pi1)

def get_random_ranks(permsize, samplesize):

   perms = fact(permsize)
   ranks = set()
   while len(ranks) < samplesize:
       ranks |= set( randrange(perms) 
                     for r in range(samplesize - len(ranks)) )
   return ranks    

def test1(comment, unranker, ranker):

   n, samplesize, n2 = 3, 4, 12
   print(comment)
   perms = []
   for r in range(fact(n)):
       pi = identity_perm(n)
       perm = unranker(n, r, pi)
       perms.append((r, perm))
   for r, pi in perms:
       pi1 = init_pi1(n, pi)
       print('  From rank %2i to %r back to %2i' % (r, pi, ranker(n, pi[:], pi1)))
   print('\n  %i random individual samples of %i items:' % (samplesize, n2))
   for r in get_random_ranks(n2, samplesize):
       pi = identity_perm(n2)
       print('    ' + ' '.join('%2i' % i for i in unranker(n2, r, pi)))
   print()

def test2(comment, unranker):

   samplesize, n2 = 4, 144
   print(comment)
   print('  %i random individual samples of %i items:' % (samplesize, n2))
   for r in get_random_ranks(n2, samplesize):
       pi = identity_perm(n2)
       print('    ' + '\n      '.join(wrap(repr(unranker(n2, r, pi)))))
   print()

if __name__ == '__main__':

   test1('First ordering:', unranker1, ranker1)
   test1('Second ordering:', unranker2, ranker2)
   test2('First ordering, large number of perms:', unranker1)</lang>
Sample output
First ordering:
  From rank  0 to [1, 2, 0] back to  0
  From rank  1 to [2, 0, 1] back to  1
  From rank  2 to [1, 0, 2] back to  2
  From rank  3 to [2, 1, 0] back to  3
  From rank  4 to [0, 2, 1] back to  4
  From rank  5 to [0, 1, 2] back to  5

  4 random individual samples of 12 items:
     1  4  5 10  7  3  2  6  9 11  0  8
     0  4  9 11  8 10  7  2  6  5  1  3
    11  8  2  3 10  7  4  6  1  0  5  9
    11  6  7  1  9  5  4  0  2  8  3 10

Second ordering:
  From rank  0 to [1, 2, 0] back to  0
  From rank  1 to [2, 1, 0] back to  1
  From rank  2 to [2, 0, 1] back to  2
  From rank  3 to [0, 2, 1] back to  3
  From rank  4 to [1, 0, 2] back to  4
  From rank  5 to [0, 1, 2] back to  5

  4 random individual samples of 12 items:
     8  9  4 11  7  0  3  6  1 10  2  5
     1  2  0 11  8  6  3  9  4 10  7  5
     9  1  4  8  2 10  3  7  0  6 11  5
     4  7 11  2  5  0  3  8  9  6 10  1

First ordering, large number of perms:
  4 random individual samples of 144 items:
    [92, 32, 141, 93, 97, 45, 70, 134, 60, 5, 99, 4, 13, 80, 68, 100, 77,
      115, 116, 34, 50, 117, 26, 31, 88, 128, 14, 35, 106, 129, 114, 73,
      101, 142, 29, 11, 1, 86, 17, 38, 130, 140, 84, 51, 81, 110, 111, 64,
      24, 3, 16, 6, 139, 104, 103, 8, 75, 62, 43, 113, 137, 48, 22, 53, 30,
      125, 33, 67, 69, 143, 83, 121, 123, 138, 102, 87, 57, 49, 58, 82, 20,
      109, 89, 59, 96, 56, 19, 10, 90, 28, 41, 94, 107, 108, 95, 74, 21, 66,
      40, 25, 46, 78, 44, 112, 124, 36, 135, 42, 132, 79, 37, 63, 15, 2, 61,
      47, 85, 72, 0, 119, 39, 52, 55, 65, 136, 23, 18, 127, 27, 126, 131,
      133, 71, 54, 7, 98, 9, 12, 118, 122, 120, 91, 76, 105]
    [36, 141, 8, 114, 28, 42, 32, 40, 75, 134, 72, 106, 78, 107, 43, 83,
      0, 13, 41, 48, 66, 4, 98, 136, 34, 35, 46, 92, 15, 135, 17, 79, 22,
      118, 95, 137, 81, 121, 60, 74, 110, 33, 80, 14, 62, 125, 3, 56, 115,
      49, 27, 1, 7, 84, 20, 64, 47, 140, 124, 119, 143, 23, 93, 87, 25, 12,
      69, 105, 68, 112, 10, 44, 101, 138, 99, 51, 127, 52, 122, 61, 21, 108,
      117, 39, 2, 85, 53, 130, 120, 18, 16, 109, 126, 103, 113, 5, 82, 19,
      73, 45, 58, 102, 129, 54, 96, 31, 57, 97, 65, 133, 59, 132, 88, 30,
      89, 86, 9, 116, 71, 142, 77, 94, 104, 63, 37, 139, 70, 131, 11, 111,
      123, 128, 24, 76, 90, 29, 6, 38, 100, 26, 55, 91, 50, 67]
    [32, 8, 35, 70, 140, 136, 59, 16, 15, 93, 118, 26, 132, 98, 71, 6,
      107, 19, 65, 11, 42, 97, 78, 85, 28, 96, 119, 82, 44, 2, 125, 58, 117,
      21, 27, 39, 48, 52, 92, 139, 142, 49, 38, 54, 79, 57, 127, 45, 109,
      17, 75, 95, 101, 86, 14, 122, 108, 131, 31, 141, 110, 128, 53, 84, 30,
      23, 66, 41, 13, 37, 36, 130, 113, 46, 50, 47, 115, 7, 100, 51, 137,
      134, 10, 55, 64, 43, 99, 102, 135, 24, 20, 73, 9, 90, 60, 111, 104,
      143, 1, 103, 3, 129, 12, 138, 33, 67, 123, 22, 112, 68, 120, 81, 133,
      91, 61, 25, 124, 56, 40, 121, 4, 18, 114, 126, 80, 106, 83, 5, 94,
      116, 88, 105, 63, 87, 77, 89, 72, 62, 74, 0, 29, 69, 34, 76]
    [28, 68, 101, 27, 40, 79, 73, 2, 75, 97, 135, 23, 17, 95, 58, 26, 80,
      93, 49, 31, 140, 39, 25, 35, 83, 114, 1, 13, 48, 29, 134, 122, 70, 0,
      91, 62, 77, 37, 14, 44, 24, 50, 126, 9, 42, 67, 100, 104, 99, 59, 55,
      21, 123, 138, 33, 116, 127, 115, 82, 61, 117, 130, 43, 86, 22, 12, 56,
      60, 47, 78, 121, 131, 36, 38, 51, 4, 139, 142, 128, 98, 84, 92, 111,
      5, 53, 106, 34, 6, 3, 20, 72, 112, 11, 136, 94, 32, 71, 132, 88, 124,
      85, 110, 108, 137, 30, 89, 74, 120, 8, 102, 41, 81, 129, 107, 63, 118,
      66, 7, 133, 65, 125, 64, 90, 141, 109, 57, 18, 69, 103, 76, 113, 16,
      52, 15, 19, 46, 96, 45, 10, 54, 105, 143, 87, 119]

Python: Iterative

Functions ranker1 and ranker2 can be changed from the recursive form as used in the paper to iterative versions if they are replaced with the following code that yields the same results: <lang python>def ranker1(n, pi, pi1):

   if n == 1: 
       return 0
   n1 = n-1
   s = pi[n1]
   pi[n1], pi[pi1[n1]] = pi[pi1[n1]], pi[n1]
   pi1[s], pi1[n1] = pi1[n1], pi1[s]
   return s + n * ranker1(n1, pi, pi1)

def ranker2(n, pi, pi1):

   result = 0
   for i in range(n-1, 0, -1):
       s = pi[i]
       pi[i], pi[pi1[i]] = pi[pi1[i]], pi[i]
       pi1[s], pi1[i] = pi1[i], pi1[s]
       result += s * fact(i)
   return result</lang>

REXX

<lang rexx>/*REXX program shows permutations of N number of objects (1,2,3, ...).*/ parse arg N seed .; if N== then N=3 /*Not specified? Assume default*/ permutes=permsets(N) /*returns N! (# of permutations)*/ w=length(permutes) /*used for aligning the SAY stuff*/

 do what=0  to permutes-1             /*traipse through each permute.  */
 z=permsets(N, what)                  /*get the  "what"  permuation.   */
 say N 'items, permute' right(what,w)   "="   z  '  rank=' permsets(N,,z)
 end   /*what*/

say; if seed\== then call random ,,seed /*seed ≡ repeatability.*/ N=12

     do 4;  ?=random(0, N**4)         /*REXX has a  100k  RANDOM range.*/
     say  N   'items, permute'    right(?,6)     " is "     permsets(N,?)
     end   /*rand*/

exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────PERMSETS subroutine─────────────────*/ permsets: procedure expose @. #; #=0; parse arg x,r,c; c=space(c); xm=x-1

 do j=1 for x; @.j=j-1; end /*j*/; _=0; do u=2 for xm; _=_ @.u; end /*u*/
 if r==#  then return _;  if c==_ then return #
 do while .permsets(x,0); #=#+1; _=@.1; do u=2 for xm; _=_ @.u; end /*u*/
 if r==#  then return _;  if c==_ then return #
 end   /*while···*/

return #+1

.permsets: procedure expose @.; parse arg p,q; pm=p-1

 do k=pm by -1 for pm; kp=k+1; if @.k<@.kp then do; q=k; leave; end;  end
 do j=q+1  while j<p;  parse value @.j @.p with @.p @.j; p=p-1; end

if q==0 then return 0

 do j=q+1  while @.j<@.q;   end;   parse value @.j @.q  with  @.q @.j

return 1</lang> output when using the default input

4 items, permute  0 = 0 1 2 3   rank= 0
4 items, permute  1 = 0 1 3 2   rank= 1
4 items, permute  2 = 0 2 1 3   rank= 2
4 items, permute  3 = 0 2 3 1   rank= 3
4 items, permute  4 = 0 3 1 2   rank= 4
4 items, permute  5 = 0 3 2 1   rank= 5
4 items, permute  6 = 1 0 2 3   rank= 6
4 items, permute  7 = 1 0 3 2   rank= 7
4 items, permute  8 = 1 2 0 3   rank= 8
4 items, permute  9 = 1 2 3 0   rank= 9
4 items, permute 10 = 1 3 0 2   rank= 10
4 items, permute 11 = 1 3 2 0   rank= 11
4 items, permute 12 = 2 0 1 3   rank= 12
4 items, permute 13 = 2 0 3 1   rank= 13
4 items, permute 14 = 2 1 0 3   rank= 14
4 items, permute 15 = 2 1 3 0   rank= 15
4 items, permute 16 = 2 3 0 1   rank= 16
4 items, permute 17 = 2 3 1 0   rank= 17
4 items, permute 18 = 3 0 1 2   rank= 18
4 items, permute 19 = 3 0 2 1   rank= 19
4 items, permute 20 = 3 1 0 2   rank= 20
4 items, permute 21 = 3 1 2 0   rank= 21
4 items, permute 22 = 3 2 0 1   rank= 22
4 items, permute 23 = 3 2 1 0   rank= 23

12 items, permute   8047  is  0 1 2 3 5 9 6 4 8 7 11 10
12 items, permute  14942  is  0 1 2 3 6 11 9 7 8 5 4 10
12 items, permute  12250  is  0 1 2 3 6 8 4 5 9 11 7 10
12 items, permute    249  is  0 1 2 3 4 5 8 6 9 10 11 7