0% found this document useful (0 votes)
29 views23 pages

Deterministic and Non Deterministic

Uploaded by

Yash Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views23 pages

Deterministic and Non Deterministic

Uploaded by

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

C ha p t er 1

DFA and NFA

1.1 DETERMINISTIC FINITE AUTOMATA (DFA)


1.1.1 Automata—What is it?
An automaton is an abstract model of a digital computer. An automaton has a
mechanism to read input, which is a string over a given alphabet. This input is
actually written on an “input file”, which can be read by the automaton but
cannot change it.
Input File

Control Unit Storage

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.

1.1.2 Types of Automaton


(a) Deterministic Automata
(b) Non-deterministic Automata
A deterministic automata is one in which each move (transition from one
state to another) is unequally determined by the current configuration.
If the internal state, input and contents of the storage are known, it is
possible to predict the future behaviour of the automaton. This is said to be
deterministic automata otherwise it is nondeterminist automata.
DFA and NFA 59

An automaton whose output response is “yes” or “No” is called an


“Acceptor”.

1.1.3 Definition of Deterministic Finite Automaton


A Deterministic Finite Automator (DFA) is a 5-tuple
M  (Q, , , q 0 , F )
where
Q = Finite state of “internal states”
 = Finite set of symbols called “Input alphabet”
 :Q    Q = Transition Function
q0 Q = Initial state
F Q = Set of Final states
The input mechanism can move only from left to right and reads exactly
one symbol on each step.
The transition from one internal state to another are governed by the
transition function .
If  ( q 0 , a )  q1 , then if the DFA is in state q0 and the current input symbol
is a, the DFA will go into state q1.

 Example 1.1.1: Design a DFA, M which accepts the language


L( M )  {w  ( a, b) * : w does not contain three consecutive b’s).
Let M  (Q, , , q 0 , F )
where
Q = {q0, q1, q2, q3}
 = {a, b}
q0 is the initial state
F = {q0, q1, q2,} are initial states
and  is defined as follows:

Initial state Symbol Final state


q   (q ,  )
q0 a q0
q0 b q1
q1 a q0
q1 b q2
q2 a q0
q2 b q3
q3 a q3
q3 b q3
60 Theory of Automata, Formal Languages and Computation

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

Fig. Finite Automaton with four states

 Example 1.1.2: Determine the DFA schematic for M  (Q, , , q, F )


where Q = {q1, q2, q3},  q1 is the start state, F = {q2} and  is
given by the table below.

Initial state Symbol Final state


q   (q ,  )

q1 0 q1
q1 1 q2
q2 0 q3
q2 1 q2
q3 0 q2
q3 1 q2

Also determine a Language L recognized by the DFA.


DFA and NFA 61

Solution
0 1
1 0
q1 q2 q3

0,1

Fig. Finite Automaton having three states.

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}

where L = L(M) and M recognized the RHS of the equation above.

 Example 1.1.3: Sketch the DFA given


M  {q1 , q 2 },{01
, }, , q1 ,{q 2 }
and  is given by
 ( q1 , 0)  q1 and  ( q 2 , 0)  q1
 ( q1 , 1)  q 2  (q 2 ,1)  q 2
Determine a Language L(M), that the DFA recognizes.

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

Fig. State diagram of DFA


62 Theory of Automata, Formal Languages and Computation

If a string ends in a 0, it is “rejected” and “accepted” only if the string ends


in a 1. Therefore the language
L(M) = {w | w ends in a 1}.

 Example 1.1.4: Design a DFA, the language recognized by the


Automaton being
L  {a n b : n  0}

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

With the given definitions, the State Transition diagram/DFA Schematic is


shown on next page.
DFA and NFA 63
b

q0 q1

a a a a

q2 q3

 Example 1.1.6: Obtain the DFA that accepts/recognizes the language


L(M) = {w | w  {a, b, c}* and w contains the pattern abac}
(Note: This is an application of DFA’s involving searching a text for a specified
pattern)

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

DFA is drawn above for the automaton M,


where M  (Q, , , q 0 , F ) with
Q  {q 0 , q1 , q 2 , q 3 , q 4 }
  {a, b}; q0 = Initial state,
F = {q3} = Final state.
and  is defined as per the language L. (q4 is “dead” state)

 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

Fig. (a) DFA

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

Fig. (b) State table diagram

 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

Fig. (a) DFA

The state table diagram is shown in Fig. (b).

 a b

q0 q0 q1
q1 q1 q2
q2 q2 q0

Fig. (b) State table diagram

 Example 1.1.11: Construct an FA accepting all strings in {0,1}* having


even number of 0’s.

Solution 1
0

The Finite Automaton M is given by q0 q1

, }, , q 0 , {q 2 }).
M ({q 0 , q1 , q 2 },{01 1
The Finite Automaton is as shown. 0
q2
DFA and NFA 67

 Example 1.1.12: Construct a finite automaton accpting all strings over


{0, 1}
(a) having odd number of 0’s
(b) having even number of 0’s and even number of 1’s.

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

Fig. (a) Fig. (b)

 Example 1.1.13: Determine an FA, M accepting L,


, }* : Every 0 in w has a 1 immediately to its right}.
where L  {w  {01

Solution
0,1

q0 0 q1 0 q3

1 0

q2
1

The finite automaton is given by


, }, , q 0 , {q 2 }).
M ({q 0 , q1 , q 2 , q 3 },{01
68 Theory of Automata, Formal Languages and Computation

 Example 1.1.14: Determine the languages produced by the FA shown


in Figs. (a) and (b).
a
q1 a q0 q1 a

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

 Example 1.1.15: Determine the FA if   {a, b} for


(a) Language generated L A  ( ab) *  {(ab) n | n  0}
(-not accepted)
(b) Language generated LB  {( ab) n | n  1}
(-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”.

 Example 1.1.16: Determine the FA with the


(a) Set of strings beginning with an ‘a’.
(b) Set of strings beginning with ‘a’ and ending with ‘b’.
(c) Set of strings having ‘aaa’ as a subword.
(d) Set of integers
(e) Set of signed integers.

Solution
(a) Set of strings beginning with an ‘a’.
a
q0 q1 a

b
b
q2
a
b

[It is not necessary always to have a dead state]


(b) Set of strings beginning with ‘a’ and ending with ‘b’.
a a b
q0 q1 q2
b
a
b
q3
b a
70 Theory of Automata, Formal Languages and Computation

(c) Set of strings having ‘aaa’ as a subword.

a a a
q0 q1 q2 q3 a
b
b
b
b

(d) Set of integers.

q0 q1 Alphabet   {0,1,  , 9}
1-9 0-9

(e) Set of signed Integers.

q0 q2 q1
+,– 1-9 0-9

1.2 NON-DETERMINISTIC FINITE AUTOMATA (NFA)


Definition
A Nondeterministic Finite Automata (NFA) is defined by a 5-tuple
M  (Q, , , q 0 , F )
where Q, , , q 0 , F are defined as follows:
Q = Finite set of internal states
 = Finite set of symbols called “Input alphabet”
 = Q  (   {})  2 Q
q 0  Qis the Initial states
F  Qis a set of Final states
NFA differs from DFA in that, the range of  in NFA is in the powerset 2Q.
A string is accepted by an NFA if there is some sequence of possible
moves that will put the machine in the final state at the end of the string.

 Example 1.2.1: Obtain an NFA for a language consisting of all strings


over {0,1} containing a 1 in the third position from the end.

Solution
q1, q2, q3 are initial states
DFA and NFA 71

q4 is the final state.

1 0,1 0,1
q1 q2 q3 q4

Please note that this is an NFA as  ( q 2 ,0)  q 3 and  ( q 2 ,1)  q 3 .

 Example 1.2.2: Determine an NFA accepting the language


(a) L1  {x | x  {a, b, c}* and x contains the pattern abac}
(b) L2  {a *  b* }

Solution
a,b,c a,b,c
(a)
q0 abac q1

q1 a

(b) q0

 q2 b

 Example 1.2.3: Determine an NFA accepting all strings over {0,1}


which end in 1 but does not contain the substring 00.

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

The NFA is shown in figure.

 Example 1.2.4: Obtain an NFA which should accept a language LA,


given by L A  {x  {a, b}* :| x|  3 and third symbol of x from the right is
{‘a’}.
72 Theory of Automata, Formal Languages and Computation

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

 Example 1.2.5: Sketch the NFA state diagram for


M  ({q 0 , q1 , q 2 , q 3 }, {01
, }, , q 0 , {q 3 })
with the state table as given below.

 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 } .

The NFA is as shown below.

q1

0,1 0
0,1
q0 q3

1
q2
DFA and NFA 73

 Example 1.2.6: Given L is the language accepted by NFA in Fig.


Determine an NFA that accepts L  {a 5 }.

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

Now to make an NFA accepting the language:


L  {a 3 }  {a n : n is odd}  {a 5 }.

This is accomplished by adding two states after state q3 viz., q6 and q7 as shown
in fig.

The NFA is given by


M  ({q 0 , q1 , q 2 , q 3 , q 4 , q 5 , q 6 , q 7 },{a}, , q 0 , {q 3 , q 5 , q 7 })

 Example 1.2.7: Find an NFA with four states for


L  {a n : n  0}  {b n a : n  1}

Solution
NFA for the language:
L  {a n : n  0}  {b n a : n  1}

For such a language two cases are to be considered.

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

{abab n : n  0}  {aba n : n  0}.

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

q2 is for the case when string is ab, i.e. abn with n = 0.


q3 is for the case when string is ababn with n  0.
q4 is for the case when string is aban with n  0
This NFA is shown in the fig. above.
DFA and NFA 75

 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

 Example 1.2.10: Determine an NFA that accepts the language

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

1.3 EQUIVALENCE OF NFA AND DFA


Definition
Two finite accepters M1 and M2 are equivalent iff
L( M 1 )  L( M 2 )
i.e., if both accept the same language.
Both DFA and NFA recognize the same class of languages. It is important
to note that every NFA has an equivalent DFA.
Let us illustrate the conversion of NDA to DFA through an example.
76 Theory of Automata, Formal Languages and Computation

 Example 1.3.1: Determine a deterministic Finite State Automaton


from the given Nondeterministic FSA.
M  ({q 0 , q1 }, {a, b}, , q 0 , {q1 })
with the state table diagram for  given below.

 a b

q0 {q0, q1} {q1}


q1  {q0, q1}

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

[q0] [q0, q1] [q1]


[q1]  [q0, q1]
[q0,q1] [q0,q1] [q0,q1]
  

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)

Determine the equivalent DFA for the above given NDA.

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

q0 {q0, q1} {q0, q3}


q1 {q2} 
q2  
q3  {q4}
q4  

The conversion of NDA to DFA is done through the subset construction.


DFA and NFA 79

 is given by the following state table.


0 1
 [q 0 ] [q0, q1] [q0, q3]
[q0 , q1] [ q 0 , q1 , q 2 ] [q0, q3]
[q0 , q3] [q0 , q1] [q 0 , q 3 , q 4 ]
[ q 0 , q1 , q 2 ] [ q 0 , q1 , q 2 ] [q0 , q3]
[q 0 , q 3 , q 4 ] [q0 , q1] [q 0 , q 3 , q 4 ]

Any state containing q2 or q4 will be a final state.


The DFA is shown below.

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

The NFA is shown below.


b

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

The state table corresponding to the DFA is derived by using subset


construction. State table for DFA is as shown below.

a b
[q0] [q1] [q2]
[q1]  [q3]
[q2] [q3] 
[q3]  
  

b a

a
[q0] [q1] [q2] [q3]

b
a b
a,b

b a

The DFA is as shown above.

1.4 REGULAR EXPRESSION


1.4.1 Regular Languages
The regular languages are those languages that can be constructed from the
“big three” set operations viz., (a) Union (b) Concatenation (c) Kleene star.
A regular language is defined as follows.
Definition: Let  be an alphabet. The class of “regular languages” over  is
defined inductively as follows:
(a)  is a regular language
(b) For each   , {} is a regular language
(c) For any natural number n  2 if L1 , L2 ,  Ln are regular
languages, then so is L1  L2     Ln.
(d) For any natural number n  2, if L1 , L2 ,  Ln are regular
languages, then so is L1  L2     Ln .
(e) If L is a regular language, then so is L*.
(f) Nothing else is a regular language unless its construction follows
from rules (a) to (e).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy