Construction of DFA
Construction of DFA
• Example #1:
Q = {q0, q1}
1
Σ = {0, 1}
Start state is q0 0
F = {q0} q0 q1 1
0
δ:
0 1
q0 q1 q0
q1 q0 q1
• Revisit example #2:
a a a/b/c
Q = {q0, q1, q2}
Σ = {a, b, c} c c
q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
0
q0 q1 1
0
• What is δ^(q0, 011)? Informally, it is the state entered by M after processing 011
having started in state q0.
• Formally:
5
• Problem: Third symbol from last is 1
0/1
1 0/1 0/1
q0 q1 q2 q3
6
Nondeterministic Finite State
Automata (NFA)
• An NFA is a five-tuple:
M = (Q, Σ, δ, q0, F)
0 1 0/1
Q = {q0, q1, q2}
0 1
Σ = {0, 1} q0 q1 q2
Start state is q0
F = {q2}
δ: 0 1
q0 {q0, q1} {}
{} {q1, q2}
q1
{q2} {q2}
q2
8
• Example #2: pair of 0’s or pair of 1’s as substring
0/1 0/1
Q = {q0, q1, q2 , q3 , q4}
Σ = {0, 1} 0 0
q0 q3 q4
Start state is q0
1 0/1
F = {q2, q4}
1
δ: 0 1 q1 q2
{} {q2}
q1
{q2} {q2}
q2
{q4} {}
q3
{q4} {q4}
q4 9
• Notes:
• δ(q,s) may not be defined for some q and s (what does that mean?)
• δ(q,s) may map to multiple q’s
• A string is said to be accepted if there exists a path from q0 to some state
in F
• A string is rejected if there exist NO path to any state in F
• The language accepted by an NFA is the set of all accepted strings
• Question: How does an NFA find the correct/accepting path for a given
string?
• NFAs are a non-intuitive computing model
• You may use backtracking to find if there exists a path to a final state
(following slide)
• Why NFA?
• We are primarily interested in NFAs as language defining capability, i.e., do
NFAs accept languages that DFAs do not?
• Other secondary questions include practical ones such as whether or not
NFA is easier to develop, or how does one implement NFA
10
• Determining if a given NFA (example #2) accepts a given string (001) can be
done algorithmically:
0 0 1
q0 q0 q0 q0
q3 q3 q1
q4 q4 accepted
0 0
q0 q3 q4
1 0/1
1
q1 q2
11
• Another example (010):
0 1 0
q0 q0 q0 q0
q3 q1 q3
not accepted
• All paths have been explored, and none lead to an accepting state.
0/1
0 0
q0 q3 q4
1
1
q1 q2
12
• Question: Why non-determinism is useful?
• Non-determinism = Backtracking
• Compressed information
• Non-determinism hides backtracking
• Programming languages, e.g., Prolog, hides backtracking => Easy to
program at a higher level: what we want to do, rather than how to do it
• Useful in algorithm complexity study
• Is NDA more “powerful” than DFA, i.e., accepts type of languages that any
DFA cannot?
13
• Construction of a DFA for the set of string over {a, b} such that length of
the string |w|=2 i.e, length of the string is exactly 2.
Solution:
The FA with double 1 is as follows:
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:
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 becom
Consider a string 111010,
NFA with ε can be converted to NFA without ε, and this NFA without ε can be converted to
DFA. To do this, we will use a method, which can remove all the ε transition from given NFA. The method
will be:
1.Find out all the ε transitions from each state from Q. That will be called as ε-closure{q1} where qi ∈ Q.
2.Then δ' transitions can be obtained. The δ' transitions mean a ε-closure on δ moves.
3.Repeat Step-2 for each input symbol and each state of given NFA.
4.Using the resultant states, the transition table for equivalent NFA without ε can be built.
Example:
Convert the following NFA with ε to NFA without ε.
Solution:
We will first obtain ε-closures of q0, q1 and q2 as follows:
ε-closure(q0) = {q0}
ε-closure(q1) = {q1, q2}
ε-closure(q2) = {q2}
Now the δ' transition on each input symbol is obtained as:
δ'(q0, a) = ε-closure(δ(δ^(q0, ε),a))
= ε-closure(δ(ε-closure(q0),a))
= ε-closure(δ(q0, a))
= ε-closure(q1)
= {q1, q2}
States a b
→q0 {q1, q2} Ф
*q1 Ф {q2}
*q2 Ф {q2}
State q1 and q2 become the final state as ε-closure of q1 and q2 contain the final state q2. The NFA can be shown
by the following transition diagram:
Conversion from NFA to DFA
In this section, we will discuss the method of converting NFA to
its equivalent DFA. In NFA, when a specific input is given to the current
state, the machine goes to multiple states. It can have zero, one or more
than one move on a given input symbol. On the other hand, in DFA,
when a specific input is given to the current state, the machine goes to
only one state. DFA has only one move on a given input symbol.
Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M).
There should be equivalent DFA denoted by M' = (Q', ∑', q0', δ', F') such
that L(M) = L(M').
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.
δ'([q0], 0) = [q0]
δ'([q0], 1) = [q1]
The δ' transition for state q1 is obtained as:
δ'([q1], 0) = [q1, q2] (new state generated)
δ'([q1], 1) = [q1]
The δ' transition for state q2 is obtained as:
δ'([q2], 0) = [q2]
δ'([q2], 1) = [q1, q2]
Now we will obtain δ' transition on [q1, q2].
δ'([q1, q2], 0) = δ(q1, 0) ∪ δ(q2, 0)
= {q1, q2} ∪ {q2}
= [q1, q2]
δ'([q1, q2], 1) = δ(q1, 1) ∪ δ(q2, 1)
= {q1} ∪ {q1, q2}
= {q1, q2}
= [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]
call it as state B.
δ'(A, 1) = ε-closure {δ((q0, q1, q2), 1) }
= ε-closure {δ((q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1) }
= ε-closure {q3}
= {q3}
= B.
Hence
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.
Minimization of DFA
Minimization of DFA means reducing the number of states from given FA. Thus, we get the FSM(finite state
machine) with redundant states after minimizing the FSM.
We have to follow the various steps to minimize the DFA. These are as follows:
Step 1: Remove all the states that are unreachable from the initial state via any set of the transition of DFA.
Step 2: Draw the transition table for all pair of states.
Step 3: Now split the transition table into two tables T1 and T2. T1 contains all final states, and T2 contains non-final
states.
Step 4: Find similar rows from T1 such that:
δ (q, a) = p
δ (r, a) = p
That means, find the two states which have the same value of a and b and remove one of them.
Step 5: Repeat step 3 until we find no similar rows available in the transition table T1.
Step 6: Repeat step 3 and step 4 for table T2 also.
Step 7: Now combine the reduced T1 and T2 tables. The combined transition table is the transition table of minimized
DFA.
Example
Solution:
Step 1: In the given DFA, q2 and q4 are the unreachable states so remove them.
Step 2: Draw the transition table for the rest of the states.
State 0 1
→q0 q1 q3
q1 q0 q3
*q3 q5 q5
*q5 q5 q5
Step 3: Now divide rows of transition table into two sets as:
• 1. One set contains those rows, which start from non-final states:
State 0 1
q0 q1 q3
q1 q0 q3
2. Another set contains those rows, which starts from final states.
State 0 1
q3 q5 q5
q5 q5 q5
Step 5: In set 2, row 1 and row 2 are similar since q3 and q5 transit to the same state on 0 and 1. So skip q5 and
then replace q5 by q3 in the rest.
State 0 1
q3 q3 q3
State 0 1
→q0 q1 q3
q1 q0 q3
*q3 q3 q3
• Write the regular expression for the language accepting all the string
which are starting with 1 and ending with 0, over ∑ = {0, 1}.
Solution:
Write the regular expression for the language starting with a but not
having consecutive b's.
R = {a + ab}*
Examples of Regular Expression
• Write the regular expression for the language over ∑ = {0} having even
length of the string.
Solution:
The regular expression has to be built for the language:
L = {ε, 00, 0000, 000000, ......}
The regular expression for the above language is:
R = (00)*
Examples of Regular Expression
• Write the regular expression for the language L over ∑ = {0, 1} such
that all the string do not contain the substring 01.
Solution:
The Language is as follows:
L = {ε, 0, 1, 00, 11, 10, 100, .....}
The regular expression for the above language is as follows:
R = (1* 0*)
Examples of Regular Expression
Write the regular expression for the language having a string which
should have atleast one 0 and alteast one 1.
Solution:
• Write the regular expression for the language containing the string in
which every 0 is immediately followed by 11.
Solution:
The regular expectation will be:
R = (011 + 1)*
Conversion of RE to FA
• Step 1:
Step 2: Step 4:
Step 3:
Step 5:
• Now we have got NFA without ε. Now we will convert it into required DFA
for that, we will first write a transition table for this NFA.
State 0 1
→q0 q3 {q1, q2}
q1 qf ϕ
q2 ϕ q3
q3 q3 qf
*qf ϕ ϕ
The equivalent DFA will be:
State 0 1
→[q0] [q3] [q1, q2]
[q1] [qf] ϕ
[q2] ϕ [q3]
[q3] [q3] [qf]
[q1, q2] [qf] [qf]
*[qf] ϕ ϕ
• Design a NFA from given regular expression 1 (1* 01* 01*)*.
Solution: The NFA for the given regular expression is as follows:
Step 1: Step 3:
Step 2:
• Design a NFA from given regular expression 1 (1* 01* 01*)*.
(Assignment)