puzzle #9
In this lesson

Wide tables, equations, and code scroll sideways. Swipe, or Tab to focus them and use the left and right arrow keys.

100 prisoners and boxes

You will learn: Follow permutation cycles and explain the dependence behind collective success.

Start with: Derangements · Joint and marginal distributions

One hundred prisoners face one hundred labeled boxes. A uniformly random permutation places each prisoner’s number in exactly one box. Each prisoner enters alone and may open at most fifty boxes. The group succeeds only if everyone finds their own number. They can agree on a strategy beforehand but cannot communicate, mark boxes, or rearrange contents during the attempts.

Opening an independently chosen random half of the boxes gives each prisoner success probability one half. The chance that all those independent choices work is 2⁻¹⁰⁰≈7.89×10⁻³¹. The cycle strategy keeps the individual success probability at one half while dramatically changing the dependence between successes. Review joint events and independence and permutations.

Each prisoner may open 50 boxes. One uniformly shuffled permutation is shared by every prisoner. Changing its size or seed restarts the selected prisoner's revealed route; the boxes are never rearranged during a route. If the selected label exceeds a new smaller N, selection moves to N.

Start with box 1.

Inspect all cycles (reveals the solution)
Cycle lengths in this one permutation relative to the opening budget00.20.40.60.8120406080100cycle lengthnumber of cycles

Filled blue cycle lengths fit the budget. Outlined red ones exceed it. All prisoners succeed in this permutation exactly when every cycle fits: all succeed.

  • Cycle 1, length 25: 1 → 86 → 45 → 78 → 5 → 59 → 85 → 15 → 79 → 94 → 26 → 19 → 88 → 28 → 75 → 53 → 93 → 31 → 10 → 74 → 35 → 11 → 38 → 34 → 54 → 1.
  • Cycle 2, length 47: 2 → 67 → 76 → 33 → 72 → 68 → 52 → 13 → 21 → 100 → 40 → 62 → 60 → 32 → 7 → 97 → 16 → 8 → 70 → 55 → 69 → 46 → 71 → 39 → 84 → 42 → 96 → 14 → 20 → 25 → 47 → 81 → 36 → 80 → 44 → 92 → 89 → 65 → 6 → 17 → 24 → 4 → 29 → 41 → 30 → 90 → 18 → 2.
  • Cycle 3, length 21: 3 → 56 → 95 → 99 → 61 → 43 → 83 → 48 → 98 → 27 → 58 → 64 → 22 → 82 → 57 → 50 → 63 → 9 → 66 → 12 → 91 → 3.
  • Cycle 4, length 7: 23 → 87 → 51 → 73 → 37 → 49 → 77 → 23.
Box labelNumber inside
186
267
356
429
559
617
797
870
966
1074
1138
1291
1321
1420
1579
168
1724
182
1988
2025
21100
2282
2387
244
2547
2619
2758
2875
2941
3090
3110
327
3372
3454
3511
3680
3749
3834
3984
4062
4130
4296
4383
4492
4578
4671
4781
4898
4977
5063
5173
5213
5393
541
5569
5695
5750
5864
5985
6032
6143
6260
639
6422
656
6612
6776
6852
6946
7055
7139
7268
7337
7435
7553
7633
7723
785
7994
8044
8136
8257
8348
8442
8515
8645
8751
8828
8965
9018
913
9289
9331
9426
9599
9614
9716
9827
9961
10040
Probability over uniform permutations / choicesValue
One prisoner's cycle strategy succeeds0.5
Every prisoner succeeds with cycle strategy0.3118278
Every prisoner succeeds with independent random half-box choices7.888609e-31
A labeled permutation puzzle with no communication or changes to boxes between prisoners. The seed displays one example; the group probabilities are exact model calculations, not estimates from that one example.

Let the numbers tell you where to go

Prisoner i begins with box i. If it contains j, the next box is j; continue until finding i or exhausting the budget. A permutation splits into disjoint cycles, so this route follows the unique cycle containing i. The prisoner finds their own number when that cycle closes.

For six boxes containing [3,1,2,5,4,6], the cycles are 1→3→2→1, 4→5→4, and 6→6. Every cycle has length at most three, so all six prisoners succeed with three openings each. Prisoner 1 opens boxes 1,3,2 and finds slips 3,2,1. A cycle of length four would make every member of that cycle fail under the same budget.

The important event is therefore that the longest cycle has length at most N/2. Individual successes are highly dependent: everyone in a given cycle succeeds or fails together. Multiplying their marginal probabilities would discard exactly the dependence the strategy exploits.

Make a prediction

If one prisoner succeeds, have you proved the whole group succeeds?

Explore the answer

No. You have learned that this prisoner’s cycle fits within the budget. Another disjoint cycle can still be too long. Inspect every cycle, or equivalently the maximum cycle length, to decide the group’s outcome for the displayed permutation.

Count the bad cycles

For a specified length k, choose its k labels, arrange them into a cycle in (k−1)! ways, and permute the remaining labels in (N−k)! ways. Dividing by N! gives expected number of k-cycles:

(Nk)(k−1)!(N−k)!N!=1k.\binom Nk\frac{(k-1)!(N-k)!}{N!}=\frac1k.

When k>N/2, there can be at most one such cycle. Its expected count then equals its occurrence probability. Events for different lengths greater than N/2 are mutually exclusive, because two such cycles cannot fit in N labels. Hence, for even N:

P(all succeed)=1−∑k=N/2+1N1k.P(\text{all succeed})=1-\sum_{k=N/2+1}^{N}\frac1k.

For N=4 the value is 1−1/3−1/4=5/12, corresponding to ten of the twenty-four permutations. For N=100 it is approximately 0.311828, or 31.18%. As even N grows, the sum approaches ln 2, so success approaches 1−ln 2≈30.69%.

For a fixed prisoner, their cycle length is uniform on 1,…,N: count cycles containing that label to get (N−1)! permutations for each possible length. Exactly half of those lengths fit. The improved group probability comes from coordination, not an improved individual marginal chance.

Which assumptions carry the result?

The probability is over uniform permutations, not over the single permutation on screen. An adversary who knows the labels and deliberately chooses a long cycle can defeat this fixed strategy. Likewise, permitting communication, moving slips, or changing the opening budget defines another problem.

The short failure formula above uses the half-box budget. With a substantially smaller budget, several over-budget cycles can coexist, so simply adding their expected counts would overcount failure. This experiment varies even N while keeping the budget exactly N/2.

Make a prediction

Does a 31% chance mean that about 31 prisoners survive in each game?

Explore the answer

No. The event is collective: all N succeed, or the group fails. It is a probability across repeated permutations, not an expected fraction of individual successful prisoners.

Sources

Richard Stanley’s algebraic-combinatorics notes, section 12.1 present the cycle strategy and its probability calculation. The six-box walk and four-box enumeration above make the mechanism directly inspectable.

Reset all settings