Jump to content

Permutations/Rank of a permutation: Difference between revisions

Added Java implementation.
(Added Java implementation.)
Line 120:
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>
 
=={{header|Java}}==
 
The basic approach used it 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 it's 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).
 
'''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:'''
 
<pre>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</pre>
 
=={{header|Python}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.