Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

This famous problem has been stated variously in terms of hats and people, letters and envelopes, tea cups and saucers – indeed, any situation in which you might want to match two kinds of items seems to have appeared somewhere as a setting for the matching problem.

In the letter-envelope setting there are nn letters labeled 1 through nn and also nn envelopes labeled 1 through nn. The letters are permuted randomly into the envelopes, one letter per envelope (a mishap usually blamed on an unfortunate hypothetical secretary), so that all permutations are equally likely. The main questions are about the number of letters that are placed into their matching envelopes.

“Real life” settings aside, the problem is about the number of fixed points of a random permutation. A fixed point is an element whose position is unchanged by the shuffle.

5.3.1Matches at Fixed Locations

Consider a random permutation of nn elements which for simplicity we will call {1,2,,n}\{1, 2, \ldots , n\}. For any ii in the range 1 through nn, what is the chance that Position ii is a fixed point? In other words, what is the chance that letter ii falls in envelope ii?

🎥 See More
Loading...

We know that there are n!n! possible permutations, all of which are equally likely. To find P(match at Position i)P(\text{match at Position }i) all we have to do is count the number of permutations that put letter ii in envelope ii. Here is a good way to count these:

  • Put letter ii in envelope ii.

  • Once that is done, the remaining n1n-1 letters can be permuted in (n1)!(n-1)! ways.

So

P(match at Position i)=(n1)!n!=1nP(\text{match at Position }i) = \frac{(n-1)!}{n!} = \frac{1}{n}

Notice the absence of ii from the answer. No matter which position you fix, the chance of a match at that position is 1/n1/n. This formalizes the intuitive notion that each letter is equally likely to fall in any envelope, so the chance that it falls in the matching envelope is 1/n1/n.

Now fix any pair of positions iji \ne j. To find P(matches at Positions i and j)P(\text{matches at Positions } i \text{ and } j), extend the method we used above:

  • Put letter ii in envelope ii and letter jj in envelope jj.

  • Once that is done, the remaining n2n-2 letters can be permuted in (n2)!(n-2)! ways.

So

P(matches at Positions i and j)=(n2)!n!=1n1n1P(\text{matches at Positions } i \text{ and } j) = \frac{(n-2)!}{n!} = \frac{1}{n} \cdot \frac{1}{n-1}

The second term in the product is P(match at jmatch at i)P(\text{match at } j \mid \text{match at } i) and is just the chance of a match at a fixed spot in the reduced set of n1n-1 letters after letter ii and envelope ii have been removed.

You should check by induction that for k=1,2,,nk = 1, 2, \ldots , n,

P(matches at a specified set of k positions)=1n1n11nk+1P(\text{matches at a specified set of } k \text{ positions}) = \frac{1}{n} \cdot \frac{1}{n-1} \cdot \cdots \cdot \frac{1}{n-k+1}

5.3.2No Matches

If letters falling in the right envelopes are good events, then the worst possible event is every letter falling in a wrong envelope. That is the event that there are no matches, and is called a derangement. Let’s find the chance of a derangement.

🎥 See More
Loading...

The key is to notice that the complement is a union, and then use the inclusion-exclusion formula.

P(no match)=1P(at least one match)=1P(i=1n{match at Position i})=1P(i=1nAi)\begin{align*}P(\text{no match}) &= 1 - P(\text{at least one match}) \\ &= 1 - P\big(\bigcup_{i=1}^n \{\text{match at Position } i\} \big) \\ &= 1 - P\big(\bigcup_{i=1}^n A_i \big) \end{align*}

where AiA_i is the event “match at Position ii”.

By the inclusion-exclusion formula and our calculations above,

P(i=1nAi)=i=1nP(Ai)1i<jnP(AiAj)+1i<j<knP(AiAjAj)+(1)n+1P(A1A2An)=i=1n1n1i<jn1n1n1+1i<j<kn1n1n11n2+(1)n+11n!\begin{align*} & P\big(\bigcup_{i=1}^n A_i \big) \\ &= \sum_{i=1}^n P(A_i) - \mathop{\sum \sum}_{1 \le i < j \le n} P(A_iA_j) + \mathop{\sum \sum \sum}_{1 \le i < j < k \le n} P(A_iA_jA_j) - \cdots + (-1)^{n+1} P(A_1A_2 \ldots A_n) \\ &= \sum_{i=1}^n \frac{1}{n} - \mathop{\sum \sum}_{1 \le i < j \le n} \frac{1}{n} \cdot \frac{1}{n-1} + \mathop{\sum \sum \sum}_{1 \le i < j < k \le n} \frac{1}{n} \cdot \frac{1}{n-1} \cdot \frac{1}{n-2} - \cdots + (-1)^{n+1} \frac{1}{n!} \end{align*}

If those sums look hair-raising, look again. None of the terms being added has an index (ii, jj, etc) in it! Each sum consists of adding a constant value multiple times, and is therefore equal to the constant times the number of terms in the sum.

The number of terms in the first sum is nn. As we observed in an earlier section, the number of terms being added in the second sum is

n(n1)2!\frac{n(n-1)}{2!}

In the third sum the number of terms is

n(n1)(n2)3!\frac{n(n-1)(n-2)}{3!}

and so on. Therefore

P(i=1nAi)=n1n  n(n1)2!1n1n1 + n(n1)(n2)3!1n1n11n2  +(1)n+11n!=112!+13!(1)n+11n!\begin{align*} & P\big(\bigcup_{i=1}^n A_i \big) \\ \\ &= n \cdot \frac{1}{n} ~-~ \frac{n(n-1)}{2!} \cdot \frac{1}{n} \cdot \frac{1}{n-1} ~+~ \frac{n(n-1)(n-2)}{3!} \cdot \frac{1}{n} \cdot \frac{1}{n-1} \cdot \frac{1}{n-2} ~-~ \cdots + (-1)^{n+1} \frac{1}{n!} \\ \\ &= 1 - \frac{1}{2!} + \frac{1}{3!} - \cdots (-1)^{n+1}\frac{1}{n!} \end{align*}

Remember that

P(i=1nAi)=P(at least one match)P\big(\bigcup_{i=1}^n A_i \big) = P(\text{at least one match})

So the chance of a derangement is

P(no match)=1(112!+13!(1)n+11n!)=11+12!13!+(1)n1n!e1\begin{align*} P(\text{no match}) &= 1 - \big(1 - \frac{1}{2!} + \frac{1}{3!} - \cdots (-1)^{n+1}\frac{1}{n!}\big) \\ &= 1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \cdots (-1)^n\frac{1}{n!} \\ &\sim e^{-1} \end{align*}

when nn is large.

In the language of random variables, let MnM_n be the number of fixed points (matches) in a random permutation of nn elements. Then for every n1n \ge 1 we have an exact formula for the chance that MnM_n is 0:

P(Mn=0)=11+12!13!+(1)n1n!P(M_n = 0) = 1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \cdots (-1)^n\frac{1}{n!}

For large nn, we also have an approximation:

P(Mn=0)e1=36.8%P(M_n = 0) \sim e^{-1} = 36.8\%

roughly. When nn is large, about 36.8% of all permutations of nn elements move all of the elements away from their original positions.

5.3.3kk Matches

For 0kn0 \le k \le n, you can find P(Mn=k)P(M_n = k) by using the following observations.

  • There are (nk)\binom{n}{k} ways of fixing the kk places for the matches.

  • Once the places have been fixed, you have to get a match at those kk places; the chance of that is 1/(n(n1)(nk+1))1/(n(n-1) \cdots (n-k+1)).

  • Given that there are matches at those kk places, there are nkn-k letters left, with the corresponding nkn-k envelopes, and there has to be a derangement of these. The conditional chance is equal to P(Mnk=0)P(M_{n-k} = 0).

So for a fixed kk in the range 0kn0 \le k \le n,

P(Mn=k)=(nk)1n(n1)(nk+1)(11+12!13!+(1)nk1(nk)!)=1k!(11+12!13!+(1)nk1(nk)!)1k!e1         for large n\begin{align*} & P(M_n = k) \\ &= \binom{n}{k} \cdot \frac{1}{n(n-1) \cdots (n-k+1)} \cdot \big( 1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \cdots (-1)^{n-k}\frac{1}{(n-k)!} \big) \\ &= \frac{1}{k!} \cdot \big( 1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \cdots (-1)^{n-k}\frac{1}{(n-k)!} \big) \\ &\approx \frac{1}{k!} e^{-1} ~~~~~~~~~ \text{for large } n \end{align*}

We will see later that these probabilities form a Poisson distribution on the infinite set of non-negative integers.