问题
I want to rank and unrank permutations with one cycle in lexicographical order with a given len.
A permutation with one cycles is where you can visit in this cycle each element.
p:= (2,3,1)
is a permutation with one cycle. Has rank 1.
p:= (3,1,2)
has 1 cycle too, but rank 2, because the permutation is lexicographical greater the frist so it becomes a greater rank.
p:= (1,2,3)
is a permutation with 3 cycles. (1),(2),(3)
How can I efficently rank (permutation with one cycle to rank) and unrank (rank + len to permutation with one cycle) in lexicographical order? I have no idea how to archive this.
回答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
.
来源:https://stackoverflow.com/questions/59800684/rank-and-unrank-permutations-with-just-one-cycle