Languages and Finite Automata

Download Report

Transcript Languages and Finite Automata

Non-Deterministic
Finite Automata
Costas Busch - LSU
1
Nondeterministic Finite Automaton (NFA)
Alphabet = {a}
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
2
Alphabet = {a}
Two choices
a
q0
q1
a
q2 No transition
a
q3 No transition
Costas Busch - LSU
3
First Choice
a a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
4
First Choice
a a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
5
First Choice
a a
All input is consumed
a
q0
q1
a
q2
“accept”
a
q3
Costas Busch - LSU
6
Second Choice
a a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
7
Second Choice
a a
Input cannot be consumed
a
q0
a
q1
a
q2
Automaton Halts
q3 “reject”
Costas Busch - LSU
8
An NFA accepts a string:
if there is a computation of the NFA
that accepts the string
i.e., all the input string is processed and the
automaton is in an accepting state
Costas Busch - LSU
9
aa is accepted by the NFA:
“accept”
a
q0
q1
a
q2
a
q0
a
q3
because this
computation
accepts aa
q1
a
q3
a
q2
“reject”
this computation
is ignored
Costas Busch - LSU
10
Rejection example
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
11
First Choice
a
“reject”
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
12
Second Choice
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
13
Second Choice
a
a
q0
q1
a
q2
a
q3 “reject”
Costas Busch - LSU
14
Another Rejection example
a a a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
15
First Choice
a a a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
16
First Choice
a a a
Input cannot be consumed
a
q0
q1
a
q3
a
q2
“reject”
Automaton halts
Costas Busch - LSU
17
Second Choice
a a a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
18
Second Choice
a a a
Input cannot be consumed
a
q0
q1
a
q2
Automaton halts
a
q3
“reject”
Costas Busch - LSU
19
An NFA rejects a string:
if there is no computation of the NFA
that accepts the string.
For each computation:
• All the input is consumed and the
automaton is in a non accepting state
OR
• The input cannot be consumed
Costas Busch - LSU
20
a
is rejected by the NFA:
“reject”
a
q0
q1
a
q2
a
q0
a
q3
“reject”
q1
a
q2
a
q3
All possible computations lead to rejection
Costas Busch - LSU
21
aaa
is rejected by the NFA:
“reject”
a
q0
q1
a
q2
a
q0
a
q3
q1
a
q3
a
q2
“reject”
All possible computations lead to rejection
Costas Busch - LSU
22
Language accepted:
a
q0
q1
L  {aa}
a
q2
a
q3
Costas Busch - LSU
23
Lambda Transitions
q0
a
q1 
q2
Costas Busch - LSU
a
q3
24
a a
q0
a
q1 
q2
Costas Busch - LSU
a
q3
25
a a
q0
a
q1 
q2
Costas Busch - LSU
a
q3
26
input tape head does not move
a a
q0
a
q1 
q2
a
q3
Automaton changes state
Costas Busch - LSU
27
all input is consumed
a a
“accept”
q0
String
a
aa
q1 
q2
a
q3
is accepted
Costas Busch - LSU
28
Rejection Example
a a a
q0
a
q1 
q2
Costas Busch - LSU
a
q3
29
a a a
q0
a
q1 
q2
Costas Busch - LSU
a
q3
30
(read head doesn’t move)
a a a
q0
a
q1 
q2
Costas Busch - LSU
a
q3
31
Input cannot be consumed
a a a
Automaton halts
“reject”
q0
String
a
aaa
q1 
q2
a
q3
is rejected
Costas Busch - LSU
32
Language accepted:
q0
a
q1 
L  {aa}
q2
Costas Busch - LSU
a
q3
33
Another NFA Example
q0
a
b
q1
q2

q3

Costas Busch - LSU
34
a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
35
a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
36
a b
“accept”
q0
a
b
q1
q2

q3

Costas Busch - LSU
37
Another String
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
38
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
39
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
40
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
41
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
42
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
43
a b a b
“accept”
q0
a
b
q1
q2

q3

Costas Busch - LSU
44
Language accepted
L  ab, abab, ababab, ...

 ab
q0
a
b
q1
q2

q3

Costas Busch - LSU
45
Another NFA Example
0
q0
1
q1
0, 1 q2

Costas Busch - LSU
46
Language accepted
L( M )   , 10, 1010, 101010, ...
 10*
0
q0
1
q1
0, 1 q2

Costas Busch - LSU
(redundant
state)
47
Remarks:
•The  symbol never appears on the
input tape
•Simple automata:
M1
q0
M2
L(M1 ) = {}
L(M 2 ) = {λ}
q0
Costas Busch - LSU
48
•NFAs are interesting because we can
express languages easier than DFAs
NFA
q0
a
M1
DFA
a
M2
q2
q1
a
q0
L( M1 ) = {a}
a
q1
L( M 2 ) = {a}
Costas Busch - LSU
49
Formal Definition of NFAs
M  Q, ,  , q0 , F 
Q:
Set of states, i.e.
q0 , q1, q2 
:
Input aplhabet, i.e.
a, b
:
Transition function
 
q0 : Initial state
F:
Accepting states
Costas Busch - LSU
50
Transition Function 
 q , x   q1, q2,, qk 
q
x
x
q1
q2
x
resulting states with
following one transition
with symbol x
qk
Costas Busch - LSU
51
States reachable from q0 scanning 1
 q0 , 1  q1
0
q0
1
q1
0, 1 q
2

Costas Busch - LSU
52
States reachable from q1 scanning 0
 (q1,0)  {q0 , q2 }
0
q0
1
q1
0, 1 q
2

Costas Busch - LSU
53
States reachable from q0 scanning nothing
 (q0 ,  )  {q0 , q2 }
0
q0
1
q1
0, 1 q
2

Costas Busch - LSU
54
States reachable from q2 scanning 1
 (q2 ,1)  
0
q0
1
q1
0, 1 q
2

Costas Busch - LSU
55
Extended Transition Function 
Same with

*
but applied on strings
 q0 , a   q1 
*
q5
q4
a
q0
a
a
b
q1
q2

q3

Costas Busch - LSU
56
States reachable from q0 scanning aa
 q0 , aa   q4 , q5 
*
q5
q4
a
q0
a
a
b
q1
q2

q3

Costas Busch - LSU
57
States reachable from q0 scanning ab
 q0 , ab   q2 , q3, q0 
*
q5
q4
a
q0
a
a
b
q1
q2

q3

Costas Busch - LSU
58
Special case:
for any state q
q   q ,  
*
Costas Busch - LSU
59
In general
q j   qi ,w  : there is a walk from qi to q j
*
with label
w
w
qi
qj
w  1 2  k
qi
1
k
2
Costas Busch - LSU
qj
60
The Language of an NFA M
The language accepted by
M
is:
LM   w1, w2 ,..., wn 
Where for each
wm
 (q0 ,w m )  {qi ,..., qk ,, q j }
*
and there is some
qk  F (accepting state)
Costas Busch - LSU
61
wm  LM 
 (q0 ,w m )
*
qi
wm
qk
q0 w
m
wm
Costas Busch - LSU
qk  F
qj
62
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 q0 , aa   q4 , q5 
aa  L(M )
*
F
Costas Busch - LSU
63
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 q0 , ab  q2 , q3 , q0 
ab LM 
*
F
Costas Busch - LSU
64
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 q0 , abaa   q4 , q5 
abaa  L(M )
*
F
Costas Busch - LSU
65
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 * q0 , aba   q1
F
Costas Busch - LSU
aba  LM 
66
q5
q4
a
q0
a
a
b
q1
q2

q3

LM   ab  ab {aa}
*
Costas Busch - LSU
*
67
NFAs accept the Regular
Languages
Costas Busch - LSU
68
Equivalence of Machines
Definition:
Machine
if
M1 is equivalent to machine M 2
L M1   L  M 2 
Costas Busch - LSU
69
Example of equivalent machines
NFA
LM1   {10} *
0
q0
q1
1
DFA
LM 2   {10} *
M1
M2
0,1
0
q0
1
q1
1
q2
0
Costas Busch - LSU
70
Theorem:
Languages
accepted
by NFAs

Regular
Languages
Languages
accepted
by DFAs
NFAs and DFAs have the same computation power,
namely, they accept the same set of languages
Costas Busch - LSU
71
Proof:
we need to show
Languages
accepted
by NFAs

Regular
Languages
AND
Languages
accepted
by NFAs

Costas Busch - LSU
Regular
Languages
72
Proof-Step 1
Languages
accepted
by NFAs

Regular
Languages
Every DFA is trivially a NFA
Any language L accepted by a DFA
is also accepted by a NFA
Costas Busch - LSU
73
Proof-Step 2
Languages
accepted
by NFAs

Regular
Languages
Any NFA can be converted to an
equivalent DFA
Any language L accepted by a NFA
is also accepted by a DFA
Costas Busch - LSU
74
Conversion of NFA to DFA
a
NFA M
 q2
q0 a
q1
b
DFA
M
q0 
Costas Busch - LSU
75
NFA
 * (q0 , a )  {q1 , q2 }
a
M
q0
a

q1
q2
b
DFA
M
q0 
a
q1,q2 
Costas Busch - LSU
76
NFA
empty set
 * (q0 , b )  
a
M
q0
a

q1
q2
b
DFA
M
q0 
a
q1,q2 

trap state
b
Costas Busch - LSU
77
 (q1 , a )  {q1 , q2 }
 * (q2 , a )  
*
NFA
a
M
q0
a

q1
union
q2
q1,q2 
b
DFA
a
M
q0 
a
q1,q2 
b

Costas Busch - LSU
78
 (q1 , b )  {q0 }
 * (q2 , b )  {q0 }
*
NFA
a
M
q0
a

q1
union
q2
q0 
b
DFA
M
a
b
q0 
a
q1,q2 
b

Costas Busch - LSU
79
NFA
a
M
q0
a

q1
q2
b
DFA
M
a
b
q0 
a
q1,q2 
b

Costas Busch - LSU
a, b trap state
80
END OF CONSTRUCTION
NFA
a
M
q0
a

q1
q1  F
q2
b
DFA
M
a
b
q0 
a
q1,q2 
b

Costas Busch - LSU
q1, q2  F 
a, b
81
General Conversion Procedure
Input: an NFA
M
Output: an equivalent DFA M 
with LM   L(M )
Costas Busch - LSU
82
The NFA has states
q0 , q1, q2 ,...
The DFA has states from the power set
, q0 , q1 , q0 , q1 , q1, q2, q3, ....
Costas Busch - LSU
83
Conversion Procedure Steps
step
1.
Initial state of NFA:
q0
 q0 ,    q0 , 
*
Initial state of DFA: q0 ,
Costas Busch - LSU
84
Example
NFA
 q0 ,    q0 
*
a
M
q0
a

q1
q2
b
DFA
M
q0 
Costas Busch - LSU
85
step
2. For every DFA’s state {qi , q j ,..., qm }
compute in the NFA
 * qi , a 

  * qj ,a
...

Union
 {qk , ql,..., qn }
  * qm , a 
add transition to DFA
 {qi , qj ,..., qm }, a   {qk , ql,..., qn }
Costas Busch - LSU
86
 * (q0 , a)  {q1, q2}
Example
NFA
a
M
a
q0

q1
q2
b
DFA
M
q0 
a
q1,q2 
 q0 , a   q1, q2 
Costas Busch - LSU
87
step
3. Repeat Step 2 for every state in DFA and
symbols in alphabet until no more states
can be added in the DFA
Costas Busch - LSU
88
Example
NFA
a
M
q0
a

q1
q2
b
DFA
M
a
b
q0 
a
q1,q2 
b

Costas Busch - LSU
a, b
89
step
4. For any DFA state {qi , q j ,..., qm }
if some
q j is accepting state in NFA
Then, {qi , q j ,..., qm }
is accepting state in DFA
Costas Busch - LSU
90
Example
NFA
a
M
q0
a

q1
q1  F
q2
b
DFA
M
a
b
q0 
a
q1,q2 
b

Costas Busch - LSU
q1, q2  F 
a, b
91
Lemma:
If we convert NFA M to DFA M 
then the two automata are equivalent:
LM   LM 
Proof:
We need to show:
LM   LM 
AND
LM   LM 
Costas Busch - LSU
92
First we show:
LM   LM 
We only need to prove:
w L(M )
w  L(M )
Costas Busch - LSU
93
NFA
Consider
w L(M )
w
q0
qf
symbols
w  1 2  k
q0
1
2
Costas Busch - LSU
k
qf
94
symbol
qi
i
qj
denotes a possible sub-path like
qi


symbol
i
Costas Busch - LSU

qj
95
We will show that if
w L(M )
w  1 2  k
NFA
M:
q0
1
k
2
qf
then
DFA
1
M:
{q0 ,}
state
label
2
w  L(M )
Costas Busch - LSU
k
{q f ,}
state
label
96
More generally, we will show that if in
(arbitrary string)
NFA
M:
q0
M:
v  a1a2  an
a1
qi
a2
qj
ql
an
qm
then
DFA
M:
a1
{q0 ,}
a2
{qi ,} {q j ,}
Costas Busch - LSU
an
{ql ,} {qm ,}
97
Proof by induction on
v  a1
Induction Basis: |v | 1
NFA
M:
DFA
M:
q0
|v|
a1
qi
a1
{q0 ,}
{qi ,}
is true by construction of M 
Costas Busch - LSU
98
Induction hypothesis:
1 | v | k
v  a1a2  ak
Suppose that the following hold
NFA
M:
DFA
M:
a1
q0
a1
{q0 ,}
qi
a2
qj
a2
{qi ,} {q j ,}
Costas Busch - LSU
qc
ak
qd
ak
{qc ,} {qd ,}
99
| v | k  1
v  a1a2  ak ak 1  vak 1


Induction Step:
v
Then this is true by construction of M 
NFA
M:
q0
a1
qi
a2
qj
qc
ak
qd
ak 1
qe
v
DFA
M:
a1
{q0 ,}
ak
a2
{qi ,} {q j ,}
v
Costas Busch - LSU
ak 1
{qc ,} {qd ,}
{qe ,}
100
Therefore if
w L(M )
w  1 2  k
NFA
M:
q0
1
k
2
qf
then
DFA
M:
1
{q0 ,}
2
w  L(M )
Costas Busch - LSU
k
{q f ,}
101
We have shown:
With a similar proof
we can show:
Therefore:
LM   LM 
LM   LM 
L M   L M 
END OF PROOF
Costas Busch - LSU
102