Lecture#6 Converting NFAs To DFAs
Lecture#6 Converting NFAs To DFAs
Lecture#06
Converting NFAs to DFAs
blog.bazaarvoice.com
Umar Faiz
Pakistan Institute of Engineering and Applied Sciences
DFAS TO NFAS
• NFAs can be constructed from DFAs using transitions:
• Called NFA-
• Suppose M1 accepts L1, M2 accepts L2
– Then an NFA can be constructed that accepts:
L1 U L2 (union)
L1L2 (concatenation)
L1* (Kleene star)
M1
M
M2
(M)*
(M1) U (M2)
M1 M2
(M1) (M2)
• Finite acceptors are equivalent iff they both accept the same language
L(M1) = L(M2)
LM1 LM 2
0,1
0
LM 2 {10} * q0 q1 1 q2
1
0 DFA M 2
Slides based on Costas Busch - RPI
NFAS TO DFAS
• To convert NFAs to DFAs we need to get rid of non-determinism from
NFAs.
• Using subset construction method to convert NFA to DFA involves the
following steps:
– For every state in the NFA, determine all reachable states for every input symbol.
– The set of reachable states constitute a single state in the converted DFA (Each
state in the DFA corresponds to a subset of states in the NFA).
– Find reachable states for each new DFA state, until no more new states can be
found.
• We will prove:
Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs
Languages
Regular
accepted
Languages
by NFAs
Languages
Regular
accepted
Languages
by NFAs
Proof: Any NFA can be converted to an
equivalent DFA
DFA M
q0
NFA M a
q0 a q1 q2
b
DFA M
q0 a
q1, q2
Slides based on Costas Busch - RPI
CONVERT NFA TO DFA
NFA M a
q0 a q1 q2
b
a
DFA M
q0 a
q1, q2
b
Slides based on Costas Busch - RPI
CONVERT NFA TO DFA
NFA M a
q0 a q1 q2
b
b a
DFA M
q0 a
q1, q2
b
Slides based on Costas Busch - RPI
CONVERT NFA TO DFA
NFA M a
q0 a q1 q2
b
b a
DFA M
q0 a
q1, q2
b
a, b
Slides based on Costas Busch - RPI
CONVERT NFA TO DFA
a
NFA M LM L(M )
q0 a q1 q2
b
a
DFA M b
q0 a
q1, q2
b
a, b
LM L(M )
q0 , q1, q2 ,...
DFA M
q0
q0 , a q1, q2
Slides based on Costas Busch - RPI
PROCEDURE NFA TO DFA
a, b
q0 a
q1, q2
b q1, q2 F
a, b
3
a
a a b
a b
2
1 a, b 5
a, b 4 a
1 a, b 5 2 {3} {5}
3 ∅ {2}
a, b 4 a 4 {5} {4}
* 5 ∅ ∅
b
Slides based on Costas Busch - RPI
NFA TO DFA
Example1: Subset Construction Method
Step2: The set of states resulting from every transition function constitutes a new state.
Calculate all reachable states for every such state for every input symbol.
q δ(q,a) δ(q,b)
q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5}
1 {1,2,3,4,5} {4,5}
2 {3} {5}
3 ∅ {2}
4 {5} {4}
5 ∅ ∅
4 {5} {4}
5 ∅ ∅
b
Slides based on Costas Busch - RPI
Single Final State for NFAs
a b
NFA
b
a Equivalent NFA
a b
b
Slides based on Costas Busch - RPI
IN GENERAL
NFA
Equivalent NFA
Single
final state
Slides based on Costas Busch - RPI
EXTREME CASE
a b
{q0, q1, q2, q3} {q0, q1, q2, q3, q4} {q2, q3, q4}
* {q0, q1, q2, q3, q4} {q0, q1, q2, q3, q4} {q2, q3, q4}
* {q2, q3, q4} {q3, q4} {q3, q4}
* {q3, q4} {q3, q4}
EXAMPLE: -NFA TO DFA CONVERSION
Compute the -Closure of each state. Convert the automaton into DFA.
Fully-specified DFA