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
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 when is an element of , and when it is not. Two sets are equal when they have the same elements: means . A set is written by a list, , or by a property, . The order of a list and the repeats in it do not matter: .
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 has the elements , nine of them. The set is and has ten elements.
Example P1.2.3. The sets and are different: the first has the elements and , and the second has the elements and . An element of a set may be a set. The set has one element, and the set has none.
Definition P1.2.4 (subset, empty set). The set is a subset of , written , when every element of is an element of : . The empty set has no element. The set is a proper subset of when and .
Lean: A ⊆ B and ∅ for Set α; Set.Subset.antisymm proves from the two inclusions, and Set.empty_subset proves (Mathlib).
Example P1.2.5. , and . Also : each set is a subset of itself.
Example P1.2.6. The empty set is a subset of every set . The statement is true, since the hypothesis is false for every (Chapter 1, Section 1). The sets and differ: is true, is true, and is false.
Theorem P1.2.7 (equality by two inclusions). For sets and , if and only if and .
Lean: Set.Subset.antisymm and Set.ext_iff (Mathlib).
Proof. If , then each element of is an element of and each element of is an element of . Conversely, let and . Take any . If , then . If , then . So for every .
Example P1.2.8. To show , take an with . Then , so or . This proves the inclusion . For the other, and .
Example P1.2.9. The theorem is the usual way to prove an equation of sets. For and over the integers, since gives . For let . Then , so .
Definition P1.2.10 (operations). For sets and inside a universe :
- the union ;
- the intersection ;
- the difference ;
- the complement ;
- the power set .
Two sets with 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 and . Then , , and . With the complement of is .
Example P1.2.12. has eight elements: , , , , , , and . The set is and has one element.
Theorem P1.2.13 (laws of sets). For sets , and in a universe :
- and ;
- and ;
- and ;
- ;
- .
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 , and then uses the laws of logic of Chapter 1. Rule 2, first part: means . The first distributive law of logic gives , which means . Each step is an equivalence, so the two sets have the same elements. Rule 3, first part: means , which is by the second law of De Morgan, and this means . Rule 4: means , which is . This is the same as , since may be said twice. Rule 5: means and . This says that every element of is in and in , so . The rest of rule 1 and rule 2 are the same, with the commutative and the second distributive law.
Example P1.2.14. For , and the set is . The set . The two agree.
Example P1.2.15. For and above, , and .
Definition P1.2.16 (ordered pair, product). The ordered pair has the property if and only if and . The product of two sets is . The product of with itself times is , the set of lists .
Lean: Prod, written α × β; for sets Set.prod and Set.mem_prod (Mathlib).
Example P1.2.17. has six elements: . The product is empty.
Example P1.2.18. has eight elements, the bit strings of length . The plane is the set of pairs of real numbers, and the pairs are ordered: .
Proof
All proofs of the section have the same pattern. To prove , the proof takes an arbitrary element and shows two implications, or it uses a chain of equivalences. To prove , the proof starts with "let " and ends with "so ". 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 is false: with and the left side is and the right side is . The element is in the left set, since , and it is not in the right set, since .
Examples
Example P1.2.19 (the symmetric difference). Let . For and the set is . An element is in exactly when it is in one of the two sets and not in both, which is the exclusive or of Chapter 1. So and .
Example P1.2.20 (a product that is not a union). The sets and have union , which has four elements. The union of and is with three elements, and it is not a product of two sets, since a product with the elements and also has .
Example P1.2.21 (sets of divisors). Let . Then and . The intersection is the set of common divisors, and its greatest element is . Chapter 5 shows that and uses it for Algorithm 1.1E.
Exercises
- [10] List the elements of , of and of .
- [12] Decide which statements are true: (a) ; (b) ; (c) ; (d) ; (e) .
- [15] Prove by elements that .
- [15] Prove by elements that .
- [M18] Show that the three statements , and are equivalent.
- [M20] Show that , and give sets and for which the inclusion is proper.
- [M22] Show that if and only if , and that .
- [M24] Show that , and find sets for which .
- [M25] Show that there is no set with , by asking whether .
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 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 , 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 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 and be sets. A function from to , written , assigns to each exactly one element . The set is the domain and is the codomain. Two functions are equal when for all . A function is a set of pairs: its graph is .
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 is a function from to . The rule is a function from to , with the values for .
Example P1.2.24. The rule " a number whose square is " is not a function from to : has two values and has none. The rule " for ", with the nonnegative root, is a function from to .
Definition P1.2.25 (composition, identity). For and the composition is . The identity on is , .
Lean: Function.comp, written g ∘ f, with Function.comp_apply and id (Lean core).
Example P1.2.26. Let and on . Then and . For the first is and the second is . So composition is not commutative.
Example P1.2.27. Composition is associative: , since both send to . And .
Definition P1.2.28 (injective, surjective, bijective). A function is injective when implies . It is surjective when each has some with . It is bijective when it is both.
Lean: Function.Injective, Function.Surjective and Function.Bijective (Mathlib).
Example P1.2.29. The function from to is injective, since gives . It is not surjective: no gives the value . The function on is not injective, since , and it is not surjective, since its values are only.
Example P1.2.30. The function from to is surjective, since for each . It is not injective, since and have the same image. The function on takes the values for . They are all different, so it is a bijection.
Theorem P1.2.31 (composition of maps). Let and .
- If and are injective, then is injective.
- If and are surjective, then is surjective.
- If is injective, then is injective.
- If is surjective, then is surjective.
Lean: Function.Injective.comp, Function.Surjective.comp, Function.Injective.of_comp and Function.Surjective.of_comp (Mathlib).
Proof. Rule 1. Let . Since is injective, , and since is injective, . Rule 2. Let . Since is surjective there is with , and since is surjective there is with . Then . Rule 3. Let . Apply to get , and the injectivity of gives . Rule 4. Let . Some has , so is an element of with .
Example P1.2.32. Let , , , with and . Then is injective and surjective, is injective, and is surjective, in agreement with rules 3 and 4. But is not injective, and is not surjective, so the converses of the rules are false.
Example P1.2.33. The functions and on are both injective, and 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 is bijective if and only if there is a function with and . The function is then unique. It is the inverse .
Lean: Function.bijective_iff_has_inverse and Equiv.ofBijective (Mathlib).
Proof. Let be bijective. For each there is an with , by surjectivity, and this is unique, by injectivity. Define to be this . Then for all . For , , and injectivity of gives . Conversely, let exist. If , then , so is injective. For , , so is surjective. For the uniqueness, if and are both inverses, then .
Example P1.2.35. The function is a bijection of . Its inverse is , since . Check: .
Example P1.2.36. The function from to is a bijection with inverse . The reader may use this bijection to change the index of a sum from to , as the sums of P2 do.
Definition P1.2.37 (image and preimage). For , and , the image of is , and the preimage of is . 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 on , the image of is and the preimage of is .
Example P1.2.39. For on , the preimage of is , the preimage of is , and the image of is .
Theorem P1.2.40 (image and preimage of sets). For :
- and ;
- ;
- , and equality holds for all , if 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: if and only if for some or , which says that or . Rule 1 for preimages and rule 2: if and only if or , and the same with "and" for the intersection. Rule 3: if with , then and . Now let be injective and . Then with and , and injectivity gives , so and .
Example P1.2.41. The inclusion in rule 3 may be proper. For , and , the sets satisfy and , but .
Example P1.2.42. For , and : , and .
Proof
The proofs follow the definitions at each step. A statement about injectivity starts with "let ", and a statement about surjectivity starts with "let ". These two openings are the same as the opening of a proof of a statement with , 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 as the unique with . The reader should note that this uses both properties of a bijection. Surjectivity says that exists, and injectivity says that it is unique, so that is a function.
Examples
Example P1.2.43 (a sequence). A sequence of real numbers is a function , with written for . A finite sequence is a function on . The sum of P2 is a sum of the values of such a function. A change of index is the use of a bijection from to .
Example P1.2.44 (the floor function). The function from to is surjective, since for an integer . It is not injective, since . The preimage of is the interval . TAOCP Section 1.2.4 studies this function.
Example P1.2.45 (a count of functions). The number of functions from a set with elements to a set with elements is , since each of the elements has choices. For and this is . Of these, are injective: the first element has values, the second has that differ from the first, and so on. A computer count over the functions gives the same . The number of surjections from elements onto is , which Section 3 derives with the rule of inclusion and exclusion.
Exercises
- [10] For each function from to decide whether it is injective and whether it is surjective: , , , .
- [12] For on , find and .
- [15] Find and for and on , and show that they differ.
- [15] Prove that the composition of two surjections is a surjection and that the composition of two bijections is a bijection, with .
- [M18] Show that if is injective, then is injective, and give an example where is not injective.
- [M20] Show that for all and only if is injective. (Take and with one element each.)
- [M22] Prove that , where the complements are in and in .
- [M22] Find the inverse of on with arithmetic mod . Find all in for which is a bijection, and prove that your list is complete.
- [M30] Let be a set and a function. Show that is not surjective, by considering .
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 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 is finite with size , written , when there is a bijection from onto . The empty set has size . 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. , with the bijection , , . The set has size .
Example P1.2.48. The set is infinite. Exercise 10 shows that no function from a finite set into itself is injective without being surjective, while maps into itself, injectively and not onto.
Theorem P1.2.49 (the bijection rule). If and are finite and there is a bijection , then .
Lean: Finset.card_bij and Finset.card_nbij (Mathlib).
Proof. Let with the bijection . Then is a bijection from onto , by the theorem on composition of maps in Section 2. So .
Example P1.2.50. The bijection maps onto , so the second set has elements. The map is a bijection from the set of subsets of of size onto those of size , so there are as many of one as of the other.
Example P1.2.51. The map that sends a subset of to the bit string with is a bijection from onto . Its inverse sends a string to the set of the positions of its ones.
Theorem P1.2.52 (the sum rule). Let and be finite and disjoint. Then is finite and . More generally, for pairwise disjoint finite sets , .
Lean: Finset.card_union_of_disjoint (older name Finset.card_disjoint_union) and Finset.card_biUnion (Mathlib).
Proof. Let and be bijections. Define by for and for . The function is injective: two values from the same part are different since and are injective, and a value of never equals a value of , since and are disjoint. It is surjective, since each element of is in or in . So . The case of sets follows by repeated use of the case of two sets, since is disjoint from (Chapter 4 makes this induction precise).
Example P1.2.53. The integers from to split into the primes with elements, the number , and the composites, which are in number: .
Example P1.2.54. The bit strings of length with at most one are the string and the four strings with one . By the sum rule there are .
Theorem P1.2.55 (the product rule). If and are finite, then . If , then .
Lean: Finset.card_product and Fintype.card_prod (Mathlib).
Proof. For each let . The map is a bijection from onto , so . The sets for different are disjoint, and their union is . By the sum rule, is the sum of numbers, each equal to , so it is . The second claim follows by repeated use, since up to the bijection .
Example P1.2.56. A code has three letters from , then three digits. There are codes.
Example P1.2.57. The number of functions from an -set to an -set is . A function is the list , and the map from the function to the list is a bijection. For and the count is .
Theorem P1.2.58 (subsets). A set with elements has subsets.
Lean: Finset.card_powerset (Mathlib).
Proof. The map of the third example of the bijection rule is a bijection from onto , once the elements of are written as . By the product rule .
Example P1.2.59. has subsets, as the list of Section 1 shows. The set has subsets, and of them have four elements.
Theorem P1.2.60 (inclusion and exclusion). For finite sets , and :
- ;
- .
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 and are disjoint with union , so . The sets and are disjoint with union , so . Combine. Rule 2. Use rule 1 for and : . Now , and rule 1 gives . Substitute from rule 1 and collect the terms.
Example P1.2.61. Among the integers , let be the multiples of and the multiples of . Then , and , the multiples of . The count of multiples of or is .
Example P1.2.62. Among , the multiples of , or number , with , , and the numbers of multiples of , , and . A direct count by computer also gives . The remaining numbers up to include and the primes above .
Theorem P1.2.63 (the pigeonhole principle). Let be a function between finite sets with . Then is not injective: there are in with . More generally, if , then some has at least 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 . Claim: for each , no injective function maps a set with more than elements into a set with elements. For , a set with at least one element has no function into the empty set at all. Let and suppose the claim for . Let be injective with and . Pick and put . Let and . For , , since is injective and . So maps into , and it is still injective. But , which contradicts the claim for . For the general claim, suppose every has at most elements in its preimage. The preimages for are disjoint with union . By the sum rule , a contradiction.
Example P1.2.64. Among any people, two were born in the same month: with the function "month of birth". Among any people, three share a month: .
Example P1.2.65. Choose numbers from . Two of them add up to . The five sets 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 . The bound is sharp: the five numbers have no pair with sum . A search over all 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 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 . 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 places and 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 ). Among any integers, two have a difference divisible by . The function goes from the integers to the remainders , so it has a repeat, and two integers with the same remainder differ by a multiple of . For and the 11 integers , the remainders are , so and have remainder and .
Example P1.2.68 (surjections). The number of surjections from a -set onto a -set is found by the rule of inclusion and exclusion. Let be the set of all functions. For each in the target, the set of functions that miss has elements. Two targets missed leave one place, so function, and all three missed leave none. The surjections are the functions that miss no : . For elements onto the same count is , and a direct count by computer agrees.
Exercises
- [10] How many subsets has ? How many have at most one element? How many bit strings of length have at most one ?
- [12] Count the integers in that are multiples of or of , and the integers that are multiples of neither.
- [15] A code has three letters and then three digits. Count the codes that have three different letters.
- [15] Prove that for finite sets.
- [M18] In a class of , take mathematics, take physics, take chemistry, take mathematics and physics, take mathematics and chemistry, take physics and chemistry, and take all three. How many take none of the three?
- [M20] Show that among any five numbers chosen from two have the sum .
- [M22] Show that among any integers two have a difference divisible by , and use it to show that among integers two have a sum or a difference divisible by .
- [M22] Ten points lie in a square of side . Show that two of them are at distance at most .
- [M25] Show that the number of surjections from an -set onto a -set is , and that the number onto a -set is . Check the second for .
- [M25] Let be finite and . Show that is injective if and only if it is surjective. Give a function on 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 places cannot hold 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.