Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Chapter 19: Turing Machines

A Turing machine is:


1. an alphabet S of input letters (D Ï S)
2. a tape, finite to the left but infinite to the right, divided
into cells. The tape contains the input word starting in the
first cell. The rest of the tape is filled with blanks.
3. a tape head initially positioned at the first cell
4. an alphabet G of output characters which can be printed
on the tape (D Ï G)
5. a finite set of states q1,q2,q3,… with exactly one start state
and some (maybe none) halt states
6. a program which is a set of rules that indicate, for a letter
on the tape and a state, the transition, the print character,
and the direction to move the tape head.

1
● The tape:

● Final state: No transition out of final state. Denoted:

HALT

● The start state can have any number of outgoing and incoming
edges.

2
Program Rules

(letter, letter, direction)


qi qj

● The first letter is the character the tape head reads from the cell
to which it is pointing.
● The second letter is what the tape head prints in the cell before
it leaves the cell.
● The direction is R (right) or L (left) and indicates if the tape
head should move one cell to the right or one cell to the left.

3
● Turing machines are deterministic. There is at most one
transition out of each state labeled with the same first letter.
● The machine crashes
§ if there is no arrow labeled with the letter in the cell pointed
to by the tape head .
§ if it tries to move left from the first cell.
● A word is accepted when a final state is entered.

4
Execution of a Turing Machine: 3 possibilities

1. The execution terminates successfully if a halt state is


entered. We say that execution halts. The word on the tape
(before execution started) is accepted. (It is in the language
accepted by the machine.)
2. The machine crashes. Either the tape head tries to move left
from the first cell on the tape, or we are in a state and read a
letter that offers no choice of path to another state. In this case
the execution terminates unsuccessfully.
3. The execution does not halt (infinite loop).
For example: (D,D,R)

The tape head moves to the right one cell at a time forever.
5
(a,a,R)
(b,b,R)
(a,a,R)
START 1 2 3 HALT 4
(b,b,R) (D,D,R)
(b,b,R)
S = {a,b}
a b a D D D … G = {a,b}
(a+b)b(a+b)*

1 à 2 à 3
a b a D … aba aba aba

a b a D … à 3 à HALT
abaD abaDD
6
Example: {anbn| n ³ 1}
S={a,b} G={a,A,B}

(a,a,R)
(B,B,R) (B,B,L) (B,B,R)

START 1 2 3 5
(a,A,R) (b,B,L) (A,A,R)
(D,D,R)
(A,A,R) (a,a,L)
4 HALT

(a,a,L)

7
8
Example: PALINDROME
(b,b,R) (b,b,L)
(a,a,R) (a,a,L)

2 3 4
(a,D,R) (D,D,L) (a,D,L) (D,D,R)

(D,D,R)

START 1
(D,D,R)
HALT 8

(D,D,R)
(b,D,R) (D,D,L) (b,D,L) (D,D,R)
5 6 7

(a,a,R) (b,b,L)
(b,b,R) (a,a,L)
9
10
11
Example:
read an a,PALINDROME
then
search for a at
the end (b,b,R) (b,b,L)
(a,a,R) (a,a,L)

2 3 4
(a,D,R) (D,D,L) (a,D,L) (D,D,R)

(D,D,R)

START 1
(D,D,R)
HALT 8 Odd
PALINDROME
(D,D,R)
(b,D,R) (D,D,L) (b,D,L) (D,D,R)
5 6 7
Even
PALINDROME
read a b, then
search for b at
(a,a,R) (b,b,L)
the end (b,b,R) (a,a,L)
12
● Theorem. For every regular language, there is a Turing
machine that accepts it.
Proof: We transform a finite automaton to a Turing machine.
§ The start state of the finite automaton is the start state of
the Turing machine.
§ Add a halt state to the Turing machine, and one transition
from every final state of the finite automaton to the new
halt state. (Erase all the + symbols.) Label the new
transitions (D,D,R).
§ Change each label “a” of the finite automaton to (a,a,R),
and similarly for all other letters of S.

13
Example: EVEN-EVEN

(b,b,R)

START 1 2
(b,b,R)
(D,D,R)
(a,a,R) (a,a,R) (a,a,R) (a,a,R)
HALT

(b,b,R)
3 4

(b,b,R)

14
(a+b)*aa(a+b)*
(D,D,R)
(b,b,R)
(a,a,R)
START 1 2 HALT 3
(a,a,R)
(b,b,R)
● If we reach the HALT state, execution terminates and the
word is accepted. (double a)
● If the machine crashes, the word is rejected. (words with no
aa ending in a)
● It is also possible to get into an infinite loop. (words with no
aa ending in b, or the empty word)

15
{anbnan| n ³ 0}
(a,a,R) (b,b,R)

2 3 4
(b,b,R) (a,a,L)
(b,a,R)
(a,*,R)
(a,a,R)
HALT START 1 5
(D,D,R)
(*,*,R) (D,D,L)

(a,D,L) (a,D,L)
8 7 6

(a,a,L)
(b,b,L)
16
17
The Subprogram “INSERT”

… b X a b b X D D …

INSERT b

… b X b a b b X D D …

18
S={a,b,X} (a,a,R) 2
G={Q,a,b,X}

(a,Q,R) (a,b,R) (b,a,R) (X,a,R)


(D,a,R)
(a,X,R)
(b,Q,R) (b,b,R)
1 3
(X,Q,R)
(b,X,R) (X,b,R) (D,b,R)
(D,b,R) (a,a,L)
4 5
(X,X,R) (b,b,L)
(D,X,R) (D,D,L) (X,X,L)
7 (Q,b,R)
6
19
EQUAL
S={a,b}
G={a,b,X,#}
(X,X,R) (X,X,L) (X,X,R) (X,X,L)
START
(b,b,R) (b,b,L) (a,a,R) (a,a,L)

INSERT # 6 7 8 9
(a,X,L) (#,#,R) (b,X,L)
(D,D,L) (#,#,R)

(X,X,L) 10 (#,#,R) HALT

b b b a a a b a D …
# X b b X a a b a D

21
The Subprogram “DELETE”

… F R I E N D D D …

DELETE

… F I E N D D D …

22
S={a,b,c} (c,c,L) 6
(D,c,R)
G={a,b,c}
(c,D,L) (c,b,L) (b,c,L) (a,c,L)

(b,D,L) (b,b,L)
3 5
(D,b,R) 7
(D,D,L) (a,D,L) (c,a,L)
(c,c,R) (b,a,L) (a,b,L)
(b,b,R)
(a,a,R) 2
(c, D,R) (a,a,L) 4 (D,a,R)
(b, D,R)
(a, D,R)
1
23

You might also like