Lecture 11 TMs
Lecture 11 TMs
Lecture 11
Turing Machine (TM)
1
Syllabus and Terminologies
5/31/2024 FCI-CU-EG 2
The Language Hierarchy
n n n ?
a b c ww ?
Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
Fall 2006 Costas Busch - RPI 3
Where do we go from here?
From the earlier example, we see that not all
languages are context-free.
Is there a mathematical model for even more
complex computations?
There are many.
We will study one in particular: an automaton
called a Turing machine.
4
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 *
Fall 2006 Costas Busch - RPI 5
Turing Machines
control
infinite tape
a b a b B B B B …
B: blank symbol
Differences between finite automata and Turing
machine
1. A Turing machine can both write on the input tape and read
from it.
2. The read-write head can move both to the left and to the
right.
3. The tape is infinite.
4. The special states for rejecting and accepting take immediate
effect … don’t need to consume all the input.
TMs can be represented in 3 different, but
yet equivalent ways:
1. Transition Graph
2. Instructions
3. Configurations
TM
in the form of
Transition Graph
9
A Turing Machine
Tape
...... ......
Read-Write head
Control Unit
...... ......
Read-Write head
Read-Write head
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
Time 1
...... ......
a b k c
1. Reads a
2. Writes k
3. Moves Left
Fall 2006 Costas Busch - RPI 13
Time 1
...... ......
a b k c
Time 2
...... ......
a f k c
1. Reads b
2. Writes f
3. Moves Right
Fall 2006 Costas Busch - RPI 14
The Input String
a b a c
...... ......
head
Input Tape
alphabet alphabet
States
M (Q, , , , q0 , , F )
Transition Accept
function states
Initial Blank
Symbol
Fall 2006
state Costas Busch - RPI 16
States & Transitions
Write
Read Move Left
q1 a b, L q2
Move Right
q1 a b, R q2
Fall 2006 Costas Busch - RPI 17
Transition Function
q1 a b, R q2
(q1, a ) (q2 , b, R )
q1 c d, L q2
(q1, c) (q2 , d , L)
a b a c
...... ......
q1
current state
Time 2
a b b c
...... ......
q2
q1 a b, R q2
new state
a b a c
...... ......
q1
Time 2
a b b c
...... ......
q2
q1 a b, L q2
Fall 2006 Costas Busch - RPI 21
Example:
Time 1
a b a c
...... ......
q1
Time 2
a b b c g
...... ......
q2
q1 g, R q2
Fall 2006 Costas Busch - RPI 22
Determinism
Turing Machines are deterministic
q1 q1
q3 a d, L q3
b d, L
q1
a b, R q2
Allowed:
No transition
for input symbol c
q1
b d, L q3
Fall 2006 Costas Busch - RPI 24
Halting
a b a c
...... ......
q1
No transition from q1
q1 HALT!!!
a b a c
...... ......
q1
No possible transition
a b, R q2 from q1 and symbol c
HALT!!!
q1
b d, L q3
Fall 2006 Costas Busch - RPI 27
Accepting States
q1 q2 Allowed
q1 q2 Not Allowed
If machine halts
in a non-accept state
Reject Input string or
If machine enters
an infinite loop
Fall 2006 Costas Busch - RPI 29
Observation:
In order to accept an input string,
it is not necessary to scan all the
symbols in the string
, L
q0 q1
q0
a a, R
, L
q0 q1
q0
a a, R
, L
q0 q1
q0
a a, R
, L
q0 q1
q0
a a, R
, L
q0 q1
q1
, L
q0 q1
Time 0 a b a
q0
a a, R
, L
q0 q1
Fall 2006 Costas Busch - RPI 37
Time 1 a b a
q0
No possible Transition
a a, R
Halt & Reject
, L
q0 q1
Fall 2006 Costas Busch - RPI 38
Infinite Loop Example
b b, L
a a, R
, L
q0 q1
q0
b b, L
a a, R
, L
q0 q1
q0
b b, L
a a, R
, L
q0 q1
q0
b b, L
a a, R
, L
q0 q1
Infinite loop
Time 3
q0
Time 4 a b a
q0
Time 5 a b a
Fall 2006
q0
Costas Busch - RPI 43
Because of the infinite loop:
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
Fall 2006
x x, R
Costas Busch - RPI 45
Basic Idea:
Match a’s with b’s:
Repeat:
replace leftmost a with x
find leftmost b and replace it with y
Until there are no more a’s or b’s
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
Fall 2006
x x, R
Costas Busch - RPI 47
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
Fall 2006
x x, R
Costas Busch - RPI 48
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
Fall 2006
x x, R
Costas Busch - RPI 49
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
Fall 2006
x x, R
Costas Busch - RPI 50
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
Fall 2006
x x, R
Costas Busch - RPI 51
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
Fall 2006
x x, R
Costas Busch - RPI 52
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
Fall 2006
x x, R
Costas Busch - RPI 53
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
Fall 2006
x x, R
Costas Busch - RPI 54
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
Fall 2006
x x, R
Costas Busch - RPI 55
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
Fall 2006
x x, R
Costas Busch - RPI 56
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
Fall 2006
x x, R
Costas Busch - RPI 57
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
Fall 2006
x x, R
Costas Busch - RPI 58
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
Fall 2006
x x, R
Costas Busch - RPI 59
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
Fall 2006
x x, R
Costas Busch - RPI 60
Computing Functions
with
Turing Machines
f ( x, y ) x y
Decimal: 5
Binary: 101
Unary: 11111
A function f
is computable if
there is a Turing Machine M such that:
w f (w)
q0 qf
initial state accept state
x, y are integers
Turing Machine:
Start
1 1 1 0 1 1
q0
initial state
Start
1 1 1 0 1 1
q0 initial state
x y
Finish
1 1 1 1 0
Fall 2006
qf final state
Costas Busch - RPI 67
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 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 68
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
q0
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 70
Time 1 1 1 0 1 1
q0
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 71
Time 2 1 1 0 1 1
q0
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 72
Time 3 1 1 1 1 1
q1
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 73
Time 4 1 1 1 1 1
q1
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 74
Time 5 1 1 1 1 1
q1
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 75
Time 6 1 1 1 1 1
q2
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 76
Time 7 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 77
Time 8 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 78
Time 9 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 79
Time 10 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 80
Time 11 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
Fall 2006 Costas Busch - RPI
q4 81
Time 12 1 1 1 1 0
q4
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 2
1 0, L q3
, R
HALT & accept q4
Fall 2006 Costas Busch - RPI 82
Another Example
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 accept state
• Repeat:
1 $, R 1 1, L 1 1, R
q0 , L q1 $ 1, R q2
, R 1, L
q3
Fall 2006 Costas Busch - RPI 86
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
Fall 2006
q3 Costas Busch - RPI 87