Professional Documents
Culture Documents
Turing machines
3. is the tape alphabet (containing t, all of ⌃, and maybe some more symbols).
The Turing machine M begins in the designated start state, q0 2 Q. The input
word is on the tape at the left hand end, one symbol per cell, with all the infinitely
many cells after it being blank. The location of the read/write head is the current
cell : the initial current cell is the cell at the leftmost end of the tape.
A typical step consists of the following. The Turing machine M consults the
current state, q 2 Q and the tape symbol g 2 that is in the current cell. If
(q, g) = (q 0 , g 0 , L) then the TM goes to state q 0 2 Q, replaces the contents of the
current cell with g 0 , and moves one cell left along the tape (unless the current cell
is at the left hand end, in which case it stays put). If (q, g) = (q 0 , g 0 , R) then the
TM does the same thing, except it moves one cell right.
27
28 CHAPTER 3. TURING MACHINES
1. The current tape symbol g 2 is like an input symbol: just like for a DFA,
the combination of it and the current state q 2 Q completely determine what
happens next.
5. The tape alphabet is like the stack alphabet of a PDA, except that it must
contain both the input alphabet ⌃ and the ‘blank’ symbol, t.
6. The TM can’t tell when it is at the left hand end of the tape, just as a PDA
can’t tell when the stack is empty. Similarly to pushing $ onto the stack at
the beginning of running a PDA, each Turing machine generally starts by
marking the first cell, so that it can be identified later.
1. Unlike a DFA, a Turing machine can write on the tape, instead of just reading
input.
Unlike a PDA, the Turing machine can write anywhere on the tape, once it
has moved the read/write head to the desired cell. At each step the Turing
machine must move one cell left or one cell right, unless the read/write head
is at the left-most end, when a “move left” instruction results in staying put.
2. There is a single accept state, qa , and a single reject state, qr — the machine
stops as soon as one of these is entered.
Example 1.2. Consider the Turing machine M1 = ({q0 , q1 , qa , qr }, {a, b}, {a, b, t},
, q0 , qa , qr ) where is given by
a b t
q0 q0 , a, R q1 , b, R qr , t, L
q1 qr , a, L qr , a, L qa , b, L
Notice that when defining , we don’t need to specify what happens when the
machine is in state qa or qr : it immediately stops.
Here is a picture of M1 . It is very similar to pictures of DFAs/PDAs - only the
arrow labels and accept/reject states have changed.
29
Example 1.2, ctd Here are the configurations that M1 enters on input aab:
Example 1.2, ctd We have just seen that M1 accepts aab and rejects aba. Let’s
explore more of what M1 does, to determine L(M1 ). We observe that M1 :
• rejects the empty word;
• stays in the start state q0 whilst reading as. If it then sees a b, then goes to q1 ,
but if instead it reaches the end of the input word and sees a blank symbol,
it rejects.
• Accepts a word if the first b is immediately followed by the end of the word,
otherwise rejects.
So M1 recognises the regular language L(a⇤ b). Furthermore, on input a word w 2
L(a⇤ b), when M halts the tape contains wb. On input a word w 2
/ L(a⇤ b), when M
halts the tape contains w.
n
Example 1.5. We design a Turing machine M2 which recognises L2 = {02 : n
0}. You can show (fairly easily) that this language is not context-free.
High level description: Cross o↵ alternate 0s until the end of the input, rejecting
if there are an odd number of 0s that’s at least three. This will cross of half of the
remaining 0s. If there’s an even number of 0s, go back to the beginning of the word,
and repeat. If we get down to a single 0, then accept.
In this picture, we have adopted a standard shorthand for the labels, where if
a transition doesn’t change a symbol (formally, if the read symbol and written
symbol are identical), then we just write x ! R (or x ! L), rather than x ! x, R
(or x ! x, L).
2. Mark the first cell with a dot, so it can be recognised again later.
3. Read each letter of x until get to the first blank cell. Reject if it contains
either no # symbols, or more than one # symbol.
(a) If there are none before the #, look after the # for the first 0 or 1. If
there are none, accept. If there is one, reject.
(b) If find a 0 or 1 before the #, replace it with an X. Then look for the
first 0 or 1 after the #. If it matches, replace it with an X. If it doesn’t
match, or doesn’t exist, reject.
6. Go to Step 4.
(a) If find an a (or a· ) before seeing any bs or blank cells, replace it with an
X and go to Step 6.
(b) If instead find a b before seeing any as or blank cells, reject.
(c) If instead find a blank before seeing any as or bs, accept.
Proof. We design a single tape Turing machine S with L(S) = L(M ). Let
k be the number of tapes of M . Then S simulates the k tapes by storing their
information on its single tape.
The input alphabet of S is ⌃M . The tape alphabet S of S contains M , but
will also contain some new symbols. The TM S uses a new symbol, say $ 2 / M,
for the very first cell on the tape. It also uses a new symbol, say # 2
/ M , to mark
the end of the used portion of each of the k tapes. For each a 2 M , the alphabet
33
S contains both a and a⇤ . The ⇤s will be used to indicate where each of the k
read/write heads of M are at each step.
On input w = w1 . . . wn , the machine S starts by setting up its tape to simulate
all k tapes of M . That is, using the ideas from Lemma 2.1, S modifies the tape so
it contains
$w1⇤ w2 . . . wn # t⇤ # t⇤ # t⇤ . . . t⇤ # t t . . .
with k # signs.
To simulate a single move of M , the TM S reads the tape from the $ to the final
#. It goes into a state which depends on the k-tuple of letters that were marked
with ⇤s, which is possible since there are only | M |k possible such tuples. It then
goes back to the beginning of the tape, and goes through the current word a second
time, updating the ith part of the tape according to how M tells M to act on tape
i: it replaces the symbol with a ⇤ by whatever M writes there, and puts a ⇤ either
one cell left or one cell right.
If at any point during this update S moves one of the imaginary read/write
heads, say head i, to the right onto a #, this means that M would be moving
read/write head i onto a new blank cell of tape i. So, as in Lemma 2.1, S copies
all of the rest of the tape one cell further down, to free up a blank cell. Then it
continues the simulation as before. ⌅
The idea of nondeterminism for Turing machines is similar to that for NFAs or
PDAs: the transition function returns a set of permitted moves rather than a single
one, and the machine splite into several copies of itself (each with its own tape) to
carry out all of the branches of computation simultaneously in parallel.
The machine accepts if any branch of the computation leads to an accept state.
We will show that non-determinism doesn’t change the set of languages that
TMs recognise, although we will see in the second half of the course that it may
possibly have a significant e↵ect on their speed.
Definition 2.5. Let A be an alphabet. We describe a way to order all of the words
in A⇤ called the len-lex ordering (for length, then lexicographical ). First, order the
elements of A, say A = {a1 , . . . , an }. Then order the elements of A⇤ as follows:
That is, order the words by length, then within each length order them lexicograph-
ically (dictionary ordering).
Example 2.6. Let A = {0, 1}. We order A as 0, 1. Then the first few elements of
A⇤ in len-lex order are:
", 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101, 110, 111.
• If N reaches a halting state before cm then this branch stops when N halts.
If so, then we ignore the part of c after the last valid step. The empty word
corresponds to considering only the start configuration.
The machine D works as follows. Tape 1 holds the input word w, and never
changes (except for any marks we need to copy w to other tapes). Tape 2 records
N s tape on the current branch of its nondeterministic computation, with an “end of
word” symbol at the end so we can easily erase the tape afterwards. Tape 3 records
which branch of N s nondeterministic computations is currently being explored by
D. Tape 3 only uses the symbols in {1, . . . , b, t}. Initially Tapes 2 and 3 contain
the empty word.
D repeatedly carries out the following 3 steps:
1. Copy the input word, w, from Tape 1 to Tape 2. Put a symbol to mark the
end of the used portion of Tape 2.
(a) Find the set T of triples returned by N at the ith step of N ’s calculations.
(b) If ci > |T |, then go to Step 3.
(c) Let (q, x, D) 2 QN ⇥ N ⇥ {L, R} be the ci th triple returned by N.
i. If q = qa then accept.
ii. If q = qr then go to Step 3.
iii. Otherwise, do to Tape 2 whatever N would do at this point, accord-
ing to (q, x, D). Move the end of word symbol one cell down along
Tape 2 if necessary.
3. Replace the word c on Tape 3 with the len-lex next word in {1, . . . , b}⇤ (see
Tutorial Sheet 2 for how to do this). Erase Tape 2. Go to Step 1.