0% found this document useful (0 votes)
6 views7 pages

5 Utm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

5 Utm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

5.

UNIVERSAL TURING MACHINES


Mihai-Valentin DUMITRU
[email protected]
October 2024

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.

1 Encoding Turing Machines


1.1 Dealing with different alphabets
Remember that the definition of a Turing Machine involves alphabets. If each machine can have its own alphabet
of arbitrary size, holding arbitrary symbols, how can we hope to design a single machine to simulate all others?
It seems like we would have to specify a single machine with a single input alphabet and a single tape alphabet
that is somehow capable of “representing” every possible alphabet; an impossible task.
Instead, our Universal Turing Machine will only be “universal” in regards to all possible machines over a single
specific alphabet, namely Σb = {0, 1}.
However, the loss in generality is not as great as it might first seem, for reasons we will explore below.
For any alphabet Σ = {s1 , s2 , ..., sn }, we can create a “binary encoding” of possible strings.

Definition 5.1. A binary encoding of an alphabet Σ = {s1 , s2 , ..., sn } is an injection:

enc : Σ∗ → {0, 1}∗


with the property that:

∀w ∈ Σ∗ (w = σ1 σ2 ...σm ; σ1 , σ2 , ..., σm ∈ Σ), enc(w) = enc(σ1 )enc(σ2 )...enc(σm )

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:

∀w ∈ Σ∗ , (M [w] → v) ⇒ (Mb [enc(w)] → enc(v))

1
Analiza Algoritmilor Universal Turing Machines

Proof. We’ll denote the symbols of Σ: σ1 , σ2 , ..., σn , where n = |Σ|.


Our encoding function will map each σi to a string of 1, followed by i 0s. E.g., enc(σ4 ) = 10000
Mb will have a copy of of all states of M (Q ⊆ Qb ), as well as all tape symbols of M (Γ ⊆ Γb ). The blank symbol
of M is the blank symbol of Mb .
The initial state of Mb will be the initial state of M .
Like in the multitape-to-single-tape simulation, we will construct a machine Mb that simulates, in turn, each
transition of M , by going through multiple phases.

1. For any transition of the form:


δ(q, g) = (r, h, D), with q ∈ Q, r ∈ Q′ , g, h ∈ Γ \ Σ, D ∈ {−, →}
Mb will have a transition:
δb (q, g) = (r, h, D)
I.e. if the transition doesn’t involve input symbols and the head won’t move left, there’s nothing
special that needs to be done.

2. For any transition of the form:


δ(q, g) = (r, h, ←), with q ∈ Q, r ∈ Q′ , g, h ∈ Γ \ Σ
Mb will have a transition:
δb (q, g) = (raux , h, ←)
and:

• δb (raux , 0) = (raux , 0, ←)

• δb (raux , 1) = (r, 1, −)

• δb (raux , c) = (r, c, −), for any c ∈ Γ \ Σ

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.

3. For any transition of the form:


δ(q, g) = (r, σj , D), with q ∈ Q, r ∈ Q′ , g ∈ Γ \ Σ, σj ∈ Σ, D ∈ {←, −, →}
Mb will have a transition: δb (q, g) = (r_shif t_j_D, 1, →)
Where the purpose of the state r_shif t_j_D is to shift the rest of the tape contents right by j symbols,
filling the space created with j 0s, transition to state r and move the simulated head in direction D.
Moving the simulated head to the right, means moving the actual head just one cell to the right of the last
inserted 0.
Keeping the simulated head still, means moving the actual head to the left, past the inserted 0s, until it
reaches a 1.
Moving the simulated head to the left, means moving the actual head left, past the inserted 0s, until it reaches
a 1, then moving it once more to the left; if there is a 0 there, move it to the left until a 1 is encountered.
We won’t present explicitly the states and transition rules needed to achieve this effect, but they should seem
reasonable.

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}.

2 Encoding Turing Machines as binary strings


Like in the example above, where we encoded symbols of some alphabet Σ, we shall use a unary encoding for
states, symbols and directions.
Let M = (Q, {0, 1}, Γ, B, q0 , δ).
Remember that Q′ is a finite set of states, so there must be some natural number n, such that |Q′ | = n† Thus we
can rename all states of Q′ to q1 , q2 , q3 , ..., qn , with the convention:

• q1 is the initial state

• q2 is the accepting state Y

• q3 is the rejecting state N

• q4 is the halting state H

The index of the other states in Q′ can be arbitrary.


Similarly, Γ is a finite set of tape symbols, so there must be some natural number r, such that |Γ| = r‡ . Thus we
can rename all symbols of Γ to g1 , g2 , g3 , ..., gr , with the convention:

• g1 is 0

• g2 is 1

• g3 is the blank symbol

The index of the other symbols in Γ can be arbitrary.


Right now, we can binary encode the first five elements of our 6-tuple. We proceed to design an encoding for the
transition function, but first, we will employ the following convention about head movement direction:

• 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

With the above conventions, each transition T has the form:


δ(qi , gj ) = (qk , gl , dm )
We encode such a transition as:
enc(T ) = 0i 10j 10k 10l 10m
Where “0i ” means “the symbol 0 repeated i times. Note this is just a sequence of the unary encodings of each
number, separated by a 1.
Remember the domain and range of the transition function:
δ : Q × Γ → Q′ × Γ × {←, −, →}
Each Turing Machine has a finite number of transitions, exactly |Q| · |Γ|, or n · r.
To encode the entire transition function, we simply take the encoding for all the transitions and concatenate them,
separating them with a “11”.
enc(δ) = enc(T1 )11enc(T2 )11enc(T3 )11...enc(Tnr−1 )enc(Tnr )
The order that the transitions appear in is irrelevant and can be arbitrary.
Note that the encoding of a transition cannot contain two consecutive 1s, so there is no ambiguity as to where a
transition’s encoding ends.
As you will soon see, it turns out that all the relevant information about our machine M is contained in the
encoding of the transition function. Thus, we conclude this section with:

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.

4 Formal definition of the UTM

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 References and further reading


The Universal Machine plays a crucial role in Turing’s 1936 article: “On computable numbers, with an applica-
tion to the Entscheidungsproblem” [1]. Turing introduces a special notation that allows him to “metaprogram”
machines by parametrizing some state templates; this is described in section 4, “Abbreviated tables”. In section
5, he defines an encoding format for machines, as a base-10 number. Then sections 6 and 7 present the detailed
construction of such a Universal Turing Machine.
“The annotated Turing” [2] covers these sections in chapters 7, 8 and 9. It also provides later corrections by Emil
Post and Donald Davies that fix some bugs in Turing’s article.

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.

You might also like