taocp

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

Exercise · Volume 1 · 1.2.1-13

Exercise 1.2.1-13

proved in Lean solution written

RatingM23
MarkersM (mathematical), recommended
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 1.2.1 of Volume 1. Companion chapter: Mathematical induction

The task

Add a clock TT to the extended Algorithm E and add to the assertions A1 to A6 conditions that imply T≤3nT \le 3n and keep the inductive proof valid.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Count the steps of the first pass, then of each later pass. Each later pass runs E2, E3 and E4.

Hint 2

The quantity that decreases is dd. Try a bound of the form T≤3(n−d)+kT \le 3(n - d) + k with a constant kk that depends on the arrow.

Hint 3

Choose kk so that each box keeps the bound: E2 and E3 add one each, E4 adds one and lowers dd by at least 11.

Solution

Show the solution

The clock rule is: each step E1, E2, E3 and E4 adds 11 to TT at its beginning. Add these conditions, besides the old assertions:

  • A1: T=0T = 0.
  • A2: T=1T = 1.
  • A3: T≤3(n−d)+2T \le 3(n - d) + 2.
  • A4: T≤3(n−d)+3T \le 3(n - d) + 3 and d>0d > 0.
  • A5: T≤3(n−d)+3T \le 3(n - d) + 3.
  • A6: T≤3(n−d)+1T \le 3(n - d) + 1.

Check of each box. The old conditions are kept as in the proof of the text, and only the clock conditions need work.

  • E1, from A1 to A2: TT goes from 00 to 11.
  • E2, from A2: T=2T = 2 and d=nd = n, so T=2=3(n−d)+2T = 2 = 3(n - d) + 2 and A3 holds. From A6: TT grows by 11 and dd does not change, so T≤3(n−d)+2T \le 3(n - d) + 2, which is A3.
  • E3: TT grows by 11 and dd does not change, so A3 gives T≤3(n−d)+3T \le 3(n-d) + 3. With r=0r = 0 this is A4, where d>0d > 0 because A3 has 0≤r<d0 \le r < d. With r≠0r \ne 0 it is A5.
  • E4, from A5: TT grows by 11, so T≤3(n−d)+4T \le 3(n - d) + 4 with the old dd. The new value of dd is rr and r<dr < d with integers, so dnew≤d−1d_{\text{new}} \le d - 1, that is d≥dnew+1d \ge d_{\text{new}} + 1. Then T≤3(n−dnew−1)+4=3(n−dnew)+1T \le 3(n - d_{\text{new}} - 1) + 4 = 3(n - d_{\text{new}}) + 1, which is A6.

The bound. In each assertion d≥1d \ge 1: this is clear in A3, A4 and A6, and in A5 since 0<r<d0 < r < d. At A2, T=1≤3nT = 1 \le 3n. In A3, A4, A5 and A6 the bound T≤3(n−d)+kT \le 3(n - d) + k with k≤3k \le 3 and d≥1d \ge 1 gives T≤3(n−1)+3=3nT \le 3(n-1) + 3 = 3n. So every assertion implies T≤3nT \le 3n. When the algorithm stops, A4 holds, so it stops after at most 3n3n steps. □\square

Check

Take m=3m = 3, n=2n = 2. E1 (T=1T = 1), E2 (T=2T = 2), E3 (T=3T = 3): r=1≠0r = 1 \ne 0. E4 (T=4T = 4): c=2c = 2, d=1d = 1. E2 (T=5T = 5): q=2q = 2, r=0r = 0. E3 (T=6T = 6): stop. The final T=6≤3n=6T = 6 \le 3n = 6, and A4 gives 3(n−d)+3=63(n - d) + 3 = 6, with equality. The bound is reached for n=2n = 2.

Remarks

The bound 3n3n is weak for large nn: the number of passes is O(log⁡n)O(\log n), as Section 4.5.3 shows. The clock is the usual way to turn a proof of assertions into a proof that the algorithm stops.

This solution is checked, and a person has not reviewed it yet. The source.

In Lean

proved The statement and its proof are in Lean. The kernel accepts the proof, and it uses the standard axioms only.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.