You are on page 1of 22

2.

4 Turing Machines
Turing machines are abstract models of computations first introduced by Alan M Turing (1912 - 1954) in a 1936 paper
37
to clarify the concept of computability and answer the question of decidability (or solvability). This section mainly
considers the computability aspect, to indicate how such a simple mechanism is capable, given enough time, to execute all
computations. In addition Turing machines are used here to illustrate that various types of tradeoffs, such as time,
complexity, memory size, that are possible in the implementation of digital processing systems. We start with the
definition of Turing machines and a brief discussion of its characteristics. Next to illustrate computability a number of
simple Turing machine executors are introduced. Following which, to indicate the various tradeoffs the implementation of
reduced and universal Turing machines are outlined. To complete this section on abstract computing machines, the
infinite abacus an easier to program variant of Turing machine that can be considered as a highly simplified version of
a digital computer, introduced in Chapter 1 (sections 1.2.4.4 and 1.3.2.2) is revisited and expanded.
2.4.1 Definition and Characteristics of Turing Machines
Turing was first exposed to the decision problem (Entscheidungsproblem, in German)
38
in an advanced course on
Foundations of Mathematics given by Max Newman
39
(1897 - 1984) in the spring of 1935 at Cambridge. The
Entscheidungproblem as stated by Hilbert and Ackermann was a challenge in symbolic logic to determine whether or not
a given formula of predicate calculus is universally valid. In other words, to discover a general procedure, to show that a
given formula is deducible from the axioms of predicate calculus.
Alternately, using the previously introduced terminology (in Sections 2.1 and 2.2) the challenge is: given a problem
could one always find an algorithm which would solve (compute) it ?. As we have seen, for many problems it is
relatively easy to find an algorithm that solves it, that is to specify a sequence of instructions which when followed
will execute the required task. The difficulty is to prove that there are problems for which one cannot define an
algorithm to solve it. To achieve this Turing had first to provide a rigorous (precise) enough definition of the intuitive
concept of algorithm, that would allow one to prove that none existed. Turing approached this, by defining a
mechanistic model of the work that a human-computer (this was the time before the first electronic computers) while
executing a well defined procedure (an algorithm).
To specify a machine capable to replace a human-computer in executing a general algorithm (working under
preassigned instructions), Turing made the following observations:
The two-dimensional paper used by the human-computer to write the various results (intermediate and final),
can be replaced by a one-dimensional paper tape divided into squares that contain a finite set of machine
symbols. If needed, the hand written results can be expressed as a sequence of machine symbols.
The behaviour of the human-computer, determined by the written results that he is observing and his state of
mind at that moment can be replaced by a scanner that can read the symbol in a given square of the tape, and the
capability of the machine to be at any given time in one of a finite number of conditions or machine states. If
needed, the capability of the more complex human operations (behaviour) can be performed by the machine as a
number of simpler (basic) operations.
The simple operations performed by the machine includes, one or a combination of the following: change of
symbol in the observed square, observing a nearby square, and/or changing machine state.
Based upon these observations, Turing defined in his paper an a-machine, for automatic machine, capable to mimic the
operations of a human-computer. Although most Turing machine described in the modern literature are quite different,
they all have a one-to-one translation into the original a-machine. In this presentation, we shall use a well accepted Turing
Problem Solving & Algorithms 2 - 57
37
A. M. Turing, On Computable Numbers, with an Application to the Entscheidungsproblem, Proceedings of the London Mathematical
Society, vol. 42 November 1936 pp. 230 - 265. The paper can be downloaded from www.emula3.com/docs/OnComputableNumbers.pdf
38
Many authors associate the Entscheidungsproblem with David Hilberts address before the Second International Congress of
Mathematics at Paris in 1900 mentioned before (Sections 1.2.4.4 and 2.1.4), although some questions of solvability was raised there, the
decision problem was formally stated in Grundzuge der Theoretischen Logik by Hilbert D. (1862 - 1943) and Ackermann W. (1895 - 1962)
the English translation Principles of Mathematical Logic Cheleea Publishing Company 1950.
39
This is the M. H. A. Newman who in 1942 was in charge of the development of COLOSSUS, the cipher breaking special purpose
computer implemented at Bletchley Park (see Section 1.4.1.2.3). It is interesting to note that Turing also worked at Bletchely Park from 1939
to 1944, but on another code breaking machine called the Bombe. Furthermore after leaving the National Physical Laboratory in 1948
Turing went to work at Manchester University under Newman.
machine model, shown below, that allow us to visualize the computational characteristics of such a machine.
R L Potentially Infinite Tape
Logic
Unit
Read/Write
Head
Q
State
Cell
x
y
y
C
q
+
q
Tape
Control
Tape
Transport
Memory Cells
j
a
S
The machine includes a potentially infinite tape which can be thought of as being finite at any moment, but such that at any
time a finite amount of blank tape may be added whenever such a demand arises. This tape like the two-dimensional
paper of the human-computer, represents the machines interface with the external world. The tape is divided into a
sequence of individual cells, each of which can carry (generally in encoded form) any symbol from a finite alphabet.
A = (b, a
1
, a
2
,..., a
k
)
This alphabet, referred to as the external alphabet, includes the letter b, an empty or blank letter. When this symbol
is written in a memory location, it erases the previous symbol producing an empty or blank cell. Note that all new
tape that is added includes only the blank symbols b.
The logic unit of a Turing machine, that defines its internal behaviour, has two inputs and three outputs. The input x of
the input-pair (x, q), is the letter of the external alphabet A in the cell currently connected to the logic unit via the
read/write head. The input q represents the present internal state of the machine. The machine can be in one of a
finite set of internal states:
Q = (s, q
1
, q
2
, ..., q
n
)
where s represents the stop state such that when the machine reaches this internal state it stops. The elements of the
output-triple (y, C, q
+
) are uniquely determined by the input pair (x, q). The symbol y is a letter of the external
alphabet that is to be written into the current cell to replace (overwrite) the scanned symbol x; it need not be different. C
represents the input to the tape control unit that controls the movement of the tape. C can take on one of the following
three values R, L, S, defined as:
R for scan the next cell to the right, i.e., move the tape left one cell.
L for scan the next cell to the left, i.e., move the tape right one cell.
S for scan the same cell again, i.e., do not move the tape.
The third output q
+
of the logic unit represents the next internal state of the machine. q
+
is fed as the next q input to the
logic unit via the state cell Q. The state cell at each moment contains the present condition of the computation. The set
of symbols:
= (R, L, S, s, q
1
, q
2
, ..., q
n
)
is referred to as the internal alphabet of the machine, that is used to define its operation. In line with this, the logic unit
of a Turing machine is specified by the following three functions:
The output symbol: y = F(x, q)
The movement: C = G(x, q)
The next state: q
+
= H(x, q)
where y, x are in A, the external alphabet, and q, q
+
, C are in , the internal alphabet. These three equations, that define
Problem Solving & Algorithms 2 - 58
the behaviour of the Turing machine, can also be represented by a set of quintuples P of the form (x, q; y, C, q
+
) such
that no two quintuples of P have the same first two members. To provide a more convenient specification, the set of
quintuples P, can be reorganized as the shown (k+1) by n matrix, referred to as the functional matrix or the
transition table of the Turing machine. Note that only the active states q
1
, q
2
, ..., q
n
are listed in the matrix since
there can be no transitions from the stop state.
q
1
q
2
q
n
b
a
1
2
a
k
a
y, C, q
+


Alternately, the internal behaviour of a Turing machine can also be represented by a state diagram. In the state diagram
there are n+1 circles representing the n+1 internal states Q, of the machine. For every quintuple (x, q; y, C, q
+
) in P
there exists a directed arrow labelled x / y, C from state q to state q
+
. The representation of a quintuple in a state diagram
form is as shown.
x / y, C
q q
+
It is interesting to note that, as a rule
40
, all the arrows entering a specific state have the same move C associated with
them. Because of this, in a number of state diagram representations, the move is associated with the state. In other
words, a Turing machine corresponds to a finite state machine, a machine with a finite number of internal states whose
behaviour is defined by the quadruple:
M = (A, Q, P, q
i
)
where A, Q and P are as defined above, and q
i
denotes the initial or starting state of the machine.
Depending on their mode of operations, there are two main types of Turing machines computers and acceptors:
A Turing machine computer is an information transducer. Computation with these Turing machines is
obtained by starting it with an initial tape configuration, T
i
or T
0
(consisting of a finite number of non-blank
symbols from the external alphabet A), and a defined starting position of the read/write head. As a result of the
Turing machine operation (as time advances), the initial tape configuration changes in discrete steps, referred to as
cycles. Thus, during the computation, a sequence of intermediate tape configurations
(T
1
, T
2
, ..., T
f
) is obtained, with T
f
being the final tape configuration that contains the result of the
computation. The tape configuration sequence:
X = (T
i
, T
1
, T
2
, ..., T
f
)
is called the external behaviour of the Turing machine.
A Turing machine is said to solve a class of problems if there exists a finite tape configuration sequence X such that
T
f
is the final configuration when T
i
is the initial configuration.
A Turing machine acceptor, on the other hand, reads an input tape (containing a finite number of non-blank
symbols from the external alphabet A) and after possibly modifying its contents, it accept or reject the tape. As a
rule, these Turing machines are used to check whether or not the string of symbols on the initial tape belongs or
does not belong to a specified class, and are used extensively in automata and language studies. Here we shall
present two simple examples of Turing machine acceptors.
The acceptance or rejection of the initial tape can be done in two ways. One, by including in the internal states Q
Problem Solving & Algorithms 2 - 59
40
I have observed this, in all the examples that I have seen, but I am not sure that this can be proven as general characteristic.
two types of final states, an accept state and a reject state. It is said that the machine accepts the input tape if it
stops in the accept state, and rejects it if it stops in the reject state. An alternate way is for the Turing machine to
accept the input tape if it stops and reject it if it does not stop.
As stated before, tape configurations are changed when the read-write head modifies the content of a cell this in turn is
controlled by the logic unit. In other words, the input-output relationship of the logic unit defines the behavior of the
Turing machine. It is said that: a Turing machine executes a given computation if its logic unit is designed to produce
the required external behavior. The specification of the logic unit is done by defining the required quintuples (x, q; y, C,
q
+
) in P that produce the desired behavior. In the next section, Examples of Turing Machines, we shall indicate via simple
example how this is done.
Perspective: Turings thesis: Capability of Turing Machines
As stated above, the motivation of Turings 1936 paper was to prove that the decision problem posed by Hilbert and
Ackerman is not solvable. Although even a brief discussion of the proof is beyond the scope of this presentation, Turing
proved it by showing that there is no Turin machine that solves the decision problem. A corollary to this is Turings
thesis
41
which states:
Any effective computation (algorithmic procedure) can be realized by a Turing machine.
42
Turings thesis has a number of interesting consequences, for example:
How come that such a simple machine like a Turing machine, can solve (execute) all theoretically solvable
(computable) problems. The answer lies in the that all operations (problems) can be reduced to a sequence of much
simpler operations (problems). Furthermore that in the case of Turing machines one assumes infinite time and
indefinitely expandable storage by being able to add tape as needed.
There is also the converse (as mentioned in section 2.2.1), that not all Turing machine computable functions are
executable using modern computers because of time and memory constraints.
Any computer program can be translated into a Turing machine, and any Turing machine can be implemented
(simulated) using a general purpose programming language.
All models (formalisms) used to describe effective computability such as, recursive functions, lambda calculus, Post
systems and Markov algorithms, have been shown to compute the same functions as Turing machines.
2.4.2 Turing Machine Examples
This section presents the design of Turing machines via a number of simple examples. Here we shall also indicate the
various representations of these Turing machines.
Example 1: Turing Machine Decrementer
It is required to specify the logic unit of a Turing machine degrementer, that produces a final tape configuration that
includes a binary number B - 1 0 embedded between strings of blank cells, given the initial tape configuration of the
binary number B > 0, represented as a string of 0's and 1's embedded between strings of blank cells. The machine is
started with q
1
as the initial state and the read-write head at the least significant bit of B.
As a first step one has to define an algorithm to execute the binary subtraction. One such algorithm is as follows:
i. Start from the least significant bit of the binary number B and move left (over the string of zeros, if any) without
changing anything until you reach the first 1.
ii. Change the 1 into a 0.
iii. Return (go right) until you reach the first blank b, changing all 0's into 1's.
Problem Solving & Algorithms 2 - 60
41
Note that this thesis is a conjecture that can only be argued and not proven formally.
42
In the literature this thesis (or equivalent formulations) are often referred in the computability literature also as Church-Turing
thesis, Churchs thesis or Churchs conjecture, after Alonzo Church who showed independently (a bit earlier) that the decision
problem is not solvable.
The intuitive term effectively computable (or calculable) was used by mathematician before the adoption of the term algorithm to specify
that the procedure for reaching the desired result is: stated in terms of a finite number of exact instructions,produces the desired result in a
finite number of steps, it can be executed mechanically without insight or ingenuity on the part of the (human) executor.
iv. Stop.
To specify the corresponding logic unit, one has to consider separately each steps of the above algorithm. That is,
checking the least significant digit of B, we can have two conditions a and b, resulting in the input combinations (0, q
1
)
and (1, q
1
) to the logic unit. Note that one cannot have a blank input in this state as it is assumed that B > 0.
a. If the least significant bit of B is 0 (for even binary number B), that is for (0, q
1
) the required output of the logic unit
has to be (0, L, q
1
). This will not change the tape configuration, and will also move (advance) the read-write head
left repeatedly until a 1 is reached. This corresponds to step i of the subtraction algorithm.
When a 1 is reached, one has to change it into a 0 and return the read-write head by changing state. That is, for
(1, q
1
) the output should be (0, R, q
2
). This corresponds to step ii of the subtraction algorithm.
For step iii of the subtraction algorithm, one has to return read-write head changing all zeros into ones. That is, for
all (0, q
2
) the output should be (1, R, q
2
). Note that one cannot have an input of 1 in this state.
If in state q
2
, a blank is reached (all zeros were changed into ones), according to step iv of the subtraction algorithm,
the machine should to stop. That is, the input (b, q
2
) should produce the output (b, S, s).
b. If the least significant bit of B is 1 (for odd binary number B), that is for (1, q
1
) the required output should be
(0, R, q
2
). Since this was the least significant bit the next input will be (b, q
2
) that produce the output (b, S, s)
stopping the machine.
The above stated specification can be sumarized as a list of input-output relations, that are sometimes referred to as the
instruction table of the Turing machine:
(0, q
1
) > (0, L, q
1
)
(1, q
1
) > (0, R, q
2
)
(0, q
2
) > (l, R, q
2
)
(b, q
2
) > (b, S, s)
This input-output specification of the Turing machine, as indicated before, can be represented alternately in the form of a
functional matrix and/or as a state diagram. The functional matrix and the state diagram
43
representation of this Turing
machine decrementer are as shown:
0, R, q
2
0, L, q
1
1, R, q
2
q
1
q
2
b, S, s
1
0
b

1 / 0, R
q
1
0 / 0, L
q
2
0 / 1, R
s
b / b, S
Note that the shown functional matrix includes two positions without any entries; corresponding to the input conditions
(x, q) that cannot occur. When it comes to implementation, these unspecified entries can be used later to simplify the
functions governing the behavior of the logic unit. Furthermore note that the functional matrix does not include a column
for the stop state s, since it acts as a sink does not have transitions leaving it.
Actually, the same functionality, could have been obtained by using the following modified algorithm:
i. Start from the least significant bit of the binary number B. Move left, changing all 0's into 1's, until you reach the
first 1.
ii. Change the 1 into a 0.
iii. Stop.
The corresponding functional matrix and state diagram are as shown.
Problem Solving & Algorithms 2 - 61
43
Note that in this presentation the initial state is always assumed to be q
1
. Others distinguish the initial state by using a double circle or
a thicker line.
q
1
1, L, q
1
0, S, s
1
0
b
q
1
0 / 1, L
s
1 / 0, S
Even though these two Turing machines give the same final result, their final tape configurations are not the same since
the read/write heads stop at different places, as indicated below by the arrows M
1
and M
2
.
T = b b 1 0 1 0 1 1 1 b b
M
1
f
M
2
The fact that the two decrementers stop at different points may or may not be important. If the required solution is only
one of the steps of a more general computation, in that case one has to know where the first computation ended.
Example 2: Turing Machine Binary to Unary Converter
It is required to specify the logic unit of a Turing machine that converts a binary number B into an equivalent number of
slashes (/) embedded between strings of blank cells. Assume that the initial tape configuration of the Turing includes the
binary number B 0 as a string of 0's and 1's embedded between strings of blank cells. The machine is started with q
1
as the initial state and the read-write head at the least significant bit of B.
The conversion procedure at high level can be stated as follows:
Subtract one from B, add a slash and repeat this until B = 0
Building on the previous example, the corresponding algorithm can be stated in terms machine steps as follows:
Step 1: Subtract 1 from B. Start from the least significant bit (LSB) of B, 0, q
1
> 1, L, q
1
change all 0's into 1's and the first 1 into 0. 1, q
1
> 0, R, q
2
Step 2: Go to the right-most place. Return the read-write head over 1, q
2
> 1, R, q
2
all the 1's and /'s (after the first subtraction) until you reach a blank. /, q
2
> /, R, q
2
Step 3: Add a slash and reinitiate. Replace the blank by a / and return the b, q
2
> /, L, q
1
read-write head to the new LSB with the machine at its initial state. /, q
1
> /, L, q
1
Step 4: Check for end and replace intermediate results by blanks. b, q1 > b, R, q3
End, if blank is reached with the machine in state q
1
, that is B was 1, q
3
> b, R, q
3
reduced to 0. Return to the first slash, erasing all the 1's. /, q
3
> /, S, s
The corresponding functional matrix and state diagram of the Turing machine converter are as shown.
0, R, q
2
1, L, q
1
q
1
q
2
b, S, s
0
1
b

q
3
/
/, L, q
1
b, R, q
3
/, S, s
/, L, q
1
b, R, q
3
1, R, q
2
/, R, q
2
q
1
s
q
3
q
2
0 / 1, L
/ / /, L
1 / 0, R
b / /, R
/ / /, R
1 / 1, R
b / b, S
/ / /, S
b / b, R
1 / b, R
Note that in the above representations we included the extra quintuple (b, q
3
; b, S, s) to stop the machine when B = 0 is
represented by a blank (string) instead of a string of zeros. Also note that the machine stops with the read-write head at
Problem Solving & Algorithms 2 - 62
the left-most slash. If we had to stop it with the read-write head at the right-most slash the two quintuples have to be
changed; (/, q
3
; /, S, s) to (/, q
3
; /, R, q
3
) and (b, q
3
; b, S, s) to (/, q
3
; /, R, q
3
) and (b, q
3
; b, L, s).
Example 3: Turing Machine Subtractor
It is required to specify the functional matrix of a Turing machine subtractor that works on the principle of subtracting
one from each number until one of them is exhausted. Assume that the initial tape configuration of the Turing machine
contains two binary numbers A, B 0, represented as two strings of 0's and 1's, separated by a minus sign, embedded
between strings of blank cells. If A > B it is required to leave the result A - B = C , as a string of 0s and 1s embedded
between strings of blank cells, and if A B the result A - B = - C should be represented by a minus sign followed by a
string of 0s and 1s embedded between strings of blank cells. The machine is started with q
1
as the initial state and the
read-write head at the least significant bit of A.
The subtraction procedure in terms of machine steps can be implemented as follows:
1.Subtract 1 from A and continue left until a blank 0, q
1
> 1, L, q
1
is reached. Check if A - 1 < 0. 1, q
1
> 0, L, q
2
0, q
2
> 0, L, q
2
1, q
2
> 1, L, q
2
i. If A - 1 > 0, that is a blank is reached in b, q
2
> b, R, q
3
state q
2
. Go to the LSB of B. 0, q
3
> 0, R, q
3
1, q
3
> 1, R, q
3
-, q
3
> -, R, q
3
ii. If A - 1 < 0, that is a blank is reached in b, q
1
> b, R, q
4
state q
1
. Erase the intermediate result of 1, q
4
> b, R, q
4
A - 1 and stop. This leaves - C. -, q
4
> -, S, s
2.Subtract 1 from B and check if B - 1 < 0. b, q
3
> b, L, q
1
i. If B - 1 0, go back to step 2. -, q
2
> -, L, q
1
ii. If B - 1 < 0, erase the intermediate result -, q
1
> b, R, q
4
of B - 1, add 1 to A and continue left. b, q
4
> b, L, q
5
Stop at the first blank leaving b C. 0, q
5
> 1, L, q
6
1, q
5
> 0, L, q
5
0, q
6
> 0, L, q
6
1, q
6
> 1, L, q
6
b, q
6
> b, S, s
The corresponding functional matrix is as shown; the drawing of the state diagram is left as an extra exercise.
0, L, q
2

0, L, q
2
1, L, q
1
q
1
q
2
b, S, s
0
1
b

q
3
-
-, L, q
1
b, R, q
4
-, S, s
b, L, q
1
b, R, q
3
b, R, q
4
q
4
q
5
q
6
b, R, q
4
1, L, q
2
0, R, q
3
1, R, q
3
-, R, q
3
b, L, q
5
b, L, q
5
1, L, q
6
1, L, q
6
0, L, q
6
0, L, q
5
Note that in both cases the machine stops to the left of the most significant bit (MSB) of C, either at - (if A B) or at b
(if A > B). On the other hand, if one does not mind for the machine to stop in the string C in the case of A > B, q
6
can be
eliminated by replacing the quintuple (0, q
5
; 1, L, q
6
) with (0, q
5
; 1, S, s). It is also interesting to note that if the
procedure is started by first subtracting one from B the corresponding Turing machine requires more states; this is left an
exercise.
Actually one could use a slightly modified algorithm, by going right after subtracting 1 from A, and using another state
Problem Solving & Algorithms 2 - 63
for subtracting 1 from B. This scheme gains some time, one does not have to go to end of A and return, but a six state
solution the machine stops in string C for A > B. This is also left as an exercise.
To complete this outline of Turing machine specification, next we shall consider two examples of simple acceptor type
machines.
Example 4: Turing Machine Spell-Checker
It is required to specify the functional matrix of a Turing machine, that check if in a word the letter q is followed by one
of: ua, ue, ui, or uo, as required in English. Assume that the Turing machine initial tape configuration includes an
English word, represented as a string of lower case letters embedded between a strings of blank cells. The machine is
started with q
1
as the initial state and the read-write head at the left-most letter of the word. The word is checked by
scanning the string of letters from left to right. To simplify the presentation instead of the full English alphabet, a reduced
(simplified) external alphabet is used, such that A = (b, q, u, v, l) where b stands for blank, q and u stand for
themselves, v for (a, e, i, o) and l for all other letters of the alphabet. The machine has two final states: an accept state
and a reject state .
It is easy to realize that in scanning the word, one has to distinguish three conditions: no q scanned represented by state
q
1
, q scanned represented by state q
2
, and qu scanned represented by state q
3
. Based on this, the scanning of accepted
words with the machine terminating in state is done by the following steps:
- No q in the word l, q
1
> l, R, q
1
u, q
1
> u, R, q
1
v, q
1
> v, R, q
1
b, q
1
> b, S,
- quv in the word q, q
1
> q, R, q
2
u, q
2
> u, R, q
3
v, q
3
> v, R, q
1
The scanning of rejected words with the machine terminating in state is done by the following steps:
- q not followed by u q, q
2
> q, S,
l, q
2
> l, S,
v, q
2
> v, S,
b, q
2
> b, S,
- qu not followed by v q, q
3
> q, S,
l, q
3
> l, S,
v, q
3
> v, S,
b, q
3
> b, S,
The corresponding functional matrix is as shown; the drawing of the state diagram is left as an extra exercise.
u, R, q
q, S,
l, R, q
1
v, R, q
1
u, R, q
1
q
1
q
2
u
v
b
q
3
q, R, q
2
b, S,
v, R, q
q
l
3
l, S,
v, S,
b, S,
q, S,
l, S,
1
b, S,
b, S,
Note that in this implementation of the spell-checker it was assumed that the initial tape contains only a single word to be
scanned and that the machine stops when it detects an error. As a result the functional matrix does not include columns
corresponding to either one of the stop states and On the other hand if the machine is supposed to check a set of
words separated by blanks one would have to add an additional symbol in the external alphabet A to indicate an error
position and specify the missing columns in the functional matrix. This is left as an extra exercise.
Problem Solving & Algorithms 2 - 64
Example 5: Turing Machine Recognizer
It is required to specify the functional matrix of a Turing machine that checks if the string of left and right brackets
embedded between strings of blank cells is a well-formed string (WFS). If the string is well-formed, the final tape
configuration should include a 1 between blanks. If the string is not well-formed, the final tape configuration should
include a string of 0's equal in number to the number of contradictions, between blanks.
Note that in Example 8 in Section 2.1.1 (Simple Examples - in problem solving) we defined WFS inductively as:
i. Each of the following strings are WFS's:
a. ( ) b. ( )( ) c. (( ))
ii. Any combination of the above strings is a WFS
As an example the following are WFS's: ()((()())); (()()(())) while the following )(; )(()); ()())); (()() are not WFS's.
For recognizing a WFS, we shall use the first solution procedure outlined in Example 8, except that instead of analyzing
the string from left to right, we shall do it the opposite way. That is:
Repeatedly remove elementary pairs ( ) from the given string, until either an empty string or a non-empty string
without elementary pairs remains. If what remains is an empty string, then the original string was WFS; if not it
wasnt.
In terms of Turing machine steps the above solution procedure can be stated as follows:
1. Start from the right-most symbol (RMS) in state q
1
.
2. Go left without changing anything until you reach 0, q
1
> 0, L, q
1
either a left parenthesis "(" or a blank "b". 1, q
1
> 1, L, q
1
), q
1
> ), L, q1
i. If you reached a left parenthesis "(", change it (, q
1
> 1, R, q
2
into a 1 and go to step 3.
ii. If you reached a blank "b", the string has been b, q
1
> b, R, q
3
analysed. Leave it as is and go to step 4.
3. Check for the pair of the left parenthesis by going 0, q
2
> 0, R, q
1
right. 1, q
2
> 1, R, q
1
i. If you reach a right parenthesis, the pair, ), q
2
> 1, L, q
1
replace it by 1 and go back to step 2.
ii. If you reach a blank "b", there is an error, b, q
2
> 0, L, q
1
since there is no pair. Place a 0 after the
RMS and go back to step 2.
4. Go right checking your string.
i. If the string contains only 1's, this was a WFS. 1, q
3
> b, R, q
3
Go right, replacing each 1 by a blank, until you b, q
3
> 1, S, s
reach a blank. Replace it by 1 and stop.
ii. If the string also contains right parentheses ")" ), q
3
> b, R, q
4
and/or 0's, place a 0 after the RMS for each 0, q
4
> 0, R, q
4
right parenthesis in the string and stop at the 1, q
4
> 1, R, q
4
first 0 that you reach. ), q
4
> ), R, q
4
b, q
4
> 0, L, q
1
0, q
3
> 0, S, s
By going through this algorithm step by step for a number of well-formed strings and a number of not well-formed
strings, it is easy to see that it will work in all cases. The corresponding functional matrix is as shown:
Problem Solving & Algorithms 2 - 65
0, R, q
4
1, R, q
4

0, R, q
2
1, L, q
1
0, S, s
1, R, q
2
0, L, q
1
1, L, q
1
0, L, q
1
q
1
q
2
1, S, s
0
1
b

q
3
), L, q
1
b, R, q
3
0, L, q
1
b, R, q
3
1, R, q
2
), R, q
4
q
4
)
(
b, R, q
4
It is interesting to note that if one olny wants indicate a not well-formed string by a single 0, without considering the
number of errors, the corresponding Turing machine recognizer requires the same number of states.
Design of complex Turing machines
In many cases when one has to specify a Turing machine for solving a complex problem, it is simpler to separate the
problem into a number of simpler problems and specify a Turing machine for each. These simpler machines can later be
integrated into a single composite Turing machine. The simplest way to combine them is to use the state diagram
representations and let the stop state of one submachine become the starting state of another submachine. In this
sense, one divides the overall execution into a sequence of simpler executions.
Another way is to define a number of submachines that execute limited operations. For example in operating upon binary
numbers B, one could define submachines that execute: B - 1, B + 1, rewrite B, erase B, match B, translate B or check
some characteristics of B. Once these submachines are defined, the overall algorithm can be specified in terms of these
operations. The difference between this and the above approach is that here each submachine is can be accessed a number
of times during the computation while in the first approach each submachine is generaly accessed only once during the
computation.
Both of these schemes are valid approaches to reduce the work required in specifying complex Turing machines. To
discuss the relative merits of these schemes is beyond the scope of this presentation. Our purpose in introducing Turing
machines was to indicate that everything that can be done by a modern computer can be done by a Turing machine. The
actual details for executing computations with Turing machines are only of academic interest, since they are only
conceptual machines used to clarify the concept of mechanistic executor of algorithms.
2.4.3 Reduced and Universal Turing Machines
To provide additional insight on Turing machine characteristics and capabilities, in this section we shall introduce two
alternate types of Turing machines:
Reduced Turing machines, that have either minimal external alphabet, or minimal number of internal states.
Universal Turing machines, that can simulate (mimic) the operation of all other Turing machines.
To be able to define these alternate types of Turing machines we need to specify what is meant by one Turing machine
simulating another. Let M = (A, Q, P, q
i
) denote a Turing machine with:
external alphabet A = (b, a
1
, a
2
, ..., a
k
), where 2
l-1
k 2
l
internal states Q = (s, q
1
, q
2
,..., q
n
) with s as the stop state.
starting state q
i
(generally q
1
)
a set of defining quintuples (x, q; y, C, q
+
) in P.
The Turing machine M is said to simulate Turing machine M if there exists a mapping (coding) between the
respective tape configurations : T > T (of the two Turing machines) that satisfies the following two relationships:
i. If T
j
and T
j+1
are two consecutive tape configurations of M, then in M, : T
j+1
is obtained from : T
j
. In general,
note that in M going from : T
j
to : T
j+1
requires more steps than going from T
j
to T
j+1
in M.
Problem Solving & Algorithms 2 - 66
ii. If T
i
and T
f
are the initial and final tape configurations of M, then : T
i
= T
i
and : T
f
=T
f
are the corresponding
initial and final tape configurations of M.
This definition states is that for every tape configuration T
i
produced by Turing machine M, there must be a
corresponding tape configuration : T
i
produced by Turing machine M. But the converse is not necessarily true, that is,
there can be and usually there are intermediate tape configurations produced by M which do not have a counterpart in M.
In practice, defining an encoding such that M simulates M, corresponds to the defnition of a sets of quintuples of M
to simulate the transition produced by each of the quintuples of M. That is for each quintuple P in M there are a set of
quintuples in P.
Reduced Turing machines
Here we shall indicate the required mappings to specify a either a reduced-alphabet or a reduced-state Turing
machine that simulates a given Turing machine M = (A, Q, P, q
i
).
Statement: Given a Turing machine M = (A, Q, P, q
i
) there exists a reduced-alphabet Turing machine
M
1
= ({b,1}, Q
1
, P
1
, q
i1
) that simulates it.
A formal proof of this statement (theorem) is beyond the scope of this presentation; we shall only present a brief
plausibility argument regarding a possible mapping of its external alphabet. We shall also indicate the simulation
functions without specifying how one defines the quintuples of the reduced machine.
Given any Turing machine M, one can define a two-letter external alphabet (b, 1) machine by ordering of the letters of
original external alphabet A, and encoding each a
i
in A as a combination of t blanks (b's) and ones (1's) as follows:
b = bb...bb, a
1
= bb...b1, a
2
= bb...1b, a
3
= bb...11 and so on.
Simulation (outline): For each quintuple (x, q; y, C, q
+
) in P one has to derive a set of quintuples in P
1
that execute
the following three functions. Note that at the start of each simulation function the read/write head of M
1
is at the first
encoded character.
i. Recognize the encoded character x in P, by moving t - 1 steps to the right, remembering the previously read
characters.
ii. Print the encoded character y in P, by moving t - 1 steps to the right, remembering the characters yet to be
printed, the next state q
1
+
, and the move C to be executed.
iii. Move the read/write head of M
1
to the correct position, and leave M
1
in the correct next state, q
1
+
. That is; if
C = S stay in the same position, if C = R move right t cells, and if C = L move left t cells.
Note that each step in M the reduced alphabet Turing machine has to execute either 2(t - 1) or 3(t -1) steps.
Furthermore for each defined quintuple in M, there may be need to define t
2
or t (t -1) states in the reduced-alphabet
machine M
1
. In other words there is both an increased execution time, the number of steps, and an explosion in the
required number of states.
Statement: Given a Turing machine M = (A, Q, P, q
i
) there exists a reduced-state Turing machine
M
2
= (A
2
, {s, q, q
i2
}, P
2
, q
i2
) that simulates it.
As above, a formal proof of this theorem is beyond the scope of this presentation. Furthermore since the required
simulation is more complex, we shall only present a brief plausibility argument regarding a possible encoding of the
external alphabet A
2
of the reduced-state Turing machine M
2
.
The basic idea is to increase the external alphabet to encode all the needed information of M. That is, given the general
form of A
2
= (b, a
1
, a
2
, ..., a
k
, {B}) the question is how to define {B}.
i. To encode all the state information in the functional matrix of M there is need for a (k + 1) n element extension of
the form B={a
i,j
} with i = 0, 1, 2, ..., k and j = 1, 2, ..., n.
ii. Note that the above encoding will only work for C = S, to encode the the other moves L and R there is need to
further extend B in the form B = {a
i,j,c
} with c = {L,R}.
Problem Solving & Algorithms 2 - 67
iii. Also in transferring the information one has to define whether the cell receives or transmits information, to allow for
this one includes a fourth index d = {r, t}.
Therefore, the extended external alphabet of the reduced-state Turing machine A
2
= (b, a
2
, a
2
, ..., a
k
, {a
i,j,c,d
}) has to
include (k+1) + (k+1) n 2 2 = (k+l)(4n+1) letters.
Note that these encodings were chosen to simplify and generalize the definition for the reduced Turing machines and thus
they are not be the most efficient ones. For any given Turing machine one could define better encodings where one
need not increase to such a large extent either the number of internal states or the external alphabet. The important thing to
remember is that if one reduces either the external alphabet or the number of internal states, the other entity has to be
increased by quite a lot. Furthermore in both cases the number of steps that the reduced machine has to execute increases
greatly.
Observation regarding Turing machine complexity
Having seen, during the years, a considerable number of various Turing machine specifications, derived by me and many
of my students, I have concluded the following:
i. Assuming that a given Turing machine specification requires n
a
letters in the external alphabet and n
q
internal states,
the product n
a
x n
q
does not remain constant when one tries to reduce or increase one of them.
ii. In most cases there exists a specific selection for which the product n
a
x n
q
, in general leads to the minimal or
near-minimal size functional matrix.
iii. Since as a rule, the size of the functional matrix is indicative of the number of steps required to produce a final tape
configuration T
f
from an initial tape configuration T
i
, this would indicate that each problem may have a minimal
complexity solution.
Note that this is only a personal observation and should be treated as such, as it may not hold for all problems. As far as I
know there was no general investigation in this area.
Universal Turing machine
The Turing machines specified in the previous section, in modern terminology are special purpose computers; machines
capable of executing a single type (class) of computations. Turing in his paper defined these basic (specific) machines to
indicate the mechanistic nature of algorithm execution. In order to disprove decidability he introduced a generalization
of these machine by defining a universal computing machine; one capable to execute the computation of any
machine M, if supplied with a tape that contains the functional matrix of M in some standard description and its initial
data. Note that in this sense the universal Turing machine is the forerunner of our present day general-purpose digital
computer, with the functional matrix of M as its program.
Using the above introduced terminology this can be restated as:
Statement: Given a Turing machine M = (A, Q, P, q
i
) it is possible to specify a universal Turing machine
that is capable to simulate its operation (behaviour).
Again, a formal proof of this statement (theorem) is beyond the scope of this presentation; we shall only present a brief
plausibility argument by presenting a possible mapping of information of M, and indicate the general operation of the
universal machine, without specifying how one defines its quintuples.
1. Encode the external and internal alphabets of the given Turing machine M, as binary numbers:
i. A = (b, a
1
, a
2
, a
3
, ..., a
k
) = (00..00, 00..01, 00..10, 00..11, ...)
ii. Q = (s, q
1
, q
2
, q
3
, ..., q
n
) = (00..00, 00..01, 00..10, 00..11, ...)
iii. C = (R, L, S) = (01, 10, 00)
2. Assume an extended alphabet of the following 11 letters for the universal Turing machine:
A = {0, 1, b,
,
, ;, (, ), [, ], *, $}
Problem Solving & Algorithms 2 - 68
3. Use the following standard tape format for the universal Turing machine.
DATA ADDRESS PROGRAM
( , , , , , , ) * ( , , ) [( , ; , , ) ... ( , ; , , ) ... (, ; , , )]
Encoded letters of A x, q, bb or sets of ( x, q; y, q
+
, C)
or $$$..$ representing y, q
+
, C the encoded quintuples
initial, intermediate, encoded current of machine M
and final tapes of M address location
4. Initialize the operation as follows:
i. In the data part of the tape configuration, replace the initial letter by $$..$$. The initial letter being the letter
under the read-write head of M at the start of the procedure.
ii. Initially place in the current address location of the machine M, the initial letter and the initial state, followed
by two blanks; that is, a
i
, q
i
, bb in an encoded form.
iii. Place the quintuples of M, in encoded form, in the program part of the above defined tape format.
5. Simulation procedure:
i. Using (x, q) as an address, copy the corresponding (y, q
+
, C) from the quintuple list into the current address
location. That is, (x, q, bb) < (y, q
+
, C)
ii. From the current address location replace $$..$ by encoded y in the data part of the tape. This updates the data
part of the tape. That is, $$..$ < y
iii. Check the value of C in the current address location; update y in the current address location as follows:
a. If C = S = 00, do nothing.
b. If C = L = 10, replace y in the current address location with the letter from the data part of the tape that is to
the left of the current position. Remember this location of the data part of the tape as .
c. If C = R = 01, replace y in the current address location with the letter from the data part of the tape that is to
the right of the current position. Remember this location of the data part of the tape as .
Note that as a result of this step (y < x) in the current address location.
iv. Check the value of q
+
and:
a. If q
+
= 00...00, stop. The computation is completed.
b. If q
+
00...00, continue. In other words q
+
becomes q, completing the new address (x, q).
v. Replace the letter in location of the data part of the tape by $$...$ (x < $$..$ ), the current position in the
data part of the tape.
vi. Replace the in the current address location C by bb (C < bb)
vii.Go back to stop (i) of the simulation procedure.
It is easy to see that each of the above simulation steps is well defined and thus one can specify a Turing machine to
execute it. In other words, by specifying for each simulation step a submachine, we can obtain a composite Turing
machine to execute the overall simulation. In other words: It is possible to specify a universal Turing machine
that can simulate any other Turing machine.
From the simple examples presented in the previous section and the definition of the universal Turing machine, we can
see that such a very simple abstract mechanism can execute all algorithms and thus, given enough time and memory, it can
execute all that a present digital computer can do. Furthermore from the definition of of reduced Turing machines we can
also draw the conclusion that in digital computers there is:
A balance of computing time versus the complexity of the machine.
A balance of memory requirement versus the complexity of the machines.
Problem Solving & Algorithms 2 - 69
2.4.4 The Infinite Abacus
The infinite abacus, introduced before
44
, is a conceptually much simpler alternative of the Turing machine, who like its
primitive namesake is best suited for data processing applications. The infinite abacus
45
also called Lambeks abacus as
it was originally proposed in 1961 by Joachim Lambek (1922 - ). It is interesting to note that also in 1961, Marvin Lee
Minsky (1927 - ) introduced independently an equivalent machine which he latter called program machine
46
As a
result in the literature the infinite abacus is also known under the name of Minsky machine or register machine,
since Minski defined the machine in terms of registers instead of abacus columns.
An aside on the coincidence of simultaneous innovations. In science, quite often two or more researchers
announce nearly simultaneous advancements, this results from the fact the field was ready (the proper background was
laid) for the given innovation. This is even more prevalent in modern times since most researchers in a given field read
and publish in the same journals. The more interesting coincidence was that both Lambek and Minsky used two
equivalent operations (instructions) in defining their machines. An increment operator of the form INC (X, i) that
increments the value stored in location X and jumps to the i-th instruction; and a conditional decrement operator of the
form DEC (X, i, j) that decrements the value stored in location X if it is not zero and jumps to the i-th instruction, and
if zero it leaves the content of X unchanged and jumps to the j-th instruction. This coincidence can be explained by the
fact that many mathematicians postulated their results using minimalistic operations such as successor and
predecessor functions. Furtermore by 1960 the use of computers in academia was widespread and it was well
known that in order to automate computations there is need for conditional transfer.
The infinite abacus has a countable infinite number of addressable locations, A
1
, A
2
, A
3
, (like the different
columns of the abacus or the different registers of a register machine), each of which can store any number 1, 2, 3, of
counters (beads used to keep count). The operation of the infinite abacus is controlled by a program that consists of a
finite set of instructions stored in the programming and control unit of the machine; see the shown figure. A
computation of the infinite abacus corresponds to the execution of its program, with given start instruction and some
initial set of data.

A
1
A
2
A
3
A
n
1. xxx
2. yyy
3. zzz

Programming
& Control
Unit
Note that although both Lambek and Minsky and also most others compare the infinite abacus (or the register machine)
with the modern digital computer (of their time) this is only true with regards to its computational capability.
Considering its basic organization, the infinite abacus, resembles a Harvard machine not a von Neumann machine. As
indicated in the above figure, the infinite abacus like a Harvard machine includes a separate program memory that controls
a number of distinct computing units that also serve as its data memory.
Problem Solving & Algorithms 2 - 70
44
See section 1.2.4.4 definition and examples, and section 1.3.2.2 a machine language for programming the infinite abacus. If you did not
cover these portions of Chapter 1, you do not need to do it now. The pertinent material will be presented again, albeit in a slightly different
form more suited to the presentation of this chapter.
45
Joachim Lambek How to Program an Infinite Abacus, Canadian Mathematical Bulletin, vol. 4 no. 3, September 1961,
pp. 295 - 302. In this paper Lambek also shows that the Infinite Abacus can do all that a Turing machine can do.
46
Marvin L. Minsky Recursive Unsolvability of Posts Problem of Tag and Other Topics in Theory of Turing Machines, Annals of
Mathematics, vol. 74 no. 3, November 1961, pp. 437 - 455. In this paper he called it arithmetization device, but in his book
Computation: Finite and Infinite Machines published by Prentice - Hall, in 1967, he elaborated on it and called it program machine, see
Chapter 11.
Regarding the instruction set of the infinite abacus, beyond the above specified INC (X, i) and DEC (X, i, j)
instructions, minimally there is need to include to additional instructions SET (X=v, i) used to initialize the content of
location X, and HLT that stops the machine. Therefore the minimal instruction set of the infinite abacus, with x and x
representing respectively the content of location X before and after executing the instruction, is as follows:
i. INC (X, i ) add 1, that is x = x + 1 and fetch the next instruction from the i-th location of the
program memory.
ii. DEC (X, i, j) if x 0 subtract 1, that is x = x - 1 and fetch the next instruction from the i-th location
of the program memory.
if x = 0 leave it 0, that is x = x and fetch the next instruction from the i-th location of the
program memory.
iii. SET (X=v, i) initializes to v, that is x = v and fetch the next instruction rom the i-th location of the
program memory.
iv. HLT halts the machine.
Here we assumed that one can observe (see the addressable locations, otherwise one needs an fifth instruction to display
specified column or columns. That is:
v. DSP (X, i ) display x, with x = x and fetch the next instruction from the i-th location of the
program memory.
To indicate the main characteristics of the infinite abacus we shall consider two simple examples:
Example 1: Infinite Abacus Adder a + b = c
It is required to develop a program that adds two positive numbers a and b originally placed in locations A
1
and A
2
and
puts the sum c into location A
3
. It is required that the values a and b should retained in A
1
and A
2
.
In the case of an infinite abacus the operation of addition (subtraction) can be implemented quite simply by repeatedly
subtracting 1 from one of the addends (subtrahend) and adding 1 to the other addend (subtracting 1 from the minuend)
until the first addend (subtrahend) becomes 0. This also means that as a result of addition (subtraction or any other
operation) both original operands are modified; hence if there is need to retain them one must store them in some
temporary locations, say A
4
and A
5
, and restore them latter. Also, if not stated otherwise, one cannot assume that the
unspecified locations are empty, therefore there is need to zero the result and temporary locations before starting the
computation. Basically this means that in writing an infinite abacus program one most consider three distinct phases:
initialization, computation and termination.
Initialize: 1 DEC (A
3
, 1, 2)
Set A
3
, A
4
and A
5
to 0, and load a into A
1
2 DEC (A
4
, 2, 3)
and b into A
2
. 3 DEC (A
5
, 3, 4)
4 SET (A
1
= a, 5)
5 SET (A
2
= b, 6)
Compute: 6 DEC (A
1
, 7, 9)
Copy a into A
3
and A
4
add b to A
3
and also 7 INC (A
3
, 8)
copy it into A
5
. 8 INC (A
4
, 6)
9 DEC (A
2
, 10, 12)
10 INC (A
3
, 11)
11 INC (A
5
, 9)
Terminate: 12 DEC (A
4
, 13, 14)
Copy A
4
and A
5
into A
1
and A
2
, and stop. 13 INC (A
1
, 12)
14 DEC (A
5
, 15, 16)
15 INC (A
2
, 16)
16 HLT
Problem Solving & Algorithms 2 - 71
In this program in the initialize phase the content of locations A
3
, A
4
and A
5
were zeroed by emptying their original
content, whatever they contained; alternately one just could set them to 0 by using the SET instruction. Also not that the
terminate phase not only restored A
1
and A
2
, it also set A
4
and A
5
to 0.
In section 1.2.4.4 in defining the infinite abacus, it was shown that its operation could also be represented by a labelled
directed graph whose nodes correspond to the instructions of its program. A similar labelled directed execution graph is
shown below for the this infinite abacus adder, in which the instructions are represented by numbered circles and the
corresponding operations. In the case of the DEC instruction only the 0 successor arrow is labelled.
3
DEC A
5
2
DEC A
4
1
DEC A
3
5
SET A
2
4
SET A
1
8
INC A
4
7
INC A
3
6
DEC A
1
11
INC A
5
10
INC A
3
9
DEC A
2
14
HLT
13
INC A
1
12
DEC A
4
15
INC A
2
14
DEC A
5
0
0
0
0
0
0
0
Initialize Compute Terminate
Start
Checking the above execution graph it corresponds to a state diagram. A state diagram in which the successor state
(next state) depends whether the associated location is empty or not, not on the actual value (content), and in which the
operation (INC, DEC, SET, and HLT) is associated with the state, not with the transition. In other words, such a state
diagram can be represented by the shown transition table that includes only one entity in the matrix, the designated
successor state, and the operation is associated with the columns of the table.
q
1
q
2
q
3
q
4
q
5
q
6
q
7
q
8
q 2 q 3 q 4 q 9
q
9
q
10
q
11
q
12
q
13
q
14
q
15
q
16
q
1
q
2
q
3
q
5
q
6
q
7
q
8
q
6
q
10
q
11
q
9
q
13
q
12
q
15
q
14
q 12 q 14 q 16
DEC
A
4
INC
A
2
SET
A
2
DEC
A
3
DEC
A
1
DEC
A
5
DEC
A
2
DEC
A
4
DEC
A
5
INC
A
1
INC
A
5
INC
A
3
INC
A
4
INC
A
3
SET
A
1
HLT
- - - - - - - -
-
-
Present
State
Location
Content
Non
0
0
Associated
Operation
Example 2: Infinite Abacus Divider a b = c (integer portion)
It is required to develop a program that computes the integer portion of the quotient of two positive numbers a and b
originally placed in locations A
1
and A
2
. There is no restriction on retaining, or not the original numbers nor on the
location of the result. Furthermore it is assumed that initially the contents of all locations are 0; A
i
= 0 for all i.
Conceptually the computation of the integer portion of the quotient is quite simple: One subtracts repeatedly originally
from the dividend, and later from the remainder, the divisor incrementing each time the partial quotient (say in A
3
) until
the remainder becomes negative. The only problem that arises is caused by the fact that each subtraction via an infinite
abacus, reduces the original value of the dividend to 0. Care must be taken to store its value (during subtraction) in some
temporary location (say in A
4
) at restore it before starting a new cycle. The corresponding program is as follows:
Initialize: 1 SET (A
1
= a, 2)
Load A
1
and A
2
with a and b. 2 SET (A
2
= b, 3)
Problem Solving & Algorithms 2 - 72
Compute: 3 DEC (A
2
, 4, 6) - b first, increment c & start new cycle.
Subtract b repeatedly from A
1
and check if A
2
(b), 4 DEC (A
1
, 5, 9) - Remainder first, go to stop
or if A
1
(remainder) becomes first 0. 5 INC (A
4
, 3)
Increment partial quotient 6 INC (A
3
, 7)
Restore dividend and start new cycle 7 DEC (A
4
, 8, 3)
8 INC (A
2
, 7)
Terminate: 9 HLT
The drawing of the corresponding state diagram and transition table is left as an exercise.
Alternate Infinite Abaci
In the literature one can find a number of alternate register machines or abaci, most being just slight modifications of the
above introduced abacus. Here we shall introduce an alternate abacus, one that has quite different ternary instruction that
was proposed by Zdzislaw Alexander Melzak
47
(1926 - ). We shall also present a modification, an infinite abacus that
uses binary commands that is much simpler to program.
Melzaks Abacus
Physically Melzaks abacus, like the infinite abacus has a countable infinite number of addressable locations,
A
1
, A
2
, A
3
, each of which can store any number 1, 2, 3, of counters, with the difference that it also includes a
source location S with indefinitely large number of counters. The difference between the two abaci is in their
arithmetic instructions (command), while the infinite abacus uses two unary instructions (DEC and INC), Melzaks
abacus uses a single ternary instruction of the form:
XYZ, i, j were XYZ represent addressable locations such that Y S, XYZ AAA and X Z,
and i, j are the addresses of two possible successor instructions.
The execution of this ternary instruction has two possible outcomes:
if x y, then x = x - y, y = y, z = z + x and the next instruction is the i-th instruction in the list,
indicated graphically by a labelled arrow y.
if x < y, then the contents of the three locations remain unchanged and the next instruction is the j-th
instruction in the list, indicated graphically by a labelled arrow n.
From this definition it can be seen that Melzaks ternary instruction is a composite instruction; what it implements depends
on the selected location. For example:
SXY corresponds to ADD instruction; s = s + x = s (s being indefinitely large), x = x and y = y + x.
XYS corresponds to conditional SUB instruction; if x y, x = x - y, y = y and s = s.
Checking Melzaks examples, one can see that these are the two instructions that are most often used. He also uses the
instructions XXY for adding x to y ( y = y + x) and setting x to 0 (x = 0), and XXS for setting x to 0 (x = 0).
Example 3: Melzaks Abacus Multiplier a x b = c
Develop a program to implement the multiplication of a x b = c using Melzaks ternary instruction. To simplify the
program lets assume that the initial data in locations A
1
, A
2
, A
3
is a, b, 1 and all other locations contain zero.
i. A
2
A
3
S, j, k subtracts 1 from b
j. S A
1
A
4
, i, i adds a to A
4
k.HLT stops after b repeated additions of a
Since it is a bit tricky to program using Melzaks commands, or even to follow his examples, I decided to introduce here a
modified Melzaks abacus, that uses for arithmetic operation two binary instructions similar to Lambeks unary
instruction.
Problem Solving & Algorithms 2 - 73
47
Z. A. Melzak An Informal Arithmetical Approach to Computability and Computation Canadian Mathematical Bulletin, vol. 4 no. 3,
September 1961, pp. 279 - 293. Melzal calls his abacus Q-machine, but we shall refer to it as Melzaks abacus. Note that it appeared in
the same issue as Lambeks Infinite abacus; Lambek refers to this article and states that his abacus can be regarded as a modification of
Melzaks.
Modified Melzaks Abacus
The modified Melzaks Abacus, physically is exactly the same as Lambeks abacus, it includes a countable infinite
number of addressable locations, A
1
, A
2
, A
3
, , each of which can store any number 1, 2, 3, of counters.
Note that it does not contain a source location. To execute arithmetic operations it includes the following two binary
instructions.
i. ADD (X, Y, i) add y to x, that is x = x + y, y = y and fetch the next instruction from the i-th
location of the program memory.
ADD (X, X, i ) is defined doubling X, that is x = 2x.
ii. SUB (X, Y, i , j) if x y subtract y from x, that is x = x - y, y = y and fetch the next instruction from
the i-th location of the program memory.
SUB (X, X, i , j) is defined as setting X to zero, that is x = 0.
if x < y subtract x from y placing the difference in X, that is x = y - x, y = y and
fetch the next instruction from the j-th location of the program memory.
The other instruction, HLT, SET (X = v, i) and DSP (X, i ) remain the same.
In terms of programming there is not much difference between this or Lambeks abacus, except that it is a bit simpler
since additions and subtractions are done in one step, and the second operand is unchanged. To indicate this consider, the
following two simple examples:
Example 4: Abacus Integer Square-root Extractor a
1/2
= b (integer portion)
Develop a modified Melzaks abacus program that extract the integer portion of a based on the equality that the sum of odd
integers from 1 to 2n +1 is equal to n
2
.
A solution procedure is to start with a in A
1
and and subtract repeatedly consecutive odd numbers starting with 1, and
increment the partial result stored in A
2
after each successful subtraction. The consecutive odd numbers are obtained by
starting with 1 and adding repeatedly 2. From this it can be seen that two implement the program one needs three
temporary locations to store the next odd number and the coonstants 1, 2. The corresponding program is as follows:
Initialize: 1. SET (A
1
= a, 2)
Load a into A
1
, set all initial values and 2. SET (A
2
= 0, 3) partial and final result
load constants 1 and 2. 3 SET (A
3
= 1, 4) next odd number starting with 1
4. SET (A
4
= 1, 5)
5. SET (A
5
= 2, 6)
Compute: 6. SUB (A
1
, A
3
, 7, 9)
Subtract repeatedly current odd number, increment 7. ADD (A
2
, A
4
, 8)
partial result and compute next odd number. 8. ADD (A
3
, A
5
, 6)
Terminate: 9. HLT
Example 5: Abacus Sorter
Develop a modified Melzaks abacus program that sorts two numbers a and b originally loaded in locations A
1
, A
2
, in
order of their magnitude.
A solution procedure is to compare the two number by subtracting say b from a, if a b, they are sorted, otherwise
reorder them. Note that when executing SUB, the minuend is modified differently for a b and a < b. Therefore to be
able to restore a if a < b, there is need to store a temporarily. The same temporary location is also used in reordering the
two numbers.
Initialize: 1. SET (A
1
= a, 2)
Load a and b into A
1
, A
2
, and set initially A
3
, to 0. 2. SET (A
2
= b, 3)
3 SET (A
3
= a, 4)
Problem Solving & Algorithms 2 - 74
Compute: 4. SUB (A
1
, A
2
, 5, 6)
Compare the two numbers: 5. ADD (A
1
, A
2
, 12) restore a for a b
if a b restore a and stop. 6. ADD (A
1
, A
3
, 7) restore a for a < b
if a < b restore a and reorder them. 7. SET (A
3
= 0, 8)
8. ADD (A
3
, A
2
, 9) - transfer b into A
3
9. SET (A
2
= 0, 10)
10. ADD (A
2
, A
1
, 11) - transfer a into A
2
11. SET (A
1
= 0, 12)
12. ADD (A
1
, A
3
, 13) - transfer b into A
1
Terminate 13. HLT
Signed Abacus Machines
All the above presented abaci only manipulate positive integers. A main reason for this restriction is that these machines
were introduced to arithmetize Turing machines. This required to prove that they can execute all Turing-computable
(recursive or effectively computable) functions; for this there was need to only operate on natural numbers; zero and
positive integers. As some originators of the infinite abaci and register machines, also suggest their applicability as a
teaching (demonstration) tool to show both the basic simplicity and mechanistic nature of digital computers; this raises the
question if it is possible to lift this restriction. As a teaching tool it would make sense to allow them to manipulate both
positive and negative integers.
Since the use of negative numbers is a fairly recent development in arithmetic (around the 7-th century in India and the
16-th in Europe), all the primitive or even modern abaci only manipulate magnitudes. On the other hand, it is quite simple
to augment them to represent all integers, both positive and negative. This can be done by adding to each column of the
abacus a sign counter position, and use the signed absolute value convention adopted in digital computers: a 0 (no
sign counter) for positive values, and 1 (sign counter) for negative values.
The next question relates to the instruction set needed to operate such a signed abacus. Interestingly there are two
quite distinct possibilities: a computer oriented one, and another that is more abacus oriented.
The computer oriented one, to implement arithmetic operations uses the subtract and branch if negative SBN
instruction
48
, a single machine language instruction sufficient to implement all Turing-computable functions. Using the
above introduced notation SBN is defined as;
SBN (A, B, +1, j) it executes the signed subtraction A - B placing the signed result in A leaving B
unchanged; if the result is positive it fetches the next instruction from the next program
memory location, and if the results is negative it fetches the next instruction from the j-th
location.
It is relatively easy to see that all other arithmetic operations and transferring content of one column to another can be done
by clever sequences of SBN instructions. The development of such programs is left as an exercise.This may look as an
elegant solution applied to computers or even register machines, but it is artificial when applied to abacus machines as the
SBN instruction does correspond to a direct manipulation of counters in abacus columns.
An alternate solution that relates more to abacus machines, is to implement signed arithmetic by using two types of
instructions. One that manipulates only the magnitude counters using the previously defined ADD and SUB instructions,
and another set of two unary instructions check sign (CKS) and change sign (CGS) that manipulate only the sign
counters. The two unary instructions are defined as:
i. CKS (X, i , j) that checks the sign position of column X: if 0 it fetches the next instruction from the i-th
location, and if 1 from the j-th location of the program memory.
ii. CGS (X, i ) that changes the content of the sign position (0 > 1, 1 > 0) and fetches the next
instruction from the i-th location of the program memory.
Note that HLT instruction stops the machine same as before, while the SET and DSP now initialize and display the
designated columns, both sign and magnitude counters.
Problem Solving & Algorithms 2 - 75
48
Computers that use this single machine language instruction for arithmetic are also referred to as single instruction computer
(SIC) or as one instruction set computer (OISC).
The following example indicates such a signed abacus program using these two types of instructions:
Example 6: Signed Abacus Adder X + Y = Z
Develop a signed abacus program to adds two signed numbers X and Y originally loaded into locations A
1
, A
2
, and
places the signed result Z into location A
3
, leaving A
1
, A
2
unchanged.
The solution procedure starts with checking the signs of the two numbers. It is easy to see that: if the signs are the same,
it is required to add the magnitudes of the two numbers, and if the signs are different one has to subtract one from the
other. Since the ADD and SUB instructions affect magnitudes (they leave the sign of the first operand unchanged), one
has to determine if the sign of the result is correct. It is clear that in the case of addition the sign is always correct, but in
the case of subtraction one has to make sure that the difference has the sign of the larger operand.
Initialize: 1. SET (A
1
= X, 2)
Load X and b into A
1
and A
3
, and Y into A
3
. 2. SET (A
2
= Y, 3)
3 SET (A
3
= X, 4) included to leave X in A
1
Compute: 4. CKS (A
1
, 5, 6)
Check the signs of the two numbers: 5. CKS (A
2
, 7, 8)
if the same signs execute ADD. 6. CKS (A
2
, 8, 7)
if not the same signs execute SUB. 7. ADD (A
3
, A
2
, 10) X and Y have the same sign
8. SUB (A
3
, A
2
, 10, 9) X and Y have different signs
9. CGS (A
3
, 10) Y > X
Terminate 10. HLT
2.4.5 New Vocabulary
Note that in the vocabulary TM stands for Turing machine and A for abacus. Also, even if not explicitly stated most
of the general terms refer either to Turing machines or abaci.
abacus program accept state complexity of TM
complutability decision problem external alphabet of TM
functional matrix of TM infinite abacus initial state
instruction set of A instruction table of TM internal alphabet of TM
internal state of TM Lambeks abacus logic unit of TM
Melzaks abacus Minsky machine modified Melzaks abacus
phases of abacus program programming & control unit of A read-write head
reduced alphabet TM reduced state TM register machine
reject state sign counter signed abacus
state diagram representation tape cells tape configuration
transition table representation Turing machine TM acceptor
TM as a finite state machine TM computer TM quintuples
TM simulator TM transducer universal TM
value counters
Problem Solving & Algorithms 2 - 76
2.4.6 Exercises on Turing Machines
Note: For each of the following Turing machine problems give the initial and final tape configurations indicating start and
stop conditions. Give detailed explanations with intermediate tapes, functional matrix and state diagram
representations. In some problems you may need to add letters to the external alphabet.
1. Design a Turing machine that transforms a finite number of slashes into the equivalent binary number.
2. Design a Turing machine that adds two positive binary numbers, separated by a plus sign.
3. Design a Turing machine subtractor, like the one defined in Example 3, but one that works on the principle of adding
a 2s complement number.
4. Design a Turing machine that performs multiplication X*Y=Z. The numbers are represented by an equivalent
number of slashes.
5. Design a Turing machine that computes 2
n
, assume n to be a positivebinary number.
6. Design a Turing machine comparator. The initial tape configuration consists of a string of intermixed n zeros and m
ones embedded between blanks. The final tape should include k = |n - m| zeros or ones, depending if n > m or n < m.
Design the machine as the combination of following two submachines:
i. A sorter that separates the zeros from the ones.
ii. A subtractor which erases equal number of zeros and ones, until only zeros or ones are left.
6. Special problem: Design a Turing machine "memory" whose tape organization is as shown below:
bb(address)(data) * [(address)(data )] [(address)(data)] [(address)(data)]bb
present address memory words
Assume that both the addresses and data are binary numbers with specified number of bits. Design the Turing machine
as composition of three sub-machines:
i. one that locates the required memory word by matching the two addresses
ii. one that reads from the memory by copying the data part of the specified memory word into the data part of the
present address
iii. one that writes into the memory by copying the data part of the present address into the data part of the specified
memory word.
Note: The next two exercises are advanced and they require much more work.
7. Using the "ideas" presented in discussing reduced Turing machines implement reduced machines for a simple Turing
machine, say, Example 2 of Section 2.3.2 as:
a. reduced-alphabet Turing machine.
b. reduced-state Turing machine.
In defining these machines, do not use a generalized procedure.
8. Corresponding to each of the simulation steps of the presented Universal Turing machine, define a Turing machine
that can execute it.
Note: For each of the following abacus machine problems outline the solution procedure and indicate the context of all
specified columns at the start, critical points and end of computation. Annotate your programs.
10. Develop the infinite abacus programs to:
i. transfer of the content of one column to another, leaving the original column unchanged.
ii compute the absolute value of the difference of two numbers a and b; c = | a - b |.
iii.compute the multiplication of two numbers a and b; c = a x b.
iv. compute c = a
b
.
Problem Solving & Algorithms 2 - 77
11. Develop the modified Melzaks abacus programs to:
i. detect if a given number a is even or odd by placing 0 or a 1 in some other column.
ii. compute the greatest common divisor of two numbers; c = gcd(a, b).
12. Develop the signed abacus programs to:
i. transfer of the content of one column to another, leaving the original column unchanged.
ii. compute the multiplication of two signed numbers X and Y; Z = X x Y.
13. Develop the sequences of SBN instructions needed to:
i. transfer the content of one column to another, leaving the original unchanged.
ii. execute the addition of two signed number A and B; C = A + B.
iii.execute the multiplication of two signed number A and B; C = A x B.
Problem Solving & Algorithms 2 - 78

You might also like