FLAT - Ch-6
FLAT - Ch-6
FLAT - Ch-6
Turing Machine
1
Alan Turing -- 1912 – 1954
• Founder of computer science, mathematician,
philosopher, codebreaker, strange visionary and
a gay man before his time.
• Influential
• development of computer sciences
• provided an influential formalisation
• of the concept of the algorithm
• and computation with the
• Turing Machine
• Turing Test contribute to the debate of AI
• Can machines think?
• http://www.turing.org.uk/turing/
2
Recursively Enumerated Languages
Turing Machines
n n n
a b c ww
Context-Free Languages
Push Down Automata
n n R
a b ww
Regular Languages
Finite Automata
a* a *b *
3
A Turing Machine
10 01 1 10 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0
...... ......
5
Example: Time 0
...... ......
a b a c
Time 1
...... ......
a b k c
1. Reads a
2. Writes
k
3. Moves Left
6
Time 1
...... ......
a b k c
Time 2
...... ......
a f k c
1. Reads
b
2. Writes
f
3. Moves Right
7
The Input String
...... ......
a b a c
head
8
States & Transitions
Write
Read Move Left
q1 a b, L q2
Move Right
q1 a b, R q2
9
Example:
Time 1
...... ......
a b a c
q1
current state
q1 a b, R q2
10
Time 1
...... ......
a b a c
q1
Time 2
...... ......
a b b c
q2
q1 a b, 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
q1 g, R q2
13
A Turing Machine…
• Turing Machines are deterministic
Allowed
Not Allowed
a b, R q2 a b, R q2
q1 q1
q3 a d, L q3
b d, L
14
Partial Transition Function
Example:
...... ......
a b a c
q1
Allowed:
a b, R q2
No transition
q1 for input symbol c
b d, L q3
15
Halting
• The machine halts in a state if there is no transition to follow.
...... ......
a b a c
q1
No transition from q1
q1
HALT!!!
16
Halting…
Example
...... ......
a b a c
q1
a b, R q2
No possible transition
from q1 and symbol c
q1
b d, L q3 HALT!!!
17
Accepting States
q1 q2 Allowed
q1 q2 Not Allowed
18
Acceptance…
If machine halts
in a non-accept state
Reject Input string or
If machine enters
an infinite loop
19
A Turing Machine… Example
Input alphabet
{a , b }
, L
q0 q1
20
Time 0
a a a
q0
a a, R
, L
q0 q1
21
Time 1
a a a
q0
a a, R
, L
q0 q1
22
Time 2
a a a
q0
a a, R
, L
q0 q1
23
Time 3
a a a
q0
a a, R
, L
q0 q1
24
Time 4
a a a
q1
, L
q0 q1
25
Rejection Example
Time 0
a b a
q0
a a, R
, L
q0 q1
26
Rejection Example…
Time 1
a b a
q0
No possible Transition
, L
q0 q1
27
Infinite Loop Example
b b, L
a a, R
, L
q0 q1
28
Time 0
a b a
q0
b b, L
a a, R
, L
q0 q1
29
Time 1
a b a
q0
b b, L
a a, R
, L
q0 q1
30
Time 2
a b a
q0
b b, L
a a, R
, L
q0 q1
31
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 32
Infinite Loop….
Because of the infinite loop:
33
A Turing Machine… Example
n n
Turing machine for the language: {a b }
n 1
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
A Turing Machine… Example
35
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 36
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 37
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 38
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 39
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 40
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 41
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 42
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 43
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 44
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 45
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 46
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 47
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 48
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 49
A Turing Machine… Example
Observation:
If we modify the n n
machine for the language {a b }
50
A Turing Machine: Formal Definition
Transition Function
q1 a b, R q2
(q1, a) (q2 , b, R)
51
A Turing Machine: Formal Definition
Transition Function
q1 c d, L q2
(q1, c) (q2 , d , L)
52
A Turing Machine: Formal Definition
Input Tape
alphabet alphabet
States
M (Q, , , , q0 , , F )
Transition Accept
function states
Initial
blank
state
53
A Turing Machine…Configuration
c a b a
q1
Instantaneous description: ca q1 ba
54
Time 4 Time 5
x a y b x a y b
q2 q0
55
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
A computation
Equivalent notation: q2 xayb xxy q1 b
57
Initial configuration: q0 w
Input string
a a b b
q0
58
The Accepted Language
For any Turing Machine M
L( M ) {w : q0 w x1 q f x2 }
59
The Accepted Language…
60
Turing Machine as Transducers:
Computing Functions with Turing Machines
• A function f(w) has:
f (w)
w D f ( w) S
61
Computing Functions with Turing Machines…
• A function may have many parameters:
f ( x, y ) x y
62
Computing Functions with Turing Machines…
Integer Domain
5
Decimal:
Binary: 101
Unary: 11111
w f (w)
q0 qf
initial state accept state
q0 w q f f ( w)
Initial Final
Configuration Configuration
x, y are integers
Turing Machine:
66
x y
Start 1 1 1 0 1 1
q0
initial state
67
x y
Start
1 1 1 0 1 1
q0 initial state
x y
Finish
1 1 1 1 0
qf final state
68
Computing Functions with Turing Machines…
x y
1 1 1 1 0
Finish
qf final state
69
Computing Functions with Turing Machines…
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4 70
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 71
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 72
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 73
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 74
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 75
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 76
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 77
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 78
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 79
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 80
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 81
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 82
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 83
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 84
Exercise
The function f ( x) 2 x is computable.
85
Reading (Self Study)
• Turing Thesis
• Universal Turing Machine.
86
Group Assignment: Due date: June 7, 2022
• Form a group of five members and prepare a report not
more than five pages on the following topics.
• Complexity Theory
• Introduction
• Polynomial- Time Algorithm
• Non- Deterministic Polynomial Time Algorithm
• NP Problems