DFA and NFA Complete Examples
DFA and NFA Complete Examples
Example 1:
1. Q = {q0, q1, q2}
2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}
Solution:Mm
Transition Diagram:
Transition Table:
→q0 q0 q1
q1 q2 q1
*q2 q2 q2
Solution:
In the given solution, we can see that only input 101 will be accepted. Hence, for input 101,
there is no other path shown for other input.
Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive 0's.
Solution:
The strings that will be generated for this particular languages are 000, 0001, 1000, 10001,
.... in which 0 always appears in a clump of 3. The transition graph is as follows:
Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's
followed by single 1.
Solution:
Solution:
Explanation:
o In the above diagram, we can see that on given 0 as input to DFA in state q0
the DFA changes state to q1 and always go to final state q1 on starting input
0. It can accept 00, 01, 000, 001,0001,....etc. It can't accept any string
which starts with 1, because it will never go to final state on a string starting
with 1.
Example :
DFA with ∑ = {0, 1} accepts all ending with 0.
Solution:
Explanation:
In the above diagram, we can see that on given 0 as input to DFA in state q0,
the DFA changes state to q1. It can accept any string which ends with 0 like 00,
10, 110, 100....etc. It can't accept any string which ends with 1, because it will
never go to the final state q1 on 1 input, so the string ending with 1, will not be
accepted or will be rejected.
Solution:
NFA Examples
Present State 0 1
q1 q3 ε
q2 q2, q3 q3
→q3 q3 q3
Solution:
The transition diagram can be drawn by using the mapping function as given in the
table.
Here,
1. δ(q0, 0) = {q0, q1}
2. δ(q0, 1) = {q0, q2}
3. Then, δ(q1, 0) = {q3}
4. Then, δ(q2, 0) = {q2, q3}
5. δ(q2, 1) = {q3}
6. Then, δ(q3, 0) = {q3}
7. δ(q3, 1) = {q3}
Example 2:
Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
Solution:
001,101
Example 3:
Design an NFA with ∑ = {0, 1} in which double '1' is followed by double '0'.
Solution:1100
Then, 011000,111001,011001,111000
Now before double 1, there can be any string of 0 and 1. Similarly, after double 0, there can
be any string of 0 and 1.
1100,011000,111001,011001,111000
Example 4:
Design an NFA in which all the string contain a substring 1110.
Solution:
The language consists of all the string containing substring 1010. The partial transition
diagram can be:
Now as 1010 could be the substring. Hence we will add the inputs 0's and 1's so that the
substring 1010 of the language can be maintained. Hence the NFA becomes:
State 0 1
→q0 q0 q1
q1 {q1, q2} q1
1. δ'([q0], 0) = [q0]
2. δ'([q0], 1) = [q1]
1. δ'([q1], 0) = [q1, q2] (new state generated)
2. δ'([q1], 1) = [q1]
1. δ'([q2], 0) = [q2]
2. δ'([q2], 1) = [q1, q2]
1. δ'([q1, q2], 0) = δ(q1, 0) ∪ δ(q2, 0)
2. = {q1, q2} ∪ {q2}
3. = [q1, q2]
4. δ'([q1, q2], 1) = δ(q1, 1) ∪ δ(q2, 1)
5. = {q1} ∪ {q1, q2}
6. = {q1, q2}
7. = [q1, q2]
The state [q1, q2] is the final state as well because it contains a final state q2. The
transition table for the constructed DFA will be:
State 0 1
Example 2:
Convert the given NFA to DFA.
Solution: For the given transition diagram we will first construct the transition table.
State 0 1
1. δ'([q0], 0) = {q0, q1}
2. = [q0, q1] (new state generated)
3. δ'([q0], 1) = {q1} = [q1]
1. δ'([q1], 0) = ϕ
2. δ'([q1], 1) = [q0, q1]
1. δ'([q0, q1], 0) = δ(q0, 0) ∪ δ(q1, 0)
2. = {q0, q1} ∪ ϕ
3. = {q0, q1}
4. = [q0, q1]
Similarly,
1. δ'([q0, q1], 1) = δ(q0, 1) ∪ δ(q1, 1)
2. = {q1} ∪ {q0, q1}
3. = {q0, q1}
4. = [q0, q1]
As in the given NFA, q1 is a final state, then in DFA wherever, q1 exists that state becomes
a final state. Hence in the DFA, final states are [q1] and [q0, q1]. Therefore set of final
states F = {[q1], [q0, q1]}.
State 0 1
Suppose
1. A = [q0]
2. B = [q1]
3. C = [q0, q1]
With these new names the DFA will be as follows:
DFA MINIMIZATION
Problem-01:
Minimize the given DFA-
Solution-
Step-01:
The given DFA contains no dead states and inaccessible states.
Step-02:
Draw a state transition table-
Present State a b
→q0 q1 q2
q1 q1 q3
q2 q1 q2
q3 q1 *q4
*q4 q1 q2
Step-03:
Now using Equivalence Theorem, we have-
P0 = { q0 , q1 , q2 , q3 } { q4 } --------------- final state and other state divide
P1 = { q0 , q1 , q2 } { q3 } { q4 } ,
P2 = { q0 , q2 } { q1 } { q3 } { q4 }
P3 = { q0 , q2 } { q1 } { q3 } { q4 }
Since P3 = P2, so we stop.
From P3, we infer that states q0 and q2 are equivalent and can be merged together.
So, Our minimal DFA is-
Solution-
Step-01:
Step-02:
Draw a state transition table-
a b
→q0 *q1 q0
Step-03:
Now using Equivalence Theorem, we have-
P0 = { q0 } { q1 , q2 } ----------
P1 = { q0 } { q1 , q2 }
Since P1 = P0, so we stop.
From P1, we infer that states q1 and q2 are equivalent and can be merged together.
So, Our minimal DFA is-
1. ε-closure {q0} = {q0, q1, q2}
2. ε-closure {q1} = {q1}
3. ε-closure {q2} = {q2}
4. ε-closure {q3} = {q3}
5. ε-closure {q4} = {q4}
Hence
Now,
For state C:
1. δ'(C, 0) = ε-closure {δ(q4, 0) }
2. = ϕ
3. δ'(C, 1) = ε-closure {δ(q4, 1) }
4. = ϕ
Present 0 1
State
Q0 Q3 Q3
Q1 Q3 -
Q2
Q3
Q4
Present state e* 0 e*
Q0 q0, - -
q1, q3 q3
q2 - -
q1 q1 q3 q3
e* 1 e*
q0 q0 - -
q1 - -
q2 q3 q3
q1 q1 - -
1. ε-closure(q0) = {q0, q1, q2}
2. ε-closure(q1) = {q1, q2}
3. ε-closure(q2) = {q2}
Now we will obtain δ' transition. Let ε-closure(q0) = {q0, q1, q2} call it as state A.
1. δ'(A, 0) = A
2. δ'(A, 1) = B
3. δ'(A, 2) = C
Now we will find the transitions on states B and C for each input.
Hence
1. δ'(B, 0) = ϕ
2. δ'(B, 1) = B
3. δ'(B, 2) = C
Now we will obtain transitions for C:
As A = {q0, q1, q2} in which final state q2 lies hence A is final state. B = {q1, q2} in which
the state q2 lies hence B is also final state. C = {q2}, the state q2 lies hence C is also a
final state.