A puzzle that sounds impossible at first reading: 100 prisoners are numbered through . There are sealed boxes in a room, each containing one of the numbers through , randomly assigned (each used exactly once). Each prisoner enters the room one at a time, opens at most boxes, and must find the box containing their own number. If even one prisoner fails, all are executed. The prisoners may agree on a strategy beforehand but may not communicate once the procession starts.
Random guessing gives each prisoner a chance of finding their number, so the probability of all succeeding is . Effectively zero.
A specific strategy succeeds with probability .
The strategy
Number the boxes through . Prisoner first opens box . They read the number inside, say . Next they open box , read , then box , and so on, until they either find their own number or have opened boxes.
That is the entire strategy.
Where the success rate comes from
The contents of the boxes define a permutation of , where is the number in box . Prisoner , following the strategy, traverses the cycle of that contains :
where is the length of that cycle. Prisoner finds their own number at step , which fits in the -box budget iff .
Because every prisoner uses the same strategy, all prisoners succeed iff every cycle of has length at most . The fate of all prisoners is determined by a single property of one random permutation: whether its longest cycle exceeds .
The probability
For a uniformly random permutation of , the probability that there is at least one cycle of length exactly is when . The counting argument: the number of permutations of that contain a fixed -element subset arranged as a single cycle is . The number of such -subsets is . Multiplying and dividing by gives .
When , at most one cycle of that length can exist (two would overflow the permutation). So the events “permutation has a cycle of length ” for distinct are disjoint, and
where is the length of the longest cycle and is the -th harmonic number. As with , this approaches , so
For specifically, the exact value is .
The strategy converts the prisoners’ problem from chaos into a single check on the cycle structure of a random permutation.
Press new trial to draw a random permutation. The grid below shows what each prisoner would find inside each box. Boxes are colored by which cycle of the permutation they belong to. The cycle outlined in red is the longest one. The trial succeeds if and only if no cycle exceeds . Press + 1000 trials to accumulate statistics.
A note on optimality: this is also (asymptotically) the best possible strategy. No strategy can succeed with probability much above in the large- limit (Curtin and Warshauer, 2006). The cycle-following strategy is essentially as good as the prisoners can do.
References
- Anna Gál, Peter Bro Miltersen. The cell probe complexity of succinct data structures. Theoretical Computer Science, 379(3):405-417, 2007. (Where the puzzle first appears in the literature.)
- Eugene Curtin, Max Warshauer. The locker puzzle. The Mathematical Intelligencer, 28(1):28-31, 2006.
- Philippe Flajolet, Robert Sedgewick. Analytic Combinatorics. Cambridge University Press, 2009. Chapter II covers the cycle structure of random permutations in detail.