Languages and Finite Automata

Download Report

Transcript Languages and Finite Automata

Turing Machines
Prof. Busch - LSU
1
The Language Hierarchy
ww ?
n n n ?
a b c
Context-Free Languages
n n
R
a b
ww
Regular Languages
a*
a *b *
Prof. Busch - LSU
2
Languages accepted by
Turing Machines
ww
n n n
a b c
Context-Free Languages
n n
R
a b
ww
Regular Languages
a*
a *b *
Prof. Busch - LSU
3
A Turing Machine
Tape
......
......
Read-Write head
Control Unit
Prof. Busch - LSU
4
The Tape
No boundaries -- infinite length
......
......
Read-Write head
The head moves Left or Right
Prof. Busch - LSU
5
......
......
Read-Write head
The head at each transition (time step):
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
Prof. Busch - LSU
6
Example:
Time 0
......
a b a c
Time 1
......
1. Reads
2. Writes
a b k c
......
......
a
k
3. Moves Left
Prof. Busch - LSU
7
Time 1
......
a b k c
Time 2
......
1. Reads
2. Writes
a f k c
......
......
b
f
3. Moves Right
Prof. Busch - LSU
8
The Input String
Input string
......
  a b a c
Blank symbol
  
......
head
Head starts at the leftmost position
of the input string
Prof. Busch - LSU
9
States & Transitions
Read
q1
Write
a  b, L
Move Left
q2
Move Right
q1
a  b, R
Prof. Busch - LSU
q2
10
Example:
Time 1
......
  a b a c
  
......
q1
current state
q1
a  b, R
q2
Prof. Busch - LSU
11
......
Time 1
  a b a c
  
......
  
......
q1
......
Time 2
  a b b c
q2
q1
a  b, R
q2
Prof. Busch - LSU
12
Example:
......
Time 1
  a b a c
  
......
  
......
q1
......
Time 2
  a b b c
q2
q1
a  b, L
q2
Prof. Busch - LSU
13
Example:
......
Time 1
  a b a c
  
......
q1
......
Time 2
  a b b c g  
......
q2
q1
  g, R
q2
Prof. Busch - LSU
14
Determinism
Turing Machines are deterministic
Not Allowed
Allowed
a  b, R
q2
a  b, R
q2
a  d, L
q3
q1
q1
b  d, L
q3
No lambda transitions allowed
Prof. Busch - LSU
15
Partial Transition Function
Example:
......
  a b a c
  
......
q1
a  b, R
q2
q1
b  d, L
q3
Allowed:
No transition
for input symbol
Prof. Busch - LSU
c
16
Halting
The machine halts in a state if there is
no transition to follow
Prof. Busch - LSU
17
Halting Example 1:
......
  a b a c
  
......
q1
q1
No transition from q1
HALT!!!
Prof. Busch - LSU
18
Halting Example 2:
......
  a b a c
  
......
q1
a  b, R
q2
b  d, L
q3
q1
No possible transition
from q1 and symbol c
Prof. Busch - LSU
HALT!!!
19
Accepting States
q1
q2
Allowed
q1
q2
Not Allowed
•Accepting states have no outgoing transitions
•The machine halts and accepts
Prof. Busch - LSU
20
Acceptance
Accept Input
string
If machine halts
in an accept state
Reject Input
string
If machine halts
in a non-accept state
or
If machine enters
an infinite loop
Prof. Busch - LSU
21
Observation:
In order to accept an input string,
it is not necessary to scan all the
symbols in the string
Prof. Busch - LSU
22
Turing Machine Example
Input alphabet
  {a , b }
Accepts the language:
a*
a  a, R
q0
  , L
Prof. Busch - LSU
q1
23
Time 0
  a a a  
q0
a  a, R
q0
  , L
Prof. Busch - LSU
q1
24
Time 1
  a a a  
q0
a  a, R
q0
  , L
Prof. Busch - LSU
q1
25
Time 2
  a a a  
q0
a  a, R
q0
  , L
Prof. Busch - LSU
q1
26
Time 3
  a a a  
q0
a  a, R
q0
  , L
Prof. Busch - LSU
q1
27
Time 4
  a a a  
q1
a  a, R
q0
Halt & Accept
  , L
Prof. Busch - LSU
q1
28
Rejection Example
Time 0
  a b a  
q0
a  a, R
q0
  , L
Prof. Busch - LSU
q1
29
Time 1
  a b a  
q0
No possible Transition
Halt & Reject
a  a, R
q0
  , L
Prof. Busch - LSU
q1
30
A simpler machine for same language
but for input alphabet
  {a }
Accepts the language:
a*
q0
Prof. Busch - LSU
31
Time 0
  a a a  
q0
Halt & Accept
q0
Not necessary to scan input
Prof. Busch - LSU
32
Infinite Loop Example
A Turing machine
for language a * b( a  b) *
b  b, L
a  a, R
q0
  , L
Prof. Busch - LSU
q1
33
Time 0
  a b a  
q0
b  b, L
a  a, R
q0
  , L
Prof. Busch - LSU
q1
34
Time 1
  a b a  
q0
b  b, L
a  a, R
q0
  , L
Prof. Busch - LSU
q1
35
Time 2
  a b a  
q0
b  b, L
a  a, R
q0
  , L
Prof. Busch - LSU
q1
36
Time 2
  a b a  
q0
  a b a  
q0
Time 4
  a b a  
q0
Time 5
  a b a  
q0
Prof. Busch - LSU
Infinite loop
Time 3
37
Because of the infinite loop:
•The accepting state cannot be reached
•The machine never halts
•The input string is rejected
Prof. Busch - LSU
38
Another Turing Machine Example
Turing machine for the language
n n
{a b }
n 1
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
39
Basic Idea:
Match a’s with b’s:
Repeat:
replace leftmost a with x
find leftmost b and replace it with y
Until there are no more a’s or b’s
If there is a remaining a or b reject
Prof. Busch - LSU
40
 a a b b  
Time 0
q0
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
41
 x a b b  
Time 1
q1
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
42
 x a b b  
Time 2
q1
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
43
 x a y b  
Time 3
q2
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
44
 x a y b  
Time 4
q2
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
45
 x a y b  
Time 5
q0
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
46
 x x y b  
Time 6
q1
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
47
 x x y b  
Time 7
q1
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
48
 x x y y  
Time 8
q2
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
49
 x x y y  
Time 9
q2
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
50
 x x y y  
Time 10
q0
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
51
 x x y y  
Time 11
q3
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
52
 x x y y  
Time 12
q3
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
53
 x x y y  
Time 13
q4
Halt & Accept
y  y, R
q3
q4
  , L
y  y, R
q0
y  y, R
a  a, R
a  x, R
q1
y  y, L
a  a, L
b  y, L
q2
x  x, R
Prof. Busch - LSU
54
Observation:
If we modify the
machine for the language
we can easily construct
a machine for the language
Prof. Busch - LSU
n n
{a b }
n n n
{a b c }
55
Formal Definitions
for
Turing Machines
Prof. Busch - LSU
56
Transition Function
q1
a  b, R
q2
 (q1, a)  (q2 , b, R)
Prof. Busch - LSU
57
Transition Function
q1
c  d, L
q2
 (q1, c)  (q2 , d , L)
Prof. Busch - LSU
58
Turing Machine:
States
Input
alphabet
Tape
alphabet
M  (Q, , ,  , q0 , , F )
Transition
function
Initial
blank
stateProf. Busch - LSU
Accept
states
59
Configuration
  c a b a  
q1
Instantaneous description:
Prof. Busch - LSU
ca q1 ba
60
Time 4
Time 5
 x a y b  
q0
q2
A Move:
 x a y b  
q2 xayb  x q0 ayb
(yields in one mode)
Prof. Busch - LSU
61
Time 4
Time 5
 x a y b  
 x a y b  
q0
q2
Time 6
Time 7
 x x y b  
 x x y b  
q1
q1
A computation
q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b
Prof. Busch - LSU
62
q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b

Equivalent notation:
q2 xayb  xxy q1 b
Prof. Busch - LSU
63
Initial configuration:
q0 w
Input string
w
 a a b b  
q0
Prof. Busch - LSU
64
The Accepted Language
For any Turing Machine
L( M )  {w :
M

q0 w  x1 q f x2 }
Initial state
Prof. Busch - LSU
Accept state
65
If a language L is accepted
by a Turing machine M
then we say that L is:
•Turing Recognizable
Other names used:
•Turing Acceptable
•Recursively Enumerable
Prof. Busch - LSU
66
Computing Functions
with
Turing Machines
Prof. Busch - LSU
67
A function
Domain:
has:
f (w)
Result Region:
D
f (w)
w D
Prof. Busch - LSU
S
f ( w)  S
68
A function may have many parameters:
Example:
Addition function
f ( x, y )  x  y
Prof. Busch - LSU
69
Integer Domain
Decimal:
5
Binary:
101
Unary:
11111
We prefer unary representation:
easier to manipulate with Turing machines
Prof. Busch - LSU
70
Definition:
f
A function
is computable if
there is a Turing Machine M such that:
Initial configuration

w
Final configuration


qf
q0
initial state
For all
f (w) 
accept state
w D
Domain
Prof. Busch - LSU
71
In other words:
f
A function
is computable if
there is a Turing Machine M such that:

q0 w  q f f ( w)
Initial
Configuration
For all
Final
Configuration
w D
Domain
Prof. Busch - LSU
72
Example
The function
f ( x, y )  x  y is computable
x, y
are integers
Turing Machine:
Input string:
x0 y
unary
Output string:
xy0
unary
Prof. Busch - LSU
73
x
Start
 1 1

y
1 0 1  1 
q0
initial state
The 0 is the delimiter that
separates the two numbers
Prof. Busch - LSU
74
y
x
Start
 1 1
1 0 1  1 

q0 initial state
x y
Finish
 1 1

1 1 0 
q f final state
Prof. Busch - LSU
75
The 0 here helps when we use
the result for other operations
x y
Finish
 1 1

1 1 0 
q f final state
Prof. Busch - LSU
76
Turing machine for function
1  1, R
f ( x, y )  x  y
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
77
Execution Example:
x  11 (=2)
y  11
Time 0
y
x
 1 1 0 1 1 
(=2)
q0
Final Result
x y
 1 1 1 1 0 
Prof. Busch - LSU
q4
78
Time 0
 1 1 0 1 1 
q0
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
79
Time 1
 1 1 0 1 1 
q0
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
80
Time 2
 1 1 0 1 1 
q0
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
81
Time 3
 1 1 1 1 1 
q1
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
82
Time 4
 1 1 1 1 1 
q1
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
83
Time 5
 1 1 1 1 1 
q1
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
84
Time 6
 1 1 1 1 1 
q2
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
85
Time 7
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
86
Time 8
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
87
Time 9
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
88
Time 10
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
89
Time 11
 1 1 1 1 0 
q3
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
Prof. Busch - LSU
q4
90
Time 12
 1 1 1 1 0 
q4
1  1, R
1  1, R
1  1, L



,
L
0

1
,
R
1

0
,
L
q
q0
q3
q1
2
  , R
HALT & accept
Prof. Busch - LSU
q4
91
Another Example
The function
f ( x)  2 x
x
is computable
is integer
Turing Machine:
Input string:
Output string:
x
unary
xx
unary
Prof. Busch - LSU
92
x
Start
 1 1

1 
q0 initial state
2x
Finish
 1 1

1 1 1 
q f accept state
Prof. Busch - LSU
93
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
Prof. Busch - LSU
94
Turing Machine for
1 $, R
f ( x)  2 x
1  1, L
1  1, R
q0   , L q1 $  1, R
  , R
q3
q2
  1, L
Prof. Busch - LSU
95
Start
Example
 1 1 
Finish
 1 1 1 1 
q0
q3
1 $, R
1  1, L
1  1, R
q0   , L q1 $  1, R
  , R
q3
q2
  1, L
Prof. Busch - LSU
96
Another Example
The function
f ( x, y ) 
is computable
Input:
Output:
1
if
x y
0
if
x y
x0 y
1
or
0
Prof. Busch - LSU
97
Turing Machine Pseudocode:
• Repeat
Match a 1 from
Until all of
x with a 1 from y
x or y is matched
• If a 1 from x is not matched
erase tape, write 1
else
erase tape, write 0
Prof. Busch - LSU
( x  y)
( x  y)
98
Combining Turing Machines
Prof. Busch - LSU
99
Block Diagram
input
Turing
Machine
Prof. Busch - LSU
output
100
Example:
x  y if x  y
f ( x, y) 
0
x, y
x, y
Comparator
if x  y
Adder
x y
Eraser
0
x y
x y
Prof. Busch - LSU
101