Video 2, chapter 16

The 100 Prisoners Puzzle

How the 100 prisoners problem is solved by the cycle-following strategy, which wins exactly when the random permutation in the boxes has no cycle longer than 50, about 31.18 percent of the time.

  • Video 2, chapter 16
  • Reason together

One hundred people must each find their own number among 100 closed boxes, but each person may open only 50 of them, and the group wins only if all 100 succeed. Once the search begins, nobody can pass on any information. If everyone opens 50 boxes at random, all of them succeed with probability (1/2)100(1/2)^{100}, less than one chance in 103010^{30}. This page explains a plan in which each person follows the numbers from box to box, shows that it wins exactly when the random arrangement has no cycle longer than 50, and computes that chance, about 31.18 percent.

Key terms

100 prisoners problem

Definition The 100 prisoners problem is a probability puzzle with fixed rules. There are 100 people, numbered 1 to 100, and a room with 100 boxes, also numbered 1 to 100. Each box holds one slip of paper, the slips carry the numbers 1 to 100, and every number is in exactly one box. The slips are placed by a uniformly random permutation, so each of the 100!100! possible arrangements is equally likely, and the arrangement does not depend on the group’s plan. Before the first person enters, the group may agree on a strategy, and after that its members cannot communicate in any way. The people enter the room one at a time. Each may open at most 50 boxes, one after another, choosing each box after seeing what the earlier ones held. Each person then leaves the room exactly as it was found, with every box closed, every slip in its own box and no marks left behind. The group wins only if every one of the 100 people opens the box that holds their own number. If each person opens 50 boxes chosen at random, independently of everyone else, the group wins with probability (1/2)100(1/2)^{100}. With the cycle-following strategy described below, the group wins with probability

1−∑k=511001k≈0.3118,1 - \sum_{k=51}^{100} \frac{1}{k} \approx 0.3118,

about 31.18 percent. This formula is exact for 100 people who may each open 50 boxes. The number 1−ln⁡2≈0.30691 - \ln 2 \approx 0.3069 that is often quoted is the limit of the same calculation for larger and larger groups in which each person may open half of the boxes, not the answer for 100 people.

In plain words Random searching is like 100 separate coin flips that must all come up heads. The cycle-following strategy leaves each person’s own chance at one half but links the outcomes, so that people succeed or fail in large groups, and in about 31 percent of arrangements everybody succeeds.

Example Under random searching, person 1 opens 50 of the 100 boxes, so the box with number 1 is among them with probability 50/100=1/250/100 = 1/2, whatever the arrangement. The choices are made independently, so the chance that all 100 people succeed is

(12)100≈7.9×10−31.\left(\frac{1}{2}\right)^{100} \approx 7.9 \times 10^{-31}.

Under cycle following each person also succeeds with probability exactly 1/21/2, yet the group wins with probability about 0.31180.3118, roughly 4×10294 \times 10^{29} times as often. A computer simulation of 200,000 uniformly random arrangements found a win in about 31.0 percent of them, close to the exact value.

Random permutation

Definition A permutation of the numbers 1 to nn is a rule π\pi that assigns to each number bb from 1 to nn a number π(b)\pi(b) from 1 to nn, in such a way that each number is assigned exactly once. In the puzzle, π(b)\pi(b) is the number on the slip in box bb. There are

n!=n(n−1)⋯2⋅1n! = n(n - 1) \cdots 2 \cdot 1

permutations of nn numbers. A permutation is uniformly random when each of the n!n! permutations has the same probability 1/n!1/n!. Then each box holds each number with probability 1/n1/n, but the boxes are not independent of one another, because no number can appear in two boxes.

In plain words A uniformly random permutation is what you get by shuffling the 100 slips thoroughly and dropping one into each box. No arrangement is more likely than any other.

Example Three boxes can be filled in 3!=63! = 6 ways. Listing the numbers in boxes 1, 2 and 3 in that order, the arrangements are 123, 132, 213, 231, 312 and 321, each with probability 1/61/6. Box 2 holds the number 2 in two of them, 123 and 321, so with probability 1/31/3. If box 1 is known to hold 1, only 123 and 132 remain, and box 2 holds 2 with probability 1/21/2. Learning about one box changed the chances for another, which shows that the boxes are not independent.

Permutation cycle

Definition Let π\pi be a permutation of the numbers 1 to nn, and start at a number bb. Applying π\pi again and again gives the sequence b,π(b),π(π(b)),…b, \pi(b), \pi(\pi(b)), \ldots, and since there are only nn numbers, some number must eventually repeat. The first number to repeat is bb itself, because π\pi never sends two different numbers to the same number. If the sequence first returns to bb after kk steps, the kk different numbers it visits form the cycle containing bb, and kk is the length of that cycle. Every number lies in exactly one cycle, so the cycles split the numbers 1 to nn into separate groups whose lengths add up to nn. A cycle is written in parentheses in the order it is followed, so (1 4 2)(1\ 4\ 2) means that 1 goes to 4, 4 goes to 2 and 2 goes back to 1. For any length kk with n/2<k≤nn/2 < k \le n, a uniformly random permutation of nn numbers has a cycle of length kk with probability exactly 1/k1/k. To count these permutations, choose the kk numbers of the cycle in (nk)\binom{n}{k} ways, link them into a cycle in (k−1)!(k - 1)! ways, and arrange the other n−kn - k numbers in any of (n−k)!(n - k)! ways. The factor (k−1)!(k - 1)! appears because the kk chosen numbers can be listed in k!k! orders and each cycle appears kk times among them, once for each starting number. A permutation cannot have two cycles longer than n/2n/2, since together they would need more than nn numbers, so no permutation is counted twice, and the count is

(nk)(k−1)! (n−k)!=n! (k−1)!k!=n!k.\begin{aligned} &\binom{n}{k}(k - 1)!\,(n - k)! \\[0.4em] &\quad = \frac{n!\,(k - 1)!}{k!} = \frac{n!}{k}. \end{aligned}

Dividing by the n!n! equally likely permutations gives 1/k1/k.

In plain words Start at any box, read its slip, go to the box with that number and keep going. Every box number is written on exactly one slip, so the path can never run into a loop partway along. It must come back to the box where it started, and the boxes it passes through form that box’s cycle.

Example Take six boxes holding 4, 1, 6, 2, 5 and 3, in that order, so box 1 holds 4, box 2 holds 1, box 3 holds 6, and so on. Starting at 1 gives 1, 4, 2 and then 1 again, so one cycle is (1 4 2)(1\ 4\ 2). Starting at 3 gives the cycle (3 6)(3\ 6), and box 5 holds its own number, which makes (5)(5) a cycle of length 1. The lengths 3, 2 and 1 add up to 6. To check the probability rule, take n=4n = 4. There are (43)⋅2!⋅1!=8\binom{4}{3} \cdot 2! \cdot 1! = 8 permutations with a cycle of length 3 and 3!=63! = 6 with a cycle of length 4, out of 4!=244! = 24, so the probabilities are 8/24=1/38/24 = 1/3 and 6/24=1/46/24 = 1/4, as the rule says.

Cycle-following strategy

Definition In the cycle-following strategy for the 100 prisoners problem, person ii first opens box ii. Whenever the slip shows a number jj other than ii, the next box that person opens is box jj, and the search ends when the slip with ii appears or after 50 boxes. The boxes opened are i,π(i),π(π(i))i, \pi(i), \pi(\pi(i)) and so on, which is the cycle containing ii, followed from box ii. The slip with number ii lies in the last box of that cycle, the one that leads back to box ii, so person ii finds it on opening number kk, where kk is the length of the cycle containing ii. Hence person ii succeeds exactly when that cycle has length at most 50, and the whole group wins exactly when every cycle of π\pi has length at most 50. The group loses exactly when there is a cycle of some length kk from 51 to 100. No two of these 50 cases can happen together, because two such cycles would need more than 100 numbers, and each has probability 1/k1/k, so

P(group wins)=1−∑k=511001k≈0.3118.\begin{aligned} &P(\text{group wins}) \\[0.4em] &\quad = 1 - \sum_{k=51}^{100} \frac{1}{k} \approx 0.3118. \end{aligned}

Each person alone still succeeds with probability exactly 1/21/2, because the cycle containing person ii is equally likely to have any length from 1 to 100. For each length kk, the other k−1k - 1 members of that cycle can be chosen and placed after ii in (99k−1)(k−1)!\binom{99}{k-1}(k - 1)! ways and the remaining numbers arranged in (100−k)!(100 - k)! ways, which multiply to 99!99!, one hundredth of all permutations. With 2n2n people who may each open nn boxes, the same argument gives the winning probability

1−∑k=n+12n1k,1 - \sum_{k=n+1}^{2n} \frac{1}{k},

which is 1/21/2 for two people and decreases as nn grows, approaching 1−ln⁡2≈0.30691 - \ln 2 \approx 0.3069 without ever reaching it. The calculation needs every rule of the puzzle and the uniformly random arrangement. If whoever fills the boxes knows the strategy and wants it to fail, putting all 100 numbers in one cycle defeats it every time. The group can protect itself by agreeing on a uniformly random permutation σ\sigma, chosen independently of the arrangement and kept secret from whoever fills the boxes. Person ii then starts at box σ(i)\sigma(i) and, after finding a number jj other than ii, opens box σ(j)\sigma(j) next. For every arrangement of the slips, the group then wins with the same probability, about 0.31180.3118.

In plain words Each person walks around their own loop of boxes and meets their number just before the loop closes. People in a loop of at most 50 boxes all succeed, and people in a longer loop all fail, so the whole outcome depends on one feature of the arrangement, the length of its longest cycle. The strategy changes nothing in the boxes and sends no messages. It only makes everyone read the same hidden structure in the same way.

Example Return to the six boxes holding 4, 1, 6, 2, 5 and 3, and let each person open at most 3 boxes. Person 1 opens box 1 and finds 4, opens box 4 and finds 2, then opens box 2 and finds 1. Person 4 goes around the same cycle from a different point, opening boxes 4, 2 and 1, and person 2 opens boxes 2, 1 and 4. Persons 3 and 6 need two openings each and person 5 needs one, so all six succeed. If the boxes held 2, 3, 4, 1, 5 and 6 instead, the cycle (1 2 3 4)(1\ 2\ 3\ 4) would have length 4, and persons 1, 2, 3 and 4 would all fail while persons 5 and 6 succeed. Over all 720 arrangements of six boxes, cycle following wins in 276, in agreement with the formula,

1−(14+15+16)=2360=276720.\begin{aligned} &1 - \left(\frac14 + \frac15 + \frac16\right) \\[0.4em] &\quad = \frac{23}{60} = \frac{276}{720}. \end{aligned}

Random searching, with 3 of the 6 boxes per person, would win with probability only (1/2)6=1/64(1/2)^6 = 1/64.

Check yourself

  1. Change the numbers. Eight people play with eight boxes, and each may open four of them. Find the probability that the cycle-following strategy wins, and compare it with random searching.
Show the answer

The group loses exactly when the permutation has a cycle of length 5, 6, 7 or 8. Each of these lengths is more than half of 8, so at most one such cycle can occur, and a cycle of length kk appears with probability 1/k1/k. The four failure probabilities add up to

15+16+17+18=533840,\frac15 + \frac16 + \frac17 + \frac18 = \frac{533}{840},

so the group wins with probability 1−533/840=307/8401 - 533/840 = 307/840, about 0.3650.365. In other words, 14,736 of the 40,320 arrangements lead to a win. Random searching wins only when all eight people succeed, each with probability 4/8=1/24/8 = 1/2 independently, which has probability (1/2)8=1/256(1/2)^8 = 1/256, about 0.00390.0039. Cycle following is about 94 times as likely to win. The value for eight people is higher than the one for 100, as expected from the decrease toward 1−ln⁡21 - \ln 2.

  1. Change one rule. Suppose everyone agrees to start at box 1, not at the box with their own number, and then follows the numbers as before, with at most 50 openings each. What is the probability that the group wins?
Show the answer

The probability is 0. Everyone now opens the same boxes in the same order, box 1, then the box named by its slip, and so on, and a person stops early only on finding their own number. So every search stays within the same first 50 boxes along the path from box 1. Those boxes hold at most 50 different numbers, so at most 50 people can find their own number, and the group always loses. Starting at your own box is what makes the plan work. The slip with your number lies in the box that leads back to your box, so your path is sure to reach it, and the only question is how long that path is.

  1. Extend the plan. Keep 100 people, but allow each of them to open mm boxes, where mm is between 50 and 100. What is the smallest mm for which cycle following wins with probability at least one half?
Show the answer

With mm openings, person ii succeeds exactly when the cycle containing ii has length at most mm, so the group loses exactly when some cycle is longer than mm. Because m≥50m \ge 50, every such cycle is longer than half of 100, so at most one can occur, and

P(group wins)=1−∑k=m+11001k.\begin{aligned} &P(\text{group wins}) \\[0.4em] &\quad = 1 - \sum_{k=m+1}^{100} \frac{1}{k}. \end{aligned}

For m=60m = 60 the sum of 1/611/61 through 1/1001/100 is about 0.50750.5075, so the group wins with probability about 0.49250.4925. For m=61m = 61 the sum loses the term 1/611/61, and the winning probability rises to about 0.50890.5089. The smallest such mm is 61. The sum is close to ln⁡(100/m)\ln(100/m), which equals 1/21/2 at m=100/e≈60.7m = 100/\sqrt{e} \approx 60.7, so the answer matches what the logarithm suggests. For mm below 50 this formula no longer applies, because several long cycles could then occur together, and the sum would count such arrangements more than once.