Rank And Unrank Permutations With Just One Cycle
Solution 1:
I discovered a solution for ranking. We know that a permutations of length n
has n-1
! permutations with one cycle. Due to this knowledge we can come to the following solution.
Ranking: example 2341
We start to calculating the rank with the 1 position this gives (n-1[position])! as tempvalue
. Then we calculating the index of the 2
which is 0, because 1
is falling out through it creates the cycle (1). To complete the calculating for the first position we need to multiply the index
of the element with the tempvalue
, which leads to 0 as temprank_0
.
Now we continue this steps for the remaining positions to add temprank_0+temprank_1+temprank_2+temprank_4 = 0
Unranking: 4
for permutation len 4:
We divide the rank through (n-2[postion+1])!
which leads 2
which is the index of 1234
which dont create a cycle so the 1 position of the permutation is 4
. Then we subtract 2 times (n-2)!
from 4
.
This we continue this twice. So we have 412
. So in the end we add just the remaining value 3
end we get the permutation 4123
.
Post a Comment for "Rank And Unrank Permutations With Just One Cycle"