The Art of Computer Programming exercise question: Chapter 1, Question 8

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.

Tags:

Taocp

Knuth