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
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
Why it counts each element once: if lies in exactly of the sets, it is counted times, because (Binomial Theorem).
Counting with divisibility
How many integers in are divisible by 2, 3 or 5? Let be the multiples of : , and , and so on.
So numbers are coprime to 30 (in range 1 to 1000). This is how Euler's formula is derived.
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 be the permutations of items that fix item . Any specified items fixed leave permutations, so
The sum is the start of the series for , so : the probability that a random permutation is a derangement tends to , and is already very close for .
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 -set onto an -set: subtract functions missing at least one target value.
For , : , 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 , and ), 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: .
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
Exercise 2: Coprime count
How many integers from 1 to 210 are coprime to 210?
Answer
Solution
, so .
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
.
Solution
.
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
, , . What is ?
- 21
- 17
- 25
- 13
As grows, the probability that a random permutation of items has no fixed point approaches…
- 0
- 1
How many integers from 1 to 100 are divisible by 2 or 5?
- 70
- 60
- 50
- 40