Principle of Inclusion–Exclusion (PIE): Fundamentals and Extensions
Lesson · Intermediate
Combinatorics
Inclusion–Exclusion Principle for two sets
For finite sets A and B:
Inclusion–Exclusion Principle for three sets
For finite sets A, B, and C:
General Inclusion–Exclusion Principle for n sets
For sets A₁, A₂, ..., Aₙ:
In compact form, the general principle is:
Counting elements in exactly one set
Let E_j denote the number of elements that are contained in exactly j of the sets A₁, A₂, ..., Aₙ.
For two sets:
For three sets:
The coefficients here are different from those in the ordinary Inclusion–Exclusion formula.
For finite sets A and B,
When we calculate
every element belonging to exactly one of the sets is counted once, but every element in A ∩ B is counted twice.
Therefore we subtract
once, giving
Start with
An element in exactly one set is counted once.
An element in exactly two sets is counted twice, so subtract
However, an element belonging to all three sets was initially counted 3 times and then subtracted 3 times, so it is now counted 0 times.
Therefore we add
once.
Hence
Consider an element x that belongs to exactly r of the sets
In the PIE expression, x is counted
times in the single-set terms,
times in the pairwise intersections,
and in general
times among the k-fold intersections.
Therefore its total contribution is
From
we obtain
Thus every element of the union is counted exactly once.
Therefore,
For two sets, we want to prove
where E₁ is the number of elements contained in exactly one of A₁, A₂.
An element in exactly one of the two sets is counted once in
An element in both sets is counted twice, so we must subtract it twice. Therefore
For three sets, we want to prove
Consider an element according to how many of the three sets contain it.
If it belongs to exactly one set, it is counted
time in
and it is not counted in any intersection term. So its total contribution is
If it belongs to exactly two sets, it is counted twice in
and once in exactly one pairwise intersection, where it is subtracted with coefficient 2. Its total contribution is
If it belongs to all three sets, it is counted 3 times in the single-set terms. It belongs to all three pairwise intersections, so it is subtracted
times. Finally, it is added back 3 times through the triple intersection. Its total contribution is
Thus only elements belonging to exactly one set contribute 1, while all other elements contribute 0. Therefore
How many integers from 1 through 1000 are divisible by 2, 3, or 5?
Let
Then
Pairwise:
Triple:
Therefore
How many onto functions are there from a 6-element set to a 3-element set?
There are
functions in total.
Let Aᵢ be the set of functions that do not use output i.
Then
For two missing outputs,
Therefore the number of onto functions is
Hence
This is the natural extension of the exactly one set section.
Define
Then the number Eᵣ of elements contained in exactly r sets is
For r = 1,
Suppose an element x belongs to exactly m of the sets A₁, ..., Aₙ.
Then x is counted in Sₖ exactly
times, because we may choose any k of those m sets whose intersection contains x.
Therefore the total contribution of x to
is
Now use the identity
Hence the contribution becomes
Let
Then
If m = r, this equals
And if m < r, the element does not occur in any term at all, so its contribution is also 0.
If m > r, by the binomial theorem,
Thus every element contained in exactly r sets is counted once, and every other element is counted zero times.
Therefore,