Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffling algorithm analysis

Tags:

algorithm

I came across this following analysis of shuffling algorithms:

Q: Given an array of distinct integers, give an algorithm to randomly reorder the integers so that each possible reordering is equally likely. In other words, given a deck of cards, how can you shuffle them such that any permutation of cards is equally likely?

Good answer: Go through the elements in order, swapping each element with a random element in the array that does not appear earlier than the element. This takes O(n) time. Note that there are several possible solutions to this problem, as well as several good‐looking answers that are incorrect. For example, a slight modification to the above algorithm whereby one switches each element with any element in the array does not give each reordering with equally probability.

What I would like to know is why switching each element with any other element in the array does not produce a good shuffle as opposed to using the Knuth shuffle (which is described). Also, how does the Knuth shuffle select values with equal probability? Any math or proof is greatly appreciated.

like image 863
OckhamsRazor Avatar asked Sep 03 '11 06:09

OckhamsRazor


People also ask

How does the Fisher-Yates shuffle algorithm work?

The "inside-out" algorithm The Fisher–Yates shuffle, as implemented by Durstenfeld, is an in-place shuffle. That is, given a preinitialized array, it shuffles the elements of the array in place, rather than producing a shuffled copy of the array. This can be an advantage if the array to be shuffled is large.

What is Spotify's shuffle algorithm?

When the Spotify system was launched, it used the Fisher-Yates algorithm for shuffling. It's the algorithmic equivalent of randomly picking tickets out of a hat until there are none left.

Is Fisher-Yates biased?

As it turns out, while the Fisher-Yates shuffle itself is unbiased, bias can still be introduced into its output depending on what you use to randomly select the card you're swapping with.


1 Answers

The easiest proof that this algorithm does not produce a uniformly random permutation

for (int i = 0; i < 3; ++i) {
   swap(a[i], a[rand() % 3]);
}

Is that it generates 27 possible outcomes, but there are only 3! = 6 permutations. Since 6 does not divide 27, there must be some permutation is that is picked too much, and some that is picked to little.

Why is an O(n) algorithm optimal? Well, a random shuffle must touch every input sometimes (to change them), so any optimal algorithm needs to do at least O(n) work.

Why is the Knuth algorithm correct? That requires a little bit more insight. You can prove via induction that the first item is selected with the correct probability (each item is equally likely to be first), and then prove that the inductive step holds as you advance through the loop, that the second, third, etc items are also selected with the correct probability from the remaining portions of the array.

like image 68
Rob Neuhaus Avatar answered Dec 07 '22 22:12

Rob Neuhaus