9 10 11turing
9 10 11turing
Turing Machines
2
The Language Hierarchy
* a
Regular Languages
Context-Free Languages
n n
b a
R
ww
n n n
c b a
ww
?
* *b a
?
3
* a
Regular Languages
Context-Free Languages
n n
b a
R
ww
n n n
c b a
ww
* *b a
Languages accepted by
Turing Machines
4
A Turing Machine
...... ......
Tape
Read-Write head
Control Unit
5
The Tape
...... ......
Read-Write head
No boundaries -- infinite length
The head moves Left or Right
6
...... ......
Read-Write head
The head at each transition (time step):
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
7
...... ......
Example:
Time 0
...... ......
Time 1
1. Reads
2. Writes
a a c
b
a
b
k
c
a
k
3. Moves Left
8
...... ......
Time 1
a
b
k
c
...... ......
Time 2
a
k
c f
1. Reads
2. Writes
b
f
3. Moves Right
9
The Input String
...... ......
^ ^ ^ ^
Blank symbol
head
^
a
b
c a
Head starts at the leftmost position
of the input string
Input string
10
States & Transitions
1
q
2
q
L b a ,
Read
Write
Move Left
1
q
2
q
R b a ,
Move Right
11
Example:
1
q
2
q
R b a ,
...... ......
^ ^ ^ ^ ^
a
b
c a
Time 1
1
q
current state
12
...... ......
^ ^ ^ ^ ^
a
b
c a
Time 1
1
q
2
q
R b a ,
...... ......
^ ^ ^ ^ ^
a
b
c
b
Time 2
1
q
2
q
13
...... ......
^ ^ ^ ^ ^
a
b
c a
Time 1
1
q
2
q
L b a ,
...... ......
^ ^ ^ ^ ^
a
b
c
b
Time 2
1
q
2
q
Example:
14
...... ......
^ ^ ^ ^ ^
a
b
c a
Time 1
1
q
2
q
R g, ^
...... ......
^ ^ ^ ^
g
a
b
c
b
Time 2
1
q
2
q
Example:
15
Determinism
1
q
2
q
R b a ,
Allowed
Not Allowed
3
q
L d b ,
1
q
2
q
R b a ,
3
q
L d a ,
No lambda transitions allowed
Turing Machines are deterministic
16
Partial Transition Function
1
q
2
q
R b a ,
3
q
L d b ,
...... ......
^ ^ ^ ^ ^
a
b
c a
1
q
Example:
No transition
for input symbol c
Allowed:
17
Halting
The machine halts in a state if there is
no transition to follow
18
Halting Example 1:
...... ......
^ ^ ^ ^ ^
a
b
c a
1
q
1
q
No transition from
HALT!!!
1
q
19
Halting Example 2:
...... ......
^ ^ ^ ^ ^
a
b
c a
1
q
1
q
2
q
R b a ,
3
q
L d b ,
No possible transition
from and symbol
HALT!!!
1
q
c
20
Accepting States
1
q
2
q
Allowed
1
q
2
q
Not Allowed
Accepting states have no outgoing transitions
The machine halts and accepts
21
Acceptance
Accept Input
If machine halts
in an accept state
Reject Input
If machine halts
in a non-accept state
or
If machine enters
an infinite loop
string
string
22
Observation:
In order to accept an input string,
it is not necessary to scan all the
symbols in the string
23
Turing Machine Example
Accepts the language:
* a
0
q
R a a ,
L , ^ ^
1
q
Input alphabet
} , { b a = E
24
^ ^ ^ ^
a a
Time 0
0
q
a
0
q
R a a ,
L , ^ ^
1
q
25
^ ^ ^ ^
a a
Time 1
0
q
a
0
q
R a a ,
L , ^ ^
1
q
26
^ ^ ^ ^
a a
Time 2
0
q
a
0
q
R a a ,
L , ^ ^
1
q
27
^ ^ ^ ^
a a
Time 3
0
q
a
0
q
R a a ,
L , ^ ^
1
q
28
^ ^ ^ ^
a a
Time 4
1
q
a
0
q
R a a ,
L , ^ ^
1
q
Halt & Accept
29
Rejection Example
0
q
R a a ,
L , ^ ^
1
q
^ ^ ^ ^
b
a
Time 0
0
q
a
30
0
q
R a a ,
L , ^ ^
1
q
^ ^ ^ ^
b
a
Time 1
0
q
a
No possible Transition
Halt & Reject
31
Accepts the language:
* a
0
q
but for input alphabet
} {a = E
A simpler machine for same language
32
^ ^ ^ ^
a a
Time 0
0
q
a
0
q
Halt & Accept
Not necessary to scan input
33
Infinite Loop Example
0
q
R a a ,
L , ^ ^
1
q
L b b ,
A Turing machine
for language
* ) ( * b a b a + +
34
^ ^ ^ ^
b
a
Time 0
0
q
a
0
q
R a a ,
L , ^ ^
1
q
L b b ,
35
^ ^ ^ ^
b
a
Time 1
0
q
a
0
q
R a a ,
L , ^ ^
1
q
L b b ,
36
^ ^ ^ ^
b
a
Time 2
0
q
a
0
q
R a a ,
L , ^ ^
1
q
L b b ,
37
^ ^ ^ ^
b
a
Time 2
0
q
a
^ ^ ^ ^
b
a
Time 3
0
q
a
^ ^ ^ ^
b
a
Time 4
0
q
a
^ ^ ^ ^
b
a
Time 5
0
q
a
I
n
f
i
n
i
t
e
l
o
o
p
38
Because of the infinite loop:
The accepting state cannot be reached
The machine never halts
The input string is rejected
39
Another Turing Machine Example
Turing machine for the language } {
n n
b a
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
1 > n
40
Match as with bs:
Repeat:
replace leftmost a with x
find leftmost b and replace it with y
Until there are no more as or bs
If there is a remaining a or b reject
Basic Idea:
41
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^ b
a
0
q
a
b Time 0 ^
42
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^ b
x
1
q
a
b ^ Time 1
43
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^ b
x
1
q
a
b ^ Time 2
44
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
2
q
a
b ^ Time 3
45
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
2
q
a
b ^ Time 4
46
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
0
q
a
b ^ Time 5
47
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
1
q
x
b ^ Time 6
48
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
1
q
x
b ^ Time 7
49
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x x y
2
q
^ Time 8
50
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x x y
2
q
^ Time 9
51
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
0
q
x y
^ Time 10
52
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
3
q
x y
^ Time 11
53
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
3
q
x y
^ Time 12
54
0
q
1
q
2
q
3
q
R x a ,
R a a ,
R y y ,
L y b ,
L a a ,
L y y ,
R x x ,
R y y ,
R y y ,
4
q
L , ^ ^
^ ^
y x
4
q
x y
^
Halt & Accept
Time 13
55
Another Turing Machine Example
Turing machine for the language
A = {0
2n
n >= O}
56
Another Turing Machine Example
Turing machine for the language
A = {0
2n
n >= O}
57
modify the
machine for the language
} {
n n
b a
And construct
a machine for the language
} {
n n n
c b a
Home Work:
58
Formal Definitions
for
Turing Machines
59
Transition Function
1
q
2
q
R b a ,
) , , ( ) , (
2 1
R b q a q = o
60
1
q
2
q
L d c ,
) , , ( ) , (
2 1
L d q c q = o
Transition Function
61
Turing Machine:
) , , , , , , (
0
F q Q M ^ I E = o
States
Input
alphabet
Tape
alphabet
Transition
function
Initial
state
blank
Accept
states
62
Configuration
^ ^ ^
b
a
1
q
a
Instantaneous description:
c
^
ba q ca
1
63
^ ^
y x
2
q
a
b
Time 4
^ ^
y x
0
q
a
b
Time 5
^ ^
A Move:
ayb q x xayb q
0 2
(yields in one mode)
64
^ ^
y x
2
q
a
b
Time 4
^ ^
y x
0
q
a
b
Time 5
^ ^
b q xxy yb q xx ayb q x xayb q
1 1 0 2
^ ^
y x
1
q
x
b
Time 6
^ ^
y x
1
q
x
b
Time 7
^ ^
A computation
65
b q xxy yb q xx ayb q x xayb q
1 1 0 2
b q xxy xayb q
1 2
-
Equivalent notation:
66
Initial configuration:
w q
0
^ ^ b
a
0
q
a
b ^
w
Input string
67
The Accepted Language
For any Turing Machine
M
} : { ) (
2 1 0
x q x w q w M L
f
-
=
Initial state Accept state
68
If a language is accepted
by a Turing machine
then we say that is:
Turing Acceptable
Recursively Enumerable
M
L
L
Turing Recognizable
Other names used:
69
Computing Functions
with
Turing Machines
70
A function
) (w f
Domain:
Result Region:
has:
D
D we
S
S w f e ) (
) (w f
71
A function may have many parameters:
y x y x f + = ) , (
Example:
Addition function
72
Integer Domain
Unary:
Binary:
Decimal:
11111
101
5
We prefer unary representation:
easier to manipulate with Turing machines
73
Definition:
A function is computable if
there is a Turing Machine such that:
f
M
Initial configuration Final configuration
D we Domain
^
0
q
^
w
^
f
q
^
) (w f
accept state initial state
For all
74
) (
0
w f q w q
f
-
Initial
Configuration
Final
Configuration
A function is computable if
there is a Turing Machine such that:
f
M
In other words:
D we Domain
For all
75
Example
The function
y x y x f + = ) , ( is computable
Turing Machine:
Input string:
y x0
unary
Output string: 0 xy
unary
y x,
are integers
76
^ 0
0
q
1 1 ^ 1 1
x y
1
Start
initial state
The 0 is the delimiter that
separates the two numbers
77
^ 0
0
q
1 1 ^ 1 1
x y
1
^ 0
f
q
1 ^ 1
y x +
1 1
Start
Finish
final state
initial state
78
^ 0
f
q
1 ^ 1
y x +
1 1
Finish
final state
The 0 here helps when we use
the result for other operations
79
Construct the TM for Unary Addition
80
0
q
Turing machine for function
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
y x y x f + = ) , (
81
Execution Example:
11 = x
11 = y
^ 0
0
q
1 1 ^ 1 1
Time 0
x y
Final Result
^ 0
4
q
1 1 ^ 1 1
y x +
(=2)
(=2)
82
^ 0
0
q
1 1 Time 0 ^
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
1 1
83
^
0
q
^
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
0 1 1 1 1 Time 1
84
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^ 0
0
q
1 1 ^ 1 1 Time 2
85
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
1
q
^ 1 1 1 1 1 Time 3
86
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
1
q
1 1 ^ 1 1 1 Time 4
87
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
1
q
^ 1 1 1 1 1 Time 5
88
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
2
q
1 1 ^ 1 1 1 Time 6
89
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
3
q
^ 1 1 1
0 1 Time 7
90
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
3
q
1 1 ^ 1
0 1 Time 8
91
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
3
q
^ 1 1 1
0 1 Time 9
92
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
3
q
1 1 ^ 1
0 1 Time 10
93
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
3
q
^ 1 1 1
0 1 Time 11
94
0
q
1
q
2
q
3
q
L , ^ ^ L , 0 1
L , 1 1
R , ^ ^
R , 1 0
R , 1 1
4
q
R , 1 1
^
4
q
1 1 ^ 1
0 1
HALT & accept
Time 12
95
Another Example
The function
x x f 2 ) ( = is computable
Turing Machine:
Input string: x
unary
Output string:
xx
unary
x
is integer
96
^
0
q
1 ^ 1
x
1
^ 1
f
q
1 ^ 1
x 2
1 1
Start
Finish
accept state
initial state
97
Turing Machine Pseudocode for x x f 2 ) ( =
Replace every 1 with $
Repeat:
Find rightmost $, replace it with 1
Go to right end, insert 1
Until no more $ remain
98
0
q
1
q
2
q
3
q
R , 1 $
L , 1 ^
L , ^ ^
R $, 1
L , 1 1 R , 1 1
R , ^ ^
Turing Machine for
x x f 2 ) ( =
99
0
q
1
q
2
q
3
q
R , 1 $
L , 1 ^
L , ^ ^
R $, 1
L , 1 1 R , 1 1
R , ^ ^
Example
^
0
q
1 ^ 1
^
3
q
^ 1 1 1 1
Start
Finish
100
Combining Turing Machines
101
Block Diagram
Turing
Machine
input output
102
Example:
= ) , ( y x f
0
y x +
y x >
y x s
if
if
Comparator
Adder
Eraser
y x,
y x,
y x >
y x s
y x +
0
103
A Universal Turing Machine
104
Turing Machines are hardwired
they execute
only one program
A limitation of Turing Machines:
Real Computers are re-programmable
105
Solution:
Universal Turing Machine
Reprogrammable machine
Simulates any other Turing Machine
Attributes:
106
Universal Turing Machine
simulates any Turing Machine M
Input of Universal Turing Machine:
Description of transitions of M
Input string of M
107
Universal
Turing
Machine
M
Description of
Tape Contents of
M
State of
M
Three tapes
Tape 2
Tape 3
Tape 1
108
We describe Turing machine
as a string of symbols:
We encode as a string of symbols
M
M
Description of M
Tape 1
109
Alphabet Encoding
Symbols:
a
b
c
d
Encoding:
1 11
111
1111
110
State Encoding
States:
1
q
2
q
3
q
4
q
Encoding:
1 11
111
1111
Head Move Encoding
Move:
Encoding:
L R
1 11
111
Transition Encoding
Transition:
) , , ( ) , (
2 1
L b q a q = o
Encoding: 1 0 11 0 11 0 1 0 1
separator
112
Turing Machine Encoding
Transitions:
) , , ( ) , (
2 1
L b q a q = o
Encoding:
1 0 11 0 11 0 1 0 1
) , , ( ) , (
3 2
R c q b q = o
11 0 111 0 111 10 1 0 11 00
separator
113
Tape 1 contents of Universal Turing Machine:
binary encoding
of the simulated machine M
1100 0 111 0 111 10 1 0 10011 0 11 0 11 0 1 0 1
Tape 1
114
A Turing Machine is described
with a binary string of 0s and 1s
The set of Turing machines
forms a language:
each string of this language is
the binary encoding of a Turing Machine
Therefore:
115
Language of Turing Machines
L = { 010100101,
00100100101111,
111010011110010101,
}
(Turing Machine 1)
(Turing Machine 2)