Is there a way to generate a specific permutation of a list using an algortihm/equation that might accept a few inputs?

67 Views Asked by At

I dont understand why the question is still closed. There is no way to explain it further. Someone answered so people can understand the question. Please reopen so I can get more helpful answers. Thank you for your consideration. Is there a way to find the $k$-th permutation of a list $v$ having $n$ items, i.e. ${(1,...,n)}$, where $1\le k\le n!$ and $n!$ is total number of permutations, using an algorithm/equation with a few inputs?

I am not familiar with this problem so bear with me.

Say we have the following sequence: $v={\{1, 2, 3, 4, 5, 6, 7, 8\}}$. There is a total of $P= 8!=40320$ permutations and out aim is to find permutation $k=2500$. Is it possible instead of listing all the possible permutations of the $v$ to formulate an equation/algorithm which may take a few inputs and can generate the k-th permutation? For instance, using MATLAB, the $k=2500$-th permutation is $v(2500)= [8, 4, 5, 1, 7, 3, 2, 6]$

1

There are 1 best solutions below

0
On

Yes there is, via the factorial number system.

Very roughly, if you have $n$ items and you want to find the $k$-th permutation of them (with $1 \leq k \leq n!$), you follow the algorithm:

  1. Start with the list $\{1, 2, \ldots, n\}$.

  2. Find the largest integer $m$ such that $m \times (n-1)! > k$.

  3. Select the $m$-th entry remaining the list as the next element of the permutation, and remove it from the list.

  4. If the list is not yet empty, set $k := k - (m-1)(n-1)!$ and $n := n - 1$ and go back to 2.

If you're uncomfortable with storing a number up to $n!$ cleanly, then you can much more simply just take in a sequence $a_1, a_2, \ldots, a_n$ where $1 \leq a_i \leq n - i$ and essentially just use it as the values of $m$ to iterate step 3 from the above over the list.