A formula for the size of a union of sets that corrects for overlaps counted multiple times.
IntuitionIdea: how big is the union when the pieces overlap?
If a class has students who like math and students who like art, simply adding "like math" plus "like art" double-counts every student who likes both. To get the true number who like at least one subject, you must subtract the overlap once: this is the whole idea behind inclusion–exclusion, and it generalizes cleanly from two sets to any number of sets.
Network diagram of three overlapping sets showing pairwise and triple intersections used in inclusion-exclusion.
Three overlapping sets A,B,C: to count ∣A∪B∪C∣, add the three circles, subtract the three pairwise lenses, and add back the central triple intersection.
SchoolTwo and three sets
∣A∪B∣=∣A∣+∣B∣−∣A∩B∣
Here ∣A∣ denotes the number of elements of a finite set A; A∪B is the set of elements in A or B (or both), and A∩B is the set of elements in both. Subtracting ∣A∩B∣ removes the double count of elements that belong to both sets.
Every element of A∪B falls into exactly one of three disjoint groups: only in A, only in B, or in both; adding ∣A∣+∣B∣ counts the "both" group twice, so one copy must be removed.
Proof
Partition A∪B into three pairwise disjoint pieces: A∖B (in A only), B∖A (in B only), and A∩B (in both). Since these pieces are disjoint and cover A∪B, ∣A∪B∣=∣A∖B∣+∣B∖A∣+∣A∩B∣.
Now observe that A itself splits into the disjoint pieces A∖B and A∩B, so ∣A∣=∣A∖B∣+∣A∩B∣, which means ∣A∖B∣=∣A∣−∣A∩B∣. Symmetrically, ∣B∖A∣=∣B∣−∣A∩B∣.
Substituting these two expressions back into the first equation gives ∣A∪B∣=(∣A∣−∣A∩B∣)+(∣B∣−∣A∩B∣)+∣A∩B∣=∣A∣+∣B∣−∣A∩B∣, which is exactly ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣.
For finite sets A1,…,An: ∣⋃i=1nAi∣=∑k=1n(−1)k+1∑1≤i1<⋯<ik≤n∣Ai1∩⋯∩Aik∣
Why is it true?
Each element that lies in several of the sets gets added once for every single set, subtracted once for every pair, added back for every triple, and so on; the alternating pattern is exactly what is needed to bring its total count back down to exactly 1.
Proof
Fix any element x. If x belongs to none of A1,…,An, it contributes 0 to both sides of the formula, so assume x belongs to exactly m≥1 of the sets.
For each k, the number of k-fold intersections Ai1∩⋯∩Aik that contain x equals the number of ways to choose k of the m sets containing x, namely (km). So x's total contribution to the right-hand side is ∑k=1n(−1)k+1(km)=∑k=1m(−1)k+1(km) (terms with k>m vanish since (km)=0).
By the binomial theorem, ∑k=0m(−1)k(km)=(1−1)m=0, so ∑k=1m(−1)k(km)=−1, and multiplying by −1 gives exactly ∑k=1m(−1)k+1(km)=1.
So every element in at least one set contributes exactly 1 to the right-hand side, matching its contribution of 1 to ∣⋃i=1nAi∣; elements in no set contribute 0 to both sides. Since every element contributes equally to both sides, the two sides are equal.
Definition: Derangement
A derangement of n objects is a permutation in which no object stays in its original position. Let Ai be the set of permutations of n objects that fix position i (object i stays put); then the number of derangements is Dn=n!−∣⋃i=1nAi∣, the permutations left over after removing every one that fixes at least one position.
Dn=n!k=0∑nk!(−1)k
Applying the general formula to these sets, ∣Ai1∩⋯∩Aik∣=(n−k)! (the other n−k objects can be arranged freely), so the inclusion–exclusion sum has (kn) identical terms of size (n−k)! at each level k, which simplifies to Dn=n!∑k=0nk!(−1)k after dividing by n! and factoring — the second theorem block on this page derives the general case in full.
UndergraduateReal-World Applications and Worked Examples
Inclusion–exclusion is a everyday tool in computer science (sieving out unwanted cases when counting), number theory (counting multiples), and reliability engineering (combining overlapping failure events), wherever "at least one of several conditions holds" needs to be counted exactly.
Example: Counting multiples with a sieve
A programmer needs to count how many integers from 1 to 100 are divisible by 2, 3, or 5 — the same sieving idea used to precompute primes or filter valid keys in cryptographic code.
Solution
Let A be multiples of 2, B multiples of 3, C multiples of 5 in {1,…,100}. Counting multiples up to 100 by division: ∣A∣=50, ∣B∣=33, ∣C∣=20.
Pairwise overlaps are multiples of the products: ∣A∩B∣=⌊100/6⌋=16, ∣A∩C∣=⌊100/10⌋=10, ∣B∩C∣=⌊100/15⌋=6.
The triple overlap is multiples of 30: ∣A∩B∩C∣=⌊100/30⌋=3.
By the three-set formula, ∣A∪B∪C∣=50+33+20−16−10−6+3=74, so 74 of the first 100 integers are divisible by 2, 3 or 5, and the sieve avoids ever listing them one by one.
Example: Redundant satellite subsystems
A satellite has three subsystems A, B, C with independent one-year failure probabilities P(A)=0.02, P(B)=0.03, P(C)=0.01, but shared components make some pairs of failures correlated: P(A∩B)=0.004, P(A∩C)=0.001, P(B∩C)=0.0006, and P(A∩B∩C)=0.0002. A reliability engineer needs the probability that at least one subsystem fails, to decide whether more redundancy is needed.
Solution
Probabilities behave like proportions of a population, so the same three-set formula applies: P(A∪B∪C)=P(A)+P(B)+P(C)−P(A∩B)−P(A∩C)−P(B∩C)+P(A∩B∩C).
Substitute the given values: P(A∪B∪C)=0.02+0.03+0.01−0.004−0.001−0.0006+0.0002.
Adding the single-subsystem terms gives 0.06, subtracting the pairwise terms gives 0.06−0.0056=0.0544, and adding back the triple overlap gives 0.0544+0.0002=0.0546.
So there is about a 5.46% chance that at least one subsystem fails in a year; without correcting for the correlated failures (i.e. just adding 0.02+0.03+0.01=0.06), the engineer would have overestimated the risk and possibly over-engineered the redundancy.
If ∣A∣=10, ∣B∣=7, and ∣A∩B∣=3, what is ∣A∪B∣?
Given ∣A∣=30, ∣B∣=20, ∣C∣=15, ∣A∩B∣=10, ∣A∩C∣=8, ∣B∩C∣=5, ∣A∩B∩C∣=3, what is ∣A∪B∪C∣?
In how many ways can 4 letters be placed into 4 addressed envelopes so that no letter goes into its correct envelope?
How many integers from 1 to 30 are divisible by neither 2 nor 3?