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

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
  1. Relations and equivalence relations
  2. Partial orders and total orders
  3. Well-founded relations and termination

Relations and equivalence relations

Statement

Definition P1.7.1 (relation). A relation from a set AA to a set BB is a subset R⊆A×BR \subseteq A \times B. A relation on AA is a subset of A×AA \times A. Write aRba \mathrel{R} b for (a,b)∈R(a, b) \in R. A function f:A→Bf : A \to B is a relation in which each aa has exactly one bb with aRba \mathrel{R} b.

Lean: Set (α × β) or the type α → β → Prop; Relation and Rel α β in Mathlib.

Example P1.7.2. On {1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\} the relation "divides" is the set of pairs (a,b)(a, b) with a∣ba \mid b, and it has 1414 pairs. The relation "<<" on the same set has 1515 pairs. The relation "has the same parity" has 1818 pairs.

Definition P1.7.3 (properties). A relation RR on AA is

  • reflexive if aRaa \mathrel{R} a for every a∈Aa \in A;
  • irreflexive if aRaa \mathrel{R} a for no aa;
  • symmetric if aRba \mathrel{R} b implies bRab \mathrel{R} a;
  • antisymmetric if aRba \mathrel{R} b and bRab \mathrel{R} a imply a=ba = b;
  • transitive if aRba \mathrel{R} b and bRcb \mathrel{R} c imply aRca \mathrel{R} c.

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 ≤\le 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 00 and negatives) it is not antisymmetric, since 2∣−22 \mid -2 and −2∣2-2 \mid 2. The relation "differs by exactly 11" on integers is symmetric and not transitive, since 11 and 22 and 33 give 1∼2∼31 \sim 2 \sim 3 but 1≁31 \not\sim 3.

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 nn elements there are 2n22^{n^2} relations, 2n2−n2^{n^2 - n} reflexive relations and 2n(n+1)/22^{n(n+1)/2} symmetric relations.

Proof. A relation is a subset of the n2n^2 pairs, and a set with n2n^2 elements has 2n22^{n^2} subsets (Chapter 4). A reflexive relation contains the nn pairs (a,a)(a, a) and is free on the other n2−nn^2 - n. A symmetric relation is determined by the choice, for each of the nn pairs (a,a)(a, a) and each of the n(n−1)/2n(n - 1)/2 unordered pairs {a,b}\{a, b\} with a≠ba \ne b, of whether it is in. These are n+n(n−1)/2=n(n+1)/2n + n(n - 1)/2 = n(n + 1)/2 choices. □\square

For n=1,2,3,4n = 1, 2, 3, 4 the numbers of relations are 2,16,512,655362, 16, 512, 65536, of reflexive relations 1,4,64,40961, 4, 64, 4096, and of symmetric relations 2,8,64,10242, 8, 64, 1024.

Definition P1.7.6 (inverse and composition). The inverse of R⊆A×BR \subseteq A \times B is R−1={(b,a):(a,b)∈R}R^{-1} = \{(b, a) : (a, b) \in R\}. The composition of R⊆A×BR \subseteq A \times B and S⊆B×CS \subseteq B \times C is S∘R={(a,c):there is b with aRb and bSc}S \circ R = \{(a, c) : \text{there is } b \text{ with } a \mathrel{R} b \text{ and } b \mathrel{S} c\}.

Example P1.7.7. Let R={(1,2),(2,3)}R = \{(1, 2), (2, 3)\} on {1,2,3}\{1, 2, 3\}. Then R∘R={(1,3)}R \circ R = \{(1, 3)\} and R−1={(2,1),(3,2)}R^{-1} = \{(2, 1), (3, 2)\}. A relation RR is transitive iff R∘R⊆RR \circ R \subseteq R: if aRbRca \mathrel{R} b \mathrel{R} c then (a,c)∈R∘R(a, c) \in R \circ R and must be in RR.

Definition P1.7.8 (equivalence relation). A relation on AA is an equivalence relation if it is reflexive, symmetric and transitive. The class of aa is [a]={x∈A:xRa}[a] = \{x \in A : x \mathrel{R} a\}.

Example P1.7.9 (congruence). Fix n≥1n \ge 1. The relation a≡b(modn)a \equiv b \pmod n, defined by n∣a−bn \mid a - b, is an equivalence relation on ℤ\mathbb Z. Reflexive: n∣0n \mid 0. Symmetric: n∣a−bn \mid a - b gives n∣b−an \mid b - a. Transitive: n∣a−bn \mid a - b and n∣b−cn \mid b - c give n∣a−cn \mid a - c by Chapter 3. The classes are the nn 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 {1,…,12}\{1, \dots, 12\} let a∼ba \sim b iff gcd⁡(a,12)=gcd⁡(b,12)\gcd(a, 12) = \gcd(b, 12). The classes are {1,5,7,11}\{1, 5, 7, 11\}, {2,10}\{2, 10\}, {3,9}\{3, 9\}, {4,8}\{4, 8\}, {6}\{6\} and {12}\{12\}, six classes, one for each divisor of 1212.

Definition P1.7.11 (partition). A partition of AA is a set of nonempty subsets of AA, the blocks, that are pairwise disjoint and have union AA.

Theorem P1.7.12 (equivalence relations and partitions). If RR is an equivalence relation on AA, the classes of RR form a partition of AA. If 𝒫\mathcal P is a partition of AA, then "aa and bb lie in the same block" is an equivalence relation whose classes are the blocks.

Proof. Let RR be an equivalence relation. Each class [a][a] is nonempty since a∈[a]a \in [a] by reflexivity, and the union of the classes is AA. Two classes [a][a] and [b][b] are equal or disjoint. Suppose c∈[a]∩[b]c \in [a] \cap [b]. Then cRac \mathrel{R} a and cRbc \mathrel{R} b, so aRba \mathrel{R} b by symmetry and transitivity. For x∈[a]x \in [a], xRaRbx \mathrel{R} a \mathrel{R} b gives x∈[b]x \in [b], so [a]⊆[b][a] \subseteq [b], and symmetrically [b]⊆[a][b] \subseteq [a].

Conversely let 𝒫\mathcal P be a partition and define a∼ba \sim b iff some block contains both. It is reflexive, since each aa lies in a block (the union is AA), symmetric by the definition, and transitive: if a,b∈Xa, b \in X and b,c∈Yb, c \in Y for blocks XX and YY, then b∈X∩Yb \in X \cap Y, so X=YX = Y (blocks are disjoint or equal), and a,c∈Xa, c \in X. The class of aa is the block that contains aa. □\square

Example P1.7.13. The number of partitions of an nn-element set is the Bell number BnB_n, with B0,…,B6=1,1,2,5,15,52,203B_0, \dots, B_6 = 1, 1, 2, 5, 15, 52, 203. So {1,2,3}\{1, 2, 3\} has 55 equivalence relations: the three with one block of size 22, the one with all in one block, and equality.

Definition P1.7.14 (transitive closure). The transitive closure of a relation RR on AA is the smallest transitive relation R+R^+ that contains RR. It is R+=R∪R2∪R3∪⋯R^+ = R \cup R^2 \cup R^3 \cup \cdots with Rk+1=R∘RkR^{k+1} = R \circ R^k, and aR+ba \mathrel{R^+} b iff there is a path a=x0Rx1R⋯Rxk=ba = x_0 \mathrel{R} x_1 \mathrel{R} \cdots \mathrel{R} x_k = b with k≥1k \ge 1.

Example P1.7.15. For R={(1,2),(2,3),(3,4)}R = \{(1, 2), (2, 3), (3, 4)\}, R+=R∪{(1,3),(2,4)}∪{(1,4)}R^+ = R \cup \{(1, 3), (2, 4)\} \cup \{(1, 4)\} has 66 pairs. On a set with nn elements, R+=R∪R2∪⋯∪RnR^+ = R \cup R^2 \cup \cdots \cup R^n, since a path without a repeated element has at most nn steps.

Proof

To prove that a relation is an equivalence relation, check the three properties, each as a universal statement: for reflexivity take aa and show aRaa \mathrel{R} a, for symmetry take a,ba, b with aRba \mathrel{R} b and show bRab \mathrel{R} a, for transitivity take a,b,ca, b, c with aRba \mathrel{R} b and bRcb \mathrel{R} c. 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 [a][a] and computes with a representative aa defines a function of the class only if the value does not depend on the representative. For congruence mod nn the rule "[a]+[b]=[a+b][a] + [b] = [a + b]" is well defined: if a≡a′a \equiv a' and b≡b′b \equiv b' then a+b≡a′+b′a + b \equiv a' + b', since n∣(a−a′)+(b−b′)n \mid (a - a') + (b - b'). The rule "[a]2[a]^2" is well defined, and the rule "[a][a] is the number a mod na \bmod n" is well defined. The rule "[a]↦[a] \mapsto the number of digits of aa" is not.

Examples

Example P1.7.16 (a false proof). "Symmetry and transitivity imply reflexivity: aRba \mathrel{R} b gives bRab \mathrel{R} a, and then aRbRaa \mathrel{R} b \mathrel{R} a gives aRaa \mathrel{R} a." The error is that some aa may have no bb with aRba \mathrel{R} b. The empty relation on a nonempty set is symmetric and transitive and not reflexive. The proof is right for each aa that is related to something.

Example P1.7.17 (the relation of a function). For a function f:A→Bf : A \to B the relation a∼a′a \sim a' iff f(a)=f(a′)f(a) = f(a') is an equivalence relation on AA. Its classes are the nonempty fibers f−1({b})f^{-1}(\{b\}). For f(a)=gcd⁡(a,12)f(a) = \gcd(a, 12) these are the six classes above. For the function f:ℤ→ℤf : \mathbb Z \to \mathbb Z, f(a)=a2f(a) = a^2, the classes are {0}\{0\} and {a,−a}\{a, -a\}.

Example P1.7.18 (the closure of a cycle). Let R={(1,2),(2,3),(3,1)}R = \{(1, 2), (2, 3), (3, 1)\}. Then R+R^+ contains all nine pairs: R2={(1,3),(2,1),(3,2)}R^2 = \{(1, 3), (2, 1), (3, 2)\} and R3={(1,1),(2,2),(3,3)}R^3 = \{(1, 1), (2, 2), (3, 3)\}. The closure of a cycle is the whole relation on its elements, including the pairs (a,a)(a, a).

Exercises

  1. [10] Decide which of reflexive, symmetric, antisymmetric and transitive hold for the relation "a∣ba \mid b" on the positive integers, on the nonzero integers, and the relation "a<ba < b" on the real numbers.
  2. [12] List the pairs of the relation "a∣ba \mid b" on {1,…,6}\{1, \dots, 6\} and check that it has 1414 pairs.
  3. [14] Prove that ≡(modn)\equiv \pmod n is an equivalence relation on ℤ\mathbb Z and list the classes for n=4n = 4.
  4. [14] Let RR be a relation on {1,2,3}\{1, 2, 3\}. How many relations are reflexive, how many are symmetric and how many are both?
  5. [15] Prove that a relation RR is transitive iff R∘R⊆RR \circ R \subseteq R.
  6. [M18] List all equivalence relations on {1,2,3}\{1, 2, 3\} as partitions, and show that there are 55.
  7. [M20] Prove that R+R^+ is the smallest transitive relation that contains RR, using the paths definition, and compute the closure for R={(1,2),(2,3),(3,4),(4,2)}R = \{(1, 2), (2, 3), (3, 4), (4, 2)\}.
  8. [M22] Prove that the relation "a−ba - b is an integer" on the real numbers is an equivalence relation, and describe the classes.
  9. [M24] Show that the rule [a]⋅[b]=[ab][a] \cdot [b] = [ab] is well defined on the classes of ≡(modn)\equiv \pmod n, and compute the table of multiplication of the classes for n=4n = 4.
  10. [M25] Let RR be reflexive and transitive on AA (a preorder). Prove that a∼ba \sim b iff aRba \mathrel{R} b and bRab \mathrel{R} a is an equivalence relation, and that RR 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 ⪯\preceq on a set AA is a partial order if it is reflexive, antisymmetric and transitive. The pair (A,⪯)(A, \preceq) is a partially ordered set, or poset. Write a≺ba \prec b for a⪯ba \preceq b and a≠ba \ne b. A partial order is a total order (or linear order) if any two elements are comparable: a⪯ba \preceq b or b⪯ab \preceq a for all a,ba, b.

Lean: PartialOrder α and LinearOrder α (Mathlib), with le_refl, le_antisymm and le_trans as the three laws.

Example P1.7.20. The relation ≤\le on ℤ\mathbb Z is a total order. The relation "a∣ba \mid b" on the positive integers is a partial order that is not total, since 2∤32 \nmid 3 and 3∤23 \nmid 2. The inclusion ⊆\subseteq 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 1212 are 1,2,3,4,6,121, 2, 3, 4, 6, 12, ordered by divisibility. The element 44 is not comparable to 33, nor to 66. The elements 22 and 33 are not comparable.

Definition P1.7.22 (the diagram of Hasse). In a finite poset, bb covers aa if a≺ba \prec b and no cc has a≺c≺ba \prec c \prec b. The Hasse diagram draws each element as a point and joins aa to bb by a line, with bb higher, when bb covers aa. The order is recovered from the diagram: a⪯ba \preceq b iff one can go from aa up to bb along lines.

Example P1.7.23. For the divisors of 1212 under divisibility the covering pairs are (1,2)(1, 2), (1,3)(1, 3), (2,4)(2, 4), (2,6)(2, 6), (3,6)(3, 6), (4,12)(4, 12), (6,12)(6, 12), seven lines. For the divisors of 3030 there are 88 elements and 1212 lines, the edges of a cube.

Definition P1.7.24 (extreme elements). Let S⊆AS \subseteq A in a poset. An element m∈Sm \in S is minimal in SS if no s∈Ss \in S has s≺ms \prec m, and least in SS if m⪯sm \preceq s for all s∈Ss \in S. 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 S={2,3,4,6}S = \{2, 3, 4, 6\} under divisibility. The minimal elements are 22 and 33, and there is no least element, since neither divides the other. The maximal elements are 44 and 66, 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 a1∈Aa_1 \in A. If it is not minimal, there is a2≺a1a_2 \prec a_1, and so on. The elements a1,a2,…a_1, a_2, \dots are all different, since aj≺aia_j \prec a_i for j>ij > i by transitivity and ai≺aia_i \prec a_i is false. A finite set has no infinite list of different elements, so the process stops at a minimal element. □\square

Theorem P1.7.27 (linear extension, topological sorting). Let ⪯\preceq be a partial order on a finite set AA. There is a total order ≤\le on AA that extends it: a⪯ba \preceq b implies a≤ba \le b. Equivalently, the elements of AA can be listed as a1,a2,…,ana_1, a_2, \dots, a_n so that ai⪯aja_i \preceq a_j implies i≤ji \le j.

Proof. Induction on n=∣A∣n = |A|. For n=0n = 0 the list is empty. Let n≥1n \ge 1. By the previous theorem, AA has a minimal element mm. The set A∖{m}A \setminus \{m\} with the restricted order has n−1n - 1 elements, and by the hypothesis it has a list a2,…,ana_2, \dots, a_n that fits the order. Put mm first: a1=ma_1 = m. If ai⪯aja_i \preceq a_j with i≠ji \ne j and j=1j = 1, then ai⪯ma_i \preceq m, so ai=ma_i = m by minimality, contrary to i≠1i \ne 1. So i≤ji \le j. □\square

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 {1,…,6}\{1, \dots, 6\} has the linear extension 1,2,3,4,5,61, 2, 3, 4, 5, 6 (the usual order) and also 1,5,2,3,4,61, 5, 2, 3, 4, 6. A computer count finds 2525 linear extensions of this order. The divisors of 1212 have 55 linear extensions, and those of 3030 have 4848.

Example P1.7.29 (a schedule). A job has the steps a,b,c,d,ea, b, c, d, e with the requirements: aa before cc, bb before cc, cc before dd, bb before ee. The minimal elements at the start are aa and bb. One order that fits is a,b,c,d,ea, b, c, d, e. Another is b,e,a,c,db, e, a, c, d. The relation has no cycle, which is why an order exists. If dd had to come before aa as well, the requirements a,c,d,aa, c, d, a 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 1,2,3,41, 2, 3, 4 elements is 1,3,19,2191, 3, 19, 219, by a computer count. The number of total orders on a set with nn elements is n!n!, since a total order is a list of the elements.

Definition P1.7.31 (product and lexicographic orders). For posets (A,⪯A)(A, \preceq_A) and (B,⪯B)(B, \preceq_B), the product order on A×BA \times B is (a,b)⪯(a′,b′)(a, b) \preceq (a', b') iff a⪯Aa′a \preceq_A a' and b⪯Bb′b \preceq_B b'. The lexicographic order is (a,b)⪯lex(a′,b′)(a, b) \preceq_{\mathrm{lex}} (a', b') iff a≺Aa′a \prec_A a', or a=a′a = a' and b⪯Bb′b \preceq_B b'.

Theorem P1.7.32 (the lexicographic order). If AA and BB are totally ordered, the lexicographic order on A×BA \times B is a total order, and it extends the product order.

Proof. Reflexive: (a,b)⪯lex(a,b)(a, b) \preceq_{\mathrm{lex}} (a, b) by the second case. Antisymmetric: if (a,b)⪯lex(a′,b′)(a, b) \preceq_{\mathrm{lex}} (a', b') and the reverse, then a≺a′a \prec a' and a′≺aa' \prec a cannot both hold, nor can one of them hold while the other case a=a′a = a' applies, so a=a′a = a' and then b⪯b′b \preceq b' and b′⪯bb' \preceq b give b=b′b = b'. Transitive: a case check on the two first coordinates. Total: if a≠a′a \ne a' one of a≺a′a \prec a' and a′≺aa' \prec a holds, and if a=a′a = a' compare bb and b′b'. Extension: if a⪯a′a \preceq a' and b⪯b′b \preceq b', then either a=a′a = a' and b⪯b′b \preceq b', or a≺a′a \prec a'. □\square

Example P1.7.33. On {0,1,2,3}×{0,1,2,3}\{0, 1, 2, 3\} \times \{0, 1, 2, 3\} the product order makes 8484 of the 16⋅15/2=12016 \cdot 15 / 2 = 120 pairs of different elements comparable, and the lexicographic order makes all 120120 comparable. The words of a dictionary are ordered by the lexicographic order on strings: ε<a<ab<abc<b<ba\varepsilon < a < ab < abc < b < ba.

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 {2,3,4,6}\{2, 3, 4, 6\} shows. The linear extension theorem needs a minimal element, not a least one, and the poset of the example {2,3,4,6}\{2, 3, 4, 6\} has the linear extensions 2,3,4,62, 3, 4, 6 and 2,3,6,42, 3, 6, 4, 3,2,4,63, 2, 4, 6 and 3,2,6,43, 2, 6, 4.

Examples

Example P1.7.34 (a poset of subsets). The subsets of {1,2,3}\{1, 2, 3\} under inclusion form a poset with 88 elements. The Hasse diagram is a cube with 1212 lines, with ∅\emptyset at the bottom and {1,2,3}\{1, 2, 3\} at the top. The minimal element of the whole set is ∅\emptyset, 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 4848, the same as for the divisors of 3030, because the two posets are the same cube under the correspondence 2↔12 \leftrightarrow 1, 3↔23 \leftrightarrow 2, 5↔35 \leftrightarrow 3.

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 3030, the set {1,2,6,30}\{1, 2, 6, 30\} is a chain and {2,3,5}\{2, 3, 5\} is an antichain. In a poset of nn elements with the longest chain of cc elements, there is an antichain of size at least n/cn / c. The proof partitions the poset into the sets of elements whose longest chain below ends at height 1,2,…,c1, 2, \dots, c, and each such set is an antichain, so one has at least n/cn / c elements.

Example P1.7.36 (a relation that is not a partial order). Let a⊑ba \sqsubseteq b mean a≤b+1a \le b + 1 on the integers. It is reflexive. It is not antisymmetric, since 1⊑21 \sqsubseteq 2 and 2⊑12 \sqsubseteq 1 and 1≠21 \ne 2. It is not transitive either: 3⊑2⊑13 \sqsubseteq 2 \sqsubseteq 1 but 3⋢13 \not\sqsubseteq 1.

Exercises

  1. [10] Decide which of the relations are partial orders, and which are total: "a∣ba \mid b" on the positive integers, "a≤ba \le b" on the reals, "A⊆BA \subseteq B" on subsets of {1,2}\{1, 2\}, "a<ba < b" on the integers.
  2. [12] Draw the Hasse diagram of the divisors of 1212 and list the minimal and maximal elements of the set {2,3,4,6}\{2, 3, 4, 6\} of them.
  3. [14] Find a linear extension of the divisibility order on {1,…,10}\{1, \dots, 10\}.
  4. [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.
  5. [15] Prove that every nonempty finite poset has a maximal element, by the dual of the proof in the text.
  6. [M18] A job has the steps a,…,fa, \dots, f with aa before bb, aa before cc, bb before dd, cc before dd, dd before ee, and cc before ff. List all topological orders.
  7. [M20] Prove that the lexicographic order on pairs from two total orders is transitive.
  8. [M22] Prove that in a finite poset with a longest chain of cc elements there is an antichain of at least n/cn / c elements, where nn is the number of elements.
  9. [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.
  10. [M25] Count the linear extensions of the poset on {a,b,c,d}\{a, b, c, d\} with a≺ca \prec c, b≺cb \prec c, b≺db \prec d, and find a formula for the number of linear extensions of a poset that consists of nn 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 ≺\prec on a set AA is well-founded if there is no infinite sequence a0,a1,a2,…a_0, a_1, a_2, \dots in AA with ai+1≺aia_{i+1} \prec a_i for all ii. 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 ℕ\mathbb N is well-founded: a descending chain of naturals has at most a0a_0 steps. The relation << on ℤ\mathbb Z and on the positive reals is not, since 0>−1>−2>⋯0 > -1 > -2 > \cdots and 1>1/2>1/4>⋯1 > 1/2 > 1/4 > \cdots go down for ever. The relation "aa is a proper divisor of bb" on the positive integers is well-founded, since a proper divisor is smaller. The relation "xx is a proper substring of yy" on strings is well-founded, since it makes the length smaller.

Theorem P1.7.39 (the minimal element form). A relation ≺\prec on AA is well-founded iff every nonempty subset S⊆AS \subseteq A has a minimal element, an m∈Sm \in S such that no s∈Ss \in S has s≺ms \prec m.

Proof. Let ≺\prec be well-founded, and suppose that a nonempty SS has no minimal element. Choose a0∈Sa_0 \in S. Since a0a_0 is not minimal, there is a1∈Sa_1 \in S with a1≺a0a_1 \prec a_0, 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 a0,a1,…a_0, a_1, \dots be a descending chain. The set {a0,a1,… }\{a_0, a_1, \dots\} has a minimal element aka_k, but ak+1≺aka_{k+1} \prec a_k lies in the set, a contradiction. □\square

For the relation << on ℕ\mathbb N this is the well-ordering principle of Chapter 5.

Theorem P1.7.40 (well-founded induction). Let ≺\prec be well-founded on AA and let PP be a property. If for every a∈Aa \in A (for all b≺a, P(b))  ⟹  P(a),\bigl(\text{for all } b \prec a,\ P(b)\bigr) \implies P(a), then P(a)P(a) holds for all a∈Aa \in A.

Proof. Suppose that the set SS of aa for which P(a)P(a) is false is nonempty. Let mm be a minimal element of SS. Then P(b)P(b) is true for all b≺mb \prec m, and by the hypothesis P(m)P(m) is true, so m∉Sm \notin S, a contradiction. □\square

Strong induction on ℕ\mathbb N is the case ≺ =<\prec\ = <, and structural induction of Chapter 6 is the case where b≺ab \prec a means that bb is a part of aa. In the induction step there is no base case to write. An element with no b≺ab \prec a is handled by the same step, with the condition on bb empty.

Definition P1.7.41 (lexicographic product). For relations ≺A\prec_A on AA and ≺B\prec_B on BB, define (a,b)≺lex(a′,b′)(a, b) \prec_{\mathrm{lex}} (a', b') iff a≺Aa′a \prec_A a', or a=a′a = a' and b≺Bb′b \prec_B b'.

Theorem P1.7.42. If ≺A\prec_A and ≺B\prec_B are well-founded, so is ≺lex\prec_{\mathrm{lex}} on A×BA \times B.

Proof. Let S⊆A×BS \subseteq A \times B be nonempty. The set of first coordinates of SS is nonempty, so it has a minimal element aa. The set {b:(a,b)∈S}\{b : (a, b) \in S\} is nonempty, so it has a minimal element bb. Then (a,b)(a, b) is minimal in SS: if (a′,b′)≺lex(a,b)(a', b') \prec_{\mathrm{lex}} (a, b) lies in SS, then either a′≺aa' \prec a, which contradicts the choice of aa, or a′=aa' = a and b′≺bb' \prec b, which contradicts the choice of bb. □\square

The same proof works for the triples and for the kk-tuples, by induction on kk. Note that the lexicographic order does not need the descent in bb to be bounded: from (1,0)(1, 0) one can go to (0,1000000)(0, 1000000) 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 ℕ×ℕ\mathbb N \times \mathbb N the pair (2,5)(2, 5) is above (2,4)(2, 4), (1,100)(1, 100) and (0,7)(0, 7), and each descending chain is finite.

Proof

Termination by a measure. To prove that a process ends, find a well-founded set (A,≺)(A, \prec) and a measure μ\mu that maps each state of the process to an element of AA, so that each step takes a state ss to a state s′s' with μ(s′)≺μ(s)\mu(s') \prec \mu(s). An infinite run would give an infinite descending chain, so the process ends. For many processes the set is (ℕ,<)(\mathbb N, <), 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 (m,n)(m, n) the measure is nn. A pass takes (m,n)(m, n) with n≥1n \ge 1 to (n,m mod n)(n, m \bmod n), and m mod n<nm \bmod n < n.

Example P1.7.45 (a process on strings). Start with a string over {a,b}\{a, b\}. A step replaces one letter bb by the two letters aaaa, or deletes one letter aa. Take the measure μ(w)=(number of b, length of w)\mu(w) = (\text{number of } b,\ \text{length of } w) in the lexicographic order on ℕ×ℕ\mathbb N \times \mathbb N. Replacing bb by aaaa lowers the first coordinate and raises the second, so the pair goes down. Deleting aa 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 A(0,n)=n+1A(0, n) = n + 1, A(m+1,0)=A(m,1)A(m + 1, 0) = A(m, 1) and A(m+1,n+1)=A(m,A(m+1,n))A(m + 1, n + 1) = A(m, A(m + 1, n)). Each call is on a pair, and each recursive call is on a pair that is smaller in the lexicographic order of ℕ×ℕ\mathbb N \times \mathbb N: (m,1)≺(m+1,0)(m, 1) \prec (m + 1, 0) and (m,⋅)≺(m+1,n+1)(m, \cdot) \prec (m + 1, n + 1) by the first coordinate, and (m+1,n)≺(m+1,n+1)(m + 1, n) \prec (m + 1, n + 1) 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 AA is a total function. The values are A(1,n)=n+2A(1, n) = n + 2, A(2,n)=2n+3A(2, n) = 2n + 3, A(3,n)=2n+3−3A(3, n) = 2^{n+3} - 3, and A(2,3)=9A(2, 3) = 9, A(3,3)=61A(3, 3) = 61, A(3,4)=125A(3, 4) = 125. The evaluation of A(2,3)A(2, 3) takes 4444 calls and that of A(3,3)A(3, 3) takes 24322432.

Example P1.7.47 (a process with no known measure). Collatz: replace nn by n/2n / 2 if nn is even and by 3n+13n + 1 if nn is odd, and stop at 11. No measure into a well-founded set is known that proves that this ends for all nn. For n=27n = 27 it takes 111111 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 a≺ba \prec b iff a>ba > b on ℕ\mathbb N. Then ai+1≺aia_{i+1} \prec a_i means ai+1>aia_{i+1} > a_i, and 0,1,2,…0, 1, 2, \dots is an infinite descending chain. The subset ℕ\mathbb N has no minimal element for this relation. By contrast, "aa is a proper divisor of bb" 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 (ℕ,<)(\mathbb N, <) 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 {a,b}\{a, b\} there are 1+2+4+8=151 + 2 + 4 + 8 = 15 strings of length at most 33.

Example P1.7.50 (the dictionary order is not well-founded). The pure lexicographic order on strings over {a,b}\{a, b\}, with a<ba < b, has the infinite descending chain b>ab>aab>aaab>⋯b > ab > aab > aaab > \cdots. 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

  1. [10] Decide which of the relations are well-founded: << on ℕ\mathbb N, >> on ℕ\mathbb N, << on the non-negative reals, ∣\mid (proper divisor) on the positive integers.
  2. [12] Prove that a subset of a well-founded set with the restricted relation is well-founded.
  3. [14] Show with a measure that the loop "while n>0n > 0: n←n−3n \gets n - 3 or n←n−1n \gets n - 1" ends for n∈ℕn \in \mathbb N when the loop stops at n≤0n \le 0 (state the loop and the measure).
  4. [14] Evaluate A(2,2)A(2, 2) and A(3,2)A(3, 2) for the function of Ackermann by hand and check against the formulas in the text.
  5. [15] Prove that A(1,n)=n+2A(1, n) = n + 2 and A(2,n)=2n+3A(2, n) = 2n + 3 by induction on nn.
  6. [M18] Prove that the relation "xx is a proper prefix of yy" on strings is well-founded, and so is "xx is a proper suffix of yy".
  7. [M20] Prove that the lexicographic product of three well-founded sets is well-founded, using the theorem for two.
  8. [M22] Prove that A(3,n)=2n+3−3A(3, n) = 2^{n+3} - 3, and that A(m,n)>nA(m, n) > n for all mm and nn.
  9. [M25] A process has a state (a,b)(a, b) of two naturals. A step takes (a,b)(a, b) with b≥1b \ge 1 to (a,b−1)(a, b - 1), or takes (a,b)(a, b) with a≥1a \ge 1 to (a−1,c)(a - 1, c) for any natural cc 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 (1,0)(1, 0) alone.
  10. [M25] Prove that a relation ≺\prec on AA is well-founded iff well-founded induction holds for it, in the sense that every property that passes the induction step holds for all aa.

Lean

WellFounded r states that rr 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 nn" 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.