The Principle of Inclusion-Exclusion
How many integers from to are divisible by , , or ? Naively adding the three counts overcounts numbers divisible by more than one of these. So we must correct for the overcounting — and then correct the correction. This simple tension between the Sum and Product Rules and overlapping sets is the seed of one of the most versatile counting tools in combinatorics: the Principle of Inclusion-Exclusion (PIE). PIE matters because most interesting counting problems are not about disjoint collections. Instead they involve unions and complements of overlapping conditions — numbers avoiding certain divisors, functions missing certain outputs, permutations avoiding certain fixed points, arrangements violating certain adjacency constraints. Whenever a problem can be phrased as "count the elements satisfying at least one of several properties," PIE helps. The same holds dually for "count the elements satisfying none." PIE turns that question into a computable alternating sum over intersections. Each of these is often far easier to count than the union directly. The big picture is that a finite set can be built from overlapping subsets. Their interactions form a Boolean lattice. PIE is precisely the Möbius-function calculation on that lattice, made elementary through a counting argument. It generalizes the humble observation that to arbitrarily many sets. As intersections deepen, the signs alternate between and . This article develops PIE rigorously from the two-set and three-set cases up to the full -set alternating-sum theorem. It proves the theorem two independent ways, and works through asymmetric numeric examples. It also sets up the machinery — counting elements satisfying none of several properties. This machinery powers the classical theory of Derangements, the counting of surjective functions, and enumeration of restricted Integer Partitions.
- PIE computes as an alternating sum of sizes of all possible intersections of the sets.
- The two-set case is the seed; three-set and -set versions add correction terms recursively.
- The complementary form counts elements satisfying none of properties, which is the engine behind derangements and surjection counts.
- The proof works by tracking, for each element, how many times it is counted at each stage — this reduces to a binomial identity.
- PIE is a special case of Möbius inversion on the subset lattice, though it can be proved without that machinery.
- Key idea: overcounting from overlaps is corrected by alternating terms indexed by the sizes of all subset intersections.
Where this lives today
Inclusion-exclusion is a working tool across mathematics and its applications. In enumerative combinatorics it underlies the count of Derangements (permutations with no fixed point), the formula for Counting Surjections used above, and the enumeration of integer solutions to equations under upper-bound constraints — a technique feeding directly into Integer Partitions with bounded part sizes. In elementary number theory, PIE proves the formula for Euler's totient function by excluding multiples of each prime factor of , and computes counts of integers free of specified divisors, exactly as in the worked example above. In probability theory, the same alternating sum (applied to probabilities instead of cardinalities) gives Bonferroni's inequalities, used to bound event probabilities when full inclusion-exclusion is computationally infeasible. In graph theory and algorithms, PIE underlies the chromatic polynomial computation via the deletion–contraction-adjacent "broken circuit" methods and dynamic-programming algorithms (such as the Held–Karp-style inclusion-exclusion algorithms for Hamiltonian cycles and graph coloring) achieving running time. In cryptography and coding theory, sieve methods analogous to PIE estimate collision probabilities and counts of "bad" keys avoiding structural weaknesses. In statistical mechanics and combinatorial physics, inclusion-exclusion sums appear in cluster expansions and in computing partition-function corrections for interacting systems on lattices.
Ask the tutor: How does inclusion-exclusion combine with the Product Rule to count permutations avoiding several forbidden positions at once, as in the derangement problem?
Let be a finite set and let be finite sets (subsets of a common finite universe, or simply finite sets in their own right). For a nonempty index set , write
The Principle of Inclusion-Exclusion states:
Equivalently, if is a finite universe containing all , the complementary form counts elements in none of the sets:
Intuition
Picture three overlapping disks representing . If you simply add , every point lying in exactly two disks gets counted twice, and every point lying in all three gets counted three times. Subtracting all pairwise intersections fixes the doubly-counted points, but it removes the triply-counted point three times (once per pair), on top of having added it three times originally — net effect zero, when it should count once. Adding back restores exactly one count for that region. This alternating add/subtract/add pattern is the whole idea; the theorem below shows it persists for any number of sets, and the proof shows why the arithmetic always lands on exactly for every element of the union.
Theorem statement and proof
Let be finite sets (contained in some common finite universe , or considered abstractly with all pairwise and multi-wise intersections finite). Then
For every integer ,
This is the binomial theorem applied to : by the Binomial Theorem (an immediate consequence of the Combinations counting rule for choosing objects from ),
∎
Technique: double counting — we show both sides count each element of exactly once, by comparing, for each fixed element , its contribution to the left side and to the right side.
Left side. Fix . The left side counts exactly once, since it is a plain set cardinality and belongs to the union.
Right side. Let
be the (nonempty, since is in the union) set of indices for which , and let . We compute how many times is counted in the right-hand alternating sum. For a given with , and a given -subset of , the element contributes to exactly when belongs to every one of , i.e. exactly when . Since , the number of -subsets with equals the number of -subsets of , which is by the definition of Combinations (and automatically when , since has no -subsets). Therefore, 's total signed contribution to the right-hand side is
Because , terms with vanish, so by the Lemma (using ). Also . Substituting,
So every element of contributes exactly to the right-hand side, matching its contribution of to the left-hand side. Elements outside the union lie in no , hence in no intersection for nonempty either, so they contribute to both sides. Since both sides are sums of contributions over all elements of that agree elementwise, the two sides are equal as cardinalities.
∎Let be a finite universe and . Then the number of elements of lying in none of the is
By the Complement Rule, . Substitute the theorem's formula for and note is exactly the missing term; the rest of the sum is , which combines with the term to give precisely .
∎Worked examples
Example 1 (asymmetric divisibility count). How many integers in are divisible by , by , or by ? Let , and let be the multiples of respectively within . For any , the number of multiples of in is . Singles:
Pairwise intersections use least common multiples: , , .
Triple intersection: , so
By the theorem,
So exactly integers from to are divisible by , , or . (Sanity check: since and all pairwise lcms also equal , actually exactly — every number divisible by any two of is automatically divisible by and hence by all three. This is a genuine, slightly surprising numerical coincidence of this example, not a hidden symmetry of the method: it happens because , , pairwise "cover" all three primes .)
Example 2 (counting onto functions — foreshadowing surjections). How many functions are surjective (i.e., hit every element of the codomain)? Let be the set of all functions from a -element domain to a -element codomain, so by the Product Rule. For , let be the set of functions that miss the value (i.e. is not in the image). A function is surjective exactly when it lies in none of — so the complementary form of PIE applies directly. A function missing a specific set of values is the same as a function from a -element set into the remaining values, and there are such functions by the Product Rule; this quantity depends only on , so
Applying the corollary with properties (missing , missing , missing ):
Compute term by term:
- : .
- : .
- : .
- : .
Sum: . So there are exactly surjective functions from a -element set onto a -element set. (This matches the general surjection-count formula that PIE produces for Counting Surjections in general: surjections from an -set onto an -set — here , .)
Drop the hypothesis that are finite and the theorem collapses, because the individual cardinalities , may be infinite (or, for infinite cardinal arithmetic, may fail to combine via ordinary subtraction at all). Concretely, let , and let
Both are infinite, and is infinite, while is finite with . The formula
would require assigning a numeric value to and subtracting from it to recover , but and are both the cardinal , and "" is not well-defined as ordinary subtraction (cardinal arithmetic has , and there is no unique inverse operation). The proof above crucially used the finiteness of (needed to invoke the binomial lemma with a well-defined finite ) and used ordinary integer subtraction throughout, both of which require finite cardinalities. Without finiteness the entire double-counting argument, and the algebra it rests on, breaks down.
Scope, edge cases & common traps
| Situation | What happens |
|---|---|
| sets | Reduces to , the classical two-set case. |
| Some pairwise disjoint | All higher intersections vanish; PIE reduces to the plain Sum and Product Rules additive count. |
| All equal | Formula still holds but is wasteful; direct reasoning is simpler. |
| Infinite index set | Theorem as stated does not apply; requires measure-theoretic or probabilistic analogues (Bonferroni-type inequalities), beyond this scope. |
| Complementary form | Requires an ambient finite universe containing all ; without it "none of the properties" is undefined. |
The mathematician behind it
Origins · 1 minAbraham de Moivre and how this was discovered
De Moivre, a French Huguenot mathematician who emigrated to England after the revocation of the Edict of Nantes, developed early inclusion-exclusion-type arguments in his work on probability, particularly in The Doctrine of Chances (1718), where he analyzed the probability that at least one of several events occurs. The systematic algebraic formulation of the principle for arbitrarily many sets — with the modern alternating-sum notation — was consolidated over the following two centuries, with contributions from Daniel da Silva, J. J. Sylvester, and Henri Poincaré, and is sometimes called Da Silva's formula or the Sylvester–Poincaré formula in older European texts.
Is inclusion-exclusion only for two or three sets, or does it really work for any number ?
It works for any finite . The two- and three-set cases are just small instances of the same general alternating-sum formula proved above. For general , the formula sums over all nonempty subsets of indices, with the sign of each term determined solely by the size of that subset: odd-sized index sets get a sign, even-sized get a sign. The proof by double counting (tracking how many times a fixed element is counted) works uniformly in , using the binomial identity , which holds for every regardless of how large is.
How is this related to the Möbius function or Möbius inversion?
Inclusion-exclusion is exactly Möbius inversion on the lattice of subsets of , ordered by inclusion. The Möbius function of this lattice is for , and the general Möbius inversion formula, when specialized to this lattice and to the function , produces precisely the alternating-sum formula proved here. The combinatorial proof given in this article, however, is self-contained and does not require developing the general theory of Möbius functions on posets — it only needs elementary counting and one binomial identity.
Can PIE be used to compute probabilities instead of counts?
Yes. If are events in a finite (or more generally discrete) probability space, dividing every term in the theorem by the size of the sample space (or working directly with probability measure instead of counting measure) gives
This is the standard probabilistic form of inclusion-exclusion, and truncating the alternating sum after terms gives the Bonferroni inequalities, useful upper and lower bounds when computing every intersection probability is impractical.
Why does the complementary "none of the properties" form include an term equal to ?
The empty intersection over zero sets is conventionally defined to be the entire universe (the intersection of no constraints excludes nothing), so by convention. This convention makes the corollary's formula uniform: summing over all subsets , including the empty one, automatically starts from and then subtracts, adds back, and so on — exactly matching once you expand the union formula from the main theorem and combine terms.
What is the connection between PIE and counting derangements?
A derangement of is a permutation with no fixed point. Let be the set of all permutations, and let be the set of permutations fixing position (so , and more generally for fixed positions). Derangements are exactly the permutations in none of the , so the complementary form of PIE gives the derangement count , the classical formula developed fully under Derangements.
Does the order in which the sets are listed affect the formula?
No. The formula sums over all subsets of the index set , and set intersection is commutative and associative, so does not depend on any ordering of the elements of . Relabeling or reordering the sets permutes the terms of the sum but does not change its value, since addition is also commutative. The formula is intrinsically defined in terms of the sets themselves, not any sequence in which they are presented.
Is there a version of PIE for more than two "overlapping conditions" phrased without sets, e.g. directly with logical predicates?
Yes — PIE is often stated for a finite set together with properties that elements of may or may not satisfy. Letting recovers the set formulation exactly, so the two phrasings are identical in content. This predicate phrasing is often more natural when properties are combinatorial conditions (e.g., "the permutation fixes position ," or "the assignment violates constraint ") rather than naturally-occurring subsets, and it is the phrasing most commonly used when applying PIE to derangements, surjections, and constraint-satisfaction counting problems.