The general inclusion–exclusion formula and how to use it to count surjections, partial derangements and seatings with forbidden neighbours.
18 min
Why the signs alternate
Inclusion–exclusion counts objects that have none of a list of "bad" properties A1,…,Am. Subtracting each ∣Ai∣ removes objects with two bad properties twice, so you add the pairwise intersections back. That puts objects with three properties back in too often, so you subtract triples, and so on.
The signs are exactly right because of one identity. Take an object with exactly r≥1 of the properties. In the alternating sum it is counted
1−(1r)+(2r)−⋯+(−1)r(rr)=(1−1)r=0
times, and an object with no bad property is counted once (by the 1 term, the whole set). Every correct application comes down to this.
The formula
Let N be the size of the universe and, for a set of indices I, let N(I)=⋂i∈IAi (with N(∅)=N). Define Sk=∑∣I∣=kN(I). Then
#{objects with no property}=k=0∑m(−1)kSk,i⋃Ai=k=1∑m(−1)k+1Sk.
The formula is useful when the intersections are easy to count even though the union is not. The workhorse case is symmetric: if N(I) depends only on ∣I∣=k, call it Nk, then Sk=(km)Nk and
#{none}=k=0∑m(−1)k(km)Nk.
The interview recipe is always the same: (1) name the bad properties, (2) count what you get when you force a given set of k of them to hold, (3) plug into the alternating sum.
Worked example
integers coprime to 30
How many integers in {1,…,1000} are divisible by none of 2, 3 and 5?
Show solutionHide solution
Bad properties: A2,A3,A5 = "divisible by 2, 3, 5". Forcing a set of them means divisibility by their product, and the count of multiples of d up to 1000 is ⌊1000/d⌋:
k
intersections
Sk
0
everything
1000
1
500+333+200
1033
2
⌊1000/6⌋+⌊1000/10⌋+⌊1000/15⌋=166+100+66
332
3
⌊1000/30⌋
33
So the answer is 1000−1033+332−33=266.
Sanity check: the density of such integers is (1−21)(1−31)(1−51)=308, and 1000⋅308≈266.7. The floors cause the small difference. (This product is exactly Euler's φ(n)/n, which is inclusion–exclusion over the prime factors of n.)
Surjections: every box gets something
How many functions from an n-set onto a k-set are there (every one of the k targets is hit)? Let Aj = "target j is missed". Forcing a given set of j targets to be missed leaves (k−j)n functions, so
Surj(n,k)=j=0∑k(−1)j(jk)(k−j)n.
Dividing by k! removes the labels on the targets and gives the Stirling number of the second kindS(n,k), the number of ways to partition n labelled items into k non-empty unlabelled groups. For example, putting 5 distinct balls into 3 distinct boxes with none empty gives 35−3⋅25+3⋅15=150 ways, and S(5,3)=150/6=25.
This is the "coupon collector after a fixed number of draws" question: P(all k types seen in n draws)=Surj(n,k)/kn.
Worked example
all six faces in ten rolls
You roll a fair die 10 times. What is the probability that every face appears at least once?
Show solutionHide solution
With Aj = "face j never appears", forcing j given faces to be missing gives (6−j)10 sequences. So the number of good sequences is
A quick estimate from the first two terms (the Bonferroni bound) is 1−6(5/6)10≈1−0.969=0.031, which is useless here: when the single-event probabilities are large, the higher-order terms matter. Compare the expected number of missing faces, 6(5/6)10≈0.97; a Poisson approximation e−0.97≈0.38 is only rough because the events are not rare.
Derangements, more carefully
The previous lesson derived Dn=n!∑k=0n(−1)k/k! for permutations with no fixed point. Three further facts come up constantly.
1. Nearest integer. The tail of the series for e−1 after the k=n term is alternating with decreasing terms, so ∣Dn−n!/e∣<n+11≤21. Hence Dn is the nearest integer to n!/e for every n≥1, and Dn/n! converges to 1/e extremely fast: the error is below 1/(n+1)!.
2. Recurrence. Look at where element 1 goes, say to j (n−1 choices). Either j goes back to 1, leaving a derangement of the other n−2, or it does not, which is equivalent to deranging n−1 elements (treat "j may not go to 1" as j's forbidden spot). So
Dn=(n−1)(Dn−1+Dn−2),D0=1,D1=0,
which also simplifies to Dn=nDn−1+(−1)n. The sequence runs 1,0,1,2,9,44,265,1854,14833.
3. Exactly k fixed points. Choose the fixed points, then derange the rest:
#{exactly k fixed}=(kn)Dn−k,P=k!1⋅(n−k)!Dn−k→k!e−1.
The number of fixed points of a random permutation is asymptotically Poisson(1). Consistent with that, its mean is exactly 1 for every n (by linearity) and its variance is exactly 1 for every n≥2.
Worked example
exactly two hats returned
Six people check their hats and get them back in a uniformly random order. What is the probability that exactly two people get their own hat?
Show solutionHide solution
Choose the two lucky people: (26)=15. The other four must all be wrong, which is a derangement of 4: D4=9. So
P=6!15⋅9=720135=163=0.1875.
The Poisson limit gives e−1/2!≈0.184, already close at n=6.
Say the structure out loud in an interview: "choose who is fixed, derange the rest". It generalises to "exactly k of the m properties hold", where the count is ∑j≥k(−1)j−k(kj)Sj.
Knowledge check
How many ways can 5 distinct balls be placed in 3 distinct boxes so that no box is empty?
Knowledge check
Seven letters are placed at random into their seven addressed envelopes, one per envelope. How many of the arrangements put every letter in a wrong envelope?
Knowledge check
For a uniformly random permutation of a large number of elements, what is the probability (to three decimals) that it has exactly one fixed point?
Key takeaways
Inclusion–exclusion works because an object with r≥1 bad properties is counted (1−1)r=0 times.
Recipe: name the bad properties, count the objects when a given k of them are forced, alternate signs. In the symmetric case the answer is ∑k(−1)k(km)Nk.
Surjections onto k targets: ∑j(−1)j(jk)(k−j)n; divide by k! for Stirling numbers S(n,k).
Dn is the nearest integer to n!/e; Dn=(n−1)(Dn−1+Dn−2)=nDn−1+(−1)n.
Exactly k fixed points: (kn)Dn−k, asymptotically Poisson(1).
Truncated sums give alternating bounds; the first term is the union bound.