How to count with the inclusion-exclusion principle

The principle of inclusion-exclusion (PIE) computes the size of the union of multiple sets by adding the sizes of the individual sets and recursively adjusting for the sizes of their intersections.

It applies whenever you need to count elements that belong to at least one of several overlapping finite sets, correcting for elements that are counted multiple times.

The setup

Define a finite collection of sets A1,A2,,AnA_1, A_2, \dots, A_n. The objective is to calculate the cardinality of their union, denoted as A1A2An|A_1 \cup A_2 \cup \dots \cup A_n|.

The steps

  1. Sum the cardinalities of the individual sets: i=1nAi\sum_{i=1}^n |A_i|.
  2. Subtract the sizes of all pairwise intersections: 1i<jnAiAj-\sum_{1 \le i < j \le n} |A_i \cap A_j|.
  3. Add the sizes of all three-way intersections: +1i<j<knAiAjAk+\sum_{1 \le i < j < k \le n} |A_i \cap A_j \cap A_k|.
  4. Continue alternating signs for each level of intersection up to the nn-way intersection: (1)n1A1A2An(-1)^{n-1} |A_1 \cap A_2 \cap \dots \cap A_n|.
  5. Sum these terms to obtain the total cardinality.

Checking the result

Verify that the resulting cardinality is a non-negative integer. Ensure the result does not exceed the cardinality of the universal set U|U| from which the elements are drawn. For n3n \le 3, verify individual regions using a Venn diagram.

Common errors

Failing to alternate signs at each depth of intersection. Miscalculating the sizes of the intersections, particularly by incorrectly assuming independence (e.g., multiplying sizes rather than finding the actual intersection set).

Worked example

Find the number of integers from 1 to 100 inclusive that are divisible by 2, 3, or 5.

Let U={1,2,,100}U = \{1, 2, \dots, 100\}. Let AA be the set of integers divisible by 2. Let BB be the set of integers divisible by 3. Let CC be the set of integers divisible by 5.

Single sets: A=100/2=50|A| = \lfloor 100/2 \rfloor = 50 B=100/3=33|B| = \lfloor 100/3 \rfloor = 33 C=100/5=20|C| = \lfloor 100/5 \rfloor = 20

Pairwise intersections (divisible by the least common multiple): AB=100/6=16|A \cap B| = \lfloor 100/6 \rfloor = 16 AC=100/10=10|A \cap C| = \lfloor 100/10 \rfloor = 10 BC=100/15=6|B \cap C| = \lfloor 100/15 \rfloor = 6

Three-way intersection: ABC=100/30=3|A \cap B \cap C| = \lfloor 100/30 \rfloor = 3

Apply the inclusion-exclusion principle: ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C| ABC=50+33+2016106+3|A \cup B \cup C| = 50 + 33 + 20 - 16 - 10 - 6 + 3 ABC=10332+3|A \cup B \cup C| = 103 - 32 + 3 ABC=74|A \cup B \cup C| = 74

There are 74 such integers.

FAQ

Run your own problem

References: Discrete Mathematics and Its Applications by Kenneth H. Rosen · Concrete Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik

See also