The derangement formula is one of the most elegant applications of the inclusion-exclusion principle, counting permutations where no element remains in its original position.
DefinitionDerangement
A derangement of a set {1,2,…,n} is a permutation σ such that σ(i)=i for all i∈{1,2,…,n}. The number of derangements of n objects is denoted Dn or !n.
Theorem: The number of derangements of n objects is:
Dn=n!∑k=0nk!(−1)k
Proof using Inclusion-Exclusion:
Let S be the set of all permutations of {1,2,…,n}, so ∣S∣=n!.
For each i∈{1,2,…,n}, let Ai be the set of permutations where element i is in position i (i.e., σ(i)=i).
We want to count permutations where no element is fixed, which is:
Dn=A1∪A2∪⋯∪An=n!−∣A1∪A2∪⋯∪An∣
We apply the inclusion-exclusion principle to compute ∣A1∪⋯∪An∣:
∣A1∪⋯∪An∣=∑k=1n(−1)k+1∑∣S∣=k⋂i∈SAi
For any subset S⊆{1,2,…,n} with ∣S∣=k, the intersection ⋂i∈SAi consists of permutations where all elements in S are fixed in their original positions. The remaining n−k elements can be arranged arbitrarily, giving (n−k)! permutations.
The number of subsets S with ∣S∣=k is (kn).
Therefore:
⋂i∈SAi=(n−k)!
and:
∑∣S∣=k⋂i∈SAi=(kn)(n−k)!
Substituting into the inclusion-exclusion formula:
∣A1∪⋯∪An∣=∑k=1n(−1)k+1(kn)(n−k)!
We can verify D3 using the formula:
D3=3!(1−1+21−61)=6⋅31=2 ✓
Remark
As n→∞, the sum ∑k=0nk!(−1)k converges to e−1=1/e. Therefore, Dn≈n!/e, and the probability that a random permutation is a derangement approaches 1/e≈0.368. This is the answer to the classic "hat check problem": if n people check their hats and the hats are returned randomly, the probability that no one gets their own hat back approaches 1/e, independent of n for large n. There's also a useful recurrence relation: Dn=(n−1)(Dn−1+Dn−2) for n≥2.