DISCRETE MATH • SETS, RELATIONS, AND FUNCTIONS

Inclusion–exclusion principle (intro and applications)

A systematic method for computing the cardinality of unions by correcting for overcounted intersections.

Historical Context & Motivation

Counting the elements in a union of overlapping sets is deceptively difficult: a naïve sum of individual cardinalities double-counts every element that belongs to more than one set. The inclusion–exclusion principle (often abbreviated PIE) resolves this overcount through a systematic alternation of additions and subtractions. Its origins trace back to classical problems in number theory and probability, where mathematicians sought exact formulas for quantities such as the count of integers coprime to a given modulus or the probability of no coincidences in a random permutation. What began as ad-hoc corrections eventually crystallized into one of the most general and elegant counting tools in combinatorics.

1713
Bernoulli's Ars Conjectandi
Abraham de Moivre and Jacob Bernoulli formulate early instances of additive corrections when computing probabilities of compound events, laying conceptual groundwork for inclusion–exclusion.
1854
Da Silva's Generalization
Daniel da Silva publishes a systematic treatment of alternating sums for counting elements in unions, offering one of the first explicit statements of the general principle.
1867
Sylvester's Sieve Formula
James Joseph Sylvester frames the principle as a sieve and uses it to derive combinatorial identities, connecting it to the broader theory of sieve methods.
20th c.
Modern Combinatorics & Möbius Inversion
Gian-Carlo Rota generalizes inclusion–exclusion through Möbius inversion on partially ordered sets, embedding the classical formula into abstract algebraic combinatorics.

The central question that inclusion–exclusion answers is both simple to state and surprisingly deep: Given finite sets A₁, A₂, …, Aₙ drawn from a common universe, how do we compute |A₁ ∪ A₂ ∪ … ∪ Aₙ| in terms of the cardinalities of all possible intersections? The answer has far-reaching consequences—from deriving Euler's totient function to computing derangement counts to solving surjection problems—making it indispensable across number theory, probability, and algorithm design.

Core Principles & Definitions

Before stating the general formula, it is essential to internalize the intuition behind the alternating-sum structure. The principle rests on a self-correcting mechanism: each successive term in the sum repairs the error introduced by the previous round of corrections. Understanding the following foundational ideas provides the conceptual scaffolding for the formal statement.

1

Overcount by Naïve Summation

Summing |A₁| + |A₂| + … + |Aₙ| counts every element once for each set it belongs to. An element in k sets is counted k times instead of once.
2

Pairwise Correction

Subtracting all pairwise intersections |Aᵢ ∩ Aⱼ| corrects the double-count but now under-counts elements in three or more sets, since those elements had too much subtracted.
3

Alternating Sign Pattern

Adding triple intersections, subtracting quadruple intersections, and so on produces a telescoping correction. The sign of the k-fold intersection term is (−1)^(k+1).
4

Exact Cancellation Guarantee

A combinatorial identity ensures that the alternating sum of binomial coefficients C(k,1) − C(k,2) + … ± C(k,k) equals exactly 1 for every k ≥ 1, guaranteeing each element is counted precisely once.
5

Complementary Counting Dual

An equivalent formulation counts elements in the complement: |U| − |A₁ ∪ … ∪ Aₙ| gives the number of elements satisfying none of the properties defined by A₁, …, Aₙ.
KEY TAKEAWAY
Think of inclusion–exclusion as balancing a checkbook with multiple shared expenses. If you and two roommates split overlapping bills, you first total everyone's payments (overcount), then subtract what was double-paid between every pair (undershoot), then add back what all three overpaid together (exact). Each correction layer fixes the error of the previous one, and the process always terminates with the correct balance.

Visual Explanation — Venn Diagram Perspective

A Venn diagram of three sets provides the canonical visualization for inclusion–exclusion with n = 3. Each region of the diagram corresponds to a distinct intersection pattern, and the principle's alternating sum can be read directly from the diagram. The following SVG illustrates three overlapping sets A, B, and C, labeling each of the seven non-empty regions and showing how the formula accounts for every region exactly once.

The seven regions of a three-set Venn diagram. Summing |A| + |B| + |C| counts the pairwise-intersection regions (A∩B only, A∩C only, B∩C only) twice each and the triple intersection A∩B∩C three times. Subtracting pairwise intersections corrects the pairs but removes A∩B∩C entirely, which must then be added back.

Observe that the diagram partitions the union A ∪ B ∪ C into seven disjoint regions. The inclusion–exclusion formula ensures that each region's contribution to the final count is exactly 1, regardless of how many sets it lies in. An element in exactly k of the three sets initially receives a count of k from the sum of individual cardinalities. After subtracting C(k, 2) pairwise intersections and adding back C(k, 3) triple intersections, the net count is C(k, 1) − C(k, 2) + C(k, 3) = 1 for k = 1, 2, and 3—a fact that generalizes to any number of sets.

Mathematical Framework

We now present the formal statement of the inclusion–exclusion principle for n finite sets, followed by its proof sketch and complementary counting form. The general formula involves a sum over all 2ⁿ − 1 non-empty subsets of the index set {1, 2, …, n}, grouped by the size of each subset.

GENERAL INCLUSION–EXCLUSION
|A₁ ∪ A₂ ∪ … ∪ Aₙ| = Σ|Aᵢ| − Σ|Aᵢ ∩ Aⱼ| + Σ|Aᵢ ∩ Aⱼ ∩ Aₖ| − … + (−1)ⁿ⁺¹|A₁ ∩ A₂ ∩ … ∩ Aₙ|
The k-th summation runs over all C(n, k) subsets of size k from {1, …, n}. The sign of the k-th term is (−1)k+1. For n = 2: |A ∪ B| = |A| + |B| − |A ∩ B|.
COMPACT SIGMA NOTATION
|⋃ᵢ₌₁ⁿ Aᵢ| = Σₖ₌₁ⁿ (−1)ᵏ⁺¹ Σ_{|S|=k, S⊆[n]} |⋂ᵢ∈S Aᵢ|
Here [n] = {1, 2, …, n}. The inner sum ranges over all subsets S of [n] with exactly k elements.
COMPLEMENTARY COUNTING FORM
|Ā₁ ∩ Ā₂ ∩ … ∩ Āₙ| = |U| − Σ|Aᵢ| + Σ|Aᵢ ∩ Aⱼ| − … + (−1)ⁿ|A₁ ∩ … ∩ Aₙ|
Counts elements in the universe U that belong to none of A₁, …, Aₙ. Used extensively in derangement and surjection problems.

Proof Sketch (Double-Counting Argument)

Fix an arbitrary element x ∈ A₁ ∪ A₂ ∪ … ∪ Aₙ and suppose x lies in exactly m of the n sets (where 1 ≤ m ≤ n). The right-hand side counts x a net total of C(m, 1) − C(m, 2) + C(m, 3) − … + (−1)m+1C(m, m). By the binomial theorem applied to (1 − 1)m = 0, we know that Σₖ₌₀ᵐ (−1)ᵏ C(m, k) = 0, which implies Σₖ₌₁ᵐ (−1)ᵏ⁺¹ C(m, k) = 1. Hence x contributes exactly 1 to the right-hand side. Since x was arbitrary, the formula correctly counts every element of the union precisely once, completing the proof.

Detailed Applications & Classification

The inclusion–exclusion principle appears across a remarkable range of discrete mathematics problems. While the formula itself is a single identity, its power lies in the freedom to define the sets A₁, …, Aₙ to encode whichever properties are convenient. This section classifies the major application families and provides a visual decision flow for recognizing when PIE is the right tool.

Application map showing the four major families of inclusion–exclusion problems: derangements, Euler's totient, surjections, and forbidden-pattern counting. The decision checklist at the bottom summarizes when PIE is the appropriate strategy.
How to set up common PIE problems by choosing the universe and property sets.
ApplicationUniverse UProperty AᵢGoal
DerangementsAll n! permutations of [n]Aᵢ = permutations fixing i|Ā₁ ∩ … ∩ Āₙ| (no fixed points)
Euler's Totient{1, 2, …, n}Aᵢ = multiples of pᵢ in U|Ā₁ ∩ … ∩ Āᵣ| = φ(n)
SurjectionsAll nᵐ functions [m] → [n]Aᵢ = functions missing i in range|Ā₁ ∩ … ∩ Āₙ| (onto functions)
Hatcheck (classic)All n! permutationsAᵢ = person i gets own hatNo one gets their hat → Dₙ

Worked Example — Derangements of 5 Elements

Five students submit homework and a careless TA returns the papers at random. How many ways can the papers be returned so that no student receives their own work? This is the classical derangement problem for n = 5, and it provides an ideal setting to see inclusion–exclusion in action.

Computing D₅ via Inclusion–Exclusion
1
Step 1 — Define the Universe and PropertiesLet U be the set of all 5! = 120 permutations of {1, 2, 3, 4, 5}. For each i ∈ {1, …, 5}, define Aᵢ as the set of permutations that fix element i (i.e., σ(i) = i). We seek |Ā₁ ∩ Ā₂ ∩ Ā₃ ∩ Ā₄ ∩ Ā₅|, the number of permutations with no fixed point.
D₅ = |U| − |A₁ ∪ A₂ ∪ A₃ ∪ A₄ ∪ A₅|
2
Step 2 — Compute Individual and Intersection SizesIf a permutation fixes a particular set S of k elements, the remaining 5 − k elements can be arranged in (5 − k)! ways. Hence |⋂ᵢ∈S Aᵢ| = (5 − k)! for every S with |S| = k. There are C(5, k) such subsets.
Σ over |S|=k: C(5,k) × (5−k)!
3
Step 3 — Apply the Alternating SumBy the complementary form of PIE: D₅ = Σₖ₌₀⁵ (−1)ᵏ C(5,k)(5−k)!. Expanding: D₅ = 5! − C(5,1)×4! + C(5,2)×3! − C(5,3)×2! + C(5,4)×1! − C(5,5)×0! = 120 − 5×24 + 10×6 − 10×2 + 5×1 − 1×1.
120 − 120 + 60 − 20 + 5 − 1
4
Step 4 — EvaluateSumming the alternating terms: 120 − 120 = 0; 0 + 60 = 60; 60 − 20 = 40; 40 + 5 = 45; 45 − 1 = 44.
D₅ = 44
5
Step 5 — Verify with the ApproximationThe derangement formula gives Dₙ ≈ n!/e. Here 120/e ≈ 120/2.71828 ≈ 44.146, which rounds to 44, confirming our answer. The probability that no student gets their own paper is 44/120 ≈ 0.3667, consistent with the well-known limiting probability of 1/e ≈ 0.3679.
D₅ = 44 ✓ (≈ 5!/e)

Strengths, Limitations, and Alternatives

Inclusion–exclusion is powerful but not universally efficient. Its computational cost and the availability of alternatives depend heavily on the problem structure. The following comparison highlights when PIE excels and when other techniques may be preferable.

Strengths and limitations of the inclusion–exclusion principle.
AspectStrengthsLimitations
GeneralityWorks for any finite collection of sets with no structural assumptions on how they overlap.Requires computing all 2ⁿ − 1 intersection sizes in the worst case—exponential in the number of sets.
ExactnessYields exact counts (or probabilities), unlike sieve bounds which give only upper or lower estimates.For approximate answers, Bonferroni inequalities or probabilistic sieves may suffice with far fewer terms.
Symmetry ExploitationWhen all k-fold intersections have equal size, the sum reduces to n terms instead of 2ⁿ − 1.Highly asymmetric problems (e.g., varying set sizes) do not simplify, and the formula can be unwieldy.
Closed-Form PotentialOften produces elegant closed forms (derangements, Euler's totient) after simplification.Algebraic simplification is not always possible; results may remain as alternating sums.
AlternativesServes as the foundation for more advanced methods (Möbius inversion, permanent computation).Generating functions, transfer matrices, or Burnside's lemma may be more efficient for specific problem classes.
KEY TAKEAWAY
Inclusion–exclusion is the Swiss Army knife of combinatorial counting: universally applicable but not always the most efficient tool. In practice, look for symmetry (all k-fold intersections equal) to collapse the exponential sum into a polynomial one. When symmetry is absent and n is large, consider whether Bonferroni bounds, generating functions, or Möbius inversion offer a more tractable path to the answer.

Connections to Advanced Theory

The inclusion–exclusion principle is not an isolated formula; it is the simplest instance of a far-reaching inversion technique on partially ordered sets (posets). Recognizing this connection enriches one's understanding of why the alternating-sign pattern arises and opens the door to more powerful counting tools.

Classical inclusion–exclusion as a special case of Möbius inversion.
FeatureClassical PIEMöbius Inversion on Posets
DomainBoolean lattice of subsets of [n] (2ⁿ elements)Any locally finite poset P
Inversion Kernel(−1)^|S| for subsets SMöbius function μ(x, y) of the poset
Key Identityg(S) = Σ_{T⊆S} f(T) ⟹ f(S) = Σ_{T⊆S} (−1)^{|S|−|T|} g(T)g(x) = Σ_{y≤x} f(y) ⟹ f(x) = Σ_{y≤x} μ(y,x) g(y)
Classical AnalogueAdditive number theory: sieve of EratosthenesMultiplicative number theory: Möbius function μ(n) and Σ_{d|n} μ(d)
Computational CostO(2ⁿ) subsets in the worst caseDepends on the poset structure; can be polynomial for lattices like the divisor lattice

Beyond Möbius inversion, inclusion–exclusion connects directly to the theory of Stirling numbers of the second kind (via surjection counting), the permanent of a 0-1 matrix (via the formula for the number of systems of distinct representatives), and the chromatic polynomial of a graph (via proper colorings as functions avoiding monochromatic edges). These advanced applications exploit the same alternating-sign machinery but interpret the sets Aᵢ in richer combinatorial or algebraic contexts. Students continuing in enumerative combinatorics will encounter PIE as a recurring motif underlying many seemingly disparate identities.

Practice Problems

PROBLEM 1CONCEPTUAL
Explain why simply adding |A| + |B| + |C| overcounts the cardinality of A ∪ B ∪ C. In your explanation, describe precisely how many times an element belonging to exactly two of the three sets is counted, and how many times an element in all three sets is counted.
PROBLEM 2BASIC CALCULATION
In a class of 100 students, 40 study French, 35 study Spanish, and 20 study German. Furthermore, 12 study both French and Spanish, 8 study both French and German, 5 study both Spanish and German, and 2 study all three languages. How many students study at least one of the three languages?
PROBLEM 3INTERMEDIATE
How many integers from 1 to 1000 are divisible by 2, 3, or 5? Use inclusion–exclusion to find the count and then determine how many integers in this range are coprime to 30.
PROBLEM 4APPLIED
A company assigns 4 distinct tasks to 4 employees by random permutation. Using inclusion–exclusion, compute the probability that no employee is assigned the task they were assigned last month (i.e., the probability of a derangement). Express your answer as a fraction.
PROBLEM 5CRITICAL THINKING
Derive, using the complementary form of inclusion–exclusion, the number of surjections (onto functions) from a set of 5 elements to a set of 3 elements. Then explain why this count equals 3! × S(5, 3), where S(5, 3) is the Stirling number of the second kind.

Lesson Summary

The inclusion–exclusion principle computes the cardinality of a union of finite sets by forming an alternating sum over all possible intersections: add individual sizes, subtract pairwise intersections, add triple intersections, and continue with sign (−1)k+1 for k-fold intersections. The complementary counting form counts elements satisfying none of a list of properties and underpins applications to derangements, Euler's totient function, and surjection counting.

The principle's correctness rests on the binomial identity C(k,1) − C(k,2) + … ± C(k,k) = 1, which guarantees that every element in the union is counted exactly once. While the general formula involves 2ⁿ − 1 intersection terms, symmetric problems—where all k-fold intersections have equal size—collapse this to a manageable sum. In the broader landscape of combinatorics, inclusion–exclusion is the Boolean-lattice special case of Möbius inversion, connecting it to deep algebraic structures in enumerative combinatorics.

Varsity Tutors • Discrete Math • Inclusion–exclusion principle (intro and applications)