Turing
Turing
Turing Machines
19.1 Introduction
• Turing machines will be our ultimate model for computers, so they need
output capabilities.
• But computers without output statements can tell us something.
• Consider the following program
1. READ X
2. IF X=1 THEN END
3. IF X=2 THEN DIVIDE X BY 0
4. IF X>2 THEN GOTO STATEMENT 4
19-1
CHAPTER 19. TURING MACHINES 19-2
• The input word is presented to the machine on the tape with one
letter per cell beginning in the leftmost cell, called cell i.
• The rest of the Tape is initially filled with blanks ∆.
• The Tape is infinitely long in one direction.
Tape Head
3. A Tape Head η that can in one step read the contents of a cell on the
Tape, replace it with some other character, and reposition itself to the
next cell to the right or to the left of the one it has just read.
6. A program Π, which is a finite set of rules that, on the basis of the state
we are in and the letter the Tape Head has just read, tells us
The program
where
• The third component, the direction, tells the Tape Head whether
to move one cell to the right, R, or one cell to the left, L.
Remarks:
• The above definition does not require that every state has an edge leaving
it corresponding to each letter of Σ + Γ.
• If we are in a state and read a letter for which there is no arc leaving
that state corresponding to that letter, then the machine crashes. In this
case, the machine terminates execution unsuccessfully.
• If Tape Head is currently in cell i and the program tells the Tape Head
to move left, then the machine crashes.
(a,a,R)
(b,b,R)
(a,a,R)
START 1 2 3 HALT 4
(b,b,R) ( , ,R)
(b,b,R)
i ii iii iv v vi
a b a
• We start in state START 1 with the Tape Head reading cell i, and we
denote this by
1
aba
The number on top denotes the state we are currently in. The things
below represent the current contents of the tape, with the letter about
to be read underlined.
• After reading in a in state 1, the TM then takes the top arc from state 1
to state 2, and so it prints a into the contents of cell i and the Tape Head
moves to the right to cell ii. We record this action by writing
1 2
−→
aba aba
CHAPTER 19. TURING MACHINES 19-6
i ii iii iv v vi
a b a
• Now we are in state 2, and the Tape Head is pointing to cell ii. Since
cell ii contains b, we will take the arc from state 2 to state 3, print b in
cell ii, and move the Tape Head to the right to cell iii. We record this
action by writing
1 2 3
−→ −→
aba aba aba
The tape now looks like
i ii iii iv v vi
a b a
• Now we are in state 3, and the Tape Head is pointing to cell iii. Since
cell iii contains a, we will take the arc labeled (a, a, R) from state 3 back
to state 3, print a in cell iii, and move the Tape Head to the right to
cell iv, which contains a blank ∆. We record this action by writing
1 2 3 3
−→ −→ −→
aba aba aba aba∆
The tape now looks like
CHAPTER 19. TURING MACHINES 19-7
i ii iii iv v vi
a b a
• Now we are in state 3, and the Tape Head is pointing to cell iv. Since
cell iv contains ∆, we will take the arc labeled (∆, ∆, R) from state 3 to
state HALT 4, print ∆ in cell iv, and move the Tape Head to the right
to cell v, which contains a blank ∆. We record this action by writing
1 2 3 3
−→ −→ −→ −→ HALT
aba aba aba aba∆
Since we reached a HALT state, the string on the input tape is accepted.
• Note that if an input string has a as its second letter, then the TM
crashes, and so the string is not accepted.
• This TM accepts the language of all strings over the alphabet Σ = {a, b}
whose second letter is b.
CHAPTER 19. TURING MACHINES 19-8
( , , R)
(b,b,R)
(a,a,R) (a,a,R)
START 1 2 HALT 3
(b,b,R)
Note that the first cell on the TAPE contains b, and so upon reading
this, the TM writes b in cell i, moves the tape head to the right to
cell ii, and then the TM loops back to state 1,
The second cell on the TAPE contains a, and so upon reading this,
the TM moves to state 2, writes a in cell ii, and moves the tape
head to the right to cell iii.
The third cell on the TAPE contains a, and so upon reading this,
the TM writes a in cell iii, moves the tape head to the right to
cell iv, and moves to state 3, which is a HALT state
The TM now halts, and so the string is accepted. Note that the
input tape still has a letter b that has not been read.
Note that each of the first two b’s results in the TM looping back
to state 1 and moving the tape head to the right one cell.
The third letter a makes the TM go to state 2 and moves the tape
head to the right one cell.
CHAPTER 19. TURING MACHINES 19-9
The fourth cell of the TAPE has a blank, and so the TM then
crashes. Thus, bba is not accepted.
Note that the first letter b results in the TM looping back to state 1
and moving the tape head to the right one cell.
The tape head then reads the a in the second cell, which causes the
TM to move to state 2 and moves the tape head to the right one
cell.
The tape head then reads the b in the third cell, which causes the
TM to move back to state 1 and moves the tape head to the right
one cell.
The fourth cell of the TAPE has a blank, and so the TM returns
to state 1, and the tape head moves one cell to the right.
All of the other cells on the TAPE are blank, and so the TM will
keep looping back to state 1 forever.
Since the TM never reaches a HALT state, the string bab is not
accepted.
• In general, we can divide the set of all possible strings into three sets:
1. Strings that contain the substring aa, which are accepted by the
TM since the TM will reach a HALT state.
2. Strings that do not contain substring aa and that end in a. For
these strings, the TM crashes, and so they are not accepted.
3. Strings that do not contain substring aa and that do not end in
a. For these strings, the TM loops forever, and so they are not
accepted.
Note: The videotaped lecture contains an error about this point.
Let S1 be the set of strings that do not contain the substring
aa and that do not end in a.
Let S2 be the set of strings that do not contain the substring
aa and that end in b.
In the videotaped lecture, I said that S2 is the set of strings
for which the TM loops forever, but actually, S1 is the set of
strings for which the TM loops forever.
CHAPTER 19. TURING MACHINES 19-10
Definition: Every Turing machine T over the alphabet Σ divides the set of
input strings into three classes:
3. LOOP(T ) is the set of all strings w ∈ Σ∗ such that if the Tape initially
contains w and T is then run, T loops forever.
(a,a,R) (b,b,R)
2 3 4
(b,b,R) (a,a,L)
(a,*,R)
(b,a,R)
(a,a,R)
( , ,R)
HALT START 1 5
( , ,L)
(*,*,R)
Step 1. Presume that we are in state 1, and we are reading the first letter of
what remains on the input.
• So initially, we are reading the first letter on the input tape, but as
we progress, we may find ourselves back in state 1 reading the first
letter of what remains on the tape.
• If we read a blank, then we go to HALT.
• If what we read is a, then change it to ∗, and move the tape head
to the right.
• If we read anything else, we crash.
CHAPTER 19. TURING MACHINES 19-12
Step 2. In state 2, we skip over the rest of the a’s in the initial clump of a’s,
looking for the first b.
Step 3. Now we are in state 5 with the tape head pointing to the first a in
the second clump of a’s, and we want to find the last a in the second
clump of a’s.
• Each a that we now read makes us return back to state 5, and move
the tape head to the right.
• If we read b, then the machine crashes.
• When we finally encounter ∆, then the tape contains no more char-
acters to the right, and the TM goes to state 6.
• We then move to state 7 and then to state 8, and change the last
two a’s to ∆’s.
• Thus, the number of a’s in the second clump has decreased by 2.
But in Step 2, we increased the number of a’s in the second clump by
1, and so now the number of a’s in the second clump has decreased
by 1 since we started in Step 1.
• Recall that Step 2, we also reduced the number of b’s by 1.
• Recall that in Step 1, we changed the first a in the first clump of
a’s to ∗.
Step 4. Now we are in state 8 with the tape head pointing to the last a
currently in the second clump of a’s, and we want to get back to the first
a that is currently in the first clump of a’s.
CHAPTER 19. TURING MACHINES 19-13
• In state 8, as long as we keep reading a’s and b’s, we move the tape
head to the left, and return back to state 8.
• Recall that in Step 1, we changed the first a in the first clump of
a’s into ∗.
• So when the tape head finally reaches the rightmost ∗ by moving
left, the TM goes to state 1, and we move the tape head to the
right, and we repeat our 4 steps.
4. Inserting a character anywhere onto the input tape and shifting over the
rest of the contents of the tape.
Example: TM to
The language of this TM is all strings over Σ = {a, b} since starting the
TM with any string in Σ∗ loaded on the Tape and running the TM will
lead to the Halt state.
6. Subroutines.