MathLabs

Combinatorics and discrete mathematics

Inclusion–exclusion principle

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,CA, B, C: to count ∣A∪B∪C∣|A\cup B\cup 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∣|A\cup B|=|A|+|B|-|A\cap B|

Here ∣A∣|A| denotes the number of elements of a finite set AA; A∪BA\cup B is the set of elements in AA or BB (or both), and A∩BA\cap B is the set of elements in both. Subtracting ∣A∩B∣|A\cap B| removes the double count of elements that belong to both sets.

∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣|A\cup B\cup C|=|A|+|B|+|C|-|A\cap B|-|A\cap C|-|B\cap C|+|A\cap B\cap C|
From two sets to n sets
Number of setsFormulaTerms in the sum
22∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A\cup B|=|A|+|B|-|A\cap B|33
33∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣|A\cup B\cup C|=|A|+|B|+|C|-|A\cap B|-|A\cap C|-|B\cap C|+|A\cap B\cap C|77
nn∣⋃i=1nAi∣=∑k=1n(−1)k+1∑1≤i1<⋯<ik≤n∣Ai1∩⋯∩Aik∣\left|\bigcup_{i=1}^n A_i\right|=\sum_{k=1}^n(-1)^{k+1}\sum_{1\le i_1<\cdots<i_k\le n}\left|A_{i_1}\cap\cdots\cap A_{i_k}\right|2n−12^n-1

UndergraduateProof for two sets and the general n-set formula

For finite sets AA, BB: ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A\cup B|=|A|+|B|-|A\cap B|

Why is it true?

Every element of A∪BA\cup B falls into exactly one of three disjoint groups: only in AA, only in BB, or in both; adding ∣A∣+∣B∣|A|+|B| counts the "both" group twice, so one copy must be removed.

Proof

Partition A∪BA\cup B into three pairwise disjoint pieces: A∖BA\setminus B (in AA only), B∖AB\setminus A (in BB only), and A∩BA\cap B (in both). Since these pieces are disjoint and cover A∪BA\cup B, ∣A∪B∣=∣A∖B∣+∣B∖A∣+∣A∩B∣|A\cup B|=|A\setminus B|+|B\setminus A|+|A\cap B|.

Now observe that AA itself splits into the disjoint pieces A∖BA\setminus B and A∩BA\cap B, so ∣A∣=∣A∖B∣+∣A∩B∣|A|=|A\setminus B|+|A\cap B|, which means ∣A∖B∣=∣A∣−∣A∩B∣|A\setminus B|=|A|-|A\cap B|. Symmetrically, ∣B∖A∣=∣B∣−∣A∩B∣|B\setminus A|=|B|-|A\cap 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∣|A\cup B|=(|A|-|A\cap B|)+(|B|-|A\cap B|)+|A\cap B|=|A|+|B|-|A\cap B|, which is exactly ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A\cup B|=|A|+|B|-|A\cap B|.

For finite sets A1,…,AnA_1,\ldots,A_n: ∣⋃i=1nAi∣=∑k=1n(−1)k+1∑1≤i1<⋯<ik≤n∣Ai1∩⋯∩Aik∣\left|\bigcup_{i=1}^n A_i\right|=\sum_{k=1}^n(-1)^{k+1}\sum_{1\le i_1<\cdots<i_k\le n}\left|A_{i_1}\cap\cdots\cap A_{i_k}\right|

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 11.

Proof

Fix any element xx. If xx belongs to none of A1,…,AnA_1,\ldots,A_n, it contributes 00 to both sides of the formula, so assume xx belongs to exactly m≥1m\ge 1 of the sets.

For each kk, the number of kk-fold intersections Ai1∩⋯∩AikA_{i_1}\cap\cdots\cap A_{i_k} that contain xx equals the number of ways to choose kk of the mm sets containing xx, namely (mk)\binom{m}{k}. So xx's total contribution to the right-hand side is ∑k=1n(−1)k+1(mk)=∑k=1m(−1)k+1(mk)\sum_{k=1}^n(-1)^{k+1}\binom{m}{k}=\sum_{k=1}^m(-1)^{k+1}\binom{m}{k} (terms with k>mk>m vanish since (mk)=0\binom{m}{k}=0).

By the binomial theorem, ∑k=0m(−1)k(mk)=(1−1)m=0\sum_{k=0}^m(-1)^k\binom{m}{k}=(1-1)^m=0, so ∑k=1m(−1)k(mk)=−1\sum_{k=1}^m(-1)^k\binom{m}{k}=-1, and multiplying by −1-1 gives exactly ∑k=1m(−1)k+1(mk)=1\sum_{k=1}^m(-1)^{k+1}\binom{m}{k}=1.

So every element in at least one set contributes exactly 11 to the right-hand side, matching its contribution of 11 to ∣⋃i=1nAi∣\left|\bigcup_{i=1}^n A_i\right|; elements in no set contribute 00 to both sides. Since every element contributes equally to both sides, the two sides are equal.

Definition: Derangement

A derangement of nn objects is a permutation in which no object stays in its original position. Let AiA_i be the set of permutations of nn objects that fix position ii (object ii stays put); then the number of derangements is Dn=n!−∣⋃i=1nAi∣D_n=n!-\left|\bigcup_{i=1}^n A_i\right|, the permutations left over after removing every one that fixes at least one position.

Dn=n!∑k=0n(−1)kk!D_n=n!\sum_{k=0}^n\dfrac{(-1)^k}{k!}

Applying the general formula to these sets, ∣Ai1∩⋯∩Aik∣=(n−k)!\left|A_{i_1}\cap\cdots\cap A_{i_k}\right|=(n-k)! (the other n−kn-k objects can be arranged freely), so the inclusion–exclusion sum has (nk)\binom{n}{k} identical terms of size (n−k)!(n-k)! at each level kk, which simplifies to Dn=n!∑k=0n(−1)kk!D_n=n!\sum_{k=0}^n\dfrac{(-1)^k}{k!} after dividing by n!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 11 to 100100 are divisible by 22, 33, or 55 — the same sieving idea used to precompute primes or filter valid keys in cryptographic code.

Solution

Let AA be multiples of 22, BB multiples of 33, CC multiples of 55 in {1,…,100}\{1,\ldots,100\}. Counting multiples up to 100100 by division: ∣A∣=50|A|=50, ∣B∣=33|B|=33, ∣C∣=20|C|=20.

Pairwise overlaps are multiples of the products: ∣A∩B∣=⌊100/6⌋=16|A\cap B|=\lfloor 100/6\rfloor=16, ∣A∩C∣=⌊100/10⌋=10|A\cap C|=\lfloor 100/10\rfloor=10, ∣B∩C∣=⌊100/15⌋=6|B\cap C|=\lfloor 100/15\rfloor=6.

The triple overlap is multiples of 3030: ∣A∩B∩C∣=⌊100/30⌋=3|A\cap B\cap C|=\lfloor 100/30\rfloor=3.

By the three-set formula, ∣A∪B∪C∣=50+33+20−16−10−6+3=74|A\cup B\cup C|=50+33+20-16-10-6+3=74, so 7474 of the first 100100 integers are divisible by 22, 33 or 55, and the sieve avoids ever listing them one by one.

Example: Redundant satellite subsystems

A satellite has three subsystems AA, BB, CC with independent one-year failure probabilities P(A)=0.02P(A)=0.02, P(B)=0.03P(B)=0.03, P(C)=0.01P(C)=0.01, but shared components make some pairs of failures correlated: P(A∩B)=0.004P(A\cap B)=0.004, P(A∩C)=0.001P(A\cap C)=0.001, P(B∩C)=0.0006P(B\cap C)=0.0006, and P(A∩B∩C)=0.0002P(A\cap B\cap 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)P(A\cup B\cup C)=P(A)+P(B)+P(C)-P(A\cap B)-P(A\cap C)-P(B\cap C)+P(A\cap B\cap C).

Substitute the given values: P(A∪B∪C)=0.02+0.03+0.01−0.004−0.001−0.0006+0.0002P(A\cup B\cup C)=0.02+0.03+0.01-0.004-0.001-0.0006+0.0002.

Adding the single-subsystem terms gives 0.060.06, subtracting the pairwise terms gives 0.06−0.0056=0.05440.06-0.0056=0.0544, and adding back the triple overlap gives 0.0544+0.0002=0.05460.0544+0.0002=0.0546.

So there is about a 5.46%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.060.02+0.03+0.01=0.06), the engineer would have overestimated the risk and possibly over-engineered the redundancy.

If ∣A∣=10|A|=10, ∣B∣=7|B|=7, and ∣A∩B∣=3|A\cap B|=3, what is ∣A∪B∣|A\cup B|?

Given ∣A∣=30|A|=30, ∣B∣=20|B|=20, ∣C∣=15|C|=15, ∣A∩B∣=10|A\cap B|=10, ∣A∩C∣=8|A\cap C|=8, ∣B∩C∣=5|B\cap C|=5, ∣A∩B∩C∣=3|A\cap B\cap C|=3, what is ∣A∪B∪C∣|A\cup B\cup 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?

References

  1. Wikipedia contributors (2024). Inclusion–exclusion principle
  2. James Maynard (2015). Small gaps between primes · arXiv:1311.4600
  3. Richard A. Brualdi (2017). Introductory Combinatorics (Classic Version), 5th edition