The Subset Construction Algorithm

advertisement
CIS 324: Language Design and Implementation
Converting NFA to DFA
1. The Subset Construction Algorithm
The algorithm for constructing a DFA from a given NFA such that
it recognizes the same language is called subset construction.
The reason is that each state of the DFA machine corresponds to
a set of states of the NFA. The DFA keeps in a particular state
all possible states to which the NFA makes a transition on the
given input symbol.
In other words, after processing a sequence of input symbols the DFA
is in a state that actually corresponds to a set of states from the NFA
reachable from the starting symbol on the same inputs.
There are three operations that can be applied on NFA states:
Operation
Definition
-closure( s ) set of NFA states reachable from state s on -transition
-closure( T ) set of NFA states reachable from some s in T on -transition
move( T, a ) set of NFA states to which there is transition on input a
from some state s in the set T
The staring state of the automaton is assumed to be s0. The -closure( s )
operation computes exactly all the states reachable from a particular state
on seeing an input symbol. When such operations are defined the states to
which our automaton can make a transition from set T on input a can be
simply specified as: -closure( move( T, a ) )
Subset Construction Algorithm
Initialize: Let -closure( s0 ) be the only state in Dstates ( of the DFA )
Repeat: while there are unmarked states T in Dstates do
mark T
for each symbol a do
U = -closure( move( T, a ) )
if U is not in Dstates then
add U as unmarked state in Dstates
Dtran[ T, a ] = U
end
end
Algorithm for Computation of -closure
Initialize: Let all states from T are on the Stack,
and -closure( T ) = T
Repeat: while Stack is not empty do
pop t from the top of the Stack
for each state u with edge from t to u labeled  do
if u is not in -closure( T ) then
add u to -closure( T )
push u onto the Stack
end
end
Example: Convert the NFA for the expression: ( a | b )*abb
into a DFA using the subset construction algorithm.

2
start
0

a
3


1
6

4
b

7
a
8
b
9
b
10

5

The derivation of the states and transitions of the DFA, including the computation
of the -closure and move functions, can be demonstrated as follows:
-closure({0}) = {1,2,4,7} = A
-closure(move({1,2,4,7},a))=-closure( {3, 8} )={1,2,3,4,6,7,8}= B
-closure(move({1,2,4,7},b))=-closure( {5} )={1,2,4,5,6,7}= C
-closure(move({1,2,3,4,6,7,8},a) =-closure( {3, 8} )= B
-closure(move({1,2,3,4,6,7,8},b) =-closure( {5, 9} )={1,2,4,5,6,7,9}= D
-closure(move({1,2,4,5,6,7},a))=-closure( {3,8} )= B
-closure(move({1,2,4,5,6,7},b))=-closure( {5} )= C
-closure(move({1,2,4,5,6,7,9},a))=-closure( {3,8} )= B
-closure(move({1,2,4,5,6,7,9},b))=-closure( {5,10} )={1,2,4,5,6,7,10}= E
-closure(move({1,2,4,5,6,7,10},a))=-closure( {3,8} )= B
-closure(move({1,2,4,5,6,7,10},b))=-closure( {5} )= C
The transition table for this DFA becomes:
state
A
B
C
D
E
a
B
B
B
B
B
b
C
D
C
E
C
The DFA is:
b
C
b
start
A
b
a
a
B
a
b
a
D
a
b
E
Download