CH9
CH9
2
Outline
3 Turing’s Thesis
3
Turing Machines
•Developed by Alan Turing in 1936
•More than just recognizing languages
•Foundation for modern theory of computation
4
5
The Language Hierarchy
n n n ?
a b c ww ?
Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
Languages accepted by
Turing Machines
n n n
a b c ww
Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
7
A Turing Machine
Tape
...... ......
Read-Write head
Control Unit
8
The Tape
No boundaries -- infinite length
...... ......
Read-Write head
9
...... ......
Read-Write head
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
10
Example:
Time 0
...... ......
a b a c
Time 1
...... ......
a b k c
1. Reads a
2. Writes k
3. Moves Left
11
Time 1
...... ......
a b k c
Time 2
...... ......
a f k c
1. Reads b
2. Writes f
3. Moves Right
12
The Input String
...... ......
a b a c
head
13
Input string Blank symbol
...... ......
a b a c
head
14
Turing Machine:
Input Tape
alphabet alphabet
States
M = (Q, , , , q0 , , F )
Transition Final
function states
Initial blank
state
15
States & Transitions
Write
Read Move Left
q1 a, b, L q2
Move Right
a, b, R
q1 q2
16
Transition Function (program)
a, b, R
q1 q2
(q1, a) = (q2 , b, R)
17
Transition Function
q1 c, d , L q2
(q1, c) = (q2 , d , L)
18
Example:
Time 1
...... ......
a b a c
q1
current state
q1 a, b, R q2
19
Time 1
...... ......
a b a c
q1
Time 2
...... ......
a b b c
q2
q1
a, b, R q2
20
Example:
Time 1
...... ......
a b a c
q1
Time 2
...... ......
a b b c
q2
a, b, L
q1 q2
21
Example:
Time 1
...... ......
a b a c
q1
Time 2
a b b c g
...... ......
q2
, g, R
q1 q2
22
Determinism
Turing Machines are deterministic
q1 q1
q3 q3
b, d , L a, d , L
q1
a, b, R q2 Allowed:
q1
b, d , L q3
24
Halting
...... ......
a b a c
q1
No transition
a, b, R q2 for input symbol c
q1 No possible transition
q3 HALT!!!
b, d , L
26
Final States
q1 q2 Allowed
q1 q2 Not Allowed
27
Acceptance
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
28
Turing Machine Example
A Turing machine that accepts the language:
aa *
a, a, R
= {a, b}
, , L
q0 q1
29
Time 0 a a a
q0
a, a, R
, , L
q0 q1
30
Time 1 a a a
q0
a, a, R
, , L
q0 q1
31
Time 2 a a a
q0
a, a, R
, , L
q0 q1
32
Time 3 a a a
q0
a, a, R
, , L
q0 q1
33
Time 4 a a a
q1
a, a, R
Halt & Accept
, , L
q0 q1
34
Rejection Example
Time 0 a b a
q0
a, a, R
, , L
q0 q1
35
Time 1 a b a
q0
No possible Transition
a, a, R Halt & Reject
, , L
q0 q1
36
Infinite Loop Example
b, b, L
a, a, R
, , L
q0 q1
Time 0 a b a
q0
b, b, L
a, a, R
, , L
q0 q1
38
Time 1 a b a
q0
b, b, L
a, a, R
, , L
q0 q1
39
Time 2 a b a
q0
b, b, L
a, a, R
, , L
q0 q1
40
Time 2 a b a
q0
Time 3 a b a
Infinite loop
q0
Time 4 a b a
q0
Time 5 a b a
q0 41
Because of the infinite loop:
42
Example 9.7
n n
Turing machine for the language {a b }
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
50
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
51
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
52
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
53
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
54
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
55
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
56
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
57
Observation:
If we modify the
n n
machine for the language {a b }
58
Configuration
c a b a
q1
Instantaneous description: ca q1 ba
59
Time 4 Time 5
x a y b x a y b
q2 q0
60
Time 4 Time 5
x a y b x a y b
q2 q0
Time 6 Time 7
x x y b x x y b
q1 q1
q2 xayb ┝ x q0 ayb ┝
xx q1 yb ┝
xxy q1 b
61
q2 xayb ┝
x q0 ayb ┝
xx q1 yb ┝ xxy q1 b
Equivalent notation: q2 xayb ┝ xxy q1 b
Infinite loop (TM never halt):
x1qx2 ┝
62
Initial configuration: q0 w
Input string
w
a a b b
q0
63
The Accepted Language
For any Turing Machine M
L( M ) = {w : q0 w ┝
x1 q f x2 }
• Deterministic
65
Computing Functions
with
Turing Machines
A function f (w) has:
f (w)
w D f ( w) S
“Transducer”
67
A function may have many parameters:
f ( x, y ) = x + y
68
Integer Domain
Decimal: 5
Binary: 101
Unary: 11111
w f (w)
q0 w ┝M q f f ( w)
Initial Final
Configuration Configuration
x, y are integers
Turing Machine:
72
x y
Start
1 1 1 0 1 1
q0
initial state
73
x y
Start
1 1 1 0 1 1
q0 initial state
x+ y
Finish 1 1 1 1 0
qf final state
74
The 0 helps when we use
the result for other operations
x+ y
Finish 1 1 1 1 0
qf final state
75
Turing machine for function f ( x, y ) = x + y
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
76
Execution Example: Time 0
x y
x = 11 (2)
1 1 0 1 1
y = 11 (2)
q0
Final Result
x+ y
1 1 1 1 0
q4
77
Time 0 1 1 0 1 1
q0
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
78
Time 1 1 1 0 1 1
q0
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
79
Time 2 1 1 0 1 1
q0
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
80
Time 3 1 1 1 1 1
q1
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
81
Time 4 1 1 1 1 1
q1
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
82
Time 5 1 1 1 1 1
q1
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
83
Time 6 1 1 1 1 1
q2
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
84
Time 7 1 1 1 1 0
q3
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
85
Time 8 1 1 1 1 0
q3
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
86
Time 9 1 1 1 1 0
q3
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
87
Time 10 1 1 1 1 0
q3
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
88
Time 11 1 1 1 1 0
q3
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
q4
89
Time 12 1 1 1 1 0
q4
1, 1, R 1, 1, R 1, 1, L
q0
0, 1, R q , , L q 1, 0, L q3
1 2
, , R
HALT & accept q4
90
Example 9.10
The function f ( x) = 2 x is computable
x is integer
Turing Machine:
Start 1 1 1
q0 initial state
2x
Finish 1 1 1 1 1
qf final state
92
Turing Machine Pseudocode for f ( x) = 2 x
• Repeat:
• Find rightmost $, replace it with 1
93
Turing Machine for f ( x) = 2 x
1, $, R 1, 1, L 1, 1, R
, , L $, 1, R q2
q0 q1
, , R
, 1, L
q3
94
Example
Start Finish
1 1 1 1 1 1
q0 q3 1, 1, R
1, $, R 1, 1, L
, , L $, 1, R q2
q0 q1
, , R
, 1, L How about f(x, y) = x*y?
q3
95
Example 9.11
1 if x y
The function f ( x, y ) =
is computable 0 if x y
96
Turing Machine for
1 if x y
f ( x, y ) =
0 if x y
Input: x0 y
Output: 1 or 0
97
Turing Machine Pseudocode:
• Repeat
Match a 1 from x with a 1 from y
Until all of x or y is matched
• If a 1 from x is not matched
erase tape, write 1 ( x y)
else
erase tape, write 0 ( x y)
98
Outline
3 Turing’s Thesis
99
Combining Turing
Machines
Block Diagram
Turing
input Machine output
101
Example 9.12: x + y if x y
f ( x, y ) =
0 if x y
qC , 0 w( x)0w( y ) ┝
q A, 0 w( x)0w( y ) If x ≧ y q A, f w( x + y )0
q A, 0 w( x)0w( y ) ┝
qC , 0 w( x)0w( y ) ┝
qE , 0 w( x)0w( y ) If x < y qE , 0 w( x)0 w( y ) ┝
qE , f 0
102
Example 9.13
Design a Turing machine that multiplies two positive integers in unary notation
104
105
f(n)=n(n+2) f(n)=n4
f(n)=2n f(n)=n!
L={WWRW}
107
Outline
3 Turing’s Thesis
108
Turing’s thesis:
109
Computer Science Law:
A computation is mechanical
if and only if
it can be performed by a Turing Machine
110
Definition of Algorithm:
111
Algorithms are Turing Machines
When we say:
There exists an algorithm
We mean:
There exists a Turing Machine
that executes the algorithm
112
Turing Thesis
Anything that can be done on any existing digital computer can also be
done by a Turing machine
113
Short Quiz
◦ L={anbncn: n≥0}
◦ Even-length binary palindromes
◦L={w∈{0,1}∗∣w=wR, ∣w∣ is even}
114
Practice 9.1-1
Construct a Turing machine that accepts the language L =L(aaaa∗b∗).
Solution: A solution that accepts the language L is
a, a, R b, b, R
口: blank symbol
a, a, R a, a, R a, a, R b, b, R
q0 q1 q2 q3 q4
口 ,口 , L
口 ,口 , L
q5
115
Practice 9.1-10
Construct a Turing machine to compute the function f(w)=wR.
116
W
Start 口 1 1 1 口
q0 initial state
WR W
Finish
口 1 1 口 口 口
qf final state
117
Practice 9.1-13(b) f(x, y)=x+2y (unary representation)
𝑤 𝑥 0𝑤 𝑦 → 𝑤 𝑥 𝑤 𝑦 𝑤(𝑦)
118