views:

919

answers:

3

I'm doing the exercises to TAOCP Volume 1 Edition 3 and have trouble understanding the syntax used in the answer to the following exercise.

Chapter 1 Exercise 8

Computing the greatest common divisor of positive integers m & n by specifying Tj,sj,aj,bj

Let your input be represented by the string ambn (m a's followed by n b's)

Answer:

Let A = {a,b,c}, N=5. The algorithm will terminate with the string agcd(m,n)

    j     Tj     sj    bj    aj
    0     ab  (empty)  1    2   Remove one a and one b, or go to 2.
    1   (empty)  c     0    0   Add c at extreme left, go back to 0.
    2     a      b     2    3   Change all a's to b's
    3     c      a     3    4   Change all c's to a's
    4     b      b     0    5   if b's remain, repeat

The part that I have trouble understanding is simply how to interpret this table. Also, when Knuth says this will terminate with the string agcd(m,n) -- why the superscript for gcd(m,n) ?

Thanks for any help!

Edited with more questions:

What is Tj -- note that T = Theta

What is sj -- note that s = phi

How do you interpret columns bj and aj?

Why does Knuth switch a new notation in the solution to an example that he doesn't explain in the text? Just frustrating. Thanks!!!

+1  A: 

The superscript for gcd(m,n) is due to how numbers are being represented in this table.

For example: m => a^m n => b^n

gcd(m,n) => a^gcd(m,n)

It looks to be like Euclids algorithm is being implemented. i.e.

gcd(m,n):
  if n==0:
    return m
  return gcd(n,m%n)

The numbers are represented as powers so as to be able to do the modulo operation m%n.

For example, 4 % 3, will be computed as follows: 4 'a's (a^4) mod 3 'b's (b^3), which will leave 1 'a' (a^1).

DasBoot
+1  A: 

Here's an implementation of that exercise answer. Perhaps it helps.

By the way, the table seems to describe a Markov algorithm.

As far as I understand so far, you start with the first command set, j = 0. Replace any occurencies of Tj with sj and jump to the next command line depending on if you replaced anything (in that case jump to bj, if nothing has been replaced, jump to aj).

EDIT: New answers:

A = {a,b,c} seems to be the character set you can operate with. c comes in during the algorithm (added to the left and later replaced by a's again).

Theta and phi could be some greek character you usually use for something like "original" and "replacement", although I wouldn't know they are.

bj and aj are the table lines to be next executed. This matches with the human-readable descriptions in the last column.

The only thing I can't answer is why Knuth uses this notation without any explanations. I browsed the first chapters and the solutions in the book again and he doesn't mention it anywhere.

EDIT2: Example for gdc(2,2) = 2

    Input string: aabb
    Line 0: Remove one a and one b, or go to 2.
    => ab => go to 1
    Line 1: Add c at extreme left, go back to 0.
    => cab => go to 0
    Line 0: Remove one a and one b, or go to 2.
    => c => go to 1
    Line 1: Add c at extreme left, go back to 0.
    => cc => go to 0
    Line 0: Remove one a and one b, or go to 2.
    No ab found, so go to 2
    Line 2: Change all a's to b's
    No a's found, so go to 3
    Line 3: Change all c's to a's
    => aa
    Line 4: if b's remain, repeat
    No b's found, so go to 5 (end).

    => Answer is "aa" => gdc(2,2) = 2

By the way, I think description to line 1 should be "Remove one "ab", or go to 2." This makes things a bit clearer.

schnaader
I added a little more to the question to help clarify what I don't understand if you can help a little more. Thanks!
Hortitude
I finally understand. The missing pieces for me was an answer of aaaa = 4. Aha!I think I understand the table notation now -- it seems like b<sub>j</sub> is used when there is still "input left" and vice-versa for a. Is this standard? e.g. if you still have a's keep repeating 2
Hortitude
About "input left": Yes, I read it like this.
schnaader
+1  A: 

the notion of am is probably a notion of input string in the state machine context.

Such notion is used to refer to m instances of consecutive a, i.e.:

a4 = aaaa
b7 = bbbbbbb
a4b7a3 = aaaabbbbbbbaaa

And what agcd(m,n) means is that after running the (solution) state machine, the resulting string should be gcd(m,n) instances of a

In other words, the number of a's in the result should be equal to the result of gcd(m,n)

And I agree with @schnaader in that it's probably a table describing Markov algorithm usages.

chakrit