Target Audience: Mathematics students, engineers, and self-learners.
Prerequisites: Basic set notation and counting.
When sets overlap, simply adding their sizes overcounts elements that lie in multiple sets.
Example with two sets:
If you just do $|A| + |B|$, then anyone who does both pickleball and archery is counted twice. The inclusion–exclusion principle is a systematic way to correct this overcounting.
For two finite sets $A$ and $B$:
Suppose in a club of students:
Then the number who study French or Physics (or both) is
A two-set diagram splits into three regions: each subject on its own, plus the overlap. Subtracting the 7 who study both from each subject's total leaves the "only" counts:
Your turn (simple check):
If 10 people grow orchids, 8 grow roses, and 3 grow both orchids and roses, how many grow at least one of the two? (Just apply the two-set formula.)
So 15 people grow at least one of the two.
With three sets, double-counting (and even triple-counting) becomes more subtle.
Let $A$, $B$, $C$ be finite sets. Then:
Pattern:
Think about one element $x$:
So every element in the union contributes exactly 1.
Think of students in a discrete math class:
Crucially, these sets overlap: a student may use more than one operating system — for instance across different devices, or by dual-booting a single machine. So a student can belong to two of the sets, or even all three. That is exactly why the intersection counts below are nonzero.
Suppose:
Then:
So 34 students use at least one of the three systems.
The diagram below shows why. A three-set diagram splits into seven disjoint regions. Working from the centre outward — subtract the triple overlap from each pairwise overlap, then subtract all of those from each circle's total — gives the count of students in exactly those sets:
Checkpoint question:
Looking at the three-set formula, can you describe in your own words why the triple intersection has to be added back at the end instead of subtracted?
An element in all three sets is counted $+3$ times by $|A|+|B|+|C|$, then removed $-3$ times by the three pairwise intersections — a net of $0$. Without a correction it would be left out of the union entirely. Adding $|A \cap B \cap C|$ once restores its correct net count of $1$. (Subtracting it would give $-1$, which is impossible for a count.)
For sets $A_1, A_2, \dots, A_n$, the Inclusion–Exclusion Principle says:
Intuition: each element that lies in exactly $k$ of the sets gets counted
so every element in the union ends up counted exactly once.
The expanded formula above — add the singles, subtract the pairs, add back the triples, and so on — can be packaged into a single compact expression:
This is the form you will most often meet in textbooks. It looks dense, but it has just two working parts: a sign-switcher $(-1)^{k+1}$ and an inner sum that gathers every $k$-way intersection. We decode each piece below.
$S$ is a subset of the index set $\{1, 2, \dots, n\}$. The indices are simply labels for the sets. For example, with sets $A_1, A_2, A_3$, the subsets of size $2$ are $\{1,2\},\ \{1,3\},\ \{2,3\}$ — each one names a particular pair of sets to intersect.
The condition $|S| = k$ under the inner $\sum$ means: sum over all subsets $S$ that have exactly $k$ elements. As $k$ climbs, $S$ ranges over deeper levels of overlap:
This is the intersection of all sets whose index lies in $S$. For example, if $S = \{1, 3\}$, then
The vertical bars $\left|\,\cdot\,\right|$ around it denote the size (cardinality) of that intersection. So the inner sum $\sum_{|S|=k}\left|\bigcap_{i \in S} A_i\right|$ reads: find every combination of $k$ sets, take the size of their intersection, and add all those sizes together. For $k=2$ with three sets it produces exactly the pairwise step $|A_1 \cap A_2| + |A_1 \cap A_3| + |A_2 \cap A_3|$.
The factor $(-1)^{k+1}$ is what "alternating signs" means in precise language. It equals $+1$ when $k$ is odd (add) and $-1$ when $k$ is even (subtract):
Compact $\to$ expanded. Stepping the compact formula through $k = 1, 2, 3$ for three sets recovers the expanded three-set formula from earlier:
The compact notation simply lets the loop variable $k$ and the sign-switcher $(-1)^{k+1}$ generate every level automatically, instead of writing each line out by hand.
Example: "How many integers from 1 to 1000 are divisible by 3 or 5 or 7?"
Let $A$ = multiples of 3, $B$ = multiples of 5, $C$ = multiples of 7. The number of multiples of $m$ in $\{1, \dots, 1000\}$ is $\left\lfloor 1000/m \right\rfloor$, and an integer is divisible by two of the moduli exactly when it is divisible by their least common multiple (here the moduli are coprime, so the lcm is just the product).
Applying the three-set formula:
So 543 of the integers from 1 to 1000 are divisible by 3, 5, or 7.
Sometimes it is easier to count objects that violate a rule and then subtract from a total. The key identity is that the number of elements of a universe $U$ avoiding every property is the total minus the size of the union:
Example: counting permutations of $\{1, \dots, n\}$ where no element stays fixed (these are called derangements).
The permutations with no fixed point are those avoiding every $A_i$, so by the complement identity above:
For example, $D_3 = 6\left(1 - 1 + \tfrac{1}{2} - \tfrac{1}{6}\right) = 2$ and $D_4 = 24\left(1 - 1 + \tfrac{1}{2} - \tfrac{1}{6} + \tfrac{1}{24}\right) = 9$.
To deepen your understanding, pick an angle and work a full example end to end. Good starting points include: