taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Volume P1 · Proof, induction and invariants

Sets, functions and finite counting

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Read first
Contents
  1. Sets and their operations
  2. Functions
  3. Finite sets and counting

Sets and their operations

Statement

A set is a collection of objects that is determined by its members. This section fixes the notation, defines the operations on sets, and gives the method for a proof that two sets are equal.

Definition P1.2.1 (set, member). A set is determined by the objects that belong to it, its elements or members. Write x∈Ax \in A when xx is an element of AA, and x∉Ax \notin A when it is not. Two sets are equal when they have the same elements: A=BA = B means ∀x (x∈A↔x∈B)\forall x\ (x \in A \leftrightarrow x \in B). A set is written by a list, {1,2,3}\{1, 2, 3\}, or by a property, {x∈D:P(x)}\{x \in D : P(x)\}. The order of a list and the repeats in it do not matter: {1,2,2,3}={3,1,2}\{1, 2, 2, 3\} = \{3, 1, 2\}.

Lean: the type Set α, with membership x ∈ A, and the lemma Set.ext for equality of two sets (Mathlib).

Example P1.2.2. The set {x∈ℤ:x2<20}\{x \in \mathbb{Z} : x^2 < 20\} has the elements −4,−3,…,4-4, -3, \dots, 4, nine of them. The set {n∈ℕ:n is a prime and n<30}\{n \in \mathbb{N} : n \text{ is a prime and } n < 30\} is {2,3,5,7,11,13,17,19,23,29}\{2, 3, 5, 7, 11, 13, 17, 19, 23, 29\} and has ten elements.

Example P1.2.3. The sets {1,2}\{1, 2\} and {{1},2}\{\{1\}, 2\} are different: the first has the elements 11 and 22, and the second has the elements {1}\{1\} and 22. An element of a set may be a set. The set {∅}\{\emptyset\} has one element, and the set ∅\emptyset has none.

Definition P1.2.4 (subset, empty set). The set AA is a subset of BB, written A⊆BA \subseteq B, when every element of AA is an element of BB: ∀x (x∈A→x∈B)\forall x\ (x \in A \to x \in B). The empty set ∅\emptyset has no element. The set AA is a proper subset of BB when A⊆BA \subseteq B and A≠BA \ne B.

Lean: A ⊆ B and ∅ for Set α; Set.Subset.antisymm proves A=BA = B from the two inclusions, and Set.empty_subset proves ∅⊆A\emptyset \subseteq A (Mathlib).

Example P1.2.5. {2,4}⊆{1,2,3,4}\{2, 4\} \subseteq \{1, 2, 3, 4\}, and {2,4}⊈{1,2,3}\{2, 4\} \not\subseteq \{1, 2, 3\}. Also {1,2,3,4}⊆{1,2,3,4}\{1, 2, 3, 4\} \subseteq \{1, 2, 3, 4\}: each set is a subset of itself.

Example P1.2.6. The empty set is a subset of every set AA. The statement ∀x (x∈∅→x∈A)\forall x\ (x \in \emptyset \to x \in A) is true, since the hypothesis is false for every xx (Chapter 1, Section 1). The sets ∅\emptyset and {∅}\{\emptyset\} differ: ∅⊆{∅}\emptyset \subseteq \{\emptyset\} is true, ∅∈{∅}\emptyset \in \{\emptyset\} is true, and {∅}⊆∅\{\emptyset\} \subseteq \emptyset is false.

Theorem P1.2.7 (equality by two inclusions). For sets AA and BB, A=BA = B if and only if A⊆BA \subseteq B and B⊆AB \subseteq A.

Lean: Set.Subset.antisymm and Set.ext_iff (Mathlib).

Proof. If A=BA = B, then each element of AA is an element of BB and each element of BB is an element of AA. Conversely, let A⊆BA \subseteq B and B⊆AB \subseteq A. Take any xx. If x∈Ax \in A, then x∈Bx \in B. If x∈Bx \in B, then x∈Ax \in A. So x∈A↔x∈Bx \in A \leftrightarrow x \in B for every xx. □\square

Example P1.2.8. To show {x∈ℤ:x2=4}={−2,2}\{x \in \mathbb{Z} : x^2 = 4\} = \{-2, 2\}, take an xx with x2=4x^2 = 4. Then (x−2)(x+2)=0(x - 2)(x + 2) = 0, so x=2x = 2 or x=−2x = -2. This proves the inclusion ⊆\subseteq. For the other, (−2)2=4(-2)^2 = 4 and 22=42^2 = 4.

Example P1.2.9. The theorem is the usual way to prove an equation of sets. For A={n:6∣n}A = \{n : 6 \mid n\} and B={n:2∣n∧3∣n}B = \{n : 2 \mid n \wedge 3 \mid n\} over the integers, A⊆BA \subseteq B since 6∣n6 \mid n gives n=6k=2(3k)=3(2k)n = 6k = 2(3k) = 3(2k). For B⊆AB \subseteq A let n=2a=3bn = 2a = 3b. Then n=3n−2n=6b−6a=6(b−a)n = 3n - 2n = 6b - 6a = 6(b - a), so 6∣n6 \mid n.

Definition P1.2.10 (operations). For sets AA and BB inside a universe UU:

  • the union A∪B={x:x∈A∨x∈B}A \cup B = \{x : x \in A \vee x \in B\};
  • the intersection A∩B={x:x∈A∧x∈B}A \cap B = \{x : x \in A \wedge x \in B\};
  • the difference A∖B={x:x∈A∧x∉B}A \setminus B = \{x : x \in A \wedge x \notin B\};
  • the complement A‾=U∖A\overline{A} = U \setminus A;
  • the power set 𝒫(A)={S:S⊆A}\mathcal{P}(A) = \{S : S \subseteq A\}.

Two sets with A∩B=∅A \cap B = \emptyset are disjoint.

Lean: A ∪ B, A ∩ B, A \ B, Aᶜ and Set.powerset, with Set.mem_union, Set.mem_inter_iff, Set.mem_diff and Set.mem_powerset_iff (Mathlib).

Example P1.2.11. Let A={1,2,3,4,5,6}A = \{1, 2, 3, 4, 5, 6\} and B={4,5,6,7,8}B = \{4, 5, 6, 7, 8\}. Then A∪B={1,…,8}A \cup B = \{1, \dots, 8\}, A∩B={4,5,6}A \cap B = \{4, 5, 6\}, A∖B={1,2,3}A \setminus B = \{1, 2, 3\} and B∖A={7,8}B \setminus A = \{7, 8\}. With U={1,…,8}U = \{1, \dots, 8\} the complement of AA is {7,8}\{7, 8\}.

Example P1.2.12. 𝒫({a,b,c})\mathcal{P}(\{a, b, c\}) has eight elements: ∅\emptyset, {a}\{a\}, {b}\{b\}, {c}\{c\}, {a,b}\{a, b\}, {a,c}\{a, c\}, {b,c}\{b, c\} and {a,b,c}\{a, b, c\}. The set 𝒫(∅)\mathcal{P}(\emptyset) is {∅}\{\emptyset\} and has one element.

Theorem P1.2.13 (laws of sets). For sets AA, BB and CC in a universe UU:

  1. A∪B=B∪AA \cup B = B \cup A and A∩B=B∩AA \cap B = B \cap A;
  2. A∩(B∪C)=(A∩B)∪(A∩C)A \cap (B \cup C) = (A \cap B) \cup (A \cap C) and A∪(B∩C)=(A∪B)∩(A∪C)A \cup (B \cap C) = (A \cup B) \cap (A \cup C);
  3. A∪B‾=A‾∩B‾\overline{A \cup B} = \overline{A} \cap \overline{B} and A∩B‾=A‾∪B‾\overline{A \cap B} = \overline{A} \cup \overline{B};
  4. A∖(B∪C)=(A∖B)∩(A∖C)A \setminus (B \cup C) = (A \setminus B) \cap (A \setminus C);
  5. 𝒫(A)∩𝒫(B)=𝒫(A∩B)\mathcal{P}(A) \cap \mathcal{P}(B) = \mathcal{P}(A \cap B).

Lean: Set.union_comm, Set.inter_comm, Set.inter_union_distrib_left, Set.union_inter_distrib_left, Set.compl_union, Set.compl_inter (Mathlib).

Proof. The method turns a statement about sets into a statement about an element xx, and then uses the laws of logic of Chapter 1. Rule 2, first part: x∈A∩(B∪C)x \in A \cap (B \cup C) means x∈A∧(x∈B∨x∈C)x \in A \wedge (x \in B \vee x \in C). The first distributive law of logic gives (x∈A∧x∈B)∨(x∈A∧x∈C)(x \in A \wedge x \in B) \vee (x \in A \wedge x \in C), which means x∈(A∩B)∪(A∩C)x \in (A \cap B) \cup (A \cap C). Each step is an equivalence, so the two sets have the same elements. Rule 3, first part: x∈A∪B‾x \in \overline{A \cup B} means ¬(x∈A∨x∈B)\neg (x \in A \vee x \in B), which is x∉A∧x∉Bx \notin A \wedge x \notin B by the second law of De Morgan, and this means x∈A‾∩B‾x \in \overline{A} \cap \overline{B}. Rule 4: x∈A∖(B∪C)x \in A \setminus (B \cup C) means x∈A∧¬(x∈B∨x∈C)x \in A \wedge \neg (x \in B \vee x \in C), which is x∈A∧x∉B∧x∉Cx \in A \wedge x \notin B \wedge x \notin C. This is the same as (x∈A∧x∉B)∧(x∈A∧x∉C)(x \in A \wedge x \notin B) \wedge (x \in A \wedge x \notin C), since x∈Ax \in A may be said twice. Rule 5: S∈𝒫(A)∩𝒫(B)S \in \mathcal{P}(A) \cap \mathcal{P}(B) means S⊆AS \subseteq A and S⊆BS \subseteq B. This says that every element of SS is in AA and in BB, so S⊆A∩BS \subseteq A \cap B. The rest of rule 1 and rule 2 are the same, with the commutative and the second distributive law. □\square

Example P1.2.14. For A={1,…,6}A = \{1, \dots, 6\}, B={4,5,6,7,8}B = \{4, 5, 6, 7, 8\} and C={2,4,6,8}C = \{2, 4, 6, 8\} the set A∩(B∪C)A \cap (B \cup C) is A∩{2,4,5,6,7,8}={2,4,5,6}A \cap \{2, 4, 5, 6, 7, 8\} = \{2, 4, 5, 6\}. The set (A∩B)∪(A∩C)={4,5,6}∪{2,4,6}={2,4,5,6}(A \cap B) \cup (A \cap C) = \{4, 5, 6\} \cup \{2, 4, 6\} = \{2, 4, 5, 6\}. The two agree.

Example P1.2.15. For AA and BB above, A∖(B∪C)={1,2,3,4,5,6}∖{2,4,5,6,7,8}={1,3}A \setminus (B \cup C) = \{1, 2, 3, 4, 5, 6\} \setminus \{2, 4, 5, 6, 7, 8\} = \{1, 3\}, and (A∖B)∩(A∖C)={1,2,3}∩{1,3,5}={1,3}(A \setminus B) \cap (A \setminus C) = \{1, 2, 3\} \cap \{1, 3, 5\} = \{1, 3\}.

Definition P1.2.16 (ordered pair, product). The ordered pair (a,b)(a, b) has the property (a,b)=(c,d)(a, b) = (c, d) if and only if a=ca = c and b=db = d. The product of two sets is A×B={(a,b):a∈A,b∈B}A \times B = \{(a, b) : a \in A, b \in B\}. The product of AA with itself nn times is AnA^n, the set of lists (a1,…,an)(a_1, \dots, a_n).

Lean: Prod, written α × β; for sets Set.prod and Set.mem_prod (Mathlib).

Example P1.2.17. {1,2}×{x,y,z}\{1, 2\} \times \{x, y, z\} has six elements: (1,x),(1,y),(1,z),(2,x),(2,y),(2,z)(1, x), (1, y), (1, z), (2, x), (2, y), (2, z). The product {1,2}×∅\{1, 2\} \times \emptyset is empty.

Example P1.2.18. {0,1}3\{0, 1\}^3 has eight elements, the bit strings 000,001,…,111000, 001, \dots, 111 of length 33. The plane ℝ2\mathbb{R}^2 is the set of pairs of real numbers, and the pairs are ordered: (1,2)≠(2,1)(1, 2) \ne (2, 1).

Proof

All proofs of the section have the same pattern. To prove A=BA = B, the proof takes an arbitrary element and shows two implications, or it uses a chain of equivalences. To prove A⊆BA \subseteq B, the proof starts with "let x∈Ax \in A" and ends with "so x∈Bx \in B". The pattern is called a proof by elements. The laws of logic of Chapter 1 do the work in each step, and a set law is a law of logic with the membership sign added.

A proof by elements is also the way to find that a claimed law is false. The claim A∪(B∖C)=(A∪B)∖CA \cup (B \setminus C) = (A \cup B) \setminus C is false: with A=C={1}A = C = \{1\} and B=∅B = \emptyset the left side is {1}\{1\} and the right side is ∅\emptyset. The element 11 is in the left set, since 1∈A1 \in A, and it is not in the right set, since 1∈C1 \in C.

Examples

Example P1.2.19 (the symmetric difference). Let A△B=(A∖B)∪(B∖A)A \triangle B = (A \setminus B) \cup (B \setminus A). For A={1,…,6}A = \{1, \dots, 6\} and B={4,…,8}B = \{4, \dots, 8\} the set is {1,2,3}∪{7,8}={1,2,3,7,8}\{1, 2, 3\} \cup \{7, 8\} = \{1, 2, 3, 7, 8\}. An element is in A△BA \triangle B exactly when it is in one of the two sets and not in both, which is the exclusive or of Chapter 1. So A△A=∅A \triangle A = \emptyset and A△∅=AA \triangle \emptyset = A.

Example P1.2.20 (a product that is not a union). The sets {1}×{1,2}\{1\} \times \{1, 2\} and {2}×{1,2}\{2\} \times \{1, 2\} have union {1,2}×{1,2}\{1, 2\} \times \{1, 2\}, which has four elements. The union of {1,2}×{1}\{1, 2\} \times \{1\} and {1}×{1,2}\{1\} \times \{1, 2\} is {(1,1),(2,1),(1,2)}\{(1, 1), (2, 1), (1, 2)\} with three elements, and it is not a product of two sets, since a product with the elements (2,1)(2, 1) and (1,2)(1, 2) also has (2,2)(2, 2).

Example P1.2.21 (sets of divisors). Let D(n)={d∈ℕ:d≥1, d∣n}D(n) = \{d \in \mathbb{N} : d \ge 1,\ d \mid n\}. Then D(12)={1,2,3,4,6,12}D(12) = \{1, 2, 3, 4, 6, 12\} and D(18)={1,2,3,6,9,18}D(18) = \{1, 2, 3, 6, 9, 18\}. The intersection D(12)∩D(18)={1,2,3,6}D(12) \cap D(18) = \{1, 2, 3, 6\} is the set of common divisors, and its greatest element is 6=gcd⁡(12,18)6 = \gcd(12, 18). Chapter 5 shows that D(m)∩D(n)=D(gcd⁡(m,n))D(m) \cap D(n) = D(\gcd(m, n)) and uses it for Algorithm 1.1E.

Exercises

  1. [10] List the elements of {x∈ℤ:∣x∣≤2}\{x \in \mathbb{Z} : |x| \le 2\}, of 𝒫({1,2})\mathcal{P}(\{1, 2\}) and of {1,2}×{a,b}\{1, 2\} \times \{a, b\}.
  2. [12] Decide which statements are true: (a) ∅∈{∅}\emptyset \in \{\emptyset\}; (b) ∅⊆{∅}\emptyset \subseteq \{\emptyset\}; (c) {1}∈{1,2}\{1\} \in \{1, 2\}; (d) {1}⊆{1,2}\{1\} \subseteq \{1, 2\}; (e) {1,2}∈𝒫({1,2})\{1, 2\} \in \mathcal{P}(\{1, 2\}).
  3. [15] Prove by elements that A∩B‾=A‾∪B‾\overline{A \cap B} = \overline{A} \cup \overline{B}.
  4. [15] Prove by elements that A∪(B∩C)=(A∪B)∩(A∪C)A \cup (B \cap C) = (A \cup B) \cap (A \cup C).
  5. [M18] Show that the three statements A⊆BA \subseteq B, A∪B=BA \cup B = B and A∩B=AA \cap B = A are equivalent.
  6. [M20] Show that 𝒫(A)∪𝒫(B)⊆𝒫(A∪B)\mathcal{P}(A) \cup \mathcal{P}(B) \subseteq \mathcal{P}(A \cup B), and give sets AA and BB for which the inclusion is proper.
  7. [M22] Show that A△B=∅A \triangle B = \emptyset if and only if A=BA = B, and that A△(A△B)=BA \triangle (A \triangle B) = B.
  8. [M24] Show that (A×B)∩(C×D)=(A∩C)×(B∩D)(A \times B) \cap (C \times D) = (A \cap C) \times (B \cap D), and find sets for which (A×B)∪(C×D)≠(A∪C)×(B∪D)(A \times B) \cup (C \times D) \ne (A \cup C) \times (B \cup D).
  9. [M25] Show that there is no set RR with R={x:x∉x}R = \{x : x \notin x\}, by asking whether R∈RR \in R.

Lean

A set of elements of type α is a term of Set α. A proof of A = B starts with ext x, which turns the goal into x ∈ A ↔ x ∈ B, the proof by elements. The lemmas Set.mem_union, Set.mem_inter_iff and Set.mem_diff rewrite membership, and simp or tauto close the logical part. For example, example (A B C : Set ℕ) : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := by ext x; simp only [Set.mem_inter_iff, Set.mem_union]; tauto is a proof of the first distributive law. A finite set of natural numbers is a Finset ℕ, and decide checks an equation between two concrete finite sets.

Notes and history

Cantor created the theory of sets in the 1870s and 1880s. Russell found the paradox of Exercise 9 in 1901, and the axioms of Zermelo, with the repairs of Fraenkel, give a theory in which the collection RR is not a set. A reader of this volume needs only the rule that a set is given by a property on a set that is already known, as in {x∈D:P(x)}\{x \in D : P(x)\}, and that rule is safe.

TAOCP uses sets in all its chapters, and Section 1.2.3 writes the sum over a set of indices as a sum over the elements of a finite set. The notation {x∈D:P(x)}\{x \in D : P(x)\} of this section is the form that P2 uses for the range of an index.

Functions

Statement

A function sends each element of one set to one element of another set. This section defines functions, their composition and the three properties of injective, surjective and bijective, and proves that a bijection is a function that has an inverse.

Definition P1.2.22 (function). Let AA and BB be sets. A function ff from AA to BB, written f:A→Bf : A \to B, assigns to each a∈Aa \in A exactly one element f(a)∈Bf(a) \in B. The set AA is the domain and BB is the codomain. Two functions f,g:A→Bf, g : A \to B are equal when f(a)=g(a)f(a) = g(a) for all a∈Aa \in A. A function is a set of pairs: its graph is {(a,f(a)):a∈A}⊆A×B\{(a, f(a)) : a \in A\} \subseteq A \times B.

Lean: a function is a term of the type α → β, and funext proves the equality of two functions (Lean core).

Example P1.2.23. The rule n↦n mod 5n \mapsto n \bmod 5 is a function from {0,…,19}\{0, \dots, 19\} to {0,1,2,3,4}\{0, 1, 2, 3, 4\}. The rule n↦⌊n/3⌋n \mapsto \lfloor n / 3 \rfloor is a function from {0,…,9}\{0, \dots, 9\} to {0,1,2,3}\{0, 1, 2, 3\}, with the values 0,0,0,1,1,1,2,2,2,30, 0, 0, 1, 1, 1, 2, 2, 2, 3 for n=0,…,9n = 0, \dots, 9.

Example P1.2.24. The rule "x↦x \mapsto a number whose square is xx" is not a function from ℝ\mathbb{R} to ℝ\mathbb{R}: x=4x = 4 has two values and x=−1x = -1 has none. The rule "x↦xx \mapsto \sqrt{x} for x≥0x \ge 0", with x\sqrt{x} the nonnegative root, is a function from {x∈ℝ:x≥0}\{x \in \mathbb{R} : x \ge 0\} to ℝ\mathbb{R}.

Definition P1.2.25 (composition, identity). For f:A→Bf : A \to B and g:B→Cg : B \to C the composition g∘f:A→Cg \circ f : A \to C is (g∘f)(a)=g(f(a))(g \circ f)(a) = g(f(a)). The identity on AA is idA:A→A\mathrm{id}_A : A \to A, idA(a)=a\mathrm{id}_A(a) = a.

Lean: Function.comp, written g ∘ f, with Function.comp_apply and id (Lean core).

Example P1.2.26. Let f(n)=n+1f(n) = n + 1 and g(n)=n2g(n) = n^2 on ℕ\mathbb{N}. Then (g∘f)(n)=(n+1)2(g \circ f)(n) = (n + 1)^2 and (f∘g)(n)=n2+1(f \circ g)(n) = n^2 + 1. For n=0,…,4n = 0, \dots, 4 the first is 1,4,9,16,251, 4, 9, 16, 25 and the second is 1,2,5,10,171, 2, 5, 10, 17. So composition is not commutative.

Example P1.2.27. Composition is associative: h∘(g∘f)=(h∘g)∘fh \circ (g \circ f) = (h \circ g) \circ f, since both send aa to h(g(f(a)))h(g(f(a))). And f∘idA=f=idB∘ff \circ \mathrm{id}_A = f = \mathrm{id}_B \circ f.

Definition P1.2.28 (injective, surjective, bijective). A function f:A→Bf : A \to B is injective when f(a)=f(a′)f(a) = f(a') implies a=a′a = a'. It is surjective when each b∈Bb \in B has some a∈Aa \in A with f(a)=bf(a) = b. It is bijective when it is both.

Lean: Function.Injective, Function.Surjective and Function.Bijective (Mathlib).

Example P1.2.29. The function n↦2n+1n \mapsto 2n + 1 from ℕ\mathbb{N} to ℕ\mathbb{N} is injective, since 2n+1=2n′+12n + 1 = 2n' + 1 gives n=n′n = n'. It is not surjective: no nn gives the value 00. The function x↦x2x \mapsto x^2 on ℤ\mathbb{Z} is not injective, since f(−3)=f(3)f(-3) = f(3), and it is not surjective, since its values are 0,1,4,9,…0, 1, 4, 9, \dots only.

Example P1.2.30. The function n↦n mod 5n \mapsto n \bmod 5 from {0,…,19}\{0, \dots, 19\} to {0,…,4}\{0, \dots, 4\} is surjective, since r mod 5=rr \bmod 5 = r for each r<5r < 5. It is not injective, since 00 and 55 have the same image. The function x↦3x+2 mod 7x \mapsto 3x + 2 \bmod 7 on {0,…,6}\{0, \dots, 6\} takes the values 2,5,1,4,0,3,62, 5, 1, 4, 0, 3, 6 for x=0,…,6x = 0, \dots, 6. They are all different, so it is a bijection.

Theorem P1.2.31 (composition of maps). Let f:A→Bf : A \to B and g:B→Cg : B \to C.

  1. If ff and gg are injective, then g∘fg \circ f is injective.
  2. If ff and gg are surjective, then g∘fg \circ f is surjective.
  3. If g∘fg \circ f is injective, then ff is injective.
  4. If g∘fg \circ f is surjective, then gg is surjective.

Lean: Function.Injective.comp, Function.Surjective.comp, Function.Injective.of_comp and Function.Surjective.of_comp (Mathlib).

Proof. Rule 1. Let g(f(a))=g(f(a′))g(f(a)) = g(f(a')). Since gg is injective, f(a)=f(a′)f(a) = f(a'), and since ff is injective, a=a′a = a'. Rule 2. Let c∈Cc \in C. Since gg is surjective there is b∈Bb \in B with g(b)=cg(b) = c, and since ff is surjective there is a∈Aa \in A with f(a)=bf(a) = b. Then g(f(a))=cg(f(a)) = c. Rule 3. Let f(a)=f(a′)f(a) = f(a'). Apply gg to get g(f(a))=g(f(a′))g(f(a)) = g(f(a')), and the injectivity of g∘fg \circ f gives a=a′a = a'. Rule 4. Let c∈Cc \in C. Some a∈Aa \in A has g(f(a))=cg(f(a)) = c, so b=f(a)b = f(a) is an element of BB with g(b)=cg(b) = c. □\square

Example P1.2.32. Let A={1}A = \{1\}, B={1,2}B = \{1, 2\}, C={1}C = \{1\}, with f(1)=1f(1) = 1 and g(1)=g(2)=1g(1) = g(2) = 1. Then g∘fg \circ f is injective and surjective, ff is injective, and gg is surjective, in agreement with rules 3 and 4. But gg is not injective, and ff is not surjective, so the converses of the rules are false.

Example P1.2.33. The functions f(n)=n+1f(n) = n + 1 and g(n)=n2g(n) = n^2 on ℕ\mathbb{N} are both injective, and g∘f(n)=(n+1)2g \circ f(n) = (n + 1)^2 is injective, in agreement with rule 1. Rule 2 does not apply, since neither function is surjective.

Theorem P1.2.34 (the inverse). A function f:A→Bf : A \to B is bijective if and only if there is a function g:B→Ag : B \to A with g∘f=idAg \circ f = \mathrm{id}_A and f∘g=idBf \circ g = \mathrm{id}_B. The function gg is then unique. It is the inverse f−1f^{-1}.

Lean: Function.bijective_iff_has_inverse and Equiv.ofBijective (Mathlib).

Proof. Let ff be bijective. For each b∈Bb \in B there is an aa with f(a)=bf(a) = b, by surjectivity, and this aa is unique, by injectivity. Define g(b)g(b) to be this aa. Then f(g(b))=bf(g(b)) = b for all bb. For a∈Aa \in A, f(g(f(a)))=f(a)f(g(f(a))) = f(a), and injectivity of ff gives g(f(a))=ag(f(a)) = a. Conversely, let gg exist. If f(a)=f(a′)f(a) = f(a'), then a=g(f(a))=g(f(a′))=a′a = g(f(a)) = g(f(a')) = a', so ff is injective. For b∈Bb \in B, f(g(b))=bf(g(b)) = b, so ff is surjective. For the uniqueness, if gg and g′g' are both inverses, then g=g∘(f∘g′)=(g∘f)∘g′=g′g = g \circ (f \circ g') = (g \circ f) \circ g' = g'. □\square

Example P1.2.35. The function f(x)=3x+2 mod 7f(x) = 3x + 2 \bmod 7 is a bijection of {0,…,6}\{0, \dots, 6\}. Its inverse is g(y)=5(y−2) mod 7g(y) = 5(y - 2) \bmod 7, since 3⋅5=15≡1(mod7)3 \cdot 5 = 15 \equiv 1 \pmod 7. Check: g(f(4))=g(0)=5⋅(−2) mod 7=4g(f(4)) = g(0) = 5 \cdot (-2) \bmod 7 = 4.

Example P1.2.36. The function n↦n+1n \mapsto n + 1 from ℕ\mathbb{N} to {1,2,3,… }\{1, 2, 3, \dots\} is a bijection with inverse m↦m−1m \mapsto m - 1. The reader may use this bijection to change the index of a sum from n=0n = 0 to n=1n = 1, as the sums of P2 do.

Definition P1.2.37 (image and preimage). For f:A→Bf : A \to B, S⊆AS \subseteq A and T⊆BT \subseteq B, the image of SS is f(S)={f(a):a∈S}f(S) = \{f(a) : a \in S\}, and the preimage of TT is f−1(T)={a∈A:f(a)∈T}f^{-1}(T) = \{a \in A : f(a) \in T\}. The preimage is defined for every function, also when no inverse function exists.

Lean: Set.image f S (written f '' S) and Set.preimage f T (written f ⁻¹' T) (Mathlib).

Example P1.2.38. For f(n)=⌊n/3⌋f(n) = \lfloor n / 3 \rfloor on {0,…,9}\{0, \dots, 9\}, the image of {3,4,9}\{3, 4, 9\} is {1,3}\{1, 3\} and the preimage of {1,2}\{1, 2\} is {3,4,5,6,7,8}\{3, 4, 5, 6, 7, 8\}.

Example P1.2.39. For f(x)=x2f(x) = x^2 on ℤ\mathbb{Z}, the preimage of {4}\{4\} is {−2,2}\{-2, 2\}, the preimage of {2}\{2\} is ∅\emptyset, and the image of {−3,…,3}\{-3, \dots, 3\} is {0,1,4,9}\{0, 1, 4, 9\}.

Theorem P1.2.40 (image and preimage of sets). For f:A→Bf : A \to B:

  1. f(S∪S′)=f(S)∪f(S′)f(S \cup S') = f(S) \cup f(S') and f−1(T∪T′)=f−1(T)∪f−1(T′)f^{-1}(T \cup T') = f^{-1}(T) \cup f^{-1}(T');
  2. f−1(T∩T′)=f−1(T)∩f−1(T′)f^{-1}(T \cap T') = f^{-1}(T) \cap f^{-1}(T');
  3. f(S∩S′)⊆f(S)∩f(S′)f(S \cap S') \subseteq f(S) \cap f(S'), and equality holds for all SS, S′S' if ff is injective.

Lean: Set.image_union, Set.preimage_union, Set.preimage_inter, Set.image_inter_subset and Set.image_inter for injective f (Mathlib).

Proof. Rule 1 for images: b∈f(S∪S′)b \in f(S \cup S') if and only if b=f(a)b = f(a) for some a∈Sa \in S or a∈S′a \in S', which says that b∈f(S)b \in f(S) or b∈f(S′)b \in f(S'). Rule 1 for preimages and rule 2: a∈f−1(T∪T′)a \in f^{-1}(T \cup T') if and only if f(a)∈Tf(a) \in T or f(a)∈T′f(a) \in T', and the same with "and" for the intersection. Rule 3: if b=f(a)b = f(a) with a∈S∩S′a \in S \cap S', then b∈f(S)b \in f(S) and b∈f(S′)b \in f(S'). Now let ff be injective and b∈f(S)∩f(S′)b \in f(S) \cap f(S'). Then b=f(a)=f(a′)b = f(a) = f(a') with a∈Sa \in S and a′∈S′a' \in S', and injectivity gives a=a′a = a', so a∈S∩S′a \in S \cap S' and b∈f(S∩S′)b \in f(S \cap S'). □\square

Example P1.2.41. The inclusion in rule 3 may be proper. For f(x)=x2f(x) = x^2, S={−1}S = \{-1\} and S′={1}S' = \{1\}, the sets satisfy S∩S′=∅S \cap S' = \emptyset and f(S∩S′)=∅f(S \cap S') = \emptyset, but f(S)∩f(S′)={1}f(S) \cap f(S') = \{1\}.

Example P1.2.42. For f(n)=⌊n/3⌋f(n) = \lfloor n / 3 \rfloor, T={0,1}T = \{0, 1\} and T′={1,2}T' = \{1, 2\}: f−1(T∩T′)=f−1({1})={3,4,5}f^{-1}(T \cap T') = f^{-1}(\{1\}) = \{3, 4, 5\}, and f−1(T)∩f−1(T′)={0,…,5}∩{3,…,8}={3,4,5}f^{-1}(T) \cap f^{-1}(T') = \{0, \dots, 5\} \cap \{3, \dots, 8\} = \{3, 4, 5\}.

Proof

The proofs follow the definitions at each step. A statement about injectivity starts with "let f(a)=f(a′)f(a) = f(a')", and a statement about surjectivity starts with "let b∈Bb \in B". These two openings are the same as the opening of a proof of a statement with ∀\forall, which the next chapters use.

The theorem on the inverse is the first place where a function is built in a proof: the proof defines g(b)g(b) as the unique aa with f(a)=bf(a) = b. The reader should note that this uses both properties of a bijection. Surjectivity says that aa exists, and injectivity says that it is unique, so that gg is a function.

Examples

Example P1.2.43 (a sequence). A sequence a0,a1,a2,…a_0, a_1, a_2, \dots of real numbers is a function a:ℕ→ℝa : \mathbb{N} \to \mathbb{R}, with ana_n written for a(n)a(n). A finite sequence a1,…,ana_1, \dots, a_n is a function on {1,…,n}\{1, \dots, n\}. The sum ∑k=1nak\sum_{k=1}^{n} a_k of P2 is a sum of the values of such a function. A change of index k=j+1k = j + 1 is the use of a bijection from {0,…,n−1}\{0, \dots, n-1\} to {1,…,n}\{1, \dots, n\}.

Example P1.2.44 (the floor function). The function x↦⌊x⌋x \mapsto \lfloor x \rfloor from ℝ\mathbb{R} to ℤ\mathbb{Z} is surjective, since ⌊m⌋=m\lfloor m \rfloor = m for an integer mm. It is not injective, since ⌊1.2⌋=⌊1.7⌋=1\lfloor 1.2 \rfloor = \lfloor 1.7 \rfloor = 1. The preimage of {3}\{3\} is the interval [3,4)[3, 4). TAOCP Section 1.2.4 studies this function.

Example P1.2.45 (a count of functions). The number of functions from a set with mm elements to a set with nn elements is nmn^m, since each of the mm elements has nn choices. For m=4m = 4 and n=6n = 6 this is 64=12966^4 = 1296. Of these, 6⋅5⋅4⋅3=3606 \cdot 5 \cdot 4 \cdot 3 = 360 are injective: the first element has 66 values, the second has 55 that differ from the first, and so on. A computer count over the 12961296 functions gives the same 360360. The number of surjections from 55 elements onto 33 is 150150, which Section 3 derives with the rule of inclusion and exclusion.

Exercises

  1. [10] For each function from ℤ\mathbb{Z} to ℤ\mathbb{Z} decide whether it is injective and whether it is surjective: x↦x+3x \mapsto x + 3, x↦x2x \mapsto x^2, x↦2xx \mapsto 2x, x↦⌊x/2⌋x \mapsto \lfloor x / 2 \rfloor.
  2. [12] For f(n)=n mod 5f(n) = n \bmod 5 on {0,…,19}\{0, \dots, 19\}, find f({3,8,13})f(\{3, 8, 13\}) and f−1({0})f^{-1}(\{0\}).
  3. [15] Find g∘fg \circ f and f∘gf \circ g for f(n)=2nf(n) = 2n and g(n)=n+3g(n) = n + 3 on ℤ\mathbb{Z}, and show that they differ.
  4. [15] Prove that the composition of two surjections is a surjection and that the composition of two bijections is a bijection, with (g∘f)−1=f−1∘g−1(g \circ f)^{-1} = f^{-1} \circ g^{-1}.
  5. [M18] Show that if g∘fg \circ f is injective, then ff is injective, and give an example where gg is not injective.
  6. [M20] Show that f(S∩S′)=f(S)∩f(S′)f(S \cap S') = f(S) \cap f(S') for all SS and S′S' only if ff is injective. (Take SS and S′S' with one element each.)
  7. [M22] Prove that f−1(T‾)=f−1(T)‾f^{-1}(\overline{T}) = \overline{f^{-1}(T)}, where the complements are in BB and in AA.
  8. [M22] Find the inverse of x↦5x+1x \mapsto 5x + 1 on {0,…,6}\{0, \dots, 6\} with arithmetic mod 77. Find all aa in {0,…,6}\{0, \dots, 6\} for which x↦ax+1 mod 7x \mapsto ax + 1 \bmod 7 is a bijection, and prove that your list is complete.
  9. [M30] Let AA be a set and f:A→𝒫(A)f : A \to \mathcal{P}(A) a function. Show that ff is not surjective, by considering {a∈A:a∉f(a)}\{a \in A : a \notin f(a)\}.

Lean

A function f : α → β is a term. A proof that it is injective is intro a a' h, and a proof that it is surjective is intro b followed by exact ⟨a, ha⟩ for a witness. The definitions are Function.Injective f and Function.Surjective f, and the lemmas of the theorem on composition are Function.Injective.comp and Function.Surjective.comp. For Finset and Fin n, decide checks a concrete claim, for example example : Function.Injective (fun x : Fin 7 => 3 * x + 2) := by decide; the function on Fin 7 uses the arithmetic mod 77 of the type. The theorem on the inverse is Function.bijective_iff_has_inverse.

Notes and history

The idea of a function as a rule with a domain goes back to Euler and Dirichlet. The definition by sets of pairs is from the twentieth century. Bourbaki fixed the words injective, surjective and bijective in the 1930s. Exercise 9 is Cantor's theorem, and its proof by the set of elements that are not in their own image is the diagonal argument of Cantor, from 1891.

TAOCP uses bijections for the change of the index in a sum (Section 1.2.3) and for the correspondence between permutations and their inversion tables (Section 5.1.1). A table of a hash function is a function from keys to places, and the question of Section 3, whether it is injective, is the question of collisions.

Finite sets and counting

Statement

To count the elements of a set, a proof compares it with a set that has a known size. This section defines the size of a finite set, proves the three basic rules of counting, and proves the pigeonhole principle, which says that a set cannot be mapped into a smaller set with no repeat.

Definition P1.2.46 (finite set, size). A set AA is finite with size nn, written ∣A∣=n|A| = n, when there is a bijection from {1,2,…,n}\{1, 2, \dots, n\} onto AA. The empty set has size 00. A set that is not finite is infinite.

Lean: Finset with Finset.card, and Fintype.card for a finite type (Mathlib).

Example P1.2.47. ∣{a,b,c}∣=3|\{a, b, c\}| = 3, with the bijection 1↦a1 \mapsto a, 2↦b2 \mapsto b, 3↦c3 \mapsto c. The set {x∈ℤ:x2<20}\{x \in \mathbb{Z} : x^2 < 20\} has size 99.

Example P1.2.48. The set ℕ\mathbb{N} is infinite. Exercise 10 shows that no function from a finite set into itself is injective without being surjective, while n↦n+1n \mapsto n + 1 maps ℕ\mathbb{N} into itself, injectively and not onto.

Theorem P1.2.49 (the bijection rule). If AA and BB are finite and there is a bijection f:A→Bf : A \to B, then ∣A∣=∣B∣|A| = |B|.

Lean: Finset.card_bij and Finset.card_nbij (Mathlib).

Proof. Let ∣A∣=n|A| = n with the bijection h:{1,…,n}→Ah : \{1, \dots, n\} \to A. Then f∘hf \circ h is a bijection from {1,…,n}\{1, \dots, n\} onto BB, by the theorem on composition of maps in Section 2. So ∣B∣=n|B| = n. □\square

Example P1.2.50. The bijection n↦n+4n \mapsto n + 4 maps {1,…,6}\{1, \dots, 6\} onto {5,…,10}\{5, \dots, 10\}, so the second set has 66 elements. The map S↦{1,…,4}∖SS \mapsto \{1, \dots, 4\} \setminus S is a bijection from the set of subsets of {1,2,3,4}\{1, 2, 3, 4\} of size 11 onto those of size 33, so there are as many of one as of the other.

Example P1.2.51. The map that sends a subset SS of {1,…,n}\{1, \dots, n\} to the bit string b1…bnb_1 \dots b_n with bi=[i∈S]b_i = [i \in S] is a bijection from 𝒫({1,…,n})\mathcal{P}(\{1, \dots, n\}) onto {0,1}n\{0, 1\}^n. Its inverse sends a string to the set of the positions of its ones.

Theorem P1.2.52 (the sum rule). Let AA and BB be finite and disjoint. Then A∪BA \cup B is finite and ∣A∪B∣=∣A∣+∣B∣|A \cup B| = |A| + |B|. More generally, for pairwise disjoint finite sets A1,…,AkA_1, \dots, A_k, ∣A1∪⋯∪Ak∣=∣A1∣+⋯+∣Ak∣|A_1 \cup \dots \cup A_k| = |A_1| + \dots + |A_k|.

Lean: Finset.card_union_of_disjoint (older name Finset.card_disjoint_union) and Finset.card_biUnion (Mathlib).

Proof. Let h:{1,…,m}→Ah : \{1, \dots, m\} \to A and k:{1,…,n}→Bk : \{1, \dots, n\} \to B be bijections. Define u:{1,…,m+n}→A∪Bu : \{1, \dots, m + n\} \to A \cup B by u(i)=h(i)u(i) = h(i) for i≤mi \le m and u(i)=k(i−m)u(i) = k(i - m) for i>mi > m. The function uu is injective: two values from the same part are different since hh and kk are injective, and a value of AA never equals a value of BB, since AA and BB are disjoint. It is surjective, since each element of A∪BA \cup B is in AA or in BB. So ∣A∪B∣=m+n|A \cup B| = m + n. The case of kk sets follows by repeated use of the case of two sets, since A1∪⋯∪Ak−1A_1 \cup \dots \cup A_{k-1} is disjoint from AkA_k (Chapter 4 makes this induction precise). □\square

Example P1.2.53. The integers from 11 to 2020 split into the primes {2,3,5,7,11,13,17,19}\{2, 3, 5, 7, 11, 13, 17, 19\} with 88 elements, the number 11, and the composites, which are 20−8−1=1120 - 8 - 1 = 11 in number: 4,6,8,9,10,12,14,15,16,18,204, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20.

Example P1.2.54. The bit strings of length 44 with at most one 11 are the string 00000000 and the four strings with one 11. By the sum rule there are 1+4=51 + 4 = 5.

Theorem P1.2.55 (the product rule). If AA and BB are finite, then ∣A×B∣=∣A∣⋅∣B∣|A \times B| = |A| \cdot |B|. If ∣A∣=m|A| = m, then ∣An∣=mn|A^n| = m^n.

Lean: Finset.card_product and Fintype.card_prod (Mathlib).

Proof. For each a∈Aa \in A let Ba={(a,b):b∈B}B_a = \{(a, b) : b \in B\}. The map b↦(a,b)b \mapsto (a, b) is a bijection from BB onto BaB_a, so ∣Ba∣=∣B∣|B_a| = |B|. The sets BaB_a for different aa are disjoint, and their union is A×BA \times B. By the sum rule, ∣A×B∣|A \times B| is the sum of ∣A∣|A| numbers, each equal to ∣B∣|B|, so it is ∣A∣⋅∣B∣|A| \cdot |B|. The second claim follows by repeated use, since An=An−1×AA^n = A^{n-1} \times A up to the bijection ((a1,…,an−1),an)↦(a1,…,an)((a_1, \dots, a_{n-1}), a_n) \mapsto (a_1, \dots, a_n). □\square

Example P1.2.56. A code has three letters from {A,…,Z}\{A, \dots, Z\}, then three digits. There are 263⋅103=17,576,00026^3 \cdot 10^3 = 17{,}576{,}000 codes.

Example P1.2.57. The number of functions from an mm-set AA to an nn-set BB is nmn^m. A function is the list (f(a1),…,f(am))∈Bm(f(a_1), \dots, f(a_m)) \in B^m, and the map from the function to the list is a bijection. For m=3m = 3 and n=2n = 2 the count is 88.

Theorem P1.2.58 (subsets). A set with nn elements has 2n2^n subsets.

Lean: Finset.card_powerset (Mathlib).

Proof. The map of the third example of the bijection rule is a bijection from 𝒫(A)\mathcal{P}(A) onto {0,1}n\{0, 1\}^n, once the elements of AA are written as a1,…,ana_1, \dots, a_n. By the product rule ∣{0,1}n∣=2n|\{0, 1\}^n| = 2^n. □\square

Example P1.2.59. {1,2,3}\{1, 2, 3\} has 23=82^3 = 8 subsets, as the list of Section 1 shows. The set {1,…,10}\{1, \dots, 10\} has 10241024 subsets, and (104)=210\binom{10}{4} = 210 of them have four elements.

Theorem P1.2.60 (inclusion and exclusion). For finite sets AA, BB and CC:

  1. ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|;
  2. ∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|.

Lean: Finset.card_union_add_card_inter for rule 1; rule 2 follows from it used twice, as in the proof (Mathlib).

Proof. Rule 1. The sets AA and B∖AB \setminus A are disjoint with union A∪BA \cup B, so ∣A∪B∣=∣A∣+∣B∖A∣|A \cup B| = |A| + |B \setminus A|. The sets B∖AB \setminus A and A∩BA \cap B are disjoint with union BB, so ∣B∖A∣=∣B∣−∣A∩B∣|B \setminus A| = |B| - |A \cap B|. Combine. Rule 2. Use rule 1 for A∪BA \cup B and CC: ∣A∪B∪C∣=∣A∪B∣+∣C∣−∣(A∪B)∩C∣|A \cup B \cup C| = |A \cup B| + |C| - |(A \cup B) \cap C|. Now (A∪B)∩C=(A∩C)∪(B∩C)(A \cup B) \cap C = (A \cap C) \cup (B \cap C), and rule 1 gives ∣(A∩C)∪(B∩C)∣=∣A∩C∣+∣B∩C∣−∣A∩B∩C∣|(A \cap C) \cup (B \cap C)| = |A \cap C| + |B \cap C| - |A \cap B \cap C|. Substitute ∣A∪B∣|A \cup B| from rule 1 and collect the terms. □\square

Example P1.2.61. Among the integers 1,…,2001, \dots, 200, let AA be the multiples of 33 and BB the multiples of 77. Then ∣A∣=66|A| = 66, ∣B∣=28|B| = 28 and ∣A∩B∣=9|A \cap B| = 9, the multiples of 2121. The count of multiples of 33 or 77 is 66+28−9=8566 + 28 - 9 = 85.

Example P1.2.62. Among 1,…,1001, \dots, 100, the multiples of 22, 33 or 55 number 50+33+20−16−10−6+3=7450 + 33 + 20 - 16 - 10 - 6 + 3 = 74, with 1616, 1010, 66 and 33 the numbers of multiples of 66, 1010, 1515 and 3030. A direct count by computer also gives 7474. The remaining 2626 numbers up to 100100 include 11 and the primes above 55.

Theorem P1.2.63 (the pigeonhole principle). Let f:A→Bf : A \to B be a function between finite sets with ∣A∣>∣B∣|A| > |B|. Then ff is not injective: there are a≠a′a \ne a' in AA with f(a)=f(a′)f(a) = f(a'). More generally, if ∣A∣>k∣B∣|A| > k |B|, then some b∈Bb \in B has at least k+1k + 1 elements in its preimage.

Lean: Finset.exists_ne_map_eq_of_card_lt_of_maps_to and Fintype.exists_ne_map_eq_of_card_lt (Mathlib).

Proof. The first claim is proved by induction on n=∣B∣n = |B|. Claim: for each n≥0n \ge 0, no injective function maps a set with more than nn elements into a set with nn elements. For n=0n = 0, a set with at least one element has no function into the empty set at all. Let n≥1n \ge 1 and suppose the claim for n−1n - 1. Let f:A→Bf : A \to B be injective with ∣B∣=n|B| = n and ∣A∣>n|A| > n. Pick a0∈Aa_0 \in A and put b0=f(a0)b_0 = f(a_0). Let A′=A∖{a0}A' = A \setminus \{a_0\} and B′=B∖{b0}B' = B \setminus \{b_0\}. For a∈A′a \in A', f(a)≠b0f(a) \ne b_0, since ff is injective and a≠a0a \ne a_0. So ff maps A′A' into B′B', and it is still injective. But ∣A′∣=∣A∣−1>n−1=∣B′∣|A'| = |A| - 1 > n - 1 = |B'|, which contradicts the claim for n−1n - 1. For the general claim, suppose every bb has at most kk elements in its preimage. The preimages f−1({b})f^{-1}(\{b\}) for b∈Bb \in B are disjoint with union AA. By the sum rule ∣A∣≤k∣B∣|A| \le k |B|, a contradiction. □\square

Example P1.2.64. Among any 1313 people, two were born in the same month: 13>1213 > 12 with the function "month of birth". Among any 2525 people, three share a month: 25>2⋅1225 > 2 \cdot 12.

Example P1.2.65. Choose 66 numbers from {1,…,10}\{1, \dots, 10\}. Two of them add up to 1111. The five sets {1,10},{2,9},{3,8},{4,7},{5,6}\{1, 10\}, \{2, 9\}, \{3, 8\}, \{4, 7\}, \{5, 6\} are the boxes. The function from the six chosen numbers to the five boxes has a repeat, and two numbers in one box sum to 1111. The bound is sharp: the five numbers 1,2,3,4,51, 2, 3, 4, 5 have no pair with sum 1111. A search over all (106)=210\binom{10}{6} = 210 choices of six numbers confirms the claim.

Proof

The proofs build on one idea: a count is a bijection with a standard set. The sum rule adds disjoint parts, and the product rule is a sum of ∣A∣|A| equal parts. Every count in this volume and in TAOCP Section 1.2.6, which counts combinations, uses one of the three rules or a bijection.

The pigeonhole principle has no construction of the pair (a,a′)(a, a'). It proves only that the pair exists. This is a typical existence proof. The induction in its proof uses the method of Chapter 4, in its simplest form, and the reader may return to it after Chapter 4.

Examples

Example P1.2.66 (hashing). A table has mm places and m+1m + 1 keys are stored. The map from keys to places has a repeat, so two keys collide. The rule is the reason that every scheme of hashing needs a way to resolve a collision, a topic of TAOCP Section 6.4.

Example P1.2.67 (a difference divisible by nn). Among any n+1n + 1 integers, two have a difference divisible by nn. The function a↦a mod na \mapsto a \bmod n goes from the n+1n + 1 integers to the nn remainders {0,…,n−1}\{0, \dots, n - 1\}, so it has a repeat, and two integers with the same remainder differ by a multiple of nn. For n=10n = 10 and the 11 integers 3,17,29,41,52,66,78,80,95,101,1173, 17, 29, 41, 52, 66, 78, 80, 95, 101, 117, the remainders are 3,7,9,1,2,6,8,0,5,1,73, 7, 9, 1, 2, 6, 8, 0, 5, 1, 7, so 4141 and 101101 have remainder 11 and 101−41=60101 - 41 = 60.

Example P1.2.68 (surjections). The number of surjections from a 55-set onto a 33-set is found by the rule of inclusion and exclusion. Let UU be the set of all 35=2433^5 = 243 functions. For each bb in the target, the set UbU_b of functions that miss bb has 25=322^5 = 32 elements. Two targets missed leave one place, so 11 function, and all three missed leave none. The surjections are the functions that miss no bb: 243−3⋅32+3⋅1−0=150243 - 3 \cdot 32 + 3 \cdot 1 - 0 = 150. For 44 elements onto 33 the same count is 81−3⋅16+3=3681 - 3 \cdot 16 + 3 = 36, and a direct count by computer agrees.

Exercises

  1. [10] How many subsets has {1,…,8}\{1, \dots, 8\}? How many have at most one element? How many bit strings of length 88 have at most one 11?
  2. [12] Count the integers in 1,…,2001, \dots, 200 that are multiples of 33 or of 77, and the integers that are multiples of neither.
  3. [15] A code has three letters and then three digits. Count the codes that have three different letters.
  4. [15] Prove that ∣A∖B∣=∣A∣−∣A∩B∣|A \setminus B| = |A| - |A \cap B| for finite sets.
  5. [M18] In a class of 8080, 4040 take mathematics, 3535 take physics, 3030 take chemistry, 1515 take mathematics and physics, 1212 take mathematics and chemistry, 1010 take physics and chemistry, and 55 take all three. How many take none of the three?
  6. [M20] Show that among any five numbers chosen from {1,…,8}\{1, \dots, 8\} two have the sum 99.
  7. [M22] Show that among any n+1n + 1 integers two have a difference divisible by nn, and use it to show that among 77 integers two have a sum or a difference divisible by 1010.
  8. [M22] Ten points lie in a square of side 33. Show that two of them are at distance at most 2\sqrt 2.
  9. [M25] Show that the number of surjections from an nn-set onto a 22-set is 2n−22^n - 2, and that the number onto a 33-set is 3n−3⋅2n+33^n - 3 \cdot 2^n + 3. Check the second for n=3n = 3.
  10. [M25] Let AA be finite and f:A→Af : A \to A. Show that ff is injective if and only if it is surjective. Give a function on ℕ\mathbb{N} for which this fails.

Lean

A finite set is Finset α, and its size is Finset.card. The sum rule for disjoint sets is Finset.card_union_of_disjoint in the current Mathlib. The product rule is Finset.card_product, the count of subsets is Finset.card_powerset, and the pigeonhole principle is Fintype.exists_ne_map_eq_of_card_lt, which has the form Fintype.card β < Fintype.card α → ∃ x y, x ≠ y ∧ f x = f y. For a small finite claim decide or simp with Finset.card gives the answer, for example example : (Finset.range 10).powerset.card = 1024 := by simp. Read the exact name in the Mathlib documentation before use, since the older names have changed.

Notes and history

The pigeonhole principle is named for Dirichlet, who used it in 1834 for a result on approximation of real numbers by fractions. The rule of inclusion and exclusion for several sets is in the work of De Moivre, from 1718, and its form for a general sum is due to Sylvester in the 1880s.

TAOCP Section 1.2.6 counts combinations by the same rules, and Section 1.2.3 uses a bijection of index sets for the change of the summation index. The reader of Section 6.4 meets the pigeonhole principle again in the study of hash tables.

Sets and functions are the objects of every proof in this volume and in TAOCP. A list of keys is a set. An algorithm computes a function from inputs to outputs. The number of steps of a loop is the size of a finite set of events. This chapter gives the exact definitions and the basic proofs about them.

Section 1 defines sets, subsets, the operations of union, intersection and difference, the power set and the product of two sets, and it shows how to prove an equation between sets by a check of elements. Section 2 defines functions as rules with a domain and a codomain, composition, and the three properties that every later chapter uses: injective, surjective and bijective. It proves that the bijective functions are exactly those that have an inverse. Section 3 counts. A finite set has a size, a bijection between two sets shows that they have the same size, and the sum rule, the product rule and the rule of inclusion and exclusion follow from this. The section ends with the pigeonhole principle, which is the first result of the volume that has a use in the design of algorithms: it proves that a hash table of mm places cannot hold m+1m + 1 keys with no collision.

The chapter prepares TAOCP Section 1.2.3, where the sum over a set of indices needs a finite set and a bijection for the change of the index, and Section 1.2.4, where the floor function is a function in the sense of Section 2. P2 uses the three counting rules of Section 3 without a reference. Chapter 7 returns to functions as relations.

The chapter assumes Chapter 1. The spine texts are Velleman, How to Prove It, Chapters 1, 4 and 5, and Lehman, Leighton and Meyer, Mathematics for Computer Science, Chapters 4 and 5. Each section ends with exercises, and the solutions are in the last chapter of the volume.

The source of this page. Something is wrong? Report it.