2-tm
2-tm
TURING MACHINES
Mihai-Valentin DUMITRU
[email protected]
October 2024
In the first part of the AA course we will focus our attention on what it is exactly that computers can or cannot do.
We shall study these issues within the framework of “computability theory”, a branch of mathematics. This means
that our approach will bear all the characteristics of mathematical rigor: we will try to capture intuitive concepts as
formal definitions and logical axioms; then we will use inference rules to derive new statements that are provably
true in our framework.
Our main question can be informally phrased as: “What problems can algorithms solve?” In order to approach
this question, we shall first need to precisely define what we mean when we use the vague terms: “problems”,
“algorithms” and “solve”.
The concept of “problem” has been addressed in the previous lecture; for our purposes, a problem is a function
from a set of strings to another. A key part of this definition is that both the domain and the codomain can be
infinite (and the domain usually is), but all their member strings must be finite in length.
We now turn our attention towards what an algorithm is and what it means for an algorithm to solve a problem.
1 Informal view
A Turing Machine is a computational model proposed by Alan Turing in 1936. It is a rigorous mathematical ab-
straction, designed to capture the intuitive notion of “computability”. However, it can be visualized as a physical
device with tangible components and visible movement, which makes it very easy to think about without delving
into symbolic notation.
• The tape is one-dimensional and split into equally-sized cells. It extends indefinitely in either direction, so
we can never “run out of tape”.
– The machine is in some state and its head is reading some cell.
– Depending on the combination of internal state and currently read symbol, the machine makes a par-
ticular transition.
3. It moves the head to point to the cell immediately on the left or right of the current one; or leaves
it where it is.
† This “new” state can be the same as the old one; i.e. the machine remains in the same state.
‡ As with the states, this “new” symbol can be the same as the old one; i.e. the machine leaves the cell unchanged.
1
Analiza Algoritmilor Turing Machines
Figure 1: Artistic visualization of a Turing Machine. You can see the tape, split into cells and unbounded in
both directions. Here, tape symbols are illustrated as different shades of gray. The printer-like object depicts the
“internal control” of the machine – the part that knows how to transition and which memorizes the current internal
state. The thin rectangle on its right, hovering above a single cell, is the head.
Image taken from: https://commons.wikimedia.org/wiki/File:Maquina.png
2 Formal definition
We shall now summarize the informal presentation from section 1 into a formal definition.
In the interest of brevity, we will use Q′ to denote the extended set of states Q ∪ {Y, N, H}.
We will also use Γ′ to denote Γ \ {B}, which we will need to refer to quite often.
The “states” and “symbols” should be thought of as atomic objects. To avoid any confusion, we’ll always consider
these sets to be disjoint: Γ ∩ Q′ = ∅.
2
Analiza Algoritmilor Turing Machines
Similarly, the three head movement directions are not members of any of these sets:
• {←, −, →} ∩ Γ = ∅
• {←, −, →} ∩ Q′ = ∅
3 Machine description
Having established a formal definition of Turing Machines, we can now start describing individual machines that
accomplish various tasks.
Here is our first machine, which we shall name isEven. It take its input – a big-endian§ binary number and checks
whether it is even (i.e. the last digit is 0).
Before trying to explain how the machine works, we should remark how arcane and intimidating this notation
looks like. Fortunately, there are clearer ways of representing a Turing Machine!
0 1 □
q1 q1 , 0, → q1 , 1, → q2 , □, ←
q2 Y, 0, − N, 1, − N, □, −
Looking at the table itself, other information about our machine can be easily extracted: namely the set of tape
symbols Γ and the set of states Q.
To be able to figure out the other elements of the 6-tuple, we establish the following conventions:
• the initial state will always be on the first row of the table
• the order of the columns will be such that first come all the input symbols, then the blank symbol □, then all
other tape symbols
Now we can represent our entire Turing Machine 6-tuple with a concise transition table; conversely, we can look
at such a transition table and identify each element of the 6-tuple.
§ Most significant digit first.
3
Analiza Algoritmilor Turing Machines
4 isEven explained
The machine starts in the initial state, q1 ; this state corresponds to the intuitive concept of “searching the end of
the input” – thus, at each step, the head is moved one position to the right, leaving each symbol as it is, until a
blank symbol is found. This is captured by the first two entries of the table.
Once on the blank symbol, the whole input must have been traversed, so the head needs to be moved one cell
left onto the last symbol of the input and the machine transitions to state q2 , which corresponds to the intuitive
concept of “checking what the last symbol is”. This is captured by the last entry in the first row.
If this symbol is a 0, the number is even, so it’s accepted by transitioning to state Y , providing a “yes” answer. The
other two elements of the transition action (namely the written symbol and the movement direction of the head)
are irrelevant because computation halted. But we choose the intuitive option of keeping the symbol as it was (a
0) and holding the head still.
If the symbol is a 1, the number is odd, not even, so it’s rejected by transitioning to state N , providing a “no”
answer.
Note that in state q1 the head is only moved right, traversing the whole input and stopping at the very first blank
symbol. This means that, in q2 , if the blank symbol is encountered, there is only one possibility: the input was
empty.
0 1 □
start remember0, 0, → remember1, 1, → Y, □, −
remember0 remember0, 0, → remember0, 1, → expect0, □, ←
remember1 remember1, 0, → remember1, 1, → expect1, □, ←
expect0 Y, 0, − N, 1, − N, □, −
expect1 N, 0, − Y, 1, − N, □, −
This time, we have chosen to give each of the five states a suggestive name, instead of generic ones like q1 , q2 etc.
From the machine’s point of view, the names are irrelevant; their purpose is simply to make things clearer for us.
We can see here an example of memorization: from the start state, depending on the contents of the first cell of
the input, the machine will transition to one of two states: remember0 or remember1. The states themselves acts
as memory, in this case retaining one bit of information.
†† Note that we could also rephrase this in a way that avoids the concept of “number representation” entirely: “is the last symbol of
4
Analiza Algoritmilor Turing Machines
Some entries in the table might surprise you. As with our first example, we need to think of a way to deal with the
empty input; we choose to accept it, by transitioning to the Y state, but this is just a convention, we could just as
well transition to N .
But what about the transition on (expect0, □)? Such a situation can never occur:
• if the input was empty, the machine goes from start directly to Y and halts, never reaching any other state
• if the input starts with a 1, then the machine will transition to remember1 and we can see, by looking at the
table, that expect0 can never be reached.
• if the input starts with a 0, then the machine will transition to remember0, will remain in this state while
the head goes right until the end of the input, then it will transition to expect0, as the head moves left onto
the last input symbol. Then the machine will transition to either Y or N and halt.
So the machine will never need to “know what to do” if it’s in state expect0 and reads a □ – this situation cannot
occur for any input.
We arbitrarily choose to halt computation with a negative answer (i.e. transition to N ), leave the symbol as it is and
keep the head still. From now on, we shall omit explicitly describing such transitions, by leaving the corresponding
entry empty, to keep our tables lighter.
0 1 □
start remember0, 0, → remember1, 1, → N, □, −
remember0 remember00, 0, → remember01, 1, → N, □, −
remember1 remember10, 0, → remember11, 1, → N, □, −
remember00 remember00, 0, → remember00, 1, → expect00, □, ←
remember01 remember01, 0, → remember01, 1, → expect10, □, ←
remember10 remember10, 0, → remember10, 1, → expect01, □, ←
remember11 remember11, 0, → remember11, 1, → expect11, □, ←
expect00 expect0, 0, ← N, 1, −
expect01 expect1, 0, ← N, 1, −
expect10 N, 0, − expect0, 1, ←
expect11 N, 0, − expect1, 1, ←
expect0 Y, 0, − N, 1, −
expect1 N, 0, − Y, 1, −
• it’s empty
• its first and last symbols are the same and there’s a palindrome between them
Our machine, isPalindrome will thus need to check if the word is empty; if not, it will check the first and last
symbols for equality, then restart the same process for the string in between:
5
Analiza Algoritmilor Turing Machines
0 1 □
start remember0, □, → remember1, □, → Y, □, −
remember0 remember0, 0, → remember0, 1, → expect0, □, ←
remember1 remember1, 0, → remember1, 1, → expect1, □, ←
expect0 reset, □, ← N, 1, − Y, □, −
expect1 N, 0, − reset, 1, ← Y, □, −
reset reset, 0, ← reset, 1, ← start, □, →
Notice that state start here is almost the same as in the example of section 6, except the initial symbol is erased –
this is done so that we can later easily find the beginning of “the inner substring”.
remember0 and remember1 are identical to those of sameFirstLast; expect0 and expect1 are similar, but when
a match is found, we can’t just accept, we need to start checking the inner substring. So we remove the last symbol
(now the tape only contains the inner substring) and move on to a state called reset, whose purpose is to bring
the head onto the leftmost non-blank symbol, then redo the whole thing by transitioning to start.
0 1 □ 0 1
start f ind_1, ̸ 0, → f ind_0, 1, → Y, □, − start, 0, → start, 1, →
f ind_0 0, ←
reset, f ind_0, 1, → N, □, − f ind_0, 0, → f ind_0, 1, →
f ind_1 f ind_1, 0, → reset, 1, ← N, □, − f ind_1, 0, → f ind_1, 1, →
reset reset, 0, ← reset, 1, ← start, □, → reset, 0, ← reset, 1, ←
Note that we could also erase the first character of the string, instead of crossing it out.
We also don’t actually need to distinguish between crossed out symbols; on paper, that is just an artifact of our pen
strokes. But the machine can just as easily overwrite both 0s and 1s with an X, like this:
0 1 □ X
start f ind_1, X, → f ind_0, X, → Y, □, − start, X, →
f ind_0 reset, X, ← f ind_0, 1, → N, □, − f ind_0, X, →
f ind_1 f ind_1, 0, → reset, X, ← N, □, − f ind_1, X, →
reset reset, 0, ← reset, 1, ← start, □, → reset, X, ←
6
Analiza Algoritmilor Turing Machines
In the future, if we encounter such a problem, we can rely on this description and confidently describe our machine
as:
“Check if the input has more 0s than 1s.”
As we continue studying examples of Turing Machines, especially at the labs, we shall be able to abstract more and
more functionality into short phrases.
Bibliography
[1] Alan Mathison Turing et al. “On computable numbers, with an application to the Entscheidungsproblem”.
In: J. of Math 58.345-363 (1936), p. 5.
[2] Charles Petzold. The annotated Turing: a guided tour through Alan Turing’s historic paper on computability
and the Turing machine. Wiley Publishing, 2008.
§§ https://turingarchive.kings.cam.ac.uk/publications-lectures-and-talks-amtb/amt-b-12