80% found this document useful (5 votes)
14K views23 pages

DFA and NFA Complete Examples

The document provides examples of DFAs (deterministic finite automata) and NFAs (non-deterministic finite automata). It includes examples of designing DFAs and NFAs to accept specific strings of 0s and 1s based on different rules. It also provides examples of converting NFAs to equivalent DFAs and minimizing DFAs.

Uploaded by

Abbas Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
80% found this document useful (5 votes)
14K views23 pages

DFA and NFA Complete Examples

The document provides examples of DFAs (deterministic finite automata) and NFAs (non-deterministic finite automata). It includes examples of designing DFAs and NFAs to accept specific strings of 0s and 1s based on different rules. It also provides examples of converting NFAs to equivalent DFAs and minimizing DFAs.

Uploaded by

Abbas Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DFA Examples

Example 1:
1. Q = {q0, q1, q2}  
2. ∑ = {0, 1}  
3. q0 = {q0}  
4. F = {q2}  

Solution:Mm

Transition Diagram:

Transition Table:

Present State Next state for Input 0 Next State of

→q0 q0 q1

q1 q2 q1

*q2 q2 q2

Design a FA with ∑ = {0, 1} accepts the only input 101.

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:

 The DFA can be shown by a transition diagram as:

DFA with ∑ = {0, 1} accepts all starting with 0.

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.

 Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that does


not contain consecutive 1's.

Solution:

When three consecutive 1's occur the DFA will be:

Here two consecutive 1's or single 1 is acceptable, hence


The stages q0, q1, q2 are the final states. The DFA will generate the strings that
do not contain consecutive 1's like 10, 1010, 101,..... etc.

NFA Examples

Present State 0 1

→q0 q0, q1 q0, q2

q1 q3 ε

q2 q2, q3 q3

→q3 q3 q3

Design a NFA for the transition table as given below:

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

Hence, NFA would be:

Example 3:
Design an NFA with ∑ = {0, 1} in which double '1' is followed by double '0'.

Solution:1100

The FA with double 1 is as follows:

It should be immediately followed by double 0.

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.

Hence the NFA becomes:

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:

Conversion of NFA TO DFA


Example 1:
Convert the given NFA to DFA.
Solution: For the given transition diagram we will first construct the transition table.

State 0 1

→q0 q0 q1

q1 {q1, q2} q1

*q2 q2 {q1, q2}

Now we will obtain δ' transition for state q0.

1. δ'([q0], 0) = [q0]  
2. δ'([q0], 1) = [q1]  

The δ' transition for state q1 is obtained as:

1. δ'([q1], 0) = [q1, q2]       (new state generated)  
2. δ'([q1], 1) = [q1]  

The δ' transition for state q2 is obtained as:

1. δ'([q2], 0) = [q2]  
2. δ'([q2], 1) = [q1, q2]  

Now we will obtain δ' transition on [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

→[q0] [q0] [q1]

[q1] [q1, q2] [q1]

*[q2] [q2] [q1, q2]

*[q1, q2] [q1, q2] [q1, q2]

The Transition diagram will be:

The state q2 can be eliminated because q2 is an unreachable state.

Example 2:
Convert the given NFA to DFA.
Solution: For the given transition diagram we will first construct the transition table.

State 0 1

→q0 {q0, q1} {q1}

*q1 ϕ {q0, q1}

Now we will obtain δ' transition for state q0.

1. δ'([q0], 0) = {q0, q1}  
2.                = [q0, q1]       (new state generated)  
3. δ'([q0], 1) = {q1} = [q1]  

The δ' transition for state q1 is obtained as:

1. δ'([q1], 0) = ϕ  
2. δ'([q1], 1) = [q0, q1]  

Now we will obtain δ' transition on [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]}.

The transition table for the constructed DFA will be:

State 0 1

→[q0] [q0, q1] [q1]

*[q1] ϕ [q0, q1]

*[q0, q1] [q0, q1] [q0, q1]

The Transition diagram will be:

Even we can change the name of the states of DFA.

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-
 

Minimize the given DFA-


 
 

Solution-
 

Step-01:
 

 State q3 is inaccessible from the initial state.


 So, we eliminate it and its associated edges from the DFA.
 
The resulting DFA is-
 

Step-02:
 
Draw a state transition table-
 
a b

→q0 *q1 q0

*q1 *q2 *q1

*q2 *q1 *q2

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-
 
 

Conveting Epsilon NFA to NFA

Let us obtain ε-closure of each state.

1. ε-closure {q0} = {q0, q1, q2}  
2. ε-closure {q1} = {q1}  
3. ε-closure {q2} = {q2}  
4. ε-closure {q3} = {q3}  
5. ε-closure {q4} = {q4}  

Now, let ε-closure {q0} = {q0, q1, q2} be state A.

Hence

δ'(A, 0) = ε-closure {δ((q0, q1, q2), 0) }


              = ε-closure {δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0) }
= ε-closure{0 U q3 U 0}
              = ε-closure {q3}
              = {q3}           call it as state B.

δ'(A, 1) = ε-closure {δ((q0, q1, q2), 1) }


              = ε-closure {δ((q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1) }
= ε-closure {0 U 0 U q3 }
              = ε-closure {q3}
              = {q3} = B.

Now,

δ'(B, 0) = ε-closure {δ(q3, 0) }


              = ϕ
δ'(B, 1) = ε-closure {δ(q3, 1) }
              = ε-closure {q4}
              = {q4}           i.e. state C

For state C:

1. δ'(C, 0) = ε-closure {δ(q4, 0) }  
2.               = ϕ  
3. δ'(C, 1) = ε-closure {δ(q4, 1) }  
4.               = ϕ  

The DFA will be,


--------------------------- 2nd way --------------

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 - -

Solution: Let us obtain the ε-closure of each state.

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.

δ'(A, 0) = ε-closure{δ((q0, q1, q2), 0)}


              = ε-closure{δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0)}
= ε-closure{q0 U 0 U 0}
              = ε-closure{q0}
              = {q0, q1, q2}

δ'(A, 1) = ε-closure{δ((q0, q1, q2), 1)}


              = ε-closure{δ(q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1)}
              = ε-closure{q1}
              = {q1, q2}         call it as state B

δ'(A, 2) = ε-closure{δ((q0, q1, q2), 2)}


              = ε-closure{δ(q0, 2) ∪ δ(q1, 2) ∪ δ(q2, 2)}
              = ε-closure{q2}
              = {q2}         call it state C

Thus we have obtained

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

δ'(B, 0) = ε-closure{δ((q1, q2), 0)}


              = ε-closure{δ(q1, 0) ∪ δ(q2, 0)}
              = ε-closure{ϕ}
              = ϕ

δ'(B, 1) = ε-closure{δ((q1, q2), 1)}


              = ε-closure{δ(q1, 1) ∪ δ(q2, 1)}
              = ε-closure{q1}
              = {q1, q2}         i.e. state B itself

δ'(B, 2) = ε-closure{δ((q1, q2), 2)}


              = ε-closure{δ(q1, 2) ∪ δ(q2, 2)}
              = ε-closure{q2}
              = {q2}         i.e. state C itself

Thus we have obtained

1. δ'(B, 0) = ϕ  
2. δ'(B, 1) = B  
3. δ'(B, 2) = C 
Now we will obtain transitions for C:

δ'(C, 0) = ε-closure{δ(q2, 0)}


              = ε-closure{ϕ}
              = ϕ

δ'(C, 1) = ε-closure{δ(q2, 1)}


              = ε-closure{ϕ}
              = ϕ

δ'(C, 2) = ε-closure{δ(q2, 2)}


              = {q2}

Hence the DFA is

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.

You might also like