CombinatoricsTopic #20 of 40

Inclusion-Exclusion

Principle of inclusion-exclusion for counting, derangements, and Euler's totient function.

Two Sets

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|

We subtract ∣A∩B∣|A \cap B| because elements in both sets were counted twice.

Three 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|

General Formula

For nn sets A1,A2,…,AnA_1, A_2, \ldots, A_n:

∣⋃i=1nAi∣=∑i∣Ai∣−∑i<j∣Ai∩Aj∣+∑i<j<k∣Ai∩Aj∩Ak∣−⋯+(−1)n+1∣A1∩A2∩⋯∩An∣\left| \bigcup_{i=1}^{n} A_i \right| = \sum_{i} |A_i| - \sum_{i<j} |A_i \cap A_j| + \sum_{i<j<k} |A_i \cap A_j \cap A_k| - \cdots + (-1)^{n+1} |A_1 \cap A_2 \cap \cdots \cap A_n|

Or more compactly: ∣⋃i=1nAi∣=∑∅≠S⊆{1,…,n}(−1)∣S∣+1∣⋂i∈SAi∣\left| \bigcup_{i=1}^{n} A_i \right| = \sum_{\emptyset \neq S \subseteq \{1,\ldots,n\}} (-1)^{|S|+1} \left| \bigcap_{i \in S} A_i \right|

Complement Form

For counting elements NOT in any set: ∣A1∪A2∪⋯∪An‾∣=∣U∣−∣⋃i=1nAi∣\left| \overline{A_1 \cup A_2 \cup \cdots \cup A_n} \right| = |U| - \left| \bigcup_{i=1}^{n} A_i \right|

This is useful for "none of the above" counting.

Examples

Integers Divisible by 2, 3, or 5

How many integers from 1 to 1000 are divisible by 2, 3, or 5?

Let A2,A3,A5A_2, A_3, A_5 be sets of multiples.

∣A2∣=⌊1000/2⌋=500|A_2| = \lfloor 1000/2 \rfloor = 500 ∣A3∣=⌊1000/3⌋=333|A_3| = \lfloor 1000/3 \rfloor = 333 ∣A5∣=⌊1000/5⌋=200|A_5| = \lfloor 1000/5 \rfloor = 200 ∣A2∩A3∣=⌊1000/6⌋=166|A_2 \cap A_3| = \lfloor 1000/6 \rfloor = 166 ∣A2∩A5∣=⌊1000/10⌋=100|A_2 \cap A_5| = \lfloor 1000/10 \rfloor = 100 ∣A3∩A5∣=⌊1000/15⌋=66|A_3 \cap A_5| = \lfloor 1000/15 \rfloor = 66 ∣A2∩A3∩A5∣=⌊1000/30⌋=33|A_2 \cap A_3 \cap A_5| = \lfloor 1000/30 \rfloor = 33

∣A2∪A3∪A5∣=500+333+200−166−100−66+33=734|A_2 \cup A_3 \cup A_5| = 500 + 333 + 200 - 166 - 100 - 66 + 33 = 734

Euler's Totient Function

ϕ(n)\phi(n) = count of integers from 1 to nn that are coprime to nn.

For n=p1a1p2a2⋯pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}:

ϕ(n)=n∏p∣n(1−1p)=n(1−1p1)(1−1p2)⋯(1−1pk)\phi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_k}\right)

Example: ϕ(12)=ϕ(22⋅3)=12⋅(1−1/2)(1−1/3)=12⋅12⋅23=4\phi(12) = \phi(2^2 \cdot 3) = 12 \cdot (1 - 1/2)(1 - 1/3) = 12 \cdot \frac{1}{2} \cdot \frac{2}{3} = 4

Derangements

A derangement is a permutation where no element is in its original position.

Let AiA_i = permutations where element ii is fixed.

The number of derangements of nn elements: Dn=n!−∣A1∪A2∪⋯∪An∣D_n = n! - |A_1 \cup A_2 \cup \cdots \cup A_n|

Using inclusion-exclusion: Dn=n!∑k=0n(−1)kk!=n!(1−1+12!−13!+⋯+(−1)nn!)D_n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!} = n! \left(1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \cdots + \frac{(-1)^n}{n!}\right)

nnDnD_nFormula
01—
101−11 - 1
212−2+02 - 2 + 0
326−6+3−16 - 6 + 3 - 1
4924−24+12−4+124 - 24 + 12 - 4 + 1
544—

As n→∞n \to \infty: Dn/n!→1/e≈0.368D_n / n! \to 1/e \approx 0.368

Surjective Functions

The number of surjective (onto) functions from an nn-set to a kk-set:

S(n,k)⋅k!=∑j=0k(−1)j(kj)(k−j)nS(n, k) \cdot k! = \sum_{j=0}^{k} (-1)^j \binom{k}{j} (k-j)^n

where S(n,k)S(n, k) is the Stirling number of the second kind.

Derivation: Let AiA_i = functions missing element ii in the range. Surjections = Total functions - (functions missing at least one element)

Sieve of Eratosthenes

Primes up to nn: Start with all numbers, sieve out multiples.

Using inclusion-exclusion with prime divisors gives the count of numbers with no small prime factors.

Hat Check Problem

nn people check hats, hats returned randomly. Probability no one gets own hat?

P=Dnn!=∑k=0n(−1)kk!≈1e≈0.368P = \frac{D_n}{n!} = \sum_{k=0}^{n} \frac{(-1)^k}{k!} \approx \frac{1}{e} \approx 0.368

Counting with "At Least" Conditions

Example: Distribute 10 identical balls into 4 distinct boxes, each box has at least 1 ball.

First give each box 1 ball (4 balls used). Distribute remaining 6: (6+4−14−1)=(93)=84\binom{6 + 4 - 1}{4 - 1} = \binom{9}{3} = 84

Alternatively, using inclusion-exclusion on "box ii is empty": (133)−4(123)+(42)(113)−(43)(103)+(44)(93)\binom{13}{3} - 4\binom{12}{3} + \binom{4}{2}\binom{11}{3} - \binom{4}{3}\binom{10}{3} + \binom{4}{4}\binom{9}{3}

Both methods give 84.