Sunday, August 20, 2006

Counterintuitive math puzzle of the day: Warning, get your morning cup of coffee before tackling this one:
The names of 100 prisoners are placed in 100 wooden boxes, one name to a box, and the boxes are lined up on a table in a room. One by one, the prisoners are led into the room. They may look into up to 50 of the boxes to try to find their own name, but must leave the room exactly as it was.

The prisoners are permitted no further communication after leaving the room. They do have a chance to plot a strategy in advance. Good thing. Unless they all find their own names, they will all be executed.

If each prisoner examines 50 boxes at random, the probability of the group's survival is a miniscule (1/2)^100, or about 0.00000000000000000000000000000008. Even worse, if they all happen to look into the same 50 boxes, their chances drop to zero.

However, there is a strategy that the prisoners can use to increase the probability of success to more than 30 percent. Incredible but true! The trick is to find this strategy.
Here's the answer:
Beforehand, the prisoners randomly assign "ownership" of one box to each prisoner. As a result, each of the 100 boxes now has a "label" on the outside.

Each prisoner goes to the box with his name on it. He finds another prisoner's name in the box. He then looks into the box labeled with that prisoner's name. He continues in this fashion until he finds his own name or ends up looking into 50 boxes.

Why does this procedure greatly increase the prisoners' chances of survival?

The random assignment of a box with a name in it to each prisoner is just a permutation of the 100 names, chosen uniformly at random from the set of all such permutations, Winkler explains.

So, in inspecting boxes, each prisoner follows a cycle of that permutation, beginning with his box. If they don't exceed the 50-box limit, they succeed in finding their own name. If the particular permutation chosen by the prisoners happens to have no cycle of length greater than 50, the process works, every prisoner finds his name, and no one gets executed.

Indeed, the probability that a random permutation of 2n objects has no cycle of length greater than n is at least 1 minus the natural logarithm of 2.

In this case, the probability of the prisoners surviving is a bit larger than (1 - ln 2). It comes to 31.18 percent.

The strategy works because the prisoners, in effect, impose a structure on their search and take advantage of a basic property of random permutations.
Some more detail can be found in this excellent paper (PDF format), "Seven puzzles you think you must not have heard correctly (with solutions)".