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.1-8

Exercise 1.1-8

not stated yet in Lean solution written

RatingM25
MarkersM (mathematical)
KindK5, design an algorithm: give a method for a task, often with a cost bound from the written solution
SectionSection 1.1 of Volume 1. Companion chapter: Algorithms

The task

Give a method on strings in the form of eq. 1.1(3), with rules θj,φj,aj,bj\theta_j, \varphi_j, a_j, b_j, that starts with ambna^m b^n and ends with agcd⁡(m,n)a^{\gcd(m,n)}.

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

Use Euclid's method with subtraction: gcd⁡(m,n)=gcd⁡(∣m−n∣,min⁡(m,n))\gcd(m, n) = \gcd(|m - n|, \min(m, n)). In string form, cancel a letter aa against a letter bb and count the cancelled pairs.

Hint 2

Count the pairs by a third letter cc. When no pair is left, the string has the letters cc and the rest of the longer block.

Hint 3

Rename the letters so that the new string is a∣m−n∣bmin⁡(m,n)a^{|m-n|} b^{\min(m,n)} in some order, and repeat. Stop when nothing is left to cancel.

Solution

Show the solution

Let A={a,b,c}A = \{a, b, c\} and N=9N = 9. In each rule, the number aja_j is the next rule when θj\theta_j does not occur, and bjb_j is the next rule when θj\theta_j occurs and is replaced by φj\varphi_j (the first occurrence). An empty θ\theta occurs at the left end of the string.

jj θj\theta_j φj\varphi_j aja_j bjb_j Action
0 abab empty 2 1 Delete one pair abab.
1 empty cc 0 0 Put a cc at the left end, go to 0.
2 baba empty 4 3 Delete one pair baba.
3 empty cc 0 0 Put a cc at the left end, go to 0.
4 aa aa 5 6 Test whether an aa is left.
5 bb bb 8 7 Test whether a bb is left.
6 cc bb 0 6 Change every cc to bb. Then go to 0.
7 bb aa 6 7 Change every bb to aa. Then go to 6.
8 cc aa 9 8 Change every cc to aa. Then stop.

Idea of a round. Suppose the string at rule 0 has the form apbqa^p b^q or bqapb^q a^p with p,q≥1p, q \ge 1, and let k=min⁡(p,q)k = \min(p, q). Rules 0 to 3 delete an adjacent pair of different letters, and put a cc at the left, until no pair abab or baba is left. The string is then ckc^k followed by d=∣p−q∣d = |p - q| letters of one kind. If d>0d > 0 and the letters are aa, rule 4 sends control to rule 6, and rule 6 turns each cc into bb: the string is bkadb^k a^d. If d>0d > 0 and the letters are bb, rule 4 finds no aa, rule 5 finds a bb, rule 7 turns each bb into aa, and rule 6 turns each cc into bb. The string is again bkadb^k a^d. So the round replaces the pair (p,q)(p, q) by (d,k)=(∣p−q∣,min⁡(p,q))(d, k) = (|p - q|, \min(p, q)) and keeps the greatest common divisor, because gcd⁡(p,q)=gcd⁡(∣p−q∣,min⁡(p,q))\gcd(p, q) = \gcd(|p-q|, \min(p,q)).

If d=0d = 0, no aa and no bb is left. Rule 4 goes to rule 5, rule 5 goes to rule 8, and rule 8 turns each cc into aa. The string is aka^k with k=p=qk = p = q, and the method stops at state 9.

Correctness and finiteness. Let s=p+qs = p + q at the start of a round. The round ends at the new pair (d,k)(d, k) with d+k=max⁡(p,q)<p+qd + k = \max(p, q) < p + q, since k≥1k \ge 1. The number kk is at least 11, so each round starts again with two nonempty blocks, or it ends with d=0d = 0. The sum p+qp + q drops at each round, so the method makes finitely many rounds. The gcd is the same at each round, and at the last round the pair is (0,k)(0, k) with gcd⁡(0,k)=k\gcd(0, k) = k, so the output aka^k is agcd⁡(m,n)a^{\gcd(m,n)}. Each loop inside a round removes or renames one letter at each step, so each round ends.

Check

Run the method on aaabbaaabb, the input for m=3m = 3 and n=2n = 2. Rules 0 and 1 delete two pairs abab and put two letters cc at the left, so the string is ccacca. Rules 2 and 4 find no baba and see an aa, so rule 6 changes both cc to bb and the string is bbabba. This is the pair (1,2)(1, 2) in the form b2ab^2 a. In the second round rule 2 deletes baba, rule 3 adds a cc, and the string is cbcb. Rules 4 and 5 find no aa and one bb, rule 7 changes bb to aa and rule 6 changes cc to bb, so the string is baba, the pair (1,1)(1, 1). In the third round rule 2 deletes baba and rule 3 gives cc. Rules 4 and 5 find neither letter, rule 8 changes cc to aa, and the method stops at state 9 with aa, and gcd⁡(3,2)=1\gcd(3, 2) = 1. It takes 3030 steps. A python3 run of all pairs 1≤m,n≤121 \le m, n \le 12 gave agcd⁡(m,n)a^{\gcd(m,n)} each time, and the input a6b4a^6 b^4 gave aaaa after 4545 steps.

Remarks

The method uses only subtraction, the form in which Euclid's algorithm is given in the Elements. The C1 chapter of Section 1.1 has a smaller method of the same kind, which deletes all letters bb.

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

In Lean

not stated yet The exercise is not stated in Lean yet.

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

Something is wrong? Report it.