Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert...
Transcript of Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert...
![Page 1: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/1.jpg)
CSE 311: Foundations of Computing
Lecture 24: NFAs, Regular expressions, and NFA→DFA
![Page 2: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/2.jpg)
Last time: Nondeterministic Finite Automata (NFA)
• Graph with start state, final states, edges labeled
by symbols (like DFA) but
– Not required to have exactly 1 edge out of each state
labeled by each symbol--- can have 0 or >1
– Also can have edges labeled by empty string ε
• Defn: x is in the language recognized by an NFA if
and only if x labels a path from the start state to
some final state
s0 s2 s3s1
111
0,10,1
![Page 3: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/3.jpg)
Last time: Three ways of thinking about NFAs
• Outside observer: Is there a path labeled by x from
the start state to some final state?
• Perfect guesser: The NFA has input x and whenever
there is a choice of what to do it magically guesses a
good one (if one exists)
• Parallel exploration: The NFA computation runs all
possible computations on x step-by-step at the same
time in parallel
![Page 4: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/4.jpg)
001 011
111
110
101010000
100
1
11 0
1
1
1
1
000
1
0
0
00
Last time: Compare with the smallest DFA
0,1
s3 s2 s1 s0
0,1 0,11
![Page 5: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/5.jpg)
0,1
s3 s2 s1 s0
0,1 0,11
Last time: Parallel Exploration view of an NFA
Input string 0101100
s3
0 1 0 1 1 0 0
s3
s1
s3
s2
s3
s0
s1
s3
s0
s2
s3 s3
s0
X
s3
s1
s2
X
![Page 6: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/6.jpg)
Theorem: For any set of strings (language) �
described by a regular expression, there is an
NFA that recognizes �.
Proof idea: Structural induction based on the
recursive definition of regular expressions...
NFAs and regular expressions
![Page 7: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/7.jpg)
Regular Expressions over Σ
• Basis:
– ∅, ɛ are regular expressions
– a is a regular expression for any a ∈ Σ
• Recursive step:
– If A and B are regular expressions then so are:
(A ∪ B)
(AB)
A*
![Page 8: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/8.jpg)
Base Case
• Case ∅:
• Case ɛ:
• Case a:
![Page 9: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/9.jpg)
Base Case
• Case ∅:
• Case ɛ:
• Case a:a
![Page 10: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/10.jpg)
Inductive Hypothesis
• Suppose that for some regular expressions
A and B there exist NFAs NA and NB such
that NA recognizes the language given by A
and NB recognizes the language given by B
NA NB
![Page 11: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/11.jpg)
Inductive Step
Case (A ∪ B):
NA
NB
![Page 12: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/12.jpg)
Inductive Step
Case (A ∪ B):
ɛ
ɛ
NA
NB
![Page 13: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/13.jpg)
Inductive Step
Case (AB):
NA NB
![Page 14: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/14.jpg)
Inductive Step
Case (AB):
ɛ
ɛ
NA NB
![Page 15: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/15.jpg)
Inductive Step
Case A*
NA
![Page 16: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/16.jpg)
Inductive Step
Case A*
ɛ
ɛ
ɛ
NA
![Page 17: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/17.jpg)
Build an NFA for (01 ∪1)*0
![Page 18: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/18.jpg)
Solution
(01 ∪1)*0
0ɛ
ɛ
ɛ
ɛ
0
1
1
ɛ
ɛ
ɛ
ɛ
ɛ
![Page 19: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/19.jpg)
NFAs and DFAs
Every DFA is an NFA
– DFAs have requirements that NFAs don’t have
Can NFAs recognize more languages?
![Page 20: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/20.jpg)
NFAs and DFAs
Every DFA is an NFA
– DFAs have requirements that NFAs don’t have
Can NFAs recognize more languages? No!
Theorem: For every NFA there is a DFA that
recognizes exactly the same language
![Page 21: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/21.jpg)
Three ways of thinking about NFAs
• Outside observer: Is there a path labeled by x from
the start state to some final state?
• Perfect guesser: The NFA has input x and whenever
there is a choice of what to do it magically guesses a
good one (if one exists)
• Parallel exploration: The NFA computation runs all
possible computations on x step-by-step at the same
time in parallel
![Page 22: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/22.jpg)
Conversion of NFAs to a DFAs
• Proof Idea:
– The DFA keeps track of ALL the states that the
part of the input string read so far can reach in
the NFA
– There will be one state in the DFA for each
subset of states of the NFA that can be reached
by some string
![Page 23: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/23.jpg)
0,1
s3 s2 s1 s0
0,1 0,11
Parallel Exploration view of an NFA
Input string 0101100
s3 s3 s3 s3 s3 s3 s3
0 1 0 1 1 0 0
s2 s1 s0
s2 s1 s0
s2 s1 s0
s3
X
X
![Page 24: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/24.jpg)
Conversion of NFAs to a DFAs
New start state for DFA
– The set of all states reachable from the start
state of the NFA using only edges labeled ɛ
a,b,e,f
f
e
baɛ
ɛ
ɛ
NFA DFA
![Page 25: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/25.jpg)
Conversion of NFAs to a DFAs
For each state of the DFA corresponding to a set S of
states of the NFA and each symbol s– Add an edge labeled s to state corresponding to T, the
set of states of the NFA reached by
∙ starting from some state in S, then
∙ following one edge labeled by s, and
then following some number of edges labeled by ɛ
– T will be ∅ if no edges from S labeled s exist
f
e
b
ɛ
ɛc
d
g
ɛ
1
1
1
1
b,e,f c,d,e,g1
![Page 26: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/26.jpg)
Conversion of NFAs to a DFAs
Final states for the DFA
– All states whose set contain some final state of
the NFA
a,b,c,e
ce
ba
NFADFA
![Page 27: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/27.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
DFA
![Page 28: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/28.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
a,b
DFA
![Page 29: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/29.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
a,b
DFA
0
c
1
![Page 30: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/30.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
a,b
DFA
0
c
1
b
b,c
1
0
![Page 31: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/31.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
a,b
DFA
0
c
1
b
b,c
1
0
∅
10
![Page 32: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/32.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
a,b
DFA
0
c
1
b
b,c
1
0
∅
1
0,1
0
![Page 33: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/33.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
a,b
DFA
0
c
1
b
b,c
1
0
a,b,c
∅
1
0,1
0
0
1
![Page 34: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/34.jpg)
Example: NFA to DFA
c
a
b
0
ɛ
0,1
1
0
NFA
a,b
DFA
0
c
1
b
b,c
1
0
a,b,c
∅
1
0,1
0
0
1
10
![Page 35: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/35.jpg)
Exponential Blow-up in Simulating Nondeterminism
• In general the DFA might need a state for every subset of states of the NFA
– Power set of the set of states of the NFA
– �-state NFA yields DFA with at most �� states
– We saw an example where roughly �� is necessary
“Is the �th char from the end a 1?”
• The famous “P=NP?” question asks whether a similar blow-up is always necessary to get rid of nondeterminism for polynomial-time algorithms
![Page 36: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/36.jpg)
DFAs ≡ NFAs ≡ Regular expressions
We have shown how to build an optimal DFA for every regular expression
– Build NFA
– Convert NFA to DFA using subset construction
– Minimize resulting DFA
Theorem: A language is recognized by a DFA (or NFA) if and only if it has a regular expression
You need to know this fact but we won’t ask you anything about the “only if” direction from DFA/NFA to regular expression. For fun, we sketch the idea.
![Page 37: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/37.jpg)
Generalized NFAs
• Like NFAs but allow
– Parallel edges
– Regular Expressions as edge labels
NFAs already have edges labeled ɛ or a
• An edge labeled by A can be followed by reading a
string of input chars that is in the language
represented by A
• Defn: A string x is accepted iff there is a path from
start to final state labeled by a regular expression
whose language contains x
![Page 38: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/38.jpg)
Starting from an NFA
Add new start state and final state
ɛ
ɛ
ɛ
A
Then eliminate original states one by one,
keeping the same language, until it looks
like:
Final regular expression will be A
![Page 39: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/39.jpg)
Only two simplification rules
• Rule 1: For any two states q1 and q2 with parallel
edges (possibly q1=q2), replace
• Rule 2: Eliminate non-start/final state q3 by
replacing all
for every pair of states q1, q2 (even if q1=q2)
q1q2
A
B
byA⋃B
q1q2
AB
C AB*Cq1 q3 q2 q1q2by
![Page 40: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/40.jpg)
Converting an NFA to a regular expression
Consider the DFA for the mod 3 sum
– Accept strings from {0,1,2}* where the digits
mod 3 sum of the digits is 0
t0 t2
t1
0
0
0
1 1
1
2
22
![Page 41: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/41.jpg)
Splicing out a state t1
Regular expressions to add to edges
t0 t2
t1
0
0
1 1
1
2
22
t0→t1→t0 : 10*2
t0→t1→t2 : 10*1
t2→t1→t0 : 20*2
t2→t1→t2 : 20*1
0
sɛ
f
ɛ
![Page 42: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/42.jpg)
Splicing out a state t1
Regular expressions to add to edges
t0 t2
0 ∪ 20*12 ∪ 10*1
t0→t1→t0 : 10*2
t0→t1→t2 : 10*1
t2→t1→t0 : 20*2
t2→t1→t2 : 20*1
0 ∪ 10*2
sɛ
f
ɛ
1 ∪ 20*2
![Page 43: Lecture 24: NFAs, Regular expressions, and NFA DFA · regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language](https://reader030.fdocument.org/reader030/viewer/2022040122/5f09b6197e708231d4282691/html5/thumbnails/43.jpg)
Splicing out state t2 (and then t0)
t0 t2
R1
R1: 0 ∪ 10*2
R2: 2 ∪ 10*1
R3: 1 ∪ 20*2
R4: 0 ∪ 20*1
R5: R1 ∪ R2R4*R3
R4R2
R3
Final regular expression: R5*=
(0 ∪ 10*2 ∪ (2 ∪ 10*1)(0 ∪ 20*1)*(1 ∪ 20*2))*
f
ɛ
sɛ
t0
R5f
ɛ
sɛ