31/08/2015

1009 views

This is Part 3, the last instalment, on the Algorithms section at the beginning of the Basic Concepts chapter in the first volume of The Art of Computer Programming. In the previous article we covered Knuth's first example of an implementation of Euclid's algorithm. But, as Knuth points out, this system doesn't restrict defined computational methods to strictly effective ones, where there are a finite number of steps or where the steps can always be performed by a human being. As such, he formally defines a computation method using a variation on a generic Markov Algorithm that can be used for the same purpose whilst keeping the defined computational method effective.

It's worth mentioning at this point that this whole example makes significantly more sense if you know what a
Markov Algorithm actually **is**. A Markov algorithm is a procedure for taking
a string of letters and converting them to a different string of letters using an **ordered** list of rules. Each rule looks for a specific series
of letters and replaces them with another. We start at the top of our list of rules and work our way down. If we find one that applies we use it to
change some letters and move back to the top of the list of rules to go again. If we don't find one, we keep moving down the list until we either
find one or get to the end of the list. If you reach the bottom of the list of rules without applying any, your process is finished. That's it.
To appreciate this a little better, let's run over a very simple example that will help make Knuth's example feel familiar later. Take the following
ordered list fo rules.

1 - "u" -> "do" 2 - "p" -> "wn" 3 - "up" -> "error"

Now, imagine we parse in the string of letters **"up"**. Starting at the top of our list we check our first rule (if you find a **"u"**, replace it with **"do"**), and it
applies so we replace the **"u"** in **"up"** with **"do"** and our result is **"dop"**. Now we start again, we check our first rule
but there is no **"u"**, so we move onto our second rule (if you find a **"p"**, replace it with **"wn"**) and it applies so we replace
our **"p"** with **"wn"** and now we have the string **"down"**. And we go again, we check our first rule and it doesn't apply, we check
our second rule and it doesn't apply, and we check our third rule and it doesn't apply to our new string. We have got to the end of our list and
applied no rules, so the process is over. We have converted the string **"up"** to the string **"down"**.

1 - "up" 2 - "dop" 3 - "down"

Remember how I emphasised that the **order** of the rules mattered? Well, if we were to re-arrange our rules and swap rules 1 and 3 we would end up
with the word **"error"** instead. Knuth's algorithm actually varies slightly from a traditional Markov algorithm, and we'll see how later. Now,
let's get back to the book and see how Knuth plans to ensure our new algorithm definition is effective.

So we have a finite limited set of letters called *A*. And we have *A** which contains all combinations of letters that can be made
with letters from *A*. Each combination of letters in *A** is an ordered sequence that is at least *n* long, but *n ≥ 0*
so *A** must also include an empty combination with no letters at all. When Knuth says "*x _{j}* is in

What this means is that each string of letters in *A** represents a state in *Q*, and so every state of our computation
(input, output and everything between) can be represented by a string in *A**. You may even be able to draw a comparison to
the Markov algorithm we defined earlier at this point. Markov algorithms, as we said earlier, have inputs that are strings, they
work on strings, and then output another string.

Now if you know in advanced that Knuth is working towards a Malkov algorithm and you understand the snippet above then we are finally
starting to see a glimpse of the bigger picture. *Q* is the set of all pair *(σ,j)* where *σ* is one of the strings
in *A** and *j* represents the step (or the state) in the algorithm. When *j = 0* it is our input (our first step), and when *j = N*
it is our output (our final step). If you got back to our example algorithm then our input **"up"** would look like this *("up",0)* and our
output would look like this *("down",3)*.

Here, Knuth is simply defining what we mean when we say that one string is **in** another. It's a fairly intuitive idea that one
string is **in** another if it exists in the middle with strings either side. For example the string "rap" is **in** "trapped" with
the strings "t" and "ped" either side. At this point it is worth mentioning that a string in *A** can have a length of *0*.
As such you can also say that the string "up" is **in** the string "up" with two empty strings either side.

And this is where everything comes together. Remembering that *j* is the number of the step we are performing, we can define
strings for *θ & φ* for each step (*θ _{j} & φ_{j}*) as well as integers

j - |
θ |
φ |
a |
b |
---|---|---|---|---|

0 - |
"u" |
"do" |
2 |
1 |

1 - |
"p" |
"wn" |
2 |
3 |

2 - |
"*" |
"error" |
3 |
3 |

Now let's walk through this. We have our input **"up"** and we have our steps (*j*) defined. Look to the
table above to see how we've defined the strings and integers *θ, φ, a & b* respectively for each step. We also
have the ordered rules for our Markov algorithm as defined by Knuth. We start with the state *("up",0)*, and we check the first rule. Our
first rule does not apply because *σ ("up")* **does** contain *θ _{0} ("u")*. We move down to our second rule which
applies. We

If our input string did not contain **"u"** at the beginning our first rule would have applied and taken us directly to step *2*,
which would have replaced the whole string with **"error"**. And that's it for the algorithms section! At the end, after the text, there are a
set of questions. Question 8 asks you to create a number of steps and values for *θ, φ, a & b* for each step in order to implement Euclid's
algorithm. Hopefully that shouldn't be too difficult for you now.

That's it for me until next time. I hope this was helpful, and as always if you have any questions or clarifications feel free to drop me a comment below. Until next time, happy reading!