CS 301 - Lecture 3 NFA DFA Equivalence Regular Expressions

advertisement
Review
•  Languages and Grammars
CS 301 - Lecture 3
NFA DFA Equivalence
Regular Expressions
Fall 2008
Equivalence of Machines
–  Alphabets, strings, languages
•  Regular Languages
–  Deterministic Finite Automata
–  Nondeterministic Finite Automata
•  Today:
–  Equivalence of NFA and DFA
–  Regular Expressions
–  Equivalence to Regular Languages
Example of equivalent
machines NFA M1
0
L(M1 ) = {10} *
Machine
M1 is equivalent to machine M 2
ifL(M1 ) =
L(M 2 )
L(M 2 ) = {10} *
q0
0
q0
1
1
DFA
q1
q1
M2
0,1
1
q2
0
1
Convert NFA to DFA
Convert NFA to DFA
a
M
a
q0
NFA
λ
q1
q2
b
DFA
M′
M
DFA start state =
Union of NFA Start States
Convert NFA
a to DFA
q0
a
λ
q1
DFA
a
{q0 }
b
∅
a
q0
DFA
λ
q1
q2
b
M′
a
{q1, q2 }
From the NFA start states, where could we get on a?
List the set of all possible states you can reach using
just an a.
NFA
q2
Convert NFA to DFA
a
M
q0
b
M′
a
M
{q0 }
Begin with a set
of start states
{q0 }
NFA
NFA
a
λ
q1
q2
b
{q1, q2 }
How did we get from q0 to q2
using only a? (think λ )
From the NFA start states, where could we get on b?
The set of all possible states you can reach may be empty!
DFA
a
M′
a
{q0 }
b
∅
{q1, q2 }
From new set, where could we
get on a?
2
NFA
Convert NFA to DFA
a
M
q0
a
NFA
λ
q1
a
{q0 }
∅
a
q0
From new set, where could we
get on b?
λ
q1
a
{q0 }
b
∅
∅
{q1, q2 }
a, b
From empty set,
we can only get
back to empyt set.
Could This Produce Infinite States?
q0 , q1, q2 ,...
a
b
M′
a
If the NFA has states
q2
b
DFA
q2
a
b
M′
b
a
M
DFA
{q0 }
Convert NFA to DFA
NFA
λ
q1
b
{q1, q2 }
b
a
q0
a
b
M′
a
M
q2
b
DFA
Convert NFA to DFA
There are a finite number of NFA states by definition
the DFA has states in the powerset
{q1, q2 }
a, b
∅, {q0 }{
, q1}{
, q1, q2 }{
, q3 , q4 , q7 },....
If a node has any
NFA Final state,
mark the node as
Final in the DFA.
Powerset of finite set can be big, but it is not infinite!
3
Procedure NFA to DFA
1.
Initial state of NFA:
Initial state of DFA:
q0
{q0 }
Procedure NFA to DFA
2. For every DFA’s state
{qi , q j ,..., qm }
Compute in the NFA
δ * (qi , a ),
δ * (q j , a ),
= {qi′ , q′j ,..., qm
′}
...
Add transition to DFA
′}
δ ({qi , q j ,..., qm }, a )= {qi′ , q′j ,..., qm
Procedure NFA to DFA
Repeat Step 2 for all letters in alphabet,
until no more transitions can be added.
Procedure NFA to DFA
3. For any DFA state {qi , q j ,..., qm }
If some
q j is a final state in the NFA
Then, {qi , q j ,..., qm }
is a final state in the DFA
4
Proof
Theorem
Take NFA
Apply procedure to obtain DFA
Then
M
L(M ) = L(M ′)
M
M′
and
M′
are equivalent :
L(M ) ⊆ L(M ′)
AND
L(M ) ⊇ L(M ′)
L(M ) = L(M ′)
First we show:
w∈ L(M )
L(M ) ⊆ L(M ′)
Take arbitrary:
w∈ L(M )
M:
We will prove:
w
q0
qf
w ∈ L(M ′)
w = σ 1σ 2 σ k
M:
q0
σ1
σ2
σk
qf
5
More generally, we will show that if in
w∈ L(M )
We will show that if
(arbitrary string)
w = σ 1σ 2 σ k
M:
M′:
q0
σ1
σ2
σk
σ1
σ2
σk
{q0 }
w ∈ L(M ′)
Proof by induction on
Induction Basis:
M:
M′:
v = a1
q0
a1
qi
a1
{q0 }
{qi ,…}
qf
{q f ,…}
M:
M′:
q0
a1
v = a1a2  an
qi
a1
a2
qj
ql
an
qm
an
a2
{qi ,…} {q j ,…}
{q0 }
M:
{ql ,…} {qm ,…}
|v|
Induction hypothesis:
1 ≤| v |≤ k
v = a1a2  ak
M:
M′:
q0
a1
a1
{q0 }
qi
a2
qj
a2
{qi ,…} {q j ,…}
qc
ak
qd
ak
{qc ,…} {qd ,…}
6
Induction Step:
Induction Step:
| v |= k + 1
v = a1a2  ak ak +1 = v′ak +1


v = a1a2  ak ak +1 = v′ak +1


v′
M:
q0
a1
qi
a2
qj
qc
ak
qd
| v |= k + 1
v′
M:
q0
a1
qi
a2
v′
M′:
a1
{q0 }
ak
{qc ,…} {qd ,…}
M′:
a1
{q0 }
a2
{qi ,…} {q j ,…}
v′
Therefore if
qc
ak
qd ak +1 qe
ak
ak +1
v′
a2
{qi ,…} {q j ,…}
qj
{qc ,…} {qd ,…}
{qe ,…}
v′
w∈ L(M )
We have shown:
L(M ) ⊆ L(M ′)
w = σ 1σ 2 σ k
M:
q0
σ1
σ2
σk
qf
We also need to show:
M′:
σ1
{q0 }
σ2
w ∈ L(M ′)
L(M ) ⊇ L(M ′)
(proof is similar)
σk
{q f ,…}
7
We will prove:
NFAs Accept Regular
Languages
Languages
accepted
by NFAs
=
NFAs and DFAs have the
same computation power
Step 1
Languages
accepted
by NFAs
Regular
Languages
Languages
accepted
by DFAs
Step 2
Regular
Languages
Proof: Every DFA is trivially an NFA
Any language L accepted by a DFA
is also accepted by an NFA
Languages
accepted
by NFAs
Regular
Languages
Proof: Any NFA can be converted to an
equivalent DFA
Any language L accepted by an NFA
is also accepted by a DFA
8
Regular Expressions
•  Regular expressions describe regular
languages
•  Example:
Recursive Definition
Primitive regular expressions:
∅, λ , α
Given regular expressions r1 and r2
r1 + r2
(a + b ⋅ c) *
r1 ⋅ r2
describes the language
r1 *
{a, bc}* = {λ , a, bc, aa, abc, bca,...}
Are regular expressions
(r1 )
Languages of Regular
Expressions
Examples
A regular expression:
• 
L(r ) :
language of regular expression
r
(a + b ⋅ c )* ⋅(c + ∅)
•  Example
Not a regular expression:
(a + b + )
L((a + b ⋅ c) *) = {λ , a, bc, aa, abc, bca,...}
9
Formal Definition
•  For primitive regular expressions:
L(∅ ) = ∅
L(λ ) = {λ }
L(a ) = {a}
Example
L((a + b )⋅ a *) = L((a + b )) L(a *)
= L(a + b ) L(a *)
= (L(a ) ∪ L(b )) (L(a ))*
= ({a}∪ {b}) ({a})*
= {a, b}{λ , a, aa, aaa,...}
Definition (continued)
•  For regular expressions r1 and r2
L(r1 + r2 ) = L(r1 ) ∪ L(r2 )
L(r1 ⋅ r2 ) = L(r1 ) L(r2 )
L(r1 *) = (L(r1 ))*
L((r1 )) = L(r1 )
Example
•  Regular expression
r = (a + b )* (a + bb )
L(r ) = {a, bb, aa, abb, ba, bbb,...}
= {a, aa, aaa,..., b, ba, baa,...}
10
What’s Next
•  Read
–  Linz Chapter 1, 2.1, 2.2, 2.3, (skip 2.4), Chapter 3
–  JFLAP Startup, Chapter 1, 2.1, (skip 2.2) 3, 4
•  Next Lecture Topics from Chapter 3.2 and 3.3
–  Regular Expressions and Regular Languages
–  Regular Grammars and Regular Languages
•  Quiz 1 in Recitation on Wednesday 9/17
–  Covers Linz 1.1, 1.2, 2.1, 2.2, 2.3, and JFLAP 1, 2.1
–  Closed book, but you may bring one sheet of 8.5 x 11 inch paper
with any notes you like.
–  Quiz will take the full hour
•  Homework
–  Homework 2 is Due Thursday
11
Download