Sub topic 6
Proving Every Permutation Is Equally Likely
This chapter assumes you know the in-place Fisher-Yates loop from the previous chapter, that a list of n distinct items has n! orderings, and that independent probabilities multiply. The goal is to show that the loop below gives each of the n! permutations with probability exactly 1/n!.
import random
def fisher_yates(items):
for i in range(len(items) - 1, 0, -1):
j = random.randint(0, i) # inclusive on both ends: 0 <= j <= i
items[i], items[j] = items[j], items[i]
return items
print(fisher_yates(list(range(10))))Step 1: Count the choice sequences
Each run of the loop is fully described by its sequence of random choices, one value of j for each i. When i = n-1 there are n possible values of j. When i = n-2 there are n-1, and so on down to i = 1, which has 2. Every choice is uniform and independent, so each complete sequence has the same probability:
1/n × 1/(n-1) × … × 1/2 = 1/n!
The number of distinct sequences is n × (n-1) × … × 2 = n!. That is the same as the number of permutations. The naive shuffle has n^n sequences, and n^n is generally not a multiple of n!. Fisher-Yates has no such mismatch.
Step 2: Show that every permutation is reachable
Equal counts are not enough on their own. Two sequences could still produce the same permutation while some other permutation never appears. To rule this out, we show that the mapping from sequences to permutations is onto, meaning every permutation can be produced.
Pick any target ordering. At step i, positions i+1 through n-1 already hold their final items, because the loop never touches them again. So the item that belongs at position i must be somewhere in positions 0 through i. Choose j to be its current index. That j lies in the allowed range 0..i, so the swap places the correct item at position i. Repeating this for every step builds the target.
A function from a set of n! elements onto another set of n! elements must be a one-to-one correspondence. Each permutation therefore comes from exactly one choice sequence, so its probability is 1/n!.
The tree for n = 3
The tree has six leaves, each reached with probability 1/3 × 1/2 = 1/6, and no leaf appears twice.
An equivalent view: the invariant
You can also argue one position at a time. The last position receives each of the n items with probability 1/n. Given what landed there, position n-2 receives each of the remaining n-1 items with probability 1/(n-1), and so on. Stated as a loop invariant: after the step for index i, the suffix from i to n-1 is a uniformly random ordered selection of distinct items, and the prefix holds the rest. Multiplying the conditional probabilities gives 1/n! again.
Exercise
Starting from [A, B, C], use the reachability argument to find the choices (j at i = 2, then j at i = 1) that produce [B, C, A].
Show answer
Position 2 must hold A, which is at index 0, so j = 0 gives [C, B, A]. Position 1 must hold C, which is now at index 0, so j = 0 gives [B, C, A]. The sequence is (0, 0), which matches the leftmost leaf of the tree.
Exercise
Suppose the bound is changed to j = random.randrange(i), so 0 <= j < i. How many choice sequences exist for n items, and why does that rule out a uniform shuffle over all n! permutations?
Show answer
Step i now has i choices, so the count is (n-1) × (n-2) × … × 1 = (n-1)!. That is fewer than n!, so at least n! - (n-1)! permutations can never appear. The identity ordering is one of them, because the item at position i is always swapped away.