Inclusion–Exclusion
When sets overlap, adding their sizes counts the shared elements more than once, so you correct for it: subtract the overlaps, then add back what you subtracted too many times. With any number of sets the signs keep alternating, and every element ends up counted exactly once.
What the symbols mean
- Size of one set
Counted once in the first sum.
- A pairwise overlap
Counted twice by the first sum, so subtracted once.
- The triple overlap
Added three times, subtracted three times, so added back once.
Explanation
Inclusion–exclusion is simply how you count groups that overlap: count each group, then take out the people you counted twice. The name describes the moves, including the groups and excluding the overlaps, back and forth. It's good whenever the sum rule fails because the cases overlap. The three-set formula is long, but it's only three moves: (count each group), minus the -type terms (the people in two groups, who got double-counted), plus (the people in all three, who got subtracted away too many times). The just means "in both." It looks like a mess of symbols, but it's add, fix, fix again.
Two sets. Say 18 students take French, 15 take Spanish and 6 take both. If you add , the 6 students in both classes got counted once in the French list and again in the Spanish list. Subtract them once and each student is counted exactly once: take at least one. In symbols, .
Three sets. The same idea, but now you have to keep track of how many times each element has been counted. Take an element in exactly two of the sets. Adding counts it twice, and it sits in one pairwise intersection, so subtracting the pairs takes it back down to once. Now take an element in all three. The singles count it three times, and it's in all three pairwise intersections, so subtracting the pairs takes it to zero. That's why the triple intersection gets added back. Either way, every element ends up counted exactly once. The picture below walks through those three steps region by region.
In general, add the sizes of the single sets, subtract all pairwise intersections, add all triple intersections, and keep alternating.
Derangements. How many arrangements of letters leave no letter in its own place? Counting the good arrangements directly is hard, but the bad ones are easy with inclusion–exclusion. Let be the arrangements where letter stays put. Fixing particular letters leaves arrangements of the rest, and running the alternating sum over all of those gives
The bracket is the start of the series for , so about 37% of all arrangements are derangements, whatever is.
A common mistake. With three sets, it's tempting to stop after subtracting the pairwise overlaps. That undercounts: anything in all three sets has been added three times and subtracted three times, so it isn't counted at all until you add the triple intersection back.
See it
Worked examples
How many integers from 1 to 100 are divisible by 2, 3 or 5?
- 1
Let the sets be the multiples of 2, 3 and 5. Their sizes are .
Practice
Type your answer and check it. Solve them all to complete the lesson.
In a class, 18 students take French, 15 take Spanish and 6 take both. How many take at least one?
How many integers from 1 to 100 are divisible by 2, 3 or 5?
How many arrangements of 4 letters leave no letter in its original place?
As grows, the fraction of arrangements that are derangements approaches:
Of 1000 server logs, 300 contain errors, 250 warnings and 400 slow responses. 120 have errors and warnings, 90 errors and slow responses, 100 warnings and slow responses, and 40 have all three. How many logs show none of these?
How many onto functions are there from a 5-element set to a 3-element set?
The hardest one here. Get it, and you've got this lesson.
How many integers from 1 to 1000 are divisible by none of 2, 3, 5 and 7?
Where it's used
Database engines estimate the size of combined queries with inclusion–exclusion, and the derangement count answers the Secret Santa problem.