Discrete Math • Counting and Combinatorics

Counting with inclusion–exclusion (applications)

Correcting for overlap to count exactly what you need across unions, derangements, and Euler's totient.

A connected graph with binary values and set braces for discrete mathematics
Inside this lesson 10 sections 5 questions 9 figures

SECTION 1

Historical Context & Motivation

Counting the size of a union of overlapping sets is one of the oldest problems in combinatorics, yet its deceptive simplicity has attracted some of the finest minds in mathematics. The core difficulty is clear: if two sets share elements, naïvely adding their sizes double-counts the intersection. The inclusion–exclusion principle (PIE) provides a systematic correction for arbitrary overlaps among any finite number of sets, and its applications reach far beyond simple set unions into number theory, probability, topology, and algorithm design.

The story of inclusion–exclusion is intertwined with the development of combinatorial analysis itself. From Abraham de Moivre's investigations into card-matching problems in the early 18th century, through Euler's work on the totient function, to the modern sieve methods used in analytic number theory, the principle has continually found new domains of applicability. Understanding its historical arc reveals why the formula takes the form it does and how mathematicians recognized its power.

1708 • De Moivre's Problem of Rencontres

Abraham de Moivre studied the problème des rencontres (matches problem): what is the probability that no card in a shuffled deck lands in its original position? His solution implicitly used inclusion–exclusion over individual position events.

1761 • Euler's Totient Function

Leonhard Euler introduced φ(n), counting the integers from 1 to n that are coprime to n. His product formula is a direct consequence of inclusion–exclusion applied to sets of multiples of each prime factor of n.

1854 • Sylvester's Sieve Formula

James Joseph Sylvester formally stated the general inclusion–exclusion formula for n sets, calling it the sieve formula. He connected it to the enumeration of permutations with forbidden patterns.

1919 • Brun's Sieve in Number Theory

Viggo Brun adapted inclusion–exclusion into a combinatorial sieve to bound the count of twin primes, proving that the sum of reciprocals of twin primes converges—a landmark in analytic number theory.

1964 • Rota's Möbius Inversion

Gian-Carlo Rota generalized inclusion–exclusion to arbitrary partially ordered sets via the Möbius function of a poset, unifying the principle with number-theoretic Möbius inversion and lattice theory.

The recurring question across all these developments is deceptively simple: given a universe of objects and several overlapping properties, how many objects possess none of the properties—or exactly k of them? The inclusion–exclusion principle answers this question completely, and its applications illustrate the extraordinary reach of a single combinatorial identity.

SECTION 2

Core Principles & Definitions

Before diving into applications, we establish the conceptual foundations that make inclusion–exclusion work. The principle rests on a careful alternating correction: first we over-count by adding, then we over-correct by subtracting pairwise intersections, then we re-correct by adding triple intersections, and so on. This alternation ensures that every element in the union is counted exactly once, regardless of how many sets it belongs to.

1

Alternating Sign Correction

Each successive order of intersection carries an alternating sign: positive for odd-sized index subsets, negative for even-sized. An element in exactly k sets is added C(k,1) − C(k,2) + C(k,3) − ⋯ = 1 time by the alternating sum identity.
2

Complement Counting

Many applications focus on counting elements in none of the sets: |U| − |A₁ ∪ ⋯ ∪ Aₙ|. Derangements, surjections, and the Euler totient all reduce to this complementary form.
3

Symmetry in Intersection Sizes

In many classical applications, the intersection of any k chosen sets depends only on k, not on which k sets are chosen. This symmetric structure collapses the exponentially many terms into a single binomial sum.
4

From Sets to Properties

We model each "bad" property as a set Aᵢ of objects possessing that property. Inclusion–exclusion then counts objects satisfying exactly zero, exactly one, or exactly k of the properties—the foundation of sieve methods.
✦

KEY TAKEAWAY

Think of inclusion–exclusion like reconciling bank statements: if a transaction appears on multiple credit-card bills, you don't want to pay it twice. You add up every bill, subtract every pairwise overlap, add back every triple overlap, and continue until every charge is counted exactly once. The alternating correction is the mathematical analogue of this reconciliation process.

SECTION 3

Visual Explanation — Venn Diagram Accounting

The Venn diagram below illustrates inclusion–exclusion for three sets A, B, and C. Each region is labeled with the net count it receives at each stage of the formula. Observe how the alternating signs systematically correct every region to a final count of exactly 1.

Inclusion–Exclusion for Three Sets: Region-by-Region AccountingA onlycount: 1B onlycount: 1C onlycount: 1A∩B+2 −1 = 1(no C)A∩C+2 −1 = 1B∩C+2 −1 = 1A∩B∩C+3 −3 +1 = 1Legend: +k means counted k times by ΣS, −k corrected by −Σ(S∩T), +k restored by +|A∩B∩C|ABC
Each region shows its net count after all three stages of the formula. Elements in exactly one set are counted once by the first sum and never subtracted. Elements in two sets are counted twice, then subtracted once. The triple intersection receives +3 − 3 + 1 = 1, confirming the alternating correction.

The diagram makes the algebraic identity tangible: the binomial alternating-sum identity C(k,1) − C(k,2) + C(k,3) − ⋯ + (−1)k+1C(k,k) = 1 is precisely the statement that an element belonging to exactly k of the n sets is counted exactly once by the inclusion–exclusion formula. This fact is the combinatorial engine behind every application we will see.

SECTION 4

Mathematical Framework

We now state the principle formally and derive the key formulas used in its most important applications. Let U be a finite universe and A₁, A₂, …, Aₙ be subsets of U.

GENERAL INCLUSION–EXCLUSION

∣A1∪A2∪⋯∪An∣=Σi∣Ai∣−Σi<j∣Ai∩Aj∣+Σi<j<k∣Ai∩Aj∩Ak∣−⋯+(−1)n+1∣A1∩⋯∩An∣|A₁ ∪ A₂ ∪ ⋯ ∪ Aₙ| = Σᵢ |Aᵢ| − Σᵢ<ⱼ |Aᵢ ∩ Aⱼ| + Σᵢ<ⱼ<ₖ |Aᵢ ∩ Aⱼ ∩ Aₖ| − ⋯ + (−1)ⁿ⁺¹ |A₁ ∩ ⋯ ∩ Aₙ|

The sum at depth k runs over all C(n, k) subsets of indices of size k. The sign of the k-th layer is (−1)k+1.

Application 1: Derangements

A derangement of [n] = {1, 2, …, n} is a permutation σ with no fixed points: σ(i) ≠ i for all i. Let Aᵢ be the set of permutations fixing i. The set of derangements is the complement of A₁ ∪ ⋯ ∪ Aₙ within the universe of all n! permutations. Since any intersection Aᵢ₁ ∩ ⋯ ∩ Aᵢₖ has size (n − k)! (the remaining n − k elements permute freely), inclusion–exclusion yields the classical formula.

DERANGEMENT COUNT Dₙ

Dn=n!Σk=0n(−1)k/k!≈n!/eDₙ = n! Σₖ₌₀ⁿ (−1)ᵏ / k! ≈ n! / e

Dₙ = number of derangements of n elements. The ratio Dₙ/n! → 1/e ≈ 0.3679 as n → ∞, so roughly 36.8% of all permutations are derangements.

Application 2: Euler's Totient Function

For a positive integer n with prime factorization n = p₁a₁ p₂a₂ ⋯ pₘaₘ, define Aᵢ = {k ∈ {1, …, n} : pᵢ | k}. Then |Aᵢ| = n/pᵢ, and more generally the intersection of sets corresponding to distinct primes pᵢ₁, …, pᵢₖ has size n/(pᵢ₁ ⋯ pᵢₖ). The complement count gives Euler's formula.

EULER'S TOTIENT

φ(n)=n∏p∣n(1−1/p)φ(n) = n ∏ₚ|ₙ (1 − 1/p)

The product runs over the distinct prime divisors p of n. For example, φ(12) = 12 × (1 − 1/2)(1 − 1/3) = 12 × 1/2 × 2/3 = 4.

Application 3: Surjections

The number of surjections (onto functions) from an m-element set to an n-element set can be computed by letting Aⱼ be the set of functions whose image misses element j. The intersection of any k such sets consists of functions mapping into a codomain of size n − k, giving (n − k)m functions.

SURJECTION COUNT

S(m,n)=Σk=0n(−1)kC(n,k)(n−k)mS(m, n) = Σₖ₌₀ⁿ (−1)ᵏ C(n, k) (n − k)ᵐ

S(m, n) counts functions from an m-set onto an n-set. The Stirling numbers of the second kind are related by S₂(m, n) = S(m, n) / n!.

SECTION 5

Detailed Applications & Classification

Inclusion–exclusion applications share a common structure: define a universe U, identify n "bad" properties, model each property as a set, compute intersection sizes, and apply the formula. The diagram below organizes the major application families by the nature of the universe and the type of forbidden property.

Taxonomy of Inclusion–Exclusion ApplicationsInclusion–Exclusion PrinciplePermutation ProblemsU = S_n (all permutations)Property: σ(i) = i (fixed pt)Function ProblemsU = n^m (all functions)Property: j ∉ imageNumber TheoryU = {1, …, n}Property: p_i | kDerangementsD_n = n!Σ(-1)^k/k!Forbidden PositionsMénage, rook polySurjectionsΣ(-1)^k C(n,k)(n-k)^mStirling NumbersS(n,k) = surj/k!Euler Totientφ(n)=nΠ(1-1/p)Sieve ofEratosthenesProbability ApplicationsP(A₁ ∪ ⋯ ∪ Aₙ) = ΣP(Aᵢ) − ΣP(Aᵢ∩Aⱼ) + ⋯ — directly analogous, dividing cardinalities by |U|All branches share the same alternating-sign engine; only the universe U and the "bad" property differ.
The taxonomy shows three major families—permutation problems, function problems, and number-theoretic problems—each branching into specific applications. The probability row at the bottom emphasizes that every counting application has a direct probabilistic analogue.
ApplicationUniverse UProperty Aᵢ|Aᵢ₁ ∩ ⋯ ∩ Aᵢₖ|
DerangementsAll n! permutationsσ(i) = i (fixed point at i)(n − k)!
SurjectionsAll nᵐ functions [m] → [n]j not in image of f(n − k)ᵐ
Euler φ(n){1, 2, …, n}pᵢ divides kn / (pᵢ₁ ⋯ pᵢₖ)
Hatcheck (probability)All n! permutations (uniform)Person i gets own hat(n − k)! / n! = 1/(n(n−1)⋯(n−k+1))

Comparison of four classic inclusion–exclusion applications

SECTION 6

Worked Example — Counting Surjections

We compute the number of surjections (onto functions) from a 5-element set onto a 3-element set. The total number of functions is 35 = 243, but most of these miss at least one element of the codomain.

How many surjections f: {1, 2, 3, 4, 5} → {a, b, c} exist?

  1. 1

    Step 1 — Define the universe and properties

    Let U be the set of all functions from {1, 2, 3, 4, 5} to {a, b, c}, so |U| = 3⁵ = 243. Define A₁ = {f : a ∉ im(f)}, A₂ = {f : b ∉ im(f)}, A₃ = {f : c ∉ im(f)}. We want |U| − |A₁ ∪ A₂ ∪ A₃|.
  2. 2

    Step 2 — Compute single-set sizes

    Each Aᵢ consists of functions mapping into a 2-element codomain, so |Aᵢ| = 2⁵ = 32. There are C(3,1) = 3 such sets.
    Σ|Aᵢ| = 3 × 32 = 96
  3. 3

    Step 3 — Compute pairwise intersections

    Aᵢ ∩ Aⱼ consists of functions mapping into a single remaining element, so |Aᵢ ∩ Aⱼ| = 1⁵ = 1. There are C(3,2) = 3 such pairs.
    Σ|Aᵢ ∩ Aⱼ| = 3 × 1 = 3
  4. 4

    Step 4 — Compute the triple intersection

    |A₁ ∩ A₂ ∩ A₃| counts functions mapping into the empty set, which is impossible.
    |A₁ ∩ A₂ ∩ A₃| = 0
  5. 5

    Step 5 — Apply inclusion–exclusion

    |A₁ ∪ A₂ ∪ A₃| = 96 − 3 + 0 = 93. Therefore, the number of surjections is 243 − 93 = 150.
    Number of surjections = 150
  6. 6

    Step 6 — Verify with the formula

    Using S(5, 3) = Σₖ₌₀³ (−1)ᵏ C(3,k)(3−k)⁵ = C(3,0)·3⁵ − C(3,1)·2⁵ + C(3,2)·1⁵ − C(3,3)·0⁵ = 243 − 96 + 3 − 0 = 150. ✓ The formula confirms our step-by-step calculation.

SECTION 7

Strengths, Limitations & Comparisons

Inclusion–exclusion is a universal tool—it works for any finite collection of sets—but universality comes at a cost. Understanding when the principle shines and when alternative methods are preferable is essential for efficient problem-solving in combinatorics.

AspectStrengthLimitation
GeneralityWorks for any finite union of finite sets with no structural assumptions on the sets.Must evaluate 2ⁿ − 1 intersection terms for n sets, which is exponential.
ExactnessYields exact counts, unlike probabilistic sieves that only give bounds.Cancellation among large terms can cause numerical instability in floating-point computation.
Symmetric casesWhen intersection sizes depend only on the number of sets, the 2ⁿ terms collapse to n + 1 terms.Asymmetric cases may require computing each intersection individually.
ExtensionsGeneralizes to weighted versions, Möbius inversion, and chromatic polynomials.For problems with special structure (e.g., graph coloring), specialized formulas (e.g., deletion-contraction) may be simpler.

Strengths and limitations of the inclusion–exclusion principle

⚡

WHEN TO USE (AND NOT USE) PIE

Reach for inclusion–exclusion when you can cleanly define "bad" properties whose intersections have computable sizes. If the number of properties is small (say ≤ 10) or symmetric, PIE is ideal. For large n with non-symmetric intersections, consider generating functions, transfer matrices, or Burnside's lemma, which may exploit additional structure that PIE ignores.

SECTION 8

Connection to Advanced Theory

The inclusion–exclusion principle is a special case of Möbius inversion on the Boolean lattice of subsets of [n]. Rota's landmark 1964 paper showed that for any locally finite partially ordered set (poset), the Möbius function μ plays the same role that (−1)|S| plays in PIE. This unification connects inclusion–exclusion to number-theoretic Möbius inversion (where the poset is the divisibility lattice), the chromatic polynomial of a graph (via the lattice of partitions), and even to the theory of species in algebraic combinatorics.

FeatureClassical PIEMöbius Inversion on Posets
DomainBoolean lattice 2^[n]Any locally finite poset (P, ≤)
Inversion weight(−1)^|S| for each subset Sμ(x, y) defined recursively on intervals of the poset
Key identityg(S) = Σ_{T⊇S} f(T) ⇒ f(S) = Σ_{T⊇S} (−1)∣T∣−∣S∣(−1)^{|T|−|S|} g(T)g(x) = Σ_{y≤x} f(y) ⇒ f(x) = Σ_{y≤x} μ(y,x) g(y)
Example applicationDerangements, surjections, Euler φNumber-theoretic Möbius function, chromatic polynomials, Tutte polynomial

Classical PIE as a special case of Möbius inversion

The connection to Möbius inversion also opens the door to efficient computation. For specific poset structures—such as the divisor lattice or the partition lattice—the Möbius function has closed-form expressions, and fast algorithms (like the fast zeta transform on subsets, running in O(n × 2ⁿ) time) can evaluate the full inclusion–exclusion sum efficiently. These techniques form the backbone of modern competitive programming and are active areas of research in algebraic combinatorics.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Explain why simply adding |A₁| + |A₂| + ⋯ + |Aₙ| over-counts the union |A₁ ∪ ⋯ ∪ Aₙ|, and describe the precise role of the alternating signs in the inclusion–exclusion formula. Why does the correction terminate after the n-fold intersection?
Show answer
An element x that belongs to exactly k of the n sets is counted k times in the first sum Σ|Aᵢ|, so it is over-counted by k − 1. The pairwise subtraction removes it C(k,2) times, which may over-correct, so the triple-intersection term adds it back C(k,3) times, and so on. The net count of x across all layers is C(k,1) − C(k,2) + C(k,3) − ⋯ + (−1)k+1(−1)^{k+1}C(k,k) = 1, by the binomial theorem identity (1−1)k(1 − 1)^k expanded and rearranged. The correction terminates at the n-fold intersection because there are at most n sets, so intersections of more than n sets are empty.

Question 2: BASIC CALCULATION

Compute D₄, the number of derangements of 4 elements, using the formula Dₙ = n! Σₖ₌₀ⁿ (−1)ᵏ / k!.
Show answer
We have D₄ = 4!(1/0! − 1/1! + 1/2! − 1/3! + 1/4!) = 24(1 − 1 + 1/2 − 1/6 + 1/24) = 24(0 + 0.5 − 0.16667 + 0.04167) = 24 × (3/8) = 9. Alternatively, the exact computation is 24 × (12/24 − 4/24 + 1/24) = 24 × 9/24 = 9. So there are exactly 9 derangements of 4 elements. These correspond to the permutations (2,1,4,3), (2,3,4,1), (2,4,1,3), (3,1,4,2), (3,4,1,2), (3,4,2,1), (4,1,2,3), (4,3,1,2), (4,3,2,1) — wait, let us be careful: (4,3,2,1) has σ(1)=4, σ(2)=3, σ(3)=2, σ(4)=1, so no fixed points — confirmed. The ratio D₄/4! = 9/24 = 0.375, already close to 1/e ≈ 0.3679.

Question 3: INTERMEDIATE

A classroom has 100 students. Among them, 40 study French, 35 study German, 30 study Spanish, 15 study both French and German, 12 study both French and Spanish, 10 study both German and Spanish, and 5 study all three languages. How many students study none of these three languages?
Show answer
Let F, G, S denote the sets of students studying French, German, and Spanish, respectively. By inclusion–exclusion: |F ∪ G ∪ S| = |F| + |G| + |S| − |F ∩ G| − |F ∩ S| − |G ∩ S| + |F ∩ G ∩ S| = 40 + 35 + 30 − 15 − 12 − 10 + 5 = 73. The number studying none of the three languages is 100 − 73 = 27. This is a direct application of the complement form: subtract the union from the universe.

Question 4: APPLIED

A software system generates 6-digit numeric PINs (digits 0–9 for each position). How many such PINs use all three digits 0, 1, and 2 at least once? (A PIN is an ordered sequence of 6 digits.)
Show answer
The universe is all 10⁶ = 1,000,000 six-digit PINs (leading zeros allowed). Let A₁ = {PINs missing 0}, A₂ = {PINs missing 1}, A₃ = {PINs missing 2}. We want |U| − |A₁ ∪ A₂ ∪ A₃|... but actually, we want PINs that do NOT miss any of 0, 1, 2, but may or may not include digits 3–9. So |A₁| = 9⁶ (choose from 9 remaining digits), |A₁ ∩ A₂| = 8⁶, |A₁ ∩ A₂ ∩ A₃| = 7⁶. By symmetry and inclusion–exclusion: |A₁ ∪ A₂ ∪ A₃| = 3 × 9⁶ − 3 × 8⁶ + 7⁶ = 3(531,441) − 3(262,144) + 117,649 = 1,594,323 − 786,432 + 117,649 = 925,540. Thus PINs containing all of {0,1,2} = 1,000,000 − 925,540 = 74,460.

Question 5: CRITICAL THINKING

Derive Euler's product formula φ(n) = n ∏_{p|n} (1 − 1/p) from the inclusion–exclusion principle. Specifically, for n = p₁^{a₁} ⋯ pₘ^{aₘ}, define appropriate sets, verify that their intersection sizes satisfy the conditions for PIE, and show that the alternating sum telescopes into the product form.
Show answer
Let U = {1, 2, …, n} and define Aᵢ = {k ∈ U : pᵢ | k} for i = 1, …, m. Then |Aᵢ| = n/pᵢ. For any subset {i₁, …, iₛ} of indices, |Aᵢ₁ ∩ ⋯ ∩ Aᵢₛ| counts multiples of lcm(pᵢ₁, …, pᵢₛ) = pᵢ₁ ⋯ pᵢₛ (since the primes are distinct), giving n/(pᵢ₁ ⋯ pᵢₛ). By inclusion–exclusion, φ(n) = |U| − |A₁ ∪ ⋯ ∪ Aₘ| = Σ_{S ⊆ [m]} (−1)∣S∣(−1)^{|S|} n/∏_{i∈S} pᵢ = n Σ_{S ⊆ [m]} ∏_{i∈S} (−1/pᵢ). This last sum factors over independent choices for each prime: n ∏_{i=1}^{m} (1 − 1/pᵢ). The factorization is valid because each subset S corresponds to an independent binary choice (include pᵢ or not) for each prime, and the product of sums equals the sum of products by distributivity. This yields the classical formula φ(n) = n ∏_{p|n} (1 − 1/p).

SUMMARY

Summary & Review

Summary

The inclusion–exclusion principle computes the size of a union of sets by an alternating sum over all possible intersections: add the individual sizes, subtract pairwise intersections, add triple intersections, and continue with alternating signs through the n-fold intersection. The key algebraic identity C(k,1) − C(k,2) + ⋯ + (−1)k+1C(k,k) = 1 guarantees that every element is counted exactly once.

Major applications include derangements (permutations with no fixed points, yielding Dₙ = n! Σ (−1)ᵏ/k!), surjections (onto functions, yielding S(m,n) = Σ (−1)ᵏ C(n,k)(n−k)ᵐ), and Euler's totient function (φ(n) = n ∏(1 − 1/p)). All share the same engine: define a universe, specify forbidden properties as sets, compute symmetric intersection sizes, and apply the alternating formula. The principle generalizes to Möbius inversion on arbitrary posets, connecting combinatorics to number theory, graph theory, and algebraic structures.

Varsity Tutors • Discrete Math • Counting with inclusion–exclusion (applications)