Applied Combinatorics III: Inclusion–Exclusion and the Number of SurjectionsTextbook
Motivation
Counting the objects that avoid every one of a list of conditions is one of the most frequent tasks in enumerative combinatorics: functions that miss no value, permutations that fix no point, integers that share no prime factor with a given number. The Principle of Inclusion-Exclusion converts such a count into an alternating sum of counts of objects that do satisfy prescribed conditions, which are usually far easier to compute. Chapter 7 of Keller and Trotter's Applied Combinatorics (2017 Edition) develops the principle in the abstract language of properties and applies it to three classical questions: the number of surjections, the number of derangements, and Euler's totient function.
This mission formalizes the first two applications, with the chapter's headline result, the closed formula for the number of surjections from an -element set onto an -element set, as its goal. The surjection count answers distribution questions of the form "in how many ways can distinct objects be handed to distinct recipients so that every recipient receives at least one", which the book illustrates with fifteen lottery tickets shared among four grandchildren ().
Setting
Let be a finite set and let be a family of properties: for each and each , either satisfies or it does not. For a subset write
so that .
Two families of properties are used.
- Functions. is the set of all functions , and satisfies when is not in the range of , i.e. for every . The functions satisfying none of the properties are the surjections, and their number is written .
- Permutations. is the set of all permutations of , and satisfies when . The permutations satisfying none of the properties are the derangements, and their number is written .
In Lean, is a type with [Fintype X], the properties are a decidable predicate family P : Fin m → X → Prop, and is AppliedComb.InclExcl.N P S. The two families are AppliedComb.InclExcl.NotInRange n m on Fin n → Fin m and AppliedComb.InclExcl.FixesPoint n on Equiv.Perm (Fin n).
Formalization targets
Goal: Theorem 7.9 (number of surjections)
For all ,
Milestones
- Theorem 7.7 (Principle of Inclusion-Exclusion). For every finite and every family of properties,
- Lemma 7.8. For the function properties, depends only on , and when .
- Lemma 7.10. For the permutation properties, depends only on , and when .
- Theorem 7.11 (number of derangements). For all ,
Milestones 1 and 2 are the two ingredients the book names for the goal; milestones 3 and 4 are the parallel derangement application, which shares the definition of and milestone 1.
Significance
The result itself. The surjection formula is the standard closed form for , where is the Stirling number of the second kind; it gives for and as special cases, and it underlies counts of ordered set partitions, of onto colourings, and of occupancy problems in which every cell is filled. The derangement formula is the standard answer to the hat-check problem and leads directly to the limit (Theorem 7.12 of the book). The Principle of Inclusion-Exclusion in the book's "none of the properties" form is the common tool behind both and behind Möbius inversion in general.
Formalizing it. All results are classical and fully proved in the book. Mathlib contains inclusion-exclusion for unions and intersections of finsets (Finset.inclusion_exclusion_card_biUnion, Finset.inclusion_exclusion_card_inf_compl), the derangement count via its own recurrence (numDerangements, card_derangements_eq_numDerangements, numDerangements_sum), and Stirling numbers of the second kind (Nat.stirlingSecond) with their recurrence, but no closed formula for the number of surjections. The platform has union-form inclusion-exclusion (FamousTheorems.inclusion_exclusion_card_biUnion) and the limit, neither in the property form used here. This mission produces the property-indexed statement of the principle, the two computations, and the surjection count as a cardinality of surjective functions, in a form that downstream missions of the series (generating functions, Pólya counting) can cite.
Difficulty
The proofs are short on paper, and the work lies in the bookkeeping that the page leaves implicit. Two points stand out. First, the Principle of Inclusion-Exclusion is phrased with properties and rather than with a union of sets; connecting the two forms, or proving the property form directly, requires handling sums over the power set of together with the sign in . Second, the step "the result follows immediately, as there are -element subsets of " requires regrouping a sum over all subsets by cardinality, which needs Lemma 7.8's statement that depends only on and a count of the subsets of each size. The book's one-line arguments for Lemmas 7.8 and 7.10 ("a string of length from an alphabet of letters", "a permutation among the remaining positions") are counting identifications that have to be made precise for subtypes of Fin n → Fin m and of Equiv.Perm (Fin n).
Formalization scope
- The book's is represented by
Fin n; this is an index shift only. - is
Fintype.card {f : Fin n → Fin m // Function.Surjective f}and isFintype.card {σ : Equiv.Perm (Fin n) // ∀ i, σ i ≠ i}. Defining by the formula, or by the sum, would make the goal hold byrfl; the counts are therefore cardinalities of the sets the book describes. is also not stated throughNat.stirlingSecond, which counts unordered partitions and differs by a factor . - All alternating sums are identities in ; the natural-number counts are cast. The differences and are computed in where ranges over (resp. ) or equals , so they are exact.
- The book introduces for positive and states Theorem 7.11 for positive . The formal statements are made for all , where they remain true with Lean's convention (, for , ). No explicit constants arise: the chapter contains no or approximate statements among the formalized results.
- The Principle of Inclusion-Exclusion is stated for an arbitrary finite type and an arbitrary decidable family of properties; for both sides equal .
A complete development needs only Mathlib's finite-set, power-set and binomial-coefficient API. The property-form inclusion-exclusion (Theorem 7.7) and the two lemmas are reusable for other sieve-type counts, for example Euler's totient function (Theorem 7.14 of the book), which is not part of this mission. Proofs of any milestone independently of the others are welcome, as are alternative proofs of the goal (for instance via exponential generating functions) that do not pass through Theorem 7.7.
Selected references
- Mitchel T. Keller and William T. Trotter, Applied Combinatorics, 2017 Edition, Chapter 7 (Theorems 7.7, 7.9, 7.11; Lemmas 7.8, 7.10), CC BY-SA 4.0. https://www.appliedcombinatorics.org/
- Richard P. Stanley, Enumerative Combinatorics, Volume 1, 2nd ed., Cambridge University Press, 2011, Chapter 2 (sieve methods). https://doi.org/10.1017/CBO9781139058520
- The Mathlib Community, The Lean Mathematical Library, CPP 2020. https://doi.org/10.1145/3372885.3373824