Deterministic and Non Deterministic
Deterministic and Non Deterministic
Output
Fig. Automaton
Input file is divided into cells, each of which can hold one symbol. The
automaton has a temporary “storage” device, which has unlimited number of
cells, the contents of which can be altered by the automaton. Automaton has a
control unit, which is said to be in one of a finite number of “internal states”.
The automaton can change state in a defined way.
Solution
M does not accept specified language, as long as three consecutive b’s have not
been read.
It should be noted that
(i) M is in state qi (where i = 0,1, or 2) immediately after reading a run
of i consecutive b’s that either began the input string or was
preceded by an ‘a’.
(ii) If an ‘a’ is read and M is in state, q0, q1, or M returns to its initial
state q0.
q0, q1 and q2 are “Final states” (as given in the problem). Therefore any input
string not containing three consecutive b’s will be accepted.
In case we get three consecutive b’s then the q3 state is reached (which is
not final state), hence M will remain in this state, irrespective of any other
symbol in the rest of the string. This state q3 is said to be “dead state” or M is
said to be “trapped” at q3.
The DFA schematic is shown below based on the discussion above.
a a b
a
q0 q1 q2 q3
b b b a
q1 0 q1
q1 1 q2
q2 0 q3
q2 1 q2
q3 0 q2
q3 1 q2
Solution
0 1
1 0
q1 q2 q3
0,1
From the given table for , the DFA is drawn, where q2 is the only final state.
(It is to be noted that a DFA can “accept” a string and it can “recognize” a language.
Catch here is that “accept” is used for strings and “recognize” for that of a language).
It could be seen that the DFA accepts strings that has at least one 1 and an
even number of 0s following the last 1.
Hence the language L is given by
L = {w | w contains at least one 1 and
an even number of 0s follow the last 1}
Solution
From the given data, it is easy to predict the schematic of DFA as follows.
Internal states = q1, q2.
Symbols = 0, 1.
Transition function = (as defined above in the given problem)
q1 = Initial state
q2 = Final state.
0 1
1
q1 q2
Solution
a a,b
b a,b
q0 q1 q2
For the given language L {a n b : n 0}, the strings could be b, ab, a2b, a3b, .
Therefore the DFA accepts all strings consisting of an arbitrary number of
a’s, followed by a single b. All other input strings are rejected.
Example 1.1.5: Obtain the state table diagram and state transistion
diagram (DFA Schematic) of the finite state Automaton M (Q, , ,
q 0 , F ), where Q {q 0 , q1 , q 2 , q 3 }, {a, b}, q 0 is the initial state, F is the
final state with the transistion defined by
(q 0 , a ) q 2 ( q 3 , a ) q 1 ( q 2 , b) q 3
( q1 , a ) q 3 (q 0 , b) q1 ( q 3 , b) q 2
(q 2 , a ) q 0 (q 1 , b ) q 0
Solution
The State Table diagram is as shown below
a b
q0 q2 q1
q1 q3 q0
q2 q0 q3
q3 q1 q2
q0 q1
a a a a
q2 q3
Solution
Let us begin by “hard coding” the pattern into the machines states as shown in
fig. (a) below.
Input
a b a c
q0 q1 q2 q3 q4
Fig. (a)
As the pattern ‘abac’ has length four, there are four states required
in addition to one intial state q0, to remember the pattern. q4 is the only
accepting state required and this state q4 can be reached only after reading
‘abac’.
The complete DFA is as shown below in Fig. (b).
b,c
b,c a b a,b,c
q0 a q1 b q2 a q3 c q4
c
a
Fig. (b)
Example 1.1.7: Given {a, b}, construct a DFA that shall recognize
the language L {b m ab n : m, n 0}.
64 Theory of Automata, Formal Languages and Computation
Solution
The given language L {b m ab n : m, n 0} has all words with exactly one ‘a’
which is neither the first nor last letter of the word i.e., there is one or more b’s
before or after ‘a’.
a,b
q4
a a a
b
b a b
q0 q1 q2 q3
Example 1.1.8: Given {a, b}, construct a DFA which recognize the
language L {a m b n : m, n 0}.
Solution
The given language L {a m b n : m, n 0} has all words which begin with one or
more a’s followed by one or more b’s.
The finite automaton M (Q, , , q 0 , F ) is with
Q = {q0, q1, q2, q3}
= {a, b}
q0 = Initial state
F = {q2} = Final state
and as defined by language L.
The DFA is as shown below.
a,b
q3
b a
a
q0 a q1 b q2
b
Here q3 is a “dead” state.
DFA and NFA 65
Example 1.1.9: Construct a DFA which recognizes the set of all strings
on {a, b} starting with the prefix ‘ab’.
Solution
Only two states (q1, q2) are required to recognize ab, in addition to the input
state. One additional state called the “trap” state is also required.
a,b
q0 a q1 b q2
b a
q3
a,b
Hence the DFA that recognizes the set of all strings on {a, b} starting
with the prefix ‘ab’ is drawn above, where the automaton M is
, }, ,{q 2 })
M ({q 0 , q1 , q 2 , q 3 }, {01
with the state table diagram for as shown below.
a b
q0 q1 q3
q1 q3 q2
q2 q2 q2
q3 q3 q3
Example 1.1.10: Determine the DFA that will accept those words from
{a, b} where the number of b’s is divisible by three. Sketch the state
table diagram of the finite Automaton M also.
Solution
The Finite Automaton M is M (Q, , , q 0 , F ) with
Q {q 0 , q1 , q 2 }
66 Theory of Automata, Formal Languages and Computation
{a, b}
q 0 Initial state
F Final state
We choose three states q0, q1, q2. The states count the number of b’s modulo 3,
with q0 as the input as well as accepting state where q1 and q2 are not accepting
states. Run arrows from q0 to q1, q1 to q2 and q2 to q0 with label ‘b’.
If any a is encountered, it does not alter the state. The suitable DFA is as
shown in the figure (a).
a
q0
b b
q1 q2
b a
a
a b
q0 q0 q1
q1 q1 q2
q2 q2 q0
Solution 1
0
, }, , q 0 , {q 2 }).
M ({q 0 , q1 , q 2 },{01 1
The Finite Automaton is as shown. 0
q2
DFA and NFA 67
Solution
(a) M ({q 0 , q1 , q 2 },{01 , }, , q 0 , {q1 }). (See Fig. (a))
, }, , q 0 , {q 0 }). (See Fig. (b))
(b) M ({q 0 , q1 , q 2 , q 3 }, {01
q0 q1
0 1
1
q0 q1
1 0 0 0 0
1 0
1
0
q2 q3 q2
1 1
Solution
0,1
q0 0 q1 0 q3
1 0
q2
1
b b b
(a) (b)
Solution
(a) For {a, b}, language generated = {a,b}*
( will be accepted when initial state equal final state).
(b) For {a, b}, language generated = {a, b}+
{ is not accepted).
Solution
(a) Given L A {( ab) n | n 0}.
The FA is shown below in Fig. (a).
a
q0 q1
b
b a
q2
b a
Fig. (a)
The FA is given by
M ({q 0 , q1 , q 2 },{a, b}, , q 0 , q 0 )
where q2 is a “dead state”.
DFA and NFA 69
(b) Given LB {(ab) n | n 1} (-not accepted) i.e., initial state final state).
The FA for this language LB is shown in Fig. (b).
a
q0 q1 b
a q2
b a
q3
b b
a
Fig.(b)
The FA is given by
M ({q 0 , q1 , q 2 , q 3 },{a, b}, , q 0 , q 2 )
where q3 is a “dead state”.
Solution
(a) Set of strings beginning with an ‘a’.
a
q0 q1 a
b
b
q2
a
b
a a a
q0 q1 q2 q3 a
b
b
b
b
q0 q1 Alphabet {0,1, , 9}
1-9 0-9
q0 q2 q1
+,– 1-9 0-9
Solution
q1, q2, q3 are initial states
DFA and NFA 71
1 0,1 0,1
q1 q2 q3 q4
Solution
a,b,c a,b,c
(a)
q0 abac q1
q1 a
(b) q0
q2 b
Solution
The conditions to be satisfied are:
(a) String should end in a 1
(b) String should not contain 00.
q1
1
q0 0 1
1 q2
Solution
The conditions are
(a) the last two symbols can be ‘a’ or ‘b’.
(b) third symbol from the right is ‘a’
(c) symbol in any position but for the last three position can be ‘a’ or
‘b’.
The NFA is shown in fig. below.
a,b
q0 q1 q2 q3
a a,b a,b
0 1
q0 q0, q1 q0, q2
q1 q3
q2 q3
q3 q3 q3
Solution
The NFA states are q0, q1, q2 and q3.
(q 0 ,0) {q 0 , q1 } ( q 0 ,1) {q 0 , q 2 }
(q1 ,0) {q 3 } ( q 2 ,1) {q 3 }
(q 3 ,0) {q 3 } ( q 3 ,1) {q 3 } .
q1
0,1 0
0,1
q0 q3
1
q2
DFA and NFA 73
q0 a q1 a q2 a q3
a
a
q4 q5
Solution
The language accepted by the given NFA is
L {a 3 } {a n : n is odd}.
This is accomplished by adding two states after state q3 viz., q6 and q7 as shown
in fig.
Solution
NFA for the language:
L {a n : n 0} {b n a : n 1}
Case (i): a n , n 0
q0 goes to a state q3 where all a’s are absorbed. Hence an is accepted.
74 Theory of Automata, Formal Languages and Computation
Case (ii): b n a : n 1
q0 goes to a state q1 where all b’s are accepted and when an ‘a’ is
encountered it goes to final state q2. An additional state q4 is added as a
rejection state for the cases when ‘b’ is encountered after a’s of case (i) or when
‘a’ or ‘b’ is encountered after bna of case (ii).
The NFA is given by
M ({q 0 , q1 , q 2 , q 3 , q 4 }, {a, b}, , q 0 , {q 2 , q 3 })
which is shown in the fig. below.
Example 1.2.8: Design an NFA with no more than five states for the set
Solution
NFA for the language
L {abab n : n 0} {aba n : n 0}
is
M ({q 0 , q1 , q 2 , q 3 , q 4 },{a, b}, , q 0 , {q 2 , q 3 , q 4 }).
Here the NFA is such that it accepts all strings of the type aban and ababn
where n 0.
b
q0 a q1 b q2 a q3
q4
Example 1.2.9: Determine an NFA with three states that accepts the
language {ab, abc}*.
Solution:
NFA for the language
L {ab, abc}*
should be such that it accepts “ab” or “abc” in the first step and then this is
looped with initial state so that any combination of “ab” and “abc” can be
accepted.
Hence we have the NFA as
M ({q 0 , q1 , q 2 }, {a, b, c}, , q 0 , {q1 })
which is shown below:
a
b
q0 a q1 q2
a
L (aa * (a b)).
Solution:
a
a a,b
q0 q1 q2
NFA is given by
M ({q 0 , q1 , q 2 }, {a, b}, , q 0 , {q 2 })
a b
Solution
Let M (Q , , , q 0 , F ) be a determine. Finite state automaton (DFA),
where
Q = {[ q 0 ], [q1 ],[ q 0 , q1 ], []},
q 0 = [q0]
and F = {[ q1 ], [ q 0 , q1 ]}
Please remember that [ ] denotes a single state. Let us now proceed to
determine to be defined for the DFA.
a b
It is to be noted that
([ q 0 , q1 ], a ) [q 0 , q1 ]
([ q 0 , q1 ], a ) (q 0 , a ) (q1 , a )
since
{q 0 , q1 }
{q1 , q1 }
and ([q 0 , q1 ], b) [q 0 , q1 ]
([q 0 , q1 ], b) (q 0 , b) (q1 , b)
since
{q1 } {q 0 , q1 }
{q 0 , q1 }
DFA and NFA 77
Here any subset containing q1 is the final state in DFA. This is shown as below.
a
a
b b
[q0] [q1] [q0,q1]
a
b
b
a
Example 1.3.2: Given the NDA as shown in Fig. (a), with as shown in
Fig. (b).
a b
q0 q1 q2
a b
Fig. (a)
a b
q0 {q0, q1}
q1 {q1, q2}
q2
Fig. (b)
Solution
Conversion of NDA to DFA is done through subset construction as shown in
the State table diagram below.
a b
[q0] [q0, q1]
[q0, q1] [q0, q1] [q1, q2]
[q1 , q2] [q1, q2]
78 Theory of Automata, Formal Languages and Computation
The corresponding DFA is shown below. Please note that here any subset
containing q2 is the final state.
Example 1.3.3: Given the NDA as shown in fig. below, determine the
equivalent DFA.
Solution
The given NDA has q2 and q4 as final states. It accepts strings ending in 00 or
11. The state table is shown below.
0 1
0 [q0] 1
1
[q0,q1] [q0,q3]
0 0 1
1
[q0,q1,q2] [q0,q3,q4]
0
Example 1.3.4: Determine a NFA accepting {ab, ba} and use it to find
a DFA accepting it.
Solution
The state table is as shown below.
a b
q0 q1 q2
q1 q3
q2 q3
q3
a a
q0 q1 q2 q3
b
q0 is the input state, q3 is the final state.
80 Theory of Automata, Formal Languages and Computation
a b
[q0] [q1] [q2]
[q1] [q3]
[q2] [q3]
[q3]
b a
a
[q0] [q1] [q2] [q3]
b
a b
a,b
b a