Inclusion-Exclusion Principle
The inclusion-exclusion principle counts the elements in a union of overlapping sets. For two sets, |A ∪ B| = |A| + |B| − |A ∩ B|: add the sizes, then subtract the overlap that was counted twice. With more sets the signs keep alternating: add single sets, subtract pairs, add back triples, and so on.
Before this lesson: Set Operations, Sum and Product Rules for Counting and Combinations with Repetition
The idea
A class has 30 students. 18 take Spanish, 15 take art and 7 take both. Adding 18 + 15 = 33 gives more than the whole class, because the 7 students in both were counted twice, once with Spanish and once with art.
Subtracting the overlap once fixes the count: 18 + 15 − 7 = 26 students take at least one of the two classes, so 30 − 26 = 4 take neither. The sum rule from the last unit works only for sets with no overlap; inclusion-exclusion is the version that works for every pair of sets.
The inclusion-exclusion formula
Two sets.
|A \cup B| = |A| + |B| - |A \cap B|
Three sets. Add the three sizes, subtract the three pairwise intersections, then add back the triple intersection.
|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|
Any number of sets. For sets A1, …, An, add the sizes of the single sets, subtract the sizes of all pairwise intersections, add the sizes of all triple intersections, and keep alternating signs up to the intersection of all n sets.
\left| A_1 \cup \cdots \cup A_n \right| = \sum_{i} |A_i| - \sum_{i \lt j} |A_i \cap A_j| + \sum_{i \lt j \lt k} |A_i \cap A_j \cap A_k| - \cdots + (-1)^{n+1} |A_1 \cap \cdots \cap A_n|Why it works. Take an element that lies in exactly m of the sets. The formula counts it \binom{m}{1} times in the single sets, subtracts it \binom{m}{2} times in the pairs, adds it \binom{m}{3} times in the triples, and so on. By the binomial theorem, (1 - 1)^m = 0, which rearranges to \binom{m}{1} - \binom{m}{2} + \binom{m}{3} - \cdots = 1, so every element in the union is counted exactly once.
Counting the elements in none of the sets. If every set sits inside a universal set U, the number of elements in none of the sets is |U| minus the size of the union. Many problems ask for this count.
How to use inclusion-exclusion
- Name the sets so that the thing you want is “in at least one set” or “in none of the sets.”
- Find the size of each single set.
- Find the size of each intersection of two sets, then of three sets, and so on.
- Combine them with alternating signs: + singles, − pairs, + triples, …
- If you want the elements in none of the sets, subtract the union from the total.
In divisibility problems the floor ⌊x⌋, the greatest integer less than or equal to x, gives the count: from 1 to N there are ⌊N/d⌋ multiples of d. A number divisible by both a and b is divisible by their least common multiple.
Inclusion-exclusion principle examples
Example 1: Two sets
In the class of 30 students above, 18 take Spanish, 15 take art and 7 take both. How many take at least one of the two? How many take neither?
|S| = 18,\ |A| = 15,\ |S \cap A| = 7
Name the sets: S for Spanish and A for art.
|S \cup A| = 18 + 15 - 7
Use the two-set formula.
18 + 15 = 33
Add the set sizes.
33 - 7 = 26
Subtract the overlap.
30 - 26 = 4
Subtract the union from the class size.
Answer: 26 students take at least one class, and 4 take neither.
Example 2: Divisibility
How many integers from 1 to 100 are divisible by 2, 3 or 5?
\lfloor 100/2 \rfloor = 50,\ \lfloor 100/3 \rfloor = 33,\ \lfloor 100/5 \rfloor = 20
Count the multiples of each number.
\lfloor 100/6 \rfloor = 16,\ \lfloor 100/10 \rfloor = 10,\ \lfloor 100/15 \rfloor = 6
Count multiples of each pair, using the least common multiple of the pair.
\lfloor 100/30 \rfloor = 3
Count the multiples of all three, the multiples of 30.
50 + 33 + 20 = 103
Add the single sets.
16 + 10 + 6 = 32
Add the pairwise counts.
103 - 32 = 71
Subtract the pairs.
71 + 3 = 74
Add back the triple.
Answer: 74 integers. The other 100 − 74 = 26 are divisible by none of 2, 3 and 5.
Example 3: Three sets (word problem)
Of 200 students, 90 take biology, 80 take chemistry and 70 take physics. 35 take biology and chemistry, 30 take biology and physics, 25 take chemistry and physics, and 10 take all three. How many take at least one science? How many take none?
90 + 80 + 70 = 240
Add the single sets.
35 + 30 + 25 = 90
Add the pairwise intersections.
240 - 90 = 150
Subtract the pairs.
150 + 10 = 160
Add back the students in all three.
200 - 160 = 40
Subtract from the total for students in none.
Answer: 160 students take at least one science and 40 take none. Without the final + 10, the 10 students in all three sciences would not be counted at all.
Example 4: Numbers relatively prime to 60
How many integers from 1 to 60 share no common factor with 60 other than 1?
60 = 2^2 \cdot 3 \cdot 5
Factor 60 (see prime factorization). A number shares a factor with 60 exactly when it is divisible by 2, 3 or 5.
30 + 20 + 12 = 62
Add the multiples of 2, 3 and 5: 60/2, 60/3 and 60/5.
10 + 6 + 4 = 20
Add the multiples of 6, 10 and 15.
60/30 = 2
Count the multiples of 30.
62 - 20 + 2 = 44
Use inclusion-exclusion for the union.
60 - 44 = 16
Subtract the union from 60.
Answer: 16 integers. This count is Euler’s totient φ(60), the same φ used in RSA encryption.
Example 5: Upper limits with stars and bars (word problem)
A red die, a blue die and a green die are rolled. In how many ways can the total be 15?
x_i = d_i - 1
Let di be each die and xi = di − 1, so each xi is from 0 to 5.
x_1 + x_2 + x_3 = 15 - 3 = 12
Subtract 1 three times from the total.
\binom{12 + 3 - 1}{3 - 1} = \binom{14}{2} = 91Count all nonnegative solutions with stars and bars: 14 · 13 ÷ 2 = 91.
x_1 \ge 6:\ \binom{12 - 6 + 2}{2} = \binom{8}{2} = 28Let Ai be the solutions with xi at least 6. Hand out 6, leaving 6 to share: 8 · 7 ÷ 2 = 28.
3 \cdot 28 = 84
The three sets A1, A2, A3 have the same size.
x_1, x_2 \ge 6:\ \binom{0 + 2}{2} = 1Two variables at least 6 use up all 12, leaving 0 to share.
3 \cdot 1 = 3
There are three pairs of sets.
6 + 6 + 6 = 18 \gt 12
All three at least 6 is impossible, so the triple intersection is empty.
91 - 84 + 3 - 0 = 10
Subtract the bad solutions with inclusion-exclusion.
Answer: 10 ways, such as (6, 6, 3) and (5, 5, 5). Listing all 216 rolls gives the same count.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Adding the set sizes without subtracting the overlap | 18 Spanish + 15 art = 33 students, in a class of 30 | Subtract the 7 counted twice: 18 + 15 − 7 = 26. |
| Forgetting to add back the triple intersection | 240 − 90 = 150 students take a science | The center was removed completely, so add it back: 150 + 10 = 160. |
| Multiplying instead of using the lcm | Multiples of 4 and 6 up to 200 counted as ⌊200/24⌋ = 8 | Divisible by 4 and 6 means divisible by lcm(4, 6) = 12: ⌊200/12⌋ = 16. |
| Using “only” counts as set sizes | “20 take only biology” used as |B| = 20 | |B| counts everyone in biology, including those in other classes too. |
| Answering “at least one” when the question asks “none” | Giving 74 for numbers divisible by none of 2, 3, 5 | Subtract the union from the total: 100 − 74 = 26. |
| Getting the signs wrong with four or more sets | Subtracting the triple intersections | Signs alternate by the number of sets: + singles, − pairs, + triples, − quadruples. |
Related ideas
In probability, the same idea gives the addition rule, P(A or B) = P(A) + P(B) − P(A and B). A famous application counts derangements, arrangements where nothing stays in its original place; Practice Problem 8 finds them for 4 items. Inclusion-exclusion also finishes the upper-limit problems from combinations with repetition, as in Example 5. The next unit starts with Recurrence Relations, another way to count by building on smaller cases.
Practice problems
- Sets A and B have |A| = 25, |B| = 40 and |A ∩ B| = 12. Find |A ∪ B|.
- A class has 35 students. 20 play an instrument, 16 sing in the choir and 9 do both. How many do neither?
- How many integers from 1 to 200 are divisible by 4 or 6?
- How many integers from 1 to 100 are divisible by none of 2, 3 and 5?
- Sets A, B and C have |A| = 12, |B| = 15, |C| = 10, |A ∩ B| = 5, |A ∩ C| = 4, |B ∩ C| = 6 and |A ∩ B ∩ C| = 2. Find |A ∪ B ∪ C|.
- Of 50 people, 25 drink coffee, 20 drink tea and 15 drink juice. 8 drink coffee and tea, 6 drink coffee and juice, 5 drink tea and juice, and 3 drink all three. How many drink none of them?
- How many integers from 1 to 90 share no common factor with 90 other than 1?
- Four letters are put into four addressed envelopes at random. In how many ways does no letter go into its own envelope?
- How many nonnegative integer solutions does x_1 + x_2 + x_3 = 10 have with each x_i \le 4?
- How many bit strings of length 8 start with 1 or end with 00?
Show answers
1.
|A \cup B| = 25 + 40 - 12
Use the two-set formula.
25 + 40 = 65
Add the set sizes.
65 - 12 = 53
Subtract the overlap.
Answer: 53
2.
20 + 16 = 36
Add the set sizes.
36 - 9 = 27
Subtract the students who do both.
35 - 27 = 8
Subtract the union from the class size.
Answer: 8 students
3.
\lfloor 200/4 \rfloor = 50,\ \lfloor 200/6 \rfloor = 33
Count the multiples of 4 and of 6.
\operatorname{lcm}(4, 6) = 12A number divisible by both is divisible by 12.
\lfloor 200/12 \rfloor = 16
Count the multiples of 12.
50 + 33 - 16 = 67
Use the two-set formula: 83 − 16 = 67.
Answer: 67 integers
4.
|A_2 \cup A_3 \cup A_5| = 74
From Example 2.
100 - 74 = 26
Subtract the union from the total.
Answer: 26 integers
5.
12 + 15 + 10 = 37
Add the single sets.
5 + 4 + 6 = 15
Add the pairwise intersections.
37 - 15 = 22
Subtract the pairs.
22 + 2 = 24
Add back the triple intersection.
Answer: 24
6.
25 + 20 + 15 = 60
Add the single sets.
8 + 6 + 5 = 19
Add the pairwise intersections.
60 - 19 = 41
Subtract the pairs.
41 + 3 = 44
Add back the triple.
50 - 44 = 6
Subtract the union from the total.
Answer: 6 people
7.
90 = 2 \cdot 3^2 \cdot 5
Factor 90. The primes are 2, 3 and 5.
45 + 30 + 18 = 93
Add the multiples of 2, 3 and 5 up to 90.
15 + 9 + 6 = 30
Add the multiples of 6, 10 and 15.
90/30 = 3
Count the multiples of 30.
93 - 30 + 3 = 66
Use inclusion-exclusion for the union.
90 - 66 = 24
Subtract the union from 90.
Answer: 24 integers
8.
4! = 24
All arrangements of the letters.
\binom{4}{1} 3! = 24Let Ai be the arrangements with letter i correct. Fix 1 letter, arrange the other 3.
\binom{4}{2} 2! = 12Fix 2 letters, arrange the other 2.
\binom{4}{3} 1! = 4Fix 3 letters; the last one is forced.
\binom{4}{4} 0! = 1All 4 letters correct.
24 - (24 - 12 + 4 - 1) = 24 - 15
Subtract the arrangements with at least one letter correct.
24 - 15 = 9
Subtract.
Answer: 9 ways
9.
\binom{10 + 2}{2} = \binom{12}{2} = 66All nonnegative solutions: 12 · 11 ÷ 2 = 66.
\binom{5 + 2}{2} = \binom{7}{2} = 21Solutions with one xi at least 5: hand out 5, leaving 5 to share.
3 \cdot 21 = 63
There are three such sets.
\binom{0 + 2}{2} = 1Two variables at least 5 use up all 10.
3 \cdot 1 = 3
There are three pairs.
66 - 63 + 3 = 6
All three at least 5 is impossible (15 is more than 10), so the triple term is 0.
Answer: 6 solutions
10.
2^7 = 128
Strings that start with 1: the other 7 bits can be anything.
2^6 = 64
Strings that end with 00: the first 6 bits can be anything.
2^5 = 32
Strings that do both: the middle 5 bits can be anything.
128 + 64 - 32 = 160
Use the two-set formula: 192 − 32 = 160.
Answer: 160 strings
Inclusion-exclusion principle FAQ
What is the inclusion-exclusion principle?
It is a rule for counting a union of overlapping sets. You add the set sizes, subtract the sizes of the overlaps, add back triple overlaps, and continue with alternating signs, so every element is counted exactly once.
What is the inclusion-exclusion formula for three sets?
|A ∪ B ∪ C| = |A| + |B| + |C| − |A ∩ B| − |A ∩ C| − |B ∩ C| + |A ∩ B ∩ C|.
Why do you add back the intersection of all three sets?
Elements in all three sets are added 3 times with the single sets and subtracted 3 times with the pairs, which leaves them counted 0 times. Adding the triple intersection once brings their count back to 1.
How do you use inclusion-exclusion to count numbers not divisible by several numbers?
Count the multiples of each number with ⌊N/d⌋, use least common multiples for the intersections, and combine with alternating signs to get the union. Then subtract the union from N.
Summary
- Two sets: |A ∪ B| = |A| + |B| − |A ∩ B|.
- Three sets: add the singles, subtract the pairs, add back the triple.
- For n sets the signs alternate, and every element of the union ends up counted exactly once.
- The count in none of the sets is the total minus the union.
- Use ⌊N/d⌋ and least common multiples for divisibility problems, and for upper limits in stars and bars, subtract the solutions that break a limit.
Previous lesson: Combinations with Repetition
Next lesson: Recurrence Relations
Back to the Discrete Math Index