Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

On creating a single-cycle permutation from a shuffle:

Take 0, 1, 2, 3, 4, 5 and shuffle it. 4, 3, 0, 5, 1, 2

Now you can read out a permutation:

4 → 3

3 → 0

0 → 5

5 → 1

1 → 2

2 → 4

It will only have one cycle.



Edit: I misinterpreted the parent; see below.

If by 'shuffle' you mean like Fisher-Yates, this can't work all the time, since that can produce, e.g. 0, 1, 2, 3, 4, 5 -> 1, 0, 4, 5, 3, which has two cycles. If by 'shuffle' you mean like Fisher-Yates, but not leaving an element in its original position, (i.e., generating a derangement) then this doesn't suffice either, since notice that the same example satisfies that condition and yet still has two cycles. You need Sattolo's algorithm (or something like it) to guarantee a single cycle.


That's not what the parent meant. You shuffle the array with e.g. Fisher-Yates and then create a permutation from the shuffled array. That always gives a permutation that has one cycle. What you did was create a permutation from the original and the shuffled variant which has the problems you describe...


Thanks for clarifying. I was on my cell phone and was very brief.


Ah, you're right! My mistake.


Some permutations have one cycle, other have more. You are "adjusting" your permutation from 4 3 0 5 1 2 to 3 0 5 1 2 4 to eliminate a cycle and obtain a suitable result. What are the rules to do that and ensure the single-cycle permutations are produced with uniform probability if the original permutation is produced with uniform probability? I.e. which set of n permutations is mapped to each single cycle permutation? It's going to be a much more complex algorithm than the optimal in-place shuffles discussed in the article.


No... it's pretty clear that there are exactly n ways to get each particular single-cycle permutation, given by the n rotations of the fisher-yates shuffle. For example, if you rotate the parent's shuffle by one:

2, 4, 3, 0, 5, 1

you get the same single-cycle permutation.

I think this might actually lead to a easier proof than in the article.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: