Given $n$ unique items and an $m^{th}$ normalised value, compute $m^{th}$ permutation without factorial expansion

64 Views Asked by At

We know that the number of permutations possible for $n$ unique items is $n!$. We can uniquely label each permutation with a number from $0$ to $(n!-1)$.

Suppose if $n=4$, the possible permutations with their labels are,

0:  1234
1:  1243
2:  1324
3:  1342
4:  1432
5:  1423
6:  2134
7:  2143
8:  2314
9:  2341
10: 2431
11: 2413
12: 3214
13: 3241
14: 3124
15: 3142
16: 3412
17: 3421
18: 4231
19: 4213
20: 4321
21: 4312
22: 4132
23: 4123

With any well defined labelling scheme, given a number $m, 0 \leq m < n!$, we can get back the permutation sequence. Further, these labels can be normalised to be between $0$ and $1$. The above labels can be transformed into,

0:       1234
0.0434:  1243
0.0869:  1324
0.1304:  1342
0.1739:  1432
0.2173:  1423
0.2608:  2134
0.3043:  2143
0.3478:  2314
0.3913:  2341
0.4347:  2431
0.4782:  2413
0.5217:  3214
0.5652:  3241
0.6086:  3124
0.6521:  3142
0.6956:  3412
0.7391:  3421
0.7826:  4231
0.8260:  4213
0.8695:  4321
0.9130:  4312
0.9565:  4132
1:       4123

Now, given $n$ and $m^{th}$ normalised label, can we get the $m^{th}$ permutation while avoiding the expansion of $n!$ ? For example, in the above set of permutations, if we were given the $m^{th}$ normalised label to be $0.9$, is it possible to get the closest sequence 4312 as the answer without computing $4!$ ?

1

There are 1 best solutions below

7
On

Given the normalized label $m$, find the integer label $k = m \times (n!-1)$. We are to find the corresponding permutation $\Pi_k = \pi^k_0\pi^k_1\cdots\pi^k_{n-1}$

Let $S_0=\{1,2\cdots ,n\}$. Set $k_0 = k$, $n_0 = n$. The first digit from the left ($\pi^k_0$) of the permutation $\Pi_k$ will be the $\left\lceil\dfrac{k_0+1}{(n_0-1)!} \right\rceil^{th}$ smallest element in $S_0$. This can be found in constant time if we have a sorted array. Similarly to find $\pi^k_1$, take $k_1 \equiv k_0 \mod (n_0-1)!$, where $0\leq k_1<(n_0-1)!$, take $n_1 = n_0 - 1$ and find the $\left\lceil\dfrac{k_1+1}{(n_1-1)!} \right\rceil^{th}$ smallest element in $S_1 = S_0\setminus\{\pi^k_0\}$. All the digits of $\Pi_k$ can be found iteratively in a similar manner.