Hat-Check Problem

Problem
The traditional wording of the question is as follows.

A hat-check girl completely loses track of which of $$n$$ hats belong to which owners, and hands them back at random to their $$n$$ owners as the latter leave.

What is the probability $$p_n$$ that nobody receives their own hat back?

Solution
Put into bald mathematical language, this boils down to:

For a set $$S$$ of $$n$$ elements, what is the number of derangements of $$S$$ divided by the number of permutations of $$S$$?

The total number of permutations is $$n!$$

That is, we want to find $$p_n$$, defined as: $$p_n = \frac {D_n} {n!}$$ = number of derangements of n / total number of permutations = Dn / n! --> 1 / e (for large n)

A derangement is a permutation that leaves none of the elements in its place.

To compute the total number of derangements, we compute the number of permutations that leave elements in the same place.

Call Pi the property that Xi is mapped to Yi at the same position.

N(Pi) = (n-1)! and there are C(n, 1) terms.

(If we consider all the permutations of a set where the property Pi holds, we have fixed a single element of the set, so there are (n-1)! possible permutations. Furthermore, we could have chosen i as any element in the set so there are C(n, 1) terms to choose from).

N(Pi, Pj) = (n-2)! and there are C(n, 2) terms.

So now we can compute the total number of derangements.

Dn = n! - C(n,1)(n-1)! + C(n,2)(n-2)! - ... + (-1)^n C(n,n)(n-n)! = n! – [n!/1!(n-1)!](n-1)! + [n!/2!(n-2)!](n-2)! ... = n!(1 - 1/1! + 1/2! -1/3! + ... + (-1)^n 1/n!)

and

1 - 1/1! + 1/2! -1/3! + ... + (-1)^n 1/n! converges to 1/e