Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Turing Machines
1
The Language Hierarchy
n n n
a b c ? ww ?
Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
COMP 335 2
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 *
COMP 335 3
A Turing Machine
Tape
...... ......
Read-Write head
Transition diagram -- program
COMP 335 4
The Tape
Read-Write head
COMP 335 5
...... ......
Read-Write head
COMP 335 6
Example:
Time 0
...... a b a c ......
Time 1
...... a b k c ......
1. Reads a
2. Writes k
3. Moves Left
COMP 335 7
Time 1
...... a b k c ......
Time 2
...... a f k c ......
1. Reads b
2. Writes f
3. Moves Right
COMP 335 8
Example Input String: “abac”
...... a b a c ......
head
Convention: When a TM begins its execution,
the R/W head is under the leftmost symbol
in the input
COMP 335 9
Input string Blank symbol
...... a b a c ......
head
COMP 335 10
States & Transitions
Read Write
Move Left
q1 a b, L q2
Move Right
q1 a b, R q2
COMP 335 11
Example:
Time 1
...... a b a c ......
q1
current state
q1 a b, R q2
COMP 335 12
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, R q2
COMP 335 13
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, L q2
COMP 335 14
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c g ......
q2
q1 g, R q2
COMP 335 15
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:
There is no transition
q1
in q1 for the input
b d, L q3 symbol c
COMP 335 17
Halting
COMP 335 18
Example:
...... a b a c ......
q1
a b, R q2
No possible transition
q1
HALT!!!
b d, L q3
COMP 335 19
Final States
q1 q2 Allowed
q1 q2 Not Allowed
If TM halts
Accept Input
in a final state
If TM halts in a
non-final state
Reject Input or
if it enters an
infinite loop ?!
COMP 335 21
Turing Machine; An Example
a a, R
, L
q0 q1
COMP 335 22
Time 0 a a a
q0
a a, R
, L
q0 q1
COMP 335 23
Time 1 a a a
q0
a a, R
, L
q0 q1
COMP 335 24
Time 2 a a a
q0
a a, R
, L
q0 q1
COMP 335 25
Time 3 a a a
q0
a a, R
, L
q0 q1
COMP 335 26
Time 4 a a a
q1
, L
q0 q1
COMP 335 27
Rejection Example
Time 0 a b a
q0
a a, R
, L
q0 q1
COMP 335 28
Time 1 a b a
q0
No possible Transition
a a, R Halt & Reject
, L
q0 q1
COMP 335 29
Infinite Loop Example
b b, L
a a, R
, L
q0 q1
COMP 335 30
Time 0 a b a
q0
b b, L
a a, R
, L
q0 q1
COMP 335 31
Time 1 a b a
q0
b b, L
a a, R
, L
q0 q1
COMP 335 32
Time 2 a b a
q0
b b, L
a a, R
, L
q0 q1
COMP 335 33
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
COMP 335 34
Because of the infinite loop:
COMP 335 35
Another Example
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
COMP 335 36
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
COMP 335 37
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
COMP 335 38
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
COMP 335 39
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
COMP 335 40
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
COMP 335 41
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
COMP 335 42
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
COMP 335 43
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
COMP 335 44
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
COMP 335 45
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
COMP 335 46
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
COMP 335 47
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
COMP 335 48
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
COMP 335 49
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
COMP 335 50
Observation:
to construct a TM for
the language {a nb n c n : n 1}
COMP 335 51
Formal Definitions
for
Turing Machines
COMP 335 52
Transition Function
q1 a b, R q2
(q1, a) (q2 , b, R)
COMP 335 53
Transition Function
q1 c d, L q2
(q1, c) (q2 , d , L)
COMP 335 54
Turing Machine:
Input Tape
alphabet alphabet
States
M (Q, , , , q0 , , F )
Transition Final
function states
Initial Blank in Γ
state COMP 335 55
Configuration
c a b a
q1
Instantaneous description: ca q1 ba
COMP 335 56
Time 4 Time 5
x a y b x a y b
q2 q0
COMP 335 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
COMP 335 59
Initial configuration: q0 w
Input string
a a b b
q0
COMP 335 60
The Accepted Language
L( M ) {w : q0 w x1 q f x2 }
COMP 335 61
Standard Turing Machine
• Deterministic
COMP 335 62
Computing Functions
with
Turing Machines
COMP 335 63
A function f (w) has:
Domain: D Range: S
f (w)
w D f ( w) S
COMP 335 64
A function may have several
parameters / arguments:
Example: The Addition function:
f ( x, y ) x y
COMP 335 65
Integer Domain
Decimal: 5
Binary: 101
Unary: 11111
COMP 335 67
In other words:
A function f is computable if there is a
Turing Machine M such that:
q0 w q f f ( w)
Initial Final
Configuration Configuration
COMP 335 69
x y
Start 1 1 1 0 1 1
q0
initial state
COMP 335 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
COMP 335 71
The 0 here may help when we use
the result for other operations
x y
Finish 1 1 1 1 0
q f final state
COMP 335 72
Turing machine M for the 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
COMP 335 73
Execution Example: Input (time 0)
x y
x 11 (2)
1 1 0 1 1
y 11 (2)
q0
q4
COMP 335 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
COMP 335 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
COMP 335 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
COMP 335 77
Time 3 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
COMP 335 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
COMP 335 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
COMP 335 80
Time 6 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
COMP 335 81
Time 7 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
COMP 335 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
COMP 335 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
COMP 335 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
COMP 335 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
COMP 335 86
Time 12 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
COMP 335 87
Time 13 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
COMP 335 88
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
COMP 335 90
Turing Machine Pseudocode for f ( x) 2 x
• Repeat:
• Find rightmost $, replace it with 1
COMP 335 91
A 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
COMP 335 92
Example
Initial Config. Final Config.
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
COMP 335 93
Another Example
1 if x y
f ( x, y )
0 if x y
COMP 335 94
Design Turing Machine for this function
1 if x y
f ( x, y )
0 if x y
Input: x0 y
Output: 1 or 0
COMP 335 95
Pseudocode/block diagram for
our Turing Machine:
• Repeat
Match a 1 from x with a 1 from y
Until all of x or y is matched
COMP 335 96
Combining Turing Machines
COMP 335 97
Block Diagram
Turing
input output
Machine
COMP 335 98
Example: x y if x y
f ( x, y )
0 if x y
x, y Adder
x y
A
x, y Comparer x y f ( x, y)
C
x y Eraser 0
E
COMP 335 99
Copyright © An Introduction to Formal Languages and Automata, Peter Linz
Copyright © Introduction to the Theory of Computation, Michael Sipser
Copyright © Costas Busch
Copyright © Nora Houari
100