Volume P1 · Proof, induction and invariants
Relations and orders
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
Relations and equivalence relations
Statement
Definition P1.7.1 (relation). A relation from a set to a set is a subset . A relation on is a subset of . Write for . A function is a relation in which each has exactly one with .
Lean: Set (α × β) or the type α → β → Prop; Relation and Rel α β in Mathlib.
Example P1.7.2. On the relation "divides" is the set of pairs with , and it has pairs. The relation "" on the same set has pairs. The relation "has the same parity" has pairs.
Definition P1.7.3 (properties). A relation on is
- reflexive if for every ;
- irreflexive if for no ;
- symmetric if implies ;
- antisymmetric if and imply ;
- transitive if and imply .
Lean: Reflexive, Irreflexive, Symmetric, Antisymmetric and Transitive (Mathlib), each a predicate on a relation r : α → α → Prop.
Example P1.7.4. Equality is reflexive, symmetric, antisymmetric and transitive. The relation on numbers is reflexive, antisymmetric and transitive, and it is not symmetric. The relation is irreflexive and transitive. The relation "is a divisor of" on the positive integers is reflexive, antisymmetric and transitive. On the integers (with and negatives) it is not antisymmetric, since and . The relation "differs by exactly " on integers is symmetric and not transitive, since and and give but .
Note that antisymmetric is not the opposite of symmetric. The relation is both. The empty relation is both symmetric and antisymmetric, and it is not reflexive on a nonempty set.
Theorem P1.7.5 (counting relations). On a set with elements there are relations, reflexive relations and symmetric relations.
Proof. A relation is a subset of the pairs, and a set with elements has subsets (Chapter 4). A reflexive relation contains the pairs and is free on the other . A symmetric relation is determined by the choice, for each of the pairs and each of the unordered pairs with , of whether it is in. These are choices.
For the numbers of relations are , of reflexive relations , and of symmetric relations .
Definition P1.7.6 (inverse and composition). The inverse of is . The composition of and is .
Example P1.7.7. Let on . Then and . A relation is transitive iff : if then and must be in .
Definition P1.7.8 (equivalence relation). A relation on is an equivalence relation if it is reflexive, symmetric and transitive. The class of is .
Example P1.7.9 (congruence). Fix . The relation , defined by , is an equivalence relation on . Reflexive: . Symmetric: gives . Transitive: and give by Chapter 3. The classes are the sets of the numbers with the same remainder by the division theorem.
Lean: Int.ModEq n a b with the notation a ≡ b [ZMOD n], and Int.ModEq.refl, Int.ModEq.symm, Int.ModEq.trans (Mathlib); Setoid is the structure of an equivalence relation.
Example P1.7.10 (same gcd). On let iff . The classes are , , , , and , six classes, one for each divisor of .
Definition P1.7.11 (partition). A partition of is a set of nonempty subsets of , the blocks, that are pairwise disjoint and have union .
Theorem P1.7.12 (equivalence relations and partitions). If is an equivalence relation on , the classes of form a partition of . If is a partition of , then " and lie in the same block" is an equivalence relation whose classes are the blocks.
Proof. Let be an equivalence relation. Each class is nonempty since by reflexivity, and the union of the classes is . Two classes and are equal or disjoint. Suppose . Then and , so by symmetry and transitivity. For , gives , so , and symmetrically .
Conversely let be a partition and define iff some block contains both. It is reflexive, since each lies in a block (the union is ), symmetric by the definition, and transitive: if and for blocks and , then , so (blocks are disjoint or equal), and . The class of is the block that contains .
Example P1.7.13. The number of partitions of an -element set is the Bell number , with . So has equivalence relations: the three with one block of size , the one with all in one block, and equality.
Definition P1.7.14 (transitive closure). The transitive closure of a relation on is the smallest transitive relation that contains . It is with , and iff there is a path with .
Example P1.7.15. For , has pairs. On a set with elements, , since a path without a repeated element has at most steps.
Proof
To prove that a relation is an equivalence relation, check the three properties, each as a universal statement: for reflexivity take and show , for symmetry take with and show , for transitivity take with and . To prove that a relation is not one, give a counterexample to one of them.
A function defined on classes needs a check: a rule that takes the class and computes with a representative defines a function of the class only if the value does not depend on the representative. For congruence mod the rule "" is well defined: if and then , since . The rule "" is well defined, and the rule " is the number " is well defined. The rule " the number of digits of " is not.
Examples
Example P1.7.16 (a false proof). "Symmetry and transitivity imply reflexivity: gives , and then gives ." The error is that some may have no with . The empty relation on a nonempty set is symmetric and transitive and not reflexive. The proof is right for each that is related to something.
Example P1.7.17 (the relation of a function). For a function the relation iff is an equivalence relation on . Its classes are the nonempty fibers . For these are the six classes above. For the function , , the classes are and .
Example P1.7.18 (the closure of a cycle). Let . Then contains all nine pairs: and . The closure of a cycle is the whole relation on its elements, including the pairs .
Exercises
- [10] Decide which of reflexive, symmetric, antisymmetric and transitive hold for the relation "" on the positive integers, on the nonzero integers, and the relation "" on the real numbers.
- [12] List the pairs of the relation "" on and check that it has pairs.
- [14] Prove that is an equivalence relation on and list the classes for .
- [14] Let be a relation on . How many relations are reflexive, how many are symmetric and how many are both?
- [15] Prove that a relation is transitive iff .
- [M18] List all equivalence relations on as partitions, and show that there are .
- [M20] Prove that is the smallest transitive relation that contains , using the paths definition, and compute the closure for .
- [M22] Prove that the relation " is an integer" on the real numbers is an equivalence relation, and describe the classes.
- [M24] Show that the rule is well defined on the classes of , and compute the table of multiplication of the classes for .
- [M25] Let be reflexive and transitive on (a preorder). Prove that iff and is an equivalence relation, and that induces an antisymmetric relation on the classes.
Lean
A relation on a type is a function α → α → Prop. The properties are Reflexive r, Symmetric r, Transitive r and Antisymmetric r, and Equivalence r is the conjunction of the first three. A structure Setoid α is a type with an equivalence relation, and Quotient is the type of its classes. The transitive closure is Relation.TransGen r, with Relation.TransGen.single and Relation.TransGen.tail as its constructors.
Notes and history
The notion of a relation as a set of ordered pairs is from the logic of the nineteenth century, in the work of De Morgan, Peirce and Schroeder, and the equivalence relation and the partition are in the work of Gauss on congruences (1801). The Bell numbers are named for the mathematician E. T. Bell, who studied them in the 1930s. TAOCP uses relations in the study of partial orders in Section 2.2.3, which is the subject of Section 2 of this chapter.
Partial orders and total orders
Statement
Definition P1.7.19 (partial order). A relation on a set is a partial order if it is reflexive, antisymmetric and transitive. The pair is a partially ordered set, or poset. Write for and . A partial order is a total order (or linear order) if any two elements are comparable: or for all .
Lean: PartialOrder α and LinearOrder α (Mathlib), with le_refl, le_antisymm and le_trans as the three laws.
Example P1.7.20. The relation on is a total order. The relation "" on the positive integers is a partial order that is not total, since and . The inclusion on the subsets of a set is a partial order, and it is not total when the set has two elements or more. The relation "is a prefix of" on strings is a partial order.
Example P1.7.21 (the order on the divisors). The divisors of are , ordered by divisibility. The element is not comparable to , nor to . The elements and are not comparable.
Definition P1.7.22 (the diagram of Hasse). In a finite poset, covers if and no has . The Hasse diagram draws each element as a point and joins to by a line, with higher, when covers . The order is recovered from the diagram: iff one can go from up to along lines.
Example P1.7.23. For the divisors of under divisibility the covering pairs are , , , , , , , seven lines. For the divisors of there are elements and lines, the edges of a cube.
Definition P1.7.24 (extreme elements). Let in a poset. An element is minimal in if no has , and least in if for all . Maximal and greatest are defined in the same way for the reverse order.
Every least element is minimal, and a set has at most one least element. The converse fails.
Example P1.7.25. Let under divisibility. The minimal elements are and , and there is no least element, since neither divides the other. The maximal elements are and , and there is no greatest element.
Theorem P1.7.26 (a finite poset has a minimal element). Every nonempty finite poset has a minimal element.
Proof. Take . If it is not minimal, there is , and so on. The elements are all different, since for by transitivity and is false. A finite set has no infinite list of different elements, so the process stops at a minimal element.
Theorem P1.7.27 (linear extension, topological sorting). Let be a partial order on a finite set . There is a total order on that extends it: implies . Equivalently, the elements of can be listed as so that implies .
Proof. Induction on . For the list is empty. Let . By the previous theorem, has a minimal element . The set with the restricted order has elements, and by the hypothesis it has a list that fits the order. Put first: . If with and , then , so by minimality, contrary to . So .
The proof is an algorithm. Repeat: choose a minimal element of what remains, write it down, and remove it. The algorithm is the topological sort of TAOCP Section 2.2.3, where the relation is "must come before" among the steps of a job.
Example P1.7.28. The order of divisibility on has the linear extension (the usual order) and also . A computer count finds linear extensions of this order. The divisors of have linear extensions, and those of have .
Example P1.7.29 (a schedule). A job has the steps with the requirements: before , before , before , before . The minimal elements at the start are and . One order that fits is . Another is . The relation has no cycle, which is why an order exists. If had to come before as well, the requirements would form a cycle, and no order would fit.
Example P1.7.30 (counting partial orders). The number of partial orders on the labeled sets of elements is , by a computer count. The number of total orders on a set with elements is , since a total order is a list of the elements.
Definition P1.7.31 (product and lexicographic orders). For posets and , the product order on is iff and . The lexicographic order is iff , or and .
Theorem P1.7.32 (the lexicographic order). If and are totally ordered, the lexicographic order on is a total order, and it extends the product order.
Proof. Reflexive: by the second case. Antisymmetric: if and the reverse, then and cannot both hold, nor can one of them hold while the other case applies, so and then and give . Transitive: a case check on the two first coordinates. Total: if one of and holds, and if compare and . Extension: if and , then either and , or .
Example P1.7.33. On the product order makes of the pairs of different elements comparable, and the lexicographic order makes all comparable. The words of a dictionary are ordered by the lexicographic order on strings: .
Proof
To prove that a relation is a partial order, check the three laws. To prove that it is total, take two elements and show that they are comparable, with cases. To prove that an element is minimal, assume a smaller element exists and derive a contradiction. To prove that an element is least, compare it to each element of the set.
The dual of a poset has the reverse order, and each theorem about minimal elements has a dual for maximal elements. A proof of the second is the proof of the first with the order reversed, so that it need not be written twice.
A common mistake is to confuse minimal and least. A set with two minimal elements has no least element, as the example shows. The linear extension theorem needs a minimal element, not a least one, and the poset of the example has the linear extensions and , and .
Examples
Example P1.7.34 (a poset of subsets). The subsets of under inclusion form a poset with elements. The Hasse diagram is a cube with lines, with at the bottom and at the top. The minimal element of the whole set is , which is also the least. The sets of one element are the minimal elements of the nonempty subsets, and there is no least element of them. The number of linear extensions is , the same as for the divisors of , because the two posets are the same cube under the correspondence , , .
Example P1.7.35 (a chain and an antichain). A chain is a set of pairwise comparable elements, and an antichain is a set of pairwise incomparable elements. In the divisors of , the set is a chain and is an antichain. In a poset of elements with the longest chain of elements, there is an antichain of size at least . The proof partitions the poset into the sets of elements whose longest chain below ends at height , and each such set is an antichain, so one has at least elements.
Example P1.7.36 (a relation that is not a partial order). Let mean on the integers. It is reflexive. It is not antisymmetric, since and and . It is not transitive either: but .
Exercises
- [10] Decide which of the relations are partial orders, and which are total: "" on the positive integers, "" on the reals, "" on subsets of , "" on the integers.
- [12] Draw the Hasse diagram of the divisors of and list the minimal and maximal elements of the set of them.
- [14] Find a linear extension of the divisibility order on .
- [14] Prove that a least element of a set is minimal, and that a least element is unique. Give an example of a minimal element that is not least.
- [15] Prove that every nonempty finite poset has a maximal element, by the dual of the proof in the text.
- [M18] A job has the steps with before , before , before , before , before , and before . List all topological orders.
- [M20] Prove that the lexicographic order on pairs from two total orders is transitive.
- [M22] Prove that in a finite poset with a longest chain of elements there is an antichain of at least elements, where is the number of elements.
- [M25] Prove that a relation on a finite set that is irreflexive and transitive is a strict partial order that has no cycle, and that the relation "must precede" of a job can be extended to a total order iff it has no cycle.
- [M25] Count the linear extensions of the poset on with , , , and find a formula for the number of linear extensions of a poset that consists of elements with no relations.
Lean
PartialOrder α has the laws le_refl, le_trans and le_antisymm, and LinearOrder α adds le_total. The order of divisibility on ℕ is Dvd, with dvd_refl, dvd_trans and Nat.dvd_antisymm as the laws. The theorems of this section about finite posets are proved by the induction of the text, and the reader can state them in Lean as exercises without relying on a library name.
Notes and history
The diagram is named for Helmut Hasse, who used it in the 1920s although it is older. The existence of a linear extension is simple for a finite poset. For infinite posets it is the theorem of Szpilrajn (1930), which needs the axiom of choice in some form. The topological sort is in TAOCP Section 2.2.3, with the algorithm and its analysis.
Well-founded relations and termination
Statement
Definition P1.7.37 (well-founded relation). A relation on a set is well-founded if there is no infinite sequence in with for all . Such a sequence is an infinite descending chain.
Lean: WellFounded r and WellFoundedLT α (Lean core and Mathlib), and WellFounded.induction for the principle of this section.
Example P1.7.38. The relation on is well-founded: a descending chain of naturals has at most steps. The relation on and on the positive reals is not, since and go down for ever. The relation " is a proper divisor of " on the positive integers is well-founded, since a proper divisor is smaller. The relation " is a proper substring of " on strings is well-founded, since it makes the length smaller.
Theorem P1.7.39 (the minimal element form). A relation on is well-founded iff every nonempty subset has a minimal element, an such that no has .
Proof. Let be well-founded, and suppose that a nonempty has no minimal element. Choose . Since is not minimal, there is with , and so on. This is an infinite descending chain, a contradiction. (The choices are made one after the other, the step that the principle of dependent choice allows.) Conversely, let every nonempty subset have a minimal element, and let be a descending chain. The set has a minimal element , but lies in the set, a contradiction.
For the relation on this is the well-ordering principle of Chapter 5.
Theorem P1.7.40 (well-founded induction). Let be well-founded on and let be a property. If for every then holds for all .
Proof. Suppose that the set of for which is false is nonempty. Let be a minimal element of . Then is true for all , and by the hypothesis is true, so , a contradiction.
Strong induction on is the case , and structural induction of Chapter 6 is the case where means that is a part of . In the induction step there is no base case to write. An element with no is handled by the same step, with the condition on empty.
Definition P1.7.41 (lexicographic product). For relations on and on , define iff , or and .
Theorem P1.7.42. If and are well-founded, so is on .
Proof. Let be nonempty. The set of first coordinates of is nonempty, so it has a minimal element . The set is nonempty, so it has a minimal element . Then is minimal in : if lies in , then either , which contradicts the choice of , or and , which contradicts the choice of .
The same proof works for the triples and for the -tuples, by induction on . Note that the lexicographic order does not need the descent in to be bounded: from one can go to in one step. The steps are finite in number, but there is no bound that depends on the start alone.
Example P1.7.43. On the pair is above , and , and each descending chain is finite.
Proof
Termination by a measure. To prove that a process ends, find a well-founded set and a measure that maps each state of the process to an element of , so that each step takes a state to a state with . An infinite run would give an infinite descending chain, so the process ends. For many processes the set is , and the measure is a quantity that is a natural number and falls at each step. The variant of a loop in Chapter 8 is this measure.
Example P1.7.44 (Euclid). For Algorithm 1.1E on the measure is . A pass takes with to , and .
Example P1.7.45 (a process on strings). Start with a string over . A step replaces one letter by the two letters , or deletes one letter . Take the measure in the lexicographic order on . Replacing by lowers the first coordinate and raises the second, so the pair goes down. Deleting keeps the first and lowers the second. So every sequence of steps is finite. A sum of the two coordinates would not work, since the first kind of step can raise it.
Example P1.7.46 (the Ackermann function). Define , and . Each call is on a pair, and each recursive call is on a pair that is smaller in the lexicographic order of : and by the first coordinate, and by the second. For the inner call the second argument is large, but the first coordinate fell. By the theorem of the lexicographic product, the recursion ends for every input, so is a total function. The values are , , , and , , . The evaluation of takes calls and that of takes .
Example P1.7.47 (a process with no known measure). Collatz: replace by if is even and by if is odd, and stop at . No measure into a well-founded set is known that proves that this ends for all . For it takes steps. The process is a reminder that the existence of a measure is the whole content of a termination proof.
Examples
Example P1.7.48 (a relation that is not well-founded). Let iff on . Then means , and is an infinite descending chain. The subset has no minimal element for this relation. By contrast, " is a proper divisor of " is well-founded.
Example P1.7.49 (the shortlex order). Order the strings over a finite alphabet first by length and then by the dictionary order. By the lexicographic product of with a total order on the strings of the same length (finite), this order is well-founded. There are finitely many strings of each length, so every string has finitely many predecessors, which is the property that a dictionary of the strings needs. Over there are strings of length at most .
Example P1.7.50 (the dictionary order is not well-founded). The pure lexicographic order on strings over , with , has the infinite descending chain . The first coordinate cannot be bounded when the lengths vary. This is why the shortlex order, and not the dictionary order, is used for a proof by induction on strings.
Exercises
- [10] Decide which of the relations are well-founded: on , on , on the non-negative reals, (proper divisor) on the positive integers.
- [12] Prove that a subset of a well-founded set with the restricted relation is well-founded.
- [14] Show with a measure that the loop "while : or " ends for when the loop stops at (state the loop and the measure).
- [14] Evaluate and for the function of Ackermann by hand and check against the formulas in the text.
- [15] Prove that and by induction on .
- [M18] Prove that the relation " is a proper prefix of " on strings is well-founded, and so is " is a proper suffix of ".
- [M20] Prove that the lexicographic product of three well-founded sets is well-founded, using the theorem for two.
- [M22] Prove that , and that for all and .
- [M25] A process has a state of two naturals. A step takes with to , or takes with to for any natural that the step chooses. Show that every run ends, and that there is no bound on the length of a run that depends on the start alone.
- [M25] Prove that a relation on is well-founded iff well-founded induction holds for it, in the sense that every property that passes the induction step holds for all .
Lean
WellFounded r states that is well-founded, and WellFounded.induction is the principle of this section. Recursion that ends by a measure is written in Lean as a def with the clause termination_by, and the clause decreasing_by gives the proof of the decrease when Lean cannot find it. The function of Ackermann is a standard test of this feature, with the pair of the two arguments as the measure.
Notes and history
Ackermann (1928) gave a function of two arguments that grows faster than every function defined by the plain recursion of the primitive recursive kind, and the form in the text is a later simplification. TAOCP proves each algorithm correct with the aid of a quantity that falls, as Chapter 8 shows. The Collatz process is an open problem.
A relation says which pairs of objects belong together. Equality, "less than", "divides", "is congruent to modulo " and "must be done before" are relations. This chapter gives the language to speak about them, and the three kinds that a programmer meets most: the equivalence relation, the partial order and the well-founded relation.
Section 1 defines a relation as a set of pairs, lists its properties (reflexive, symmetric, antisymmetric, transitive), counts the relations on a small set, and proves that an equivalence relation is the same thing as a partition. It ends with the composition of relations and the transitive closure. Section 2 treats the partial orders and the total orders, the diagram of Hasse, the least and the minimal elements, and the theorem that every finite partial order can be extended to a total order, which is the problem of TAOCP Section 2.2.3, the topological sort. Section 3 treats the well-founded relations, the induction that goes with them, the lexicographic order and its use to prove that a procedure ends.
The chapter assumes Chapters 1 to 6. The spine texts are Velleman, How to Prove It, Chapters 4 and 5, and Lehman, Leighton and Meyer, Mathematics for Computer Science, Chapters 9 and 10. Chapter 8 uses the well-founded relations of Section 3 to prove that loops end.
The source of this page. Something is wrong? Report it.