5 Utm
5 Utm
We have seen various examples of Turing Machines designed to solve some particular problem. For each problem
we encountered, we created a new machine: we defined a new 6-tuple (usually implicitly, by just writing the
transition table), specifying what states the machine has, what tape symbol it uses, what transitions it can perform
etc.
We will now show that it is possible to create a single machine capable of doing anything that some Turing
Machine can do. In particular, this one machine could solve any solvable problem. We will call this a “Universal
Turing Machine” (UTM).
A UTM receives as input the encoding of a pair (M, w), consisting of some Turing Machine M and some string w,
then decodes the description of M in order to simulate how M would run when given input w.
The only thing that a UTM has to “know” how to do is read a Turing Machine description and correctly apply its
effects on the string w.
In order to prove such a machine can exist, we need to first design a proper encoding for arbitrary Turing Machines,
then specify the implementation of a UTM: how it can simulate another machine based on its encoding.
Here we abused notation a bit; in the expression w = σ1 σ2 ...σm , each σi refers to some symbol from Σ; however,
in the final expression enc(σ1 )enc(σ2 )...enc(σm ), each σi refers to a string consisting of a single symbol.
Put into words, the binary encoding of a word is the concatenation of the encodings of its symbols.
Theorem 5.1. There exists a binary encoding enc : Σ∗ → {0, 1}∗ , such that for any Turing Machine M =
(Q, Σ, Γ, B, q1 , δ), there exists a Turing Machine Mb = (Qb , {0, 1}, Γb , Bb , q1b , δb ) such that:
1
Analiza Algoritmilor Universal Turing Machines
• δb (raux , 0) = (raux , 0, ←)
• δb (raux , 1) = (r, 1, −)
If the transition doesn’t involve input symbols and the head moves left, we might encounter a binary encoding
of an input symbol; if we do, we need to skip over the 0s of our encoding to place Mb ’s head over the initial
1.
If we encounter a non-input symbol, we just go to the next state.
4. From any state q ∈ Q, when encountering a 1, Mb will need to determine the symbol from Σ whose encoding
it just came across. To do this, it will go right, counting each 0 and memorizing the number of 0s (using
states). So Mb will have the following transitions:
• δb (q, 1) = (q_sym, 1, →)
2
Analiza Algoritmilor Universal Turing Machines
• δb (q_sym, 0) = (q_sym1 , 0, →)
• δb (q_sym1 , 0) = (q_sym2 , 0, →)
• δb (q_sym2 , 0) = (q_sym3 , 0, →)
• ...
• δb (q_symn−1 , 0) = (q_symn , 0, →)
From each q_symi on any non-0 symbol, Mb will transition to a state whose purpose is to shift the right
part of the tape left, by i symbols, deleting the 0s that form the encoding of σi .
Then, looking at δ(q, σi ) = (r, g, D), it will either overwrite the remaining 1 with g, if g ∈ Γ \ Σ, or apply the
same mechanism as in item 3 to shift the right part of the tape to the right and insert a new symbol σj .
We can formulate and prove a similar theorem for accepting, rejecting or never halting on an input. Now we can
focus strictly on machines whose input alphabet is {0, 1}.
• g1 is 0
• g2 is 1
• d1 is left (←)
• d2 is right (→)
• d3 is hold (−)
†n must be at least four, because each TM Q′ consists at least of an initial states and the three final states Y , N , H.
‡r must also be at least three, because our TMs have at least three tape symbols: 0, 1 and □.
3
Analiza Algoritmilor Universal Turing Machines
enc(M ) = enc(δ)
3 Example encodings
We provide an example encoding for a Turing Machine previously studied: isEven.
0 1 □
q1 q1 , 0, → q1 , 1, → q2 , □, ←
q2 Y, 0, − N, 1, − N, □, −
First, we need to map the internal states and tape symbols to new names based on our convention.
q1 =⇒ q1
Y =⇒ q2
N =⇒ q3
H =⇒ q4
q2 =⇒ q5
0 =⇒ g1
1 =⇒ g2
□ =⇒ g3
Let’s take the transition δ(q2 , 0) = (Y, 0, −) and rewrite it according to the renaming scheme above:
δ(q5 , g1 ) = (q2 , g1 , d3 )
Next, we encode it as a binary string:
00000
| {z }
1 |{z}
0 1 |{z}
00 1 |{z}
0 1 |000
{z }
q2 0 Y 0 −
4
Analiza Algoritmilor Universal Turing Machines
We then to do this for every transition and concatenate all of them, separated by “11”s (remember that the order
is irrelevant, but we’ll use the ordering in which they appear above):
0101010100110100101001001101000100000100010110000010100101000110000010010001001000 ←-
11001000100010001000
(The line break, ←- is not part of the string, the string just wouldn’t fit inside this page).
Recall that the input to the Universal Turing Machine is a tuple (M, w), where M is a Turing Machine and w is a
word on which to simulate M .
To encode this tuple, we will simply concatenate the machine’s encoding with that of the input, separated by three
consecutive ones, “111”§ .
enc((M, w)) = enc(M )111w
With this in mind, let’s proceed to describing the Universal Turing Machine.
Definition 5.2. A “Universal Turing Machine” U is a Turing Machine such that for any Turing Machine
M = (Q, {0, 1}, Γ, B, q1 , δ) and string w, U [enc((M, w))] ≡ M [w].
This isn’t exactly true, but we opted to write it in a concise form. If M [w] computes some string v, then
U [enc((M, w))] will actually compute enc(v).
5 UTM behavior
A UTM is not unique, there’s an infinite number of machines which respect Definition 4; they may possess various
interesting characteristics, but our goal in designing one will be to keep its description clear and concise.
In order to easily visualize our Universal Machine (and write less text) we shall design it as a three-tape Turing Ma-
chine. Combined with our single-tape simulation for multitape TMs, we could then obtain a single-tape Universal
Turing Machine.
The three-tapes will have the following roles:
1. The first tape will contain the input: the encoding of a (M, w) tuple. The UTM will treat this as a “read-only”
tape – it will never modify any symbol on it.
2. The second tape will contain M ’s current state during the simulation, encoded as a string of zeros. E.g. if M
is in state q5 , the second tape will contain five 0s: “00000”
3. The third tape will simulate the contents of M ’s tape; in any given configuration of U , it will contain the
encoding of exactly those symbols that are part of the current configuration of M .
First, the UTM goes through an initialization phase, where it properly initializes the second tape with the encoding
of of M ’s initial state, q1 and the third tape with the encoding w.
Recall the single tape simulation of a multitape Turing Machine; similarly, U will precisely simulate the effect of
each transition of M , by using multiple transitions. We will refer to the simulation of one transition of M as a
“phase”.
At the beginning of each phase, U ’s first and second heads will be positioned over the first non-blank symbol of
their respective tape; the third head will match where M ’s head should be (on the first 0 of the encoding of M ’s
“current symbol”). Each phase consists of the following steps:
1. Move the first and second heads in tandem to the right, while they’re both reading 0.
If the first head reaches a 1 at the same time that the third head reaches a blank, it means that on the first
tape we’ve reached a transition relevant to the current state of M : go to item 3.
§ Our construction is carefully designed so that no string of three consecutive ones can appear inside the encoding of a Turing
Machine.
5
Analiza Algoritmilor Universal Turing Machines
2. If we’re not reading the encoding of a transition concerning the current state, we need to continue searching;
reset the second head to the leftmost non-blank symbol and move the first head right until it reaches two
consecutive ones, then on the cell immediately to the right of them. Go to item 1.
3. On the first tape, skip the following 1, then move the first and third head in tandem, while they’re both
reading 0.
If the first head reaches a 1 at the same time that the third head reaches a 1 or a blank, it means that on the
first tape we’ve reached a transition relevant to the current state and the currently read symbol of M : go to
item 5.
4. If we’re not reading the encoding of a transition concerning the current symbol, we need to continue search-
ing; move the third head left until it reaches a 1 (or blank, for the first symbol), then one cell to the right.
Move the first head right until it reaches two consecutive ones, then on the cell immediately to the right of
them. Go to item 3.
5. Assuming we’re in state qi reading symbol gj , we must now perform the necessary changes for M ’s transition:
δ(qi , gj ) = (qk , gl , dm )
(a) Erase the contents of the second tape. On the first tape, skip over a 1 and start copying each following 0
onto the second tape until reaching another 1. Then reset the second head onto the leftmost non-blank
symbol.
(b) on the third tape (the head should be on the 1 immediately after the current symbol’s encoding, or on a
blank if the current symbol was the last symbol), go left and remove 0s until reaching a 1. By “removal”
we mean shifting the entire right part of the tape, one cell to the left.
Once the symbol is erased, on the first tape, skip the 1 and go on the first of the following l 0s. On the
third tape, insert a 0 to the right. By “insertion”, we mean shifting the entire right part of the tape, one
cell to the right, then writing a 0 in the gap that appears.
Repeat these steps (move the first head one cell to the right; insert one 0 on the third tape) until the
first head reaches a 1. We have finished overwriting symbol gj with symbol gl .
(c) Move the first head one cell to the right and count the 0s (memorizing them with the help of auxiliary
states).
We now know the direction the head of M should move:
• If the head of M should stay where it is, move the third head left until it reaches a 1, then one cell
to the right (remember that were positioned on the last 0 of the encoding of the written symbol;
we do this to go to the first 0 of its encoding).
• If the head of M should move left, move the third head left until it reaches a 1, then once more,
then one cell to the right.
• If the head of M should move right, move the third head two cells to the right.
Doing this repeatedly, U will behave the same as M : if M [w] never halts, neither will U [enc((M, w))]’s simulation;
if M [w] accepts, so will U [enc((M, w))] etc.
6
Analiza Algoritmilor Universal Turing Machines
The construction in this lecture is adapted from the sections 9.1.2 and 9.2.3 of the textbook “Automata Theory,
Languages and Computation”, third edition [3].
Neither our construction, nor Turing’s, actually provides an explicit transition table for the Universal Machine, on
the basis that it would be too much effort to do so for no considerable benefit. But it would be interesting to “see”
such a machine in a way that allows us to appreciate its complexity. Fortunately, in his 1989 book “The Emperor’s
New Mind” [4], Roger Penrose dedicates around three physical pages of the last part of his second chapter to
printing out in full the binary encoding of a UTM.
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.
[3] John E Hopcroft, Rajeev Motwani, and Jeffrey D Ullman. Introduction to Automata Theory, Languages, and
Computation. Addison-Wesley, 2006.
[4] Roger Penrose. The emperor’s new mind. Oxford University Press, 1989.