Discrete MathCounting

Inclusion–Exclusion

|A ∪ B ∪ C| = |A| + |B| + |C| − |A ∩ B| − |A ∩ C| − |B ∩ C| + |A ∩ B ∩ C|.

Not started Jump to practice

The complement handles "at least one" when "none" is easy. When the sets overlap in complicated ways, we need a way to count a union without double counting. You met the two-set version with Venn diagrams; here is the full idea.

Key idea

Inclusion–exclusion: $|A \cup B| = |A| + |B| - |A \cap B|$, and for three sets $$|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|.$$ Add the single sets, subtract the pairs, add the triple.

Why it works

Check that every element of the union is counted exactly once. An element in exactly one set is added once. An element in exactly two sets, say $A$ and $B$, is added twice and subtracted once (by $|A \cap B|$): net $1$. An element in all three is added $3$ times, subtracted $3$ times (once for each pair) and added back once by the triple term: net $3 - 3 + 1 = 1$. Without the last term, those elements would be counted zero times.
Three sets with the size of each region. Pick $A \cup B \cup C$ and compare the shaded total with the formula: the center region is the one the triple term rescues.
How often each element is counted by the three-set formula.
Element is insinglespairstripletotal
exactly one set$+1$$0$$0$$1$
exactly two sets$+2$$-1$$0$$1$
all three sets$+3$$-3$$+1$$1$

Worked examples

Worked example: Divisibility

How many integers from $1$ to $100$ are divisible by $2$, $3$ or $5$?
  1. Singles: $$\lfloor 100/2 \rfloor + \lfloor 100/3 \rfloor + \lfloor 100/5 \rfloor = 50 + 33 + 20 = 103.$$
  2. Pairs, using the least common multiples $6$, $10$, $15$: $16 + 10 + 6 = 32$.
  3. Triple, multiples of $30$: $3$.
  4. Total: $103 - 32 + 3 = 74$. So $100 - 74 = 26$ numbers are divisible by none of them.

Answer: $74$

Worked example: A survey

Of $60$ students, $30$ play soccer, $25$ play basketball and $20$ swim; $10$ do soccer and basketball, $8$ soccer and swimming, $6$ basketball and swimming, and $3$ do all three. How many do none of the three?
  1. At least one: $30 + 25 + 20 - 10 - 8 - 6 + 3 = 54$.
  2. None: $60 - 54 = 6$.

Answer: $6$

Common mistake

Stopping after the pairs. $|A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C|$ misses every element in all three sets: they were added three times and subtracted three times. In the divisibility example that would give $71$ instead of $74$, losing $30$, $60$ and $90$. Check: test the formula on one element that lies in every set; it must be counted exactly once.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • Oscar Levin, Discrete Mathematics: An Open Introduction (3rd ed.), section "Advanced Counting Using PIE"
  • MIT OpenCourseWare 6.042J Mathematics for Computer Science (Lehman, Leighton and Meyer), chapter "Cardinality Rules" (inclusion–exclusion)
@teddyrookbook GitHub