Sub topic 15
Sattolo's Algorithm and Cyclic Permutations
This chapter assumes you already know the in-place Fisher-Yates shuffle, its uniformity proof, and the off-by-one pitfalls in its index range. It covers the variant you get when you exclude the current position from the swap range.
One Bound, a Different Algorithm
In 1986, Sandra Sattolo published a shuffle that is almost identical to Durstenfeld's version of Fisher-Yates. The only change is the range of j:
| Algorithm | Choice of `j` at step `i` | Number of possible outputs |
|---|---|---|
| Fisher-Yates | `0 <= j <= i` | n! |
| Sattolo | `0 <= j < i` | (n-1)! |
import random
def sattolo(items):
for i in range(len(items) - 1, 0, -1):
j = random.randrange(i) # 0 <= j < i: excludes i itself
items[i], items[j] = items[j], items[i]
return itemsSattolo's algorithm is not a broken shuffle. It is a correct algorithm for a different problem: choosing a uniformly random cyclic permutation.
What a Cyclic Permutation Is
Read a permutation p of 0..n-1 as a mapping: index i points to p[i]. If you start at 0 and keep following the arrows, you trace a cycle. A cyclic permutation, also called a single n-cycle, is one where that walk visits all n positions before it returns to the start.
There are exactly (n-1)! cyclic permutations of n items. To see why, fix 0 as the starting point. The other n-1 items can follow it in any order, and each order gives a different cycle.
Why Sattolo Is Uniform over Cycles
The counting argument has the same shape as the Fisher-Yates proof. At step i, there are i choices for j, so the number of equally likely choice sequences is:
(n-1) × (n-2) × ... × 1 = (n-1)!
Each sequence produces a different cyclic permutation. You can prove this by induction: forbidding j = i means element i is always joined to the cycle of something earlier, and never left as a fixed point or a separate cycle. That gives (n-1)! distinct outputs from (n-1)! equally likely sequences, so each cycle appears with probability 1/(n-1)!.
Checking the Output
The function below counts cycles. For Sattolo's output it should always return 1.
import random
from collections import Counter
def sattolo(items):
for i in range(len(items) - 1, 0, -1):
j = random.randrange(i)
items[i], items[j] = items[j], items[i]
return items
def cycle_count(perm):
seen = [False] * len(perm)
cycles = 0
for start in range(len(perm)):
if not seen[start]:
cycles += 1
k = start
while not seen[k]:
seen[k] = True
k = perm[k]
return cycles
assert all(cycle_count(sattolo(list(range(8)))) == 1 for _ in range(10000))
counts = Counter(tuple(sattolo([0, 1, 2])) for _ in range(60000))
print(counts) # only (1, 2, 0) and (2, 0, 1), about 30000 eachWhere Cycles Are Useful
- Pointer-chasing benchmarks. Store
next[i] = p[i]and follow the chain. A single cycle guarantees the walk touches every slot in a cache-unfriendly order and never gets stuck in a short loop. - Rings and round-robin pairings. Give each person the "next" person in the cycle, for example in a gift exchange where everyone must be in one chain.
- Every element moves. A single cycle has no fixed points when n > 1. It does not produce every derangement, though. A derangement is any permutation with no fixed points, and that includes ones made of several smaller cycles.
Exercise
For n = 4, how many distinct outputs can Sattolo's algorithm produce, and what fraction of all permutations of 4 items is that?
Show answer
(4-1)! = 6 outputs out of 4! = 24 permutations, which is 1/4 of them.
Exercise
A Secret Santa tool needs a uniformly random derangement of 4 people. Is Sattolo's algorithm suitable?
Show answer
No. There are 9 derangements of 4 items, but Sattolo only produces the 6 that are single 4-cycles. It never produces derangements made of two 2-cycles, such as [1, 0, 3, 2], so it is not uniform over all derangements.