Introduction to
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 consists of an infinite length tape, on which
input is provided as a finite sequence of symbols.
A head reads the input tape. The TM starts at start
state s0. On reading an input symbol it optionally
replaces it with another symbol, changes its
internal state and moves one cell to the right or left.
2
The Turing Machine
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
No boundaries -- infinite length
...... ......
Read-Write head
The head moves Left or Right
5
...... ......
Read-Write head
The head at each time step:
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
Input string Blank symbol
...... # # 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
Allowed Not Allowed
a → b, R q2 a → b, R q2
q1 q1
q3 a → d, L q3
b → d, L
No lambda transitions allowed
14
Partial Transition Function
Example:
...... # # a b a c # # # ......
q1
a → b, R q2 Allowed:
q1 No transition
for input symbol c
b → d, L q3
15
Halting
The machine halts if there are
no possible transitions to follow
...... # # 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
• Final states have no outgoing transitions
• In a final state the machine halts
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 Turing machine that accepts the language:
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
a → a, R Halt & Accept
→ , 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:
•The final state cannot be reached
•The machine never halts
•The input is not accepted
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 }
we can easily construct
n n n
a machine for the language {a b c }
48
Construct a TM to erase the input string
• Language recognizers such as DFA’s cannot
perform computational tasks such as
erasing strings.
• So, no DFA can be used for erasing
strings.
• Language generators such as CFG’s cannot
perform computational tasks such as
erasing strings.
• So, no CFG can be used for erasing strings.
• TM’s are more powerful than language
recognizers and language generators. A
TM can be used for erasing strings
49
50
51
Formal Definitions
for
Turing Machines
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
A Move: q2 xayb x q0 ayb
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
q2 xayb x q0 ayb xx q1 yb xxy q1 b
58
q2 xayb x q0 ayb xx q1 yb xxy q1 b
Equivalent notation: q2 xayb xxy q1 b
59
Initial configuration: q0 w
Input string
a a b b
q0
60
The Accepted Language
For any Turing Machine M
L( M ) = {w : q0 w x1 q f x2 }
Initial state Final state
61
Standard Turing Machine
The machine we described is the standard:
• Deterministic
• Infinite tape in both directions
•Tape is the input/output file
62
Computing Functions
with
Turing Machines
63
A function f (w) has:
Domain: D Result Region: S
f (w)
w D f ( w) S
64
A function may have many parameters:
Example: Addition function
f ( x, y ) = x + y
65
Integer Domain
Decimal: 5
Binary: 101
Unary: 11111
We prefer unary representation:
easier to manipulate with Turing machines
66
Definition:
A function f is computable if
there is a Turing Machine M such that:
Initial configuration Final configuration
w f (w)
q0 initial state q f final state
For all w D Domain
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
For all w D Domain
68
Example
The function f ( x, y ) = x + y is computable
x, y are integers
Turing Machine:
Input string: x0 y unary
Output string: xy 0 unary
69
x y
Start 1 1 1 0 1 1
q0
initial state
The 0 is the delimiter that
separates the two numbers
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
The function f ( x) = 2 x is computable
x is integer
Turing Machine:
Input string: x unary
Output string: xx unary
88
x
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
• Replace every 1 with $
• Repeat:
• Find rightmost $, replace it with 1
• Go to right end, insert 1
Until no more $ remain
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
Turing Machine U+1:
Given a string of 1s on a tape (followed by
an infinite number of 0s), add one more 1 at the
end of the string.
#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
Match a 1 from x with a 1 from y
Until all of x or y is matched
• If a 1 from x is not matched
erase tape, write 1 ( x y)
else
erase tape, write 0 ( x y) 97
Combining Turing Machines
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
Any mathematical problem solving that
can be described by a mechanical procedure
(algorithm) can be modeled by a
Turing machine.
All computers today perform only mechanical
problem solving. They are no more expressive
than a Turing machine.
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