Languages and Finite Automata
Download
Report
Transcript Languages and Finite Automata
Non-Deterministic
Finite Automata
Prof. Busch - LSU
1
Nondeterministic Finite Automaton (NFA)
Alphabet = {a}
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
2
Alphabet = {a}
Two choices
a
q0
q1
a
q2 No transition
a
q3 No transition
Prof. Busch - LSU
3
First Choice
a a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
4
First Choice
a a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
5
First Choice
a a
All input is consumed
a
q0
q1
a
q2
“accept”
a
q3
Prof. Busch - LSU
6
Second Choice
a a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
7
Second Choice
a a
Input cannot be consumed
a
q0
a
q1
a
q2
Automaton Halts
q3 “reject”
Prof. 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
Prof. 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
Prof. Busch - LSU
10
Rejection example
a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
11
First Choice
a
“reject”
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
12
Second Choice
a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
13
Second Choice
a
a
q0
q1
a
q2
a
q3 “reject”
Prof. Busch - LSU
14
Another Rejection example
a a a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
15
First Choice
a a a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
16
First Choice
a a a
Input cannot be consumed
a
q0
q1
a
q3
a
q2
“reject”
Automaton halts
Prof. Busch - LSU
17
Second Choice
a a a
a
q0
q1
a
q2
a
q3
Prof. Busch - LSU
18
Second Choice
a a a
Input cannot be consumed
a
q0
q1
a
q2
Automaton halts
a
q3
“reject”
Prof. 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
Prof. 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
Prof. 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
Prof. Busch - LSU
22
Language accepted:
a
q0
q1
L {aa}
a
q2
a
q3
Prof. Busch - LSU
23
Lambda Transitions
q0
a
q1
q2
Prof. Busch - LSU
a
q3
24
a a
q0
a
q1
q2
Prof. Busch - LSU
a
q3
25
a a
q0
a
q1
q2
Prof. Busch - LSU
a
q3
26
input tape head does not move
a a
q0
a
q1
q2
a
q3
Automaton changes state
Prof. Busch - LSU
27
all input is consumed
a a
“accept”
q0
String
a
aa
q1
q2
a
q3
is accepted
Prof. Busch - LSU
28
Rejection Example
a a a
q0
a
q1
q2
Prof. Busch - LSU
a
q3
29
a a a
q0
a
q1
q2
Prof. Busch - LSU
a
q3
30
(read head doesn’t move)
a a a
q0
a
q1
q2
Prof. 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
Prof. Busch - LSU
32
Language accepted:
q0
a
q1
L {aa}
q2
Prof. Busch - LSU
a
q3
33
Another NFA Example
q0
a
b
q1
q2
q3
Prof. Busch - LSU
34
a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
35
a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
36
a b
“accept”
q0
a
b
q1
q2
q3
Prof. Busch - LSU
37
Another String
a b a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
38
a b a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
39
a b a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
40
a b a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
41
a b a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
42
a b a b
q0
a
b
q1
q2
q3
Prof. Busch - LSU
43
a b a b
“accept”
q0
a
b
q1
q2
q3
Prof. Busch - LSU
44
Language accepted
L ab, abab, ababab, ...
ab
q0
a
b
q1
q2
q3
Prof. Busch - LSU
45
Another NFA Example
0
q0
1
q1
0, 1 q2
Prof. Busch - LSU
46
Language accepted
L(M ) = {λ, 10, 1010, 101010, ...}
= {10} *
0
q0
1
q1
0, 1 q2
Prof. 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
Prof. Busch - LSU
48
•NFAs are interesting because we can
express languages easier than DFAs
NFA
q0
a
DFA
M1
a
M2
q2
q1
a
q0
L( M1 ) = {a}
a
q1
L( M 2 ) = {a}
Prof. 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
Prof. Busch - LSU
50
Transition Function
q , x q1, q2,, qk
q
x
x
q1
q1
x
resulting states with
following one transition
with symbol x
qk
Prof. Busch - LSU
51
q0 , 1 q1
0
q0
1
q1
0, 1 q
2
Prof. Busch - LSU
52
(q1,0) {q0 , q2 }
0
q0
1
q1
0, 1 q
2
Prof. Busch - LSU
53
(q0 , ) {q2 }
0
q0
1
q1
0, 1 q
2
Prof. Busch - LSU
54
(q2 ,1)
0
q0
1
q1
0, 1 q
2
Prof. 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
Prof. Busch - LSU
56
q0 , aa q4 , q5
*
q5
q4
a
q0
a
a
b
q1
q2
q3
Prof. Busch - LSU
57
q0 , ab q2 , q3, q0
*
q5
q4
a
q0
a
a
b
q1
q2
q3
Prof. Busch - LSU
58
Special case:
for any state q
q q ,
*
Prof. 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
Prof. Busch - LSU
qj
60
The Language of an NFA M
The language accepted by
M
is:
LM w1,w2,...wn
where
(q0 ,w m ) {qi ,..., qk ,, q j }
*
and there is some
qk F (accepting state)
Prof. Busch - LSU
61
wm LM
(q0 ,w m )
*
qi
wm
qk
q0 w
m
wm
Prof. Busch - LSU
qk F
qj
62
F q0 ,q5
q5
q4
a
a
q0
a
b
q1
q2
q3
q0 , aa q4 , q5
*
aa L(M )
F
Prof. Busch - LSU
63
F q0 ,q5
q5
q4
a
a
q0
a
b
q1
q2
q3
q0 , ab q2, q3, q0
*
ab LM
F
Prof. Busch - LSU
64
F q0 ,q5
q5
q4
a
q0
a
a
b
q1
q2
q3
q0 , abaa q4 , q5
*
aaba L(M )
F
Prof. Busch - LSU
65
F q0 ,q5
q5
q4
a
q0
a
a
b
q1
q2
q3
* q0 , aba q1
F
Prof. Busch - LSU
aba LM
66
q5
q4
a
q0
a
a
b
q1
q2
q3
LM ab * ab * {aa }
Prof. Busch - LSU
67
NFAs accept the Regular
Languages
Prof. Busch - LSU
68
Equivalence of Machines
Definition:
Machine
if
M1 is equivalent to machine M 2
L M1 L M 2
Prof. Busch - LSU
69
Example of equivalent machines
NFA
LM1 {10} *
0
q0
q1
1
DFA
LM 2 {10} *
M1
M2
0,1
0
q0
1
q1
1
q2
0
Prof. Busch - LSU
70
Theorem:
Languages
accepted
by NFAs
Regular
Languages
Languages
accepted
by DFAs
NFAs and DFAs have the same computation power,
accept the same set of languages
Prof. Busch - LSU
71
Proof:
we only need to show
Languages
accepted
by NFAs
Regular
Languages
AND
Languages
accepted
by NFAs
Prof. Busch - LSU
Regular
Languages
72
Proof-Step 1
Languages
accepted
by NFAs
Regular
Languages
Every DFA is trivially an NFA
Any language L accepted by a DFA
is also accepted by an NFA
Prof. 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 an NFA
is also accepted by a DFA
Prof. Busch - LSU
74
Conversion NFA to DFA
a
NFA M
a
q
q
q
0
1
2
b
DFA
M
q0
Prof. Busch - LSU
75
NFA
* (q0 , a ) {q1 , q2 }
a
M
q0
a
q1
q2
b
DFA
M
q0
a
q1,q2
Prof. Busch - LSU
76
NFA
empty set
* (q0 , b )
a
M
q0
a
q1
q2
b
DFA
M
q0
a
q1,q2
trap state
b
Prof. 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
Prof. 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
Prof. Busch - LSU
79
NFA
a
M
q0
a
q1
q2
b
DFA
M
a
b
q0
a
q1,q2
b
Prof. 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
Prof. Busch - LSU
q1, q2 F
a, b
81
General Conversion Procedure
Input: an NFA
M
Output: an equivalent DFA M
with LM L(M )
Prof. 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, ....
Prof. Busch - LSU
83
Conversion Procedure Steps
step
1.
Initial state of NFA:
q0
q0 , q0 ,
*
Initial state of DFA: q0 ,
Prof. Busch - LSU
84
Example
NFA
q0 , q0
*
a
M
q0
a
q1
q2
b
DFA
M
q0
Prof. 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 }
Prof. 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
Prof. 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
Prof. Busch - LSU
88
Example
NFA
a
M
q0
a
q1
q2
b
DFA
M
a
b
q0
a
q1,q2
b
Prof. 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
Prof. Busch - LSU
90
Example
NFA
a
M
q0
a
q1
q1 F
q2
b
DFA
M
a
b
q0
a
q1,q2
b
Prof. Busch - LSU
q1, q2 F
a, b
91
Lemma:
If we convert NFA M to DFA M
then the two automata are equivalent:
LM LM
Proof:
We only need to show:
LM LM
AND
LM LM
Prof. Busch - LSU
92
First we show:
LM LM
We only need to prove:
w L(M )
w L(M )
Prof. Busch - LSU
93
NFA
Consider
w L(M )
w
q0
qf
symbols
w 1 2 k
q0
1
2
Prof. Busch - LSU
k
qf
94
symbol
qi
i
qj
denotes a possible sub-path like
qi
symbol
i
Prof. 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 )
Prof. 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 ,}
Prof. 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
Prof. 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 ,}
Prof. Busch - LSU
qc
ak
qd
ak
{qc ,} {qd ,}
99
| v | k 1
v a1a2 ak ak 1 vak 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
Prof. 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 )
Prof. Busch - LSU
k
{q f ,}
101
We have shown:
With a similar proof
we can show:
Therefore:
LM LM
LM LM
L M L M
END OF LEMMA PROOF
Prof. Busch - LSU
102