Professional Documents
Culture Documents
ToA - Lecture 21 22 - Turing Machine
ToA - Lecture 21 22 - Turing Machine
Turing Machines
Lecture 21 & 22
0 01 1 10 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0
1
The Turing Machine
10 01 1 10 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0
A TM is defined as:
TM = <S, T, s0, , H> where,
S is a set of TM states
T is a set of tape symbols
s0 is the start state
HS is a set of halting states
: S x T →S x T x {L,R}
is the transition function
3
A Turing Machine
Tape
...... ......
Read-Write head
Control Unit
4
The Tape
Read-Write head
5
...... ......
Read-Write head
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
6
Example:
Time 0
...... a b a c ......
Time 1
...... a b d c ......
1. Reads a
2. Writes d
3. Moves Left
7
Time 1
...... a b s c ......
Time 2
...... a f s c ......
1. Reads b
2. Writes f
3. Moves Right
8
The Input String
...... # # a b a c # # # ......
head
•Head starts at the leftmost position of the input string.
•The input string is never empty
9
States & Transitions
Read Write
Move Left
q1 a → b, L q2
Move Right
q1 a → b, R q2
10
Example:
Time 1
...... a b a c ......
q1 current state
Time 2
...... a b d c ......
q2
q1 a → d, R q2
11
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a → b, L q2
12
Example:
Time 1
...... # # a b a c # #
......
q1
Time 2
...... # # a b b c g #
......
q2
→ g, L
q1 q2
13
Determinism
Turing Machines are deterministic
q1 q1
q3 a → d, L q3
b → d, L
...... # # a b a c # # # ......
q1
a → b, R q2 Allowed:
q1 No transition
for input symbol c
b → d, L q3
15
Halting
...... # # a b c c # # # ......
q1
a → b, R q2 No possible transition
q1
q3
HALT!!!
b → d, L
16
Final States
q1 q2 Allowed
q1 q2 Not Allowed
17
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
18
Turing Machine Example
a*
a → a, R
→ , L
q0 q1
19
Time 0 a a a
q0
a → a, R
→ , L
q0 q1
20
Time 1 a a a
q0
a → a, R
→ , L
q0 q1
21
Time 2 a a a
q0
a → a, R
→ , L
q0 q1
22
Time 3 a a a
q0
a → a, R
→ , L
q0 q1
23
Time 4 a a a
q1
→ , L
q0 q1
24
Rejection Example
Time 0 a b a
q0
a → a, R
→ , L
q0 q1
25
Time 1 a b a
q0
No possible Transition
a → a, R Halt & Reject
→ , L
q0 q1
26
Infinite Loop Example
Let’s run it for a string
aa * +b(a + b) *
b → b, L
a → a, R
→ , L
q0 q1
27
Time 0 a b a
q0
b → b, L
a → a, R
→ , L
q0 q1
28
Time 1 a b a
q0
b → b, L
a → a, R
→ , L
q0 q1
29
Time 2 a b a
q0
b → b, L
a → a, R
→ , L
q0 q1
30
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
31
Because of the infinite loop:
32
Another Turing Machine Example
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
33
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
34
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
35
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
36
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
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
Observation:
If we modify the
machine for the language n n
{a b }
48
Construct a TM to erase the input string
52
Transition Function
q1 a → b, R q2
(q1, a) = (q2 , b, R)
53
Transition Function
q1 c → d, L q2
(q1, c) = (q2 , d , L)
54
Turing Machine:
Input Tape
alphabet alphabet
States
M = (Q, , , , q0 , , F )
Transition Final
function states
Initial blank
state 55
Configuration
c a b a
q1
Instantaneous description: ca q1 ba
56
Time 4 Time 5
x a y b x a y b
q2 q0
57
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
Equivalent notation: q2 xayb xxy q1 b
59
Initial configuration: q0 w
Input string
a a b b
q0
60
The Accepted Language
L( M ) = {w : q0 w x1 q f x2 }
61
Standard Turing Machine
• Deterministic
62
Computing Functions
with
Turing Machines
63
A function f (w) has:
f (w)
w D f ( w) S
64
A function may have many parameters:
f ( x, y ) = x + y
65
Integer Domain
Decimal: 5
Binary: 101
Unary: 11111
A function f is computable if
there is a Turing Machine M such that:
q0 w q f f ( w)
Initial Final
Configuration Configuration
x, y are integers
Turing Machine:
69
x y
Start 1 1 1 0 1 1
q0
initial state
70
x y
Start 1 1 1 0 1 1
q0 initial state
x+ y
Finish 1 1 1 1 0
q f final state
71
The 0 helps when we use
the result for other operations
x+ y
Finish 1 1 1 1 0
q f final state
72
Turing machine for function f ( x, y ) = x + y
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
73
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
74
Time 0 1 1 0 1 1
q0
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
75
Time 1 1 1 0 1 1
q0
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
76
Time 2 1 1 0 1 1
q0
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
77
Time 3 1 1 1 1 1
q1
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
78
Time 4 1 1 1 1 1
q1
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
79
Time 5 1 1 1 1 1
q1
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
80
Time 6 1 1 1 1 1
q2
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
81
Time 7 1 1 1 1 0
q3
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
82
Time 8 1 1 1 1 0
q3
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
83
Time 9 1 1 1 1 0
q3
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
84
Time 10 1 1 1 1 0
q3
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
85
Time 11 1 1 1 1 0
q3
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
q4
86
Time 12 1 1 1 1 0
q4
1 → 1, R 1 → 1, R 1 → 1, L
q0 0 → 1, R q1 → , L q 1 → 0, L q3
2
→ , R
HALT & accept q4
87
Another Example
x is integer
Turing Machine:
Start 1 1 1
q0 initial state
2x
Finish 1 1 1 1 1
q f final state
89
Turing Machine Pseudocode for f ( x) = 2 x
• Repeat:
• Find rightmost $, replace it with 1
90
Turing Machine for f ( x) = 2 x
1 → $, R 1 → 1, L 1 → 1, R
q0 → , L q1 $ → 1, R q2
→ , R → 1, L
q3
91
Example
Start Finish
1 1 1 1 1 1
q0 q3
1 → $, R 1 → 1, L 1 → 1, R
q0 → , L q1 $ → 1, R q2
→ , R → 1, L
q3
92
Simple TM Examples
#111100000000…….
➔
#1111100000000……….
93
Simple TM Examples
TM: U+1
(s0, 1) |-- (s0, 1, R)
(s0, 0) |-- (h, 1, STOP)
#s0111100000….. →
#1s011100000….. →
#11s01100000….. →
#111s0100000….. →
#1111s000000….. →
#11111h0000….. STOP
94
Another Example
1 if x y
The function f ( x, y ) =
0 if x y
is computable
95
Turing Machine for
1 if x y
f ( x, y ) =
0 if x y
Input: x0 y
Output: 1 or 0
96
Turing Machine Pseudocode:
• Repeat
98
Block Diagram
Turing
input output
Machine
99
Example: x + y if x y
f ( x, y ) =
0 if x y
x, y
Adder x+ y
x, y x y
Comparer
x y Eraser 0
100
Turing’s Thesis
101
Turing’s Thesis
Turing’s thesis is not a “theorem” there is no
“proof” for the thesis.
The theorem may be refuted by showing at least
one task that is performed by a digital computer
which cannot be performed by a Turing machine.
Many contentions have been made to this end.
However, till date there have not been any
conclusive evidence to refute Turing’s thesis.
102
Conclusions
TMs are at a level that is much below the assembly
language of any typical microprocessor.
So in the practical world, TMs are more useful in
what they cannot do rather than in what they
can.
103
Lab2
Write some simpleTuring machine programs
QUESTIONS?
104