TExcelTAD
Home › Discrete Mathematics › Inclusion-Exclusion

Inclusion-Exclusion

The sum rule needs disjoint sets. When sets overlap, adding their sizes counts shared elements several times. Inclusion-exclusion corrects for this by alternately subtracting and adding the sizes of intersections. It counts integers with divisibility conditions, surjective functions and derangements (permutations with no fixed point).

Two and three sets

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣\lvert A \cup B \cup C \rvert = \lvert A \rvert + \lvert B \rvert + \lvert C \rvert - \lvert A \cap B \rvert - \lvert A \cap C \rvert - \lvert B \cap C \rvert + \lvert A \cap B \cap C \rvert

An element in exactly two of the three sets is added twice and subtracted once. An element in all three is added 3 times, subtracted 3 times and added back once. Either way it is counted once.

The general rule

∣⋃i=1nAi∣=∑∅≠S⊆{1..n}(−1)∣S∣+1∣⋂i∈SAi∣\left\lvert \bigcup_{i=1}^{n} A_i \right\rvert = \sum_{\emptyset \ne S \subseteq \{1..n\}} (-1)^{\lvert S \rvert + 1} \left\lvert \bigcap_{i \in S} A_i \right\rvert

Why it counts each element once: if xx lies in exactly m≥1m \ge 1 of the sets, it is counted (m1)−(m2)+(m3)−⋯=1\binom{m}{1} - \binom{m}{2} + \binom{m}{3} - \cdots = 1 times, because ∑k=0m(−1)k(mk)=0\sum_{k=0}^{m} (-1)^k \binom{m}{k} = 0 (Binomial Theorem).

Counting with divisibility

How many integers in 1,…,10001, \ldots, 1000 are divisible by 2, 3 or 5? Let AdA_d be the multiples of dd: ∣Ad∣=⌊1000/d⌋\lvert A_d \rvert = \lfloor 1000 / d \rfloor, and A2∩A3=A6A_2 \cap A_3 = A_6, and so on.

500+333+200−166−100−66+33=734500 + 333 + 200 - 166 - 100 - 66 + 33 = 734

So 1000−734=2661000 - 734 = 266 numbers are coprime to 30 (in range 1 to 1000). This is how Euler's ϕ\phi formula ϕ(n)=n∏p∣n(1−1p)\phi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) is derived.

python · try it yourself
from itertools import combinations
from math import prod

def count_divisible(N, primes):
    total = 0
    for k in range(1, len(primes) + 1):
        for S in combinations(primes, k):
            total += (-1) ** (k + 1) * (N // prod(S))
    return total

print("inclusion-exclusion:", count_divisible(1000, [2, 3, 5]))
print("brute force:", sum(1 for x in range(1, 1001) if x % 2 == 0 or x % 3 == 0 or x % 5 == 0))
inclusion-exclusion: 734
brute force: 734

Derangements

A derangement is a permutation with no fixed point: nobody gets their own hat back. Let AiA_i be the permutations of nn items that fix item ii. Any kk specified items fixed leave (n−k)!(n-k)! permutations, so

Dn=n!−(n1)(n−1)!+(n2)(n−2)!−⋯=n!∑k=0n(−1)kk!D_n = n! - \binom{n}{1}(n-1)! + \binom{n}{2}(n-2)! - \cdots = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}

The sum is the start of the series for e−1e^{-1}, so Dn≈n!/eD_n \approx n!/e: the probability that a random permutation is a derangement tends to 1e≈0.3679\frac{1}{e} \approx 0.3679, and is already very close for n=6n = 6.

python · try it yourself
from itertools import permutations
from math import factorial, e

def derangements(n):
    return sum((-1) ** k * factorial(n) // factorial(k) for k in range(n + 1))

for n in range(1, 9):
    brute = sum(1 for p in permutations(range(n)) if all(p[i] != i for i in range(n))) if n <= 7 else None
    print(f"n={n}: D_n={derangements(n):6d}  brute={brute}  n!/e={factorial(n) / e:10.3f}  fraction={derangements(n) / factorial(n):.6f}")
n=1: D_n=     0  brute=0  n!/e=     0.368  fraction=0.000000
n=2: D_n=     1  brute=1  n!/e=     0.736  fraction=0.500000
n=3: D_n=     2  brute=2  n!/e=     2.207  fraction=0.333333
n=4: D_n=     9  brute=9  n!/e=     8.829  fraction=0.375000
n=5: D_n=    44  brute=44  n!/e=    44.146  fraction=0.366667
n=6: D_n=   265  brute=265  n!/e=   264.873  fraction=0.368056
n=7: D_n=  1854  brute=1854  n!/e=  1854.112  fraction=0.367857
n=8: D_n= 14833  brute=None  n!/e= 14832.899  fraction=0.367882

Counting surjections

The number of surjective (onto) functions from an mm-set onto an nn-set: subtract functions missing at least one target value.

surj⁡(m,n)=∑k=0n(−1)k(nk)(n−k)m\operatorname{surj}(m, n) = \sum_{k=0}^{n} (-1)^k \binom{n}{k} (n - k)^m

For m=4m = 4, n=3n = 3: 81−3⋅16+3⋅1−0=3681 - 3 \cdot 16 + 3 \cdot 1 - 0 = 36, matching the brute-force count in Finite Cardinality.

In software

Inclusion-exclusion appears in database query planners (estimating the size of A OR B from statistics on AA, BB and A∩BA \cap B), in reliability engineering (probability that at least one of several components fails), and in contest problems that count numbers avoiding several conditions. The same formula holds for probabilities: P(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B).

Exercises

Exercise 1: Three languages

In a team of 50, 30 know Python, 20 know Go, 15 know Rust; 10 know Python and Go, 8 Python and Rust, 5 Go and Rust, and 3 know all three. How many know none?

Answer
Solution
∣P∪G∪R∣=30+20+15−10−8−5+3=45,50−45=5\lvert P \cup G \cup R \rvert = 30 + 20 + 15 - 10 - 8 - 5 + 3 = 45, \qquad 50 - 45 = 5

Exercise 2: Coprime count

How many integers from 1 to 210 are coprime to 210?

Answer
Solution

210=2⋅3⋅5⋅7210 = 2 \cdot 3 \cdot 5 \cdot 7, so ϕ(210)=210⋅12⋅23⋅45⋅67=48\phi(210) = 210 \cdot \frac12 \cdot \frac23 \cdot \frac45 \cdot \frac67 = 48.

Exercise 3: Secret Santa (code)

5 friends each draw a name for a gift exchange. What is the probability nobody draws their own name?

Answer

44120=1130≈0.367\frac{44}{120} = \frac{11}{30} \approx 0.367.

Solution

D5=120−120+60−20+5−1=44D_5 = 120 - 120 + 60 - 20 + 5 - 1 = 44.

python · try it yourself
from fractions import Fraction
from itertools import permutations

good = sum(1 for p in permutations(range(5)) if all(p[i] != i for i in range(5)))
print(good, Fraction(good, 120))
44 11/30

Test yourself

∣A∣=12\lvert A \rvert = 12, ∣B∣=9\lvert B \rvert = 9, ∣A∩B∣=4\lvert A \cap B \rvert = 4. What is ∣A∪B∣\lvert A \cup B \rvert?

  1. 21
  2. 17
  3. 25
  4. 13

As nn grows, the probability that a random permutation of nn items has no fixed point approaches…

  1. 0
  2. 1/21/2
  3. 1/e1/e
  4. 1

How many integers from 1 to 100 are divisible by 2 or 5?

  1. 70
  2. 60
  3. 50
  4. 40