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 . The objective is to calculate the cardinality of their union, denoted as .
The steps
- Sum the cardinalities of the individual sets: .
- Subtract the sizes of all pairwise intersections: .
- Add the sizes of all three-way intersections: .
- Continue alternating signs for each level of intersection up to the -way intersection: .
- 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 from which the elements are drawn. For , 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 . Let be the set of integers divisible by 2. Let be the set of integers divisible by 3. Let be the set of integers divisible by 5.
Single sets:
Pairwise intersections (divisible by the least common multiple):
Three-way intersection:
Apply the inclusion-exclusion principle:
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