Dfa 1
Dfa 1
Dfa 1
Topics Covered:
By:
Aditya budhwar
Anuj Dubey
(2nd CSE)
Nondeterministic Finite
Automata
A nondeterministic finite automaton (NFA) is a
mathematical model that consists of
• A set of states S
• A set of input symbols Σ
• A transition function that maps state/symbol pairs
to a set of states:
S x {Σ + ε} set of S
• A special state s0 called the start state
• A set of states F (subset of S) of final states
INPUT: string
OUTPUT: yes or no
Example NFA
Transition Table:
ε
0 a 1 b 2 b 3 STATE
a b ε
a,b 0 0,1 0 3
S = { 0,1,2,3} 1 2
S0 = 0
2 3
Σ = {a,b}
F = {3} 3
Deterministic Finite Automata
0 a 1 b 2 b 3 STATE
a b ε
b 0 0,1 0 3
S = { 0,1,2,3} 1 2
S0 = 0
2 3
Σ = {a,b}
F = {3} 3
Regular Languages
ε ε
Nq
PQ (concatenation)
Np Nq
ε ε ε
Converting Regular
Expressions to NFAs
If Q is a regular expression with NFA Nq:
Q* (closure) ε
ε Nq ε
ε
Example (ab* | a*b)*
Starting with:
a
ab* 1 2
a*b 3
b
4
b a
ab* | a*b
a
1 2
ε ε
5 b 6
ε ε
3 b 4
a
Example (ab* | a*b)*
ab* | a*b
a
ε
1 2 ε
5 b 6
ε
3 b 4 ε
(ab* | a*b)*
a a
ε
1 2 ε
ε b
ε
7 5 6 8
ε ε
ε b
ε 3 4
a
Converting NFAs to DFAs
Idea: Each state in the new DFA will correspond to
some set of states from the NFA. The DFA will be in
state {s0,s1,…} after input if the NFA could be in any
of these states for the same input.
Input: NFA N with state set SN, alphabet Σ, start state sN, final states FN,
transition function TN: SN x {Σ U ε} set of SN
Output: DFA D with state set SD, alphabet Σ, start state
sD = ε-closure(sN), final states FD, transition function TD: SD x Σ SD
ε-closure()
a λ
b 5 ε-closure({1,2,5}) = {1,2,5}
a
ε-closure({4}) = {1,4}
3
λ
4 ε-closure({3}) = {1,3,4}
ε-closure({3,5}) = {1,3,4,5}
Algorithm: Subset
Construction
sD = ε-closure(sN) -- create start state for DFA
SD = {sD} (unmarked)
while there is some unmarked state R in SD
mark state R
for all a in Σ do
s = ε-closure(ΤΝ(R,a));
if s not already in SD then add it (unmarked)
TD(R,a) = s;
end for
end while
FD = any element of SD that contains a state in FN
Example 1: Subset
Construction
NFA
ε
1 2 a,b
a b 5
a,b
3 4
b
Example 1: Subset
Construction
NFA
1,2
ε
1 2 a,b
a b 5
a,b a b
3 4
b {1,2}
Example 1: Subset
Construction
NFA
b
1,2 4,5
ε a
1 2 a,b
a b 5 3,5
a,b a b
3 4
b {1,2} {3,5} {4,5}
{3,5}
{4,5}
Example 1: Subset
Construction
NFA
b
1,2 4,5
ε a
1 2 a,b
b 3,5 b 4
a 5
a,b
3 4 a b
b
{1,2} {3,5} {4,5}
{3,5} - {4}
{4,5}
{4}
Example 1: Subset
Construction
NFA
b a,b
1,2 4,5 5
ε a
1 2 a,b
b 3,5 b 4
a 5
a,b a b
3 4
b {1,2} {3,5} {4,5}
{3,5} - {4}
{4,5} {5} {5}
{4}
{5}
Example 1: Subset
Construction
NFA
b a,b
1,2 4,5 5
ε a a,b
1 2 a,b
b 3,5 b 4
a 5
a,b
3 4 a b
b
{1,2} {3,5} {4,5}
{3,5} - {4}
{4,5} {5} {5}
All final states since the
NFA final state is included {4} {5} {5}
{5} - -
Example 2: Subset
Construction
NFA
b
1 2 b
a ε b 5
a
3 4
ε
Example 2: Subset
Construction
NFA DFA
b a
1 2 b 1 1,3,4
ε b b b
a 5 a
a 2 b a
3 4 b 1,3,4,5
ε b
a
1,4,5
Example 3: Subset
Construction
NFA DFA
b
ε b
1 2 a 3 5 1,2,4
ε a
b b
b 3,4 3,5 3
a 4 5
b b
a
4
a