Often, a mathematical puzzle or problem is interesting because the *answer* is interesting. The recent card tricks and other prisoner puzzles discussed lately are of this type. There is a solution when it seems none exists, or we can succeed with a much higher probability than intuition would suggest. As Peter Winkler puts it, these are “puzzles you think you must not have heard correctly.”

Other problems, like the one presented last week, are interesting because the *method of solution* is interesting. The actual answer may not be terribly surprising, but the process of arriving at the answer involves something surprisingly simple, elegant, and/or useful in other applications. (Dick Lipton at *Gödel’s Lost Letter* has an interesting article about historical examples of this in mathematics and computer science, where “the proof is more important than the result.”)

In last week’s problem, we were asked for the probability of successfully unlocking all of boxes, given that we may initially select and break into of them. The answer is rather unsurprising; in fact, it is what you might guess the answer to be without any thought: the probability is . But I like this problem anyway, because *showing* that the probability is involves a very simple yet handy idea, that of the *canonical cycle representation* of a permutation.

First, we can represent the random distribution of keys into boxes by a randomly selected permutation , where box contains the key that unlocks box . There are several different ways to write such a permutation. For example, we can simply list the image elements in order.

Another representation that is more convenient for this problem is to decompose the permutation into cycles, where each element is mapped to the element appearing immediately after it in the same cycle (wrapping around if necessary). Using parentheses to group elements in a cycle, the following notations represent the same permutation:

Cycles are important in this prisoner puzzle. If we assume WLOG that we break open boxes 1 through , then we can unlock all of the remaining boxes if and only if every cycle in contains an element in . We can find the desired probability by counting the number of permutations that have this property.

There are two disadvantages of cycle representations of a permutation. First, we need the parentheses to indicate which elements belong to the same cycle. Thinking like a computer scientist, if we want to store a permutation as a simple linear array of elements, then the cycle representation requires storing additional information about where the parentheses go.

The second disadvantage is that cycle representations are not unique. It does not matter in what order we write the cycles; nor does it matter which element we write first in any particular cycle. For example, all of the following represent the same permutation:

The *canonical cycle representation* simply fixes a particular ordering of cycles and elements within each cycle, by writing each cycle with its least element first, and ordering the cycles from left to right in *decreasing* order of first element. For example, the last of the three representations above is the “canonical” one. (It is worth pointing out that an equally workable “canonical” representation would be to write each cycle with its *largest* element first, and order the cycles from left to right in *increasing* order of first element.)

This representation solves both of our problems: it is unique… and it does not require the parentheses! That is, each of the possible lists of elements corresponds to a unique permutation whose canonical cycle representation has that same ordering of elements.

Coming back to the puzzle again, we can now restate the desired property of a permutation very simply in terms of its canonical cycle representation. We can unlock all of the remaining boxes if and only if the first element in the canonical cycle representation of the corresponding permutation is at most . The number of permutations with this property is , and so the probability is .