chapter 6 part 1
chapter 6 part 1
Turing Machines
1
Turing Machines
Turing machines (TMs) were introduced
by Alan Turing in 1936
2
The Language Hierarchy
n n n ?
a b c
Context-Free Languages
n n
a b
Regular Languages
a* a *b *
3
Languages accepted by
Turing Machines
n n n
a b c
Context-Free Languages
n n
a b
Regular Languages
a* a *b *
4
Basic design of Turing machine
Tape
......
Read-Write head
Control Unit
5
The Tape
Read-Write head
6
......
Read-Write head
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
7
TM instructions (transition functions)
Each Turing machine instruction contains the
following five parts:
The current machine state.
A tape symbol read from the current tape
cell.
A tape symbol to write into the current tape
cell.
The next machine state.
A direction for the tape head to move.
Time 1
a b k c ......
1. Reads a
2. Writes k
3. Moves Left
9
Time 1
a b k c ......
Time 2
a f k c ......
1. Reads b
2. Writes f
3. Moves Right
10
The Input String
a b a c ......
head
a b a c ......
head
12
States & Transitions
Read Write
Move Left
(a , b, L )
q1 q2
Move Right
(a, b, R)
q1 q2
13
Example:
Time 1
a b a c ......
q1
current state
(a, b, R)
q1 q2
14
Time 1
a b a c ......
q1
Time 2
a b b c ......
q2
(a, b, R)
q1 q2
15
Example:
Time 1
a b a c ......
q1
Time 2
a b b c ......
q2
( a , b, L )
q1 q2
16
Example:
Time 1
a b a c ......
q1
Time 2
a b b c g ......
q2
( ,g, R)
q1 q2
17
Determinism
Turing Machines are deterministic
q1 q1
(b , d , L ) q3 ( a, d , L ) q3
18
Partial Transition Function
Example:
a b a c ......
q1
(a, b, R) q2 Allowed:
q1 No transition
for input symbol c
(b , d , L ) q3
19
Halting
20
Example:
a b a c ......
q1
(a, b, R) q2
No possible transition
q1
HALT!!!
(b , d , L ) q3
21
Final States
q1 q2 Allowed
q1 q2 Not Allowed
If machine halts
Accept Input
in a final state
If machine halts
in a non-final state
Reject Input or
If machine enters
an infinite loop
23
Turing Machine Example
(a, a, R )
( , ,L)
q0 q1
24
Time 0
a a a
q0
(a, a, R )
( , ,L)
q0 q1
25
Time 1
a a a
q0
(a, a, R )
( , ,L)
q0 q1
26
Time 2
a a a
q0
(a, a, R )
( , ,L)
q0 q1
27
Time 3
a a a
q0
(a, a, R )
( , ,L)
q0 q1
28
Time 4
a a a
q1
(a, a, R )
Halt & Accept
( , ,L)
q0 q1
29
Rejection Example
Time 0
a b a
q0
(a, a, R )
( , ,L)
q0 q1
30
Time 1
a b a
q0
No possible Transition
Halt & Reject
(a, a, R )
( , ,L)
q0 q1
31
Infinite Loop Example
A Turing machine
for language aa * b( a b) *
(b , b , L )
(a, a, R )
( , ,L)
q0 q1
32
Time 0 a b a
q0
(b , b , L )
(a, a, R )
q0 ( , ,L) q1
33
Time 1
a b a
q0
(b , b , L )
(a, a, R )
q0 ( , ,L) q1
34
Time 2
a b a
q0
(b , b , L )
(a, a, R )
q0 ( , ,L) q1
35
Time 2 a b a
q0
Time 3 a b a
q0
Time 4 a b a
q0
Time 5 a b a
q0
36
Because of the infinite loop:
37
Formal Definitions
for
Turing Machines
38
Turing Machine:
Input Tape
alphabet alphabet
States
Transition
Reject state
function
(a, a, R )
q1 q2
(q1, a ) (q2 , b, R)
40
Transition Function
(c , d , L )
q1 q2
(q1, c) (q2 , d , L)
41
Example : Consider aTuring machine with following
transitions:
• (q0 , a ) = (q1 , a, R )
• (q0 , b ) = (q1 , b, R )
• (q0, ) = (q1 , , R )
• (q1 , a ) = (q0 , a, L )
• (q1 , b ) = (q0 , b, L )
• (q1 , ) = (q0 , , R )
42
Configuration
c a b a
q1
Instantaneous description: ca q1 ba
43
TM configurations
• The configuration of a Turing machine is
the current setting i.e.
– Current state These three items are a
configuration of the TM
– Current tape contents
– Current head location
• Configurations are represented in a special way:
– When the TM is in state q, and
– The contents of the tape is two strings uv, and
– The head is on the leftmost position of string v
– Then we represent this configuration as “u q v ”- this
string is the Instantaneous Description (ID).
44
Configurations of TMs
• Example: 1011q7011111
q7
45
Instantaneous Descriptions of a
Turing Machine
46
Standard Turing Machine
• Deterministic
47
Construction of Turing Machines
48
Example I
• Given: w is a bitstring
• Construct TM that accepts the language
L ={w: w contains at least two 0s}
49
Example I (Cont’d)
1 1, R 1 1, R
0 0, R
q0 q3
0 0, R
q4
50
Example II
• Construct TM that accepts the
language
L= {anbn : n 1 }
51
Design
• Check first symbol is an a
– If not, then reject
– If so, replace with X (to mean counted) and begin
recursion
• Move to the right all the way to the first unread
b, and mark it with Y
• Move back (to the left) all the way to the last
marked X, and then move one position to the
right.
• If the next position is a, then go to step 2.
• Else move all the way to the right to ensure there
are no excess bs. If not move right to the next
blank symbol and stop & accept.
52
Example II
n n
Turing machine for the language {a b }
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
53
Time 0 a a b b
q0
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
54
Time 1 x a b b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
55
Time 2 x a b b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
56
Time 3
x a y b
q2
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
57
Time 4
x a y b
q2
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
58
Time 5 x a y b
q0
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
59
Time 6
x x y b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
60
Time 7
x x y b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
61
Time 8
x x y y
q2
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
62
Time 9 x x y y
q2
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
63
Time 10
x x y y
q0
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
64
Time 11
x x y y
q3
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
65
Time 12 x x y y
q3
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
66
Time 13 x x y y
q4
Halt & Accept
q4 y y, R y y, L
y y, R a a, R a a, L
,L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
67
Observation:
If we modify the
machine for the language n n
{a b }
68
Turing Languages
69
Recursively Enumerable
and
Recursive Languages
70
Definition: Let M = (Q, Σ, Г, δ, q0, qacc, qrej)
be a TM, and let w be a string in Σ*. Then
w is accepted by M iff
q0w |—* α1 qf α2
where qf is in F and α1 and α2 are in Г*
71
Notes:
72
Definition:
A language is recursively enumerable
if some Turing machine accepts it
73
Let L be a recursively enumerable language
For string w:
if w L then M halts in a final state
q0 w |-* x1 qf x2
75
Definition:
A language is recursive
if some Turing machine accepts it
and halts on any input string
In other words:
A language is recursive if there is
a membership algorithm for it
76
Let L be a recursive language
For string w:
77
What do you think is the advantage of
being recursive language over recursively
enumerable language?
78
Examples of Recursively Enumerable Languages
79
Non- Recursively Enumerable
Recursively Enumerable
Recursive
80
Notes:
The set of all recursive languages is
a subset of the set of all recursively
enumerable languages
81