Turing Machine Basics
Turing Machine Basics
Turing Machines
The Language Hierarchy
n n n ?
a b c ww ?
Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
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 *
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 k c ......
1. Reads a
2. Writes k
3. Moves Left
7
Time 1
...... a b k c ......
Time 2
...... a f k c ......
1. Reads b
2. Writes f
3. Moves Right
8
The Input String
...... a b a c ......
head
12
we assume that Σ ⊆ Γ – {□}, that is, that the input alphabet is a subset of
the tape alphabet, not including the blank. 13
The transition function δ is defined as
δ : Q × Γ → Q × Γ × {L,R}
14
• The whole process may terminate, which we achieve in a Turing
machine by putting it into a halt state.
• A Turing machine is said to halt whenever it reaches a configuration
for which δ is not defined.
• A Turing machine is said to halt whenever it reaches a configuration
for which δ is defined or when string gets accepted.
15
• If this Turing machine is started in state q0 with the symbol a under the
read-write head, the applicable transition rule is δ (q0,a)= (q0,b,R).
• Therefore, the read-write head will replace the a with a ‘b’, then move
right on the tape.
• Any subsequent a will also be replaced with a ‘b’, but b's will not be
modified.
• When the machine encounters the first blank, it will move left one cell,
then halt in final state q1.
Sequence of moves
16
Transition Graph
Infinite Loop
17
Summarize the main features of our model
18
q0aa, bq0a, bbq0 □, bq1b
19
The sequence of configurations leading to a halt state will
be called a computation.
20
Turing Machines as Language Accepters
The machine is started in the initial state q0 with the read write
head positioned on the leftmost symbol of ω. If, after a
sequence of moves, the Turing machine enters a final state
and halts, then w is considered to be accepted.
21
Example 1:
For Σ = {0,1}, design a Turing machine that accepts the language
denoted by the regular expression 00*.
• Starting at the left end of the input, we read each symbol and check that it is a
0.
• If it is, we continue by moving right.
• If we reach a blank without encountering anything but 0, we terminate
and accept the string
• If the input contains a 1 anywhere, the string is not in L(00*), and we halt
in a nonfinal state.
• To keep track of the computation, two internal states Q= {q0,q1} and one
final state F= {q1} can be assumed.
22
Example 1: a/ a , R □ / □, R
q0 q1 qf
L(aa*) a/a,R
□ a a a □ □
ID: q0 a a a Ͱ a q1 a a Ͱ a a q1 a Ͱ a a a q1 □ Ͱ a a a □ qf □
23
Example 2:
δ( q0, a) = (q1, a, R)
δ( q1, a) = (q1, a, R)
δ( q1, b) = (q1, b, R)
δ( q1, □) = (q2, □, R)
ID: q0 a b a Ͱ a q1 b a Ͱ a b q1 a Ͱ a b a q1 □ Ͱ a b a □ q2 □
24
a/a, R | b/b, R
Example 3:
qf
ID: q0 a b a b Ͱ a q1 b ab Ͱ a b q0 a b Ͱ a b a q1b □ Ͱ a b a b q0
□ Ͱ a b a b □ q2 □
25
Example 4: L= {w | w ϵ Σ = (0+1)*} containing substring 001
0
1 0, 1
q0 0
q1 q2 q3
0 1
δ( q2, 0) = (q2, 0, R) qf
δ( q2, 1) = (q3, 1, R)
δ( q3, 0) = (q3, 0, R)
δ( q3, 1) = (q3, 1, R)
δ( q3, □) = (qf, □, R)
26
Input string Blank symbol
...... a b a c ......
head
27
States & Transitions
Read Write
Move Left
q1 a b, L q2
Move Right
q1 a b, R q2
28
Example:
Time 1
...... a b a c ......
q1
current state
q1 a b, R q2
29
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, R q2
30
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, L q2
31
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c g ......
q2
q1 g, R q2
32
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
34
Halting
35
Example:
...... a b a c ......
q1
a b, R q2
No possible transition
q1
HALT!!!
b d, L q3
36
Final States
q1 q2 Allowed
q1 q2 Not Allowed
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
38
Turing Machine Example
Language?
a a, R
, L
q0 q1
39
Turing Machine Example
a a, R
, L
q0 q1
40
Time 0 a a a
q0
a a, R
, L
q0 q1
41
Time 1 a a a
q0
a a, R
, L
q0 q1
42
Time 2 a a a
q0
a a, R
, L
q0 q1
43
Time 3 a a a
q0
a a, R
, L
q0 q1
44
Time 4 a a a
q1
, L
q0 q1
45
Rejection Example
Time 0 a b a
q0
a a, R
, L
q0 q1
46
Time 1 a b a
q0
No possible Transition
a a, R Halt & Reject
, L
q0 q1
47
Language?
b b, L
a a, R
, L
q0 q1
48
Infinite Loop Example
A Turing machine
for language aa * b( a b) *
b b, L
a a, R
, L
q0 q1
49
Time 0 a b a
q0
b b, L
a a, R
, L
q0 q1
50
Time 1 a b a
q0
b b, L
a a, R
, L
q0 q1
51
Time 2 a b a
q0
b b, L
a a, R
, L
q0 q1
52
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
53
Because of the infinite loop:
54
• Starting at the leftmost a, we check it off by replacing it with some
symbol, say x.
• We then let the read-write head travel right to find the leftmost b, which
in turn is checked off by replacing it with another symbol, say y.
• Traveling back and forth this way, we match each a with a corresponding
b. If after some time no a's or b's remain, then the string must be in L.
55
Q= {q0,q1,q2,q3,q4},F= {q4}, Σ= {a,b},Γ={a,b, x, y, }.
The transitions can be broken into several parts.
Phase 1:
• The set replaces the leftmost a with an x, then causes the read-write head
to travel right to the first b, replacing it with a y.
• When the y is written, the machine enters a state q2, indicating that an a has
been successfully paired with a b.
56
Final check…. is made to see if all a’s and b’s have been
replaced (to detect input where an a follows a b)
57
58
Another Turing Machine Example
Language?
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
59
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
60
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
61
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
62
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
63
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
64
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
65
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
66
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
67
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
68
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
69
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
70
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
71
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
72
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
73
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
74
Observation:
If we modify the
machine for the language n n
{a b }
75
we can easily construct a machine for the language
{a nb nc n }
y/y,R z/z,R
a/a,R b/b,R
a/x,R z/z,L
b/y,R
q0 q1 q2 q3 y/y,L
c/z,L b/b,L
y/y,R a/a,L
x/x,R
B/B,R
q4 qf
y/y, R
z/z,R
76
Turing Machines as Transducers
Example 1:
Given two positive integers x and y, design a Turing machine that computes x + y.
• We will assume that w(x) and w(y) are on the tape in unary notation, separated by a
single 0, with the read-write head on the leftmost symbol of w(x).
• After the computation, w (x+ y) will be on the tape followed by a single 0, and the
read-write head will be positioned at the left end of the result.
77
Integer Domain
Decimal: 5
Binary: 101
Unary: 11111
Instantaneous Description
79
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
80
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
81
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
82
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
83
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
84
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
85
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
86
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
87
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
88
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
89
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
90
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
91
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
92
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
93
Design a Turing machine that copies strings of 1’s. More precisely, find
a machine that performs the computation
for any w ∈{1}+.
94
Transition Graph
Instantaneous Description
95
A function may have many parameters:
f ( x, y ) x y
96
Definition:
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:
99
x y
Start 1 1 1 0 1 1
q0 initial state
x y
Finish 1 1 1 1 0
q f final state
100
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
102
Turing Machine Pseudocode for f ( x) 2 x
• Repeat:
• Find rightmost $, replace it with 1
103
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
104
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
105
• Instead of matching a's and b’s, we match each 1 on the left of
the dividing 0 with the 1 on the right.
or
106
• In the first case, when we attempt to match another 1, we encounter
the blank at the right of the working space. This can be used as a
signal to enter the state q y.
Case 1 :
• In the second case, we still find a 1 on the right when all 1’s on the left
have been replaced. We use this to get into the other state q n.
Case 2:
Try …
Design a Turing machine that multiplies two positive integers
in unary notation.
107
Combining Turing Machines
108
Block Diagram
Turing
input output
Machine
109
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
110
111
Consider the macroinstruction if a then qj else qk, with the following
interpretation.
112