1. Non Deterministic Finite Automata
To
Deterministic Finite Automata Construction
1
Course Name: Compiler Design
Course Code: CSE331
Level:3, Term:3
Department of Computer Science and Engineering
Daffodil International University
2. There are three main cases of non- determinism in NFAs:
1. Transition to a state without consuming any input.
2. Multiple transitions on the same input symbol.
3. No transition on an input symbol.
To convert NFAs to DFAs we need to get rid of non-
determinism from NFAs.
Non Deterministic Features of NFA
3. 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.
Subset Construction Method
4. Fig1. NFA without λ-transitions
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
Subset Construction Method
5. Fig1. NFA without λ-transitions
Step1
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
Construct a transition table showing
all reachable states for every state
for every input signal.
Subset Construction Method
6. Fig1. NFA without λ-transitions
Fig2. Transition table
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
Subset Construction Method
7. Fig1. NFA without λ-transitions
Fig2. Transition table
1
3
2
4
5
a
a
a
b
a
b
b
a
a,b
a,b
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
2 {3} {5}
3 ∅ {2}
4 {5} {4}
5 ∅ ∅
Subset Construction Method
8. Fig1. NFA without λ-transitions
Fig2. Transition table
1
3
2
4
5
a
a
a
a
a
b
b
b
a,b
a,b
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
2 {3} {5}
3 ∅ {2}
4 {5} {4}
5 ∅ ∅
Transition from state q with
input a
Transition from state q
with input b
Starts here
Subset Construction Method
9. Fig2. Transition table
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
2 {3} {5}
3 ∅ {2}
4 {5} {4}
5 ∅ ∅
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
signal.
Subset Construction Method
12. Fig2. Transition table
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
2 {3} {5}
3 ∅ {2}
4 {5} {4}
5 ∅ ∅
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
{1,2,3,4,5}
{4,5}
Starts with
Initial state
Fig3. Subset Construction table
Step3
Repeat this process(step2) until no
more new states are reachable.
21. Fig2. Transition table
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
2 {3} {5}
3 ∅ {2}
4 {5} {4}
5 ∅ ∅
q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
{4,5} 5 4
{2,4,5} {3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5} ∅ 2
∅ ∅ ∅
2 3 5
3 ∅ 2
Fig3. Subset Construction table
Stops here as there are
no more reachable states
22. q δ(q,a) δ(q,b)
1 {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
{4,5} 5 4
{2,4,5} {3,5} {4,5}
5 ∅ ∅
4 5 4
{3,5} ∅ 2
∅ ∅ ∅
2 3 5
3 ∅ 2
1
45
12345 245
35
5
4
∅
3
2
a
a
a
a
a
a
a
a
b
b
b
b
b
b
b
b
a,b
a,b
Fig3. Subset Construction table
Fig4. Resulting FA after applying
Subset Construction to fig1
23. NFA to DFA Conversion
Let X = (Qx, ∑, δx, q0, Fx) be an NFA which accepts the language L(X). We have to
design an equivalent DFA Y = (Qy, ∑, δy, q0, Fy) such that L(Y) = L(X).
The following procedure converts the NFA to its equivalent DFA −
Algorithm:
Input − An NFA
Output − An equivalent DFA
Step 1 − Create state table from the given NFA.
Step 2 − Create a blank state table under possible input alphabets for the
equivalent DFA.
Step 3 − Mark the start state of the DFA by q0 (Same as the NFA).
Step 4 − Find out the combination of States {Q0, Q1,... , Qn} for each possible input
alphabet.
Step 5 − Each time we generate a new DFA state under the input alphabet
columns, we have to apply step 4 again, otherwise go to step 6.
Step 6 − The states which contain any of the final states of the NFA are the final
states of the equivalent DFA.
23
24. Example
Let us consider the NFA
shown in the figure:
q δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
The state transition table of
the NFA is:
24
25. q δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
Using the algorithm, we find its
equivalent DFA.
The state transition table of the
DFA is:
q δ(q,0) δ(q,1)
[a] [a,b,c,d,e] [d,e]
[a,b,c,d,e] [a,b,c,d,e] [b,d,e]
[d,e] [e] ∅
[b,d,e] [c,e] [e]
[e] ∅ ∅
[c, e] ∅ [b]
[b] [c] [e]
[c] ∅ [b]
Example…
The state transition table
of the NFA is:
25
26. Example…
The state transition table of
the DFA is:
q δ(q,0) δ(q,1)
[a] [a,b,c,d,e] [d,e]
[a,b,c,d,e] [a,b,c,d,e] [b,d,e]
[d,e] [e] ∅
[b,d,e] [c,e] [e]
[e] ∅ ∅
[c, e] ∅ [b]
[b] [c] [e]
[c] ∅ [b]
The state diagram of the DFA is as follows:
26