Chapter2 Finite Automata
Chapter2 Finite Automata
Used in software for verifying all kinds of systems with a finite number of states, such as
communication protocols
Used in software for scanning text, to find certain patterns
Used in Lexical analyzers of compilers (to turn program text into tokens, e.g. identifiers,
keywords, brackets, punctuation)
Part of Turing machines and abacus machines
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Formal Definition
A deterministic finite automaton is a quintuple M = (K,, , s, F) where
K is a finite set of states,
is an alphabet,
s K is the initial state,
F K is the set of final states, and
, the transition function, is a function from K x K.
If M is in state q K and the symbol read from the input tape is a , then (q, a) K is the uniquely
determined state to which K passes.
The transition from one internal state to another are governed by transition function .
If (q0, a) q1 then if the DFA is in state q0 and the current input symbol is 'a' the DFA will go into state
q1 .
Configuration of DFA
A configuration is determined by the current state and the unread part of the string being processed.
Let M = (K, , , s, F) be a DFA ,we say that a word in K x * is a configuration of M. It represents the
current states of M and remaining unread input of M. e.g (q2, ababab) is one configuration
a b a b a b
The binary relation holds between two configurations of M if and only if the machine can pass
from one to the other as a result of a single move. Thus if (q, w) and (q', w') are two configurations of M,
then (q, w) (q', w') if and only if w = aw' for some symbol a , and (q, a) = q'. For every
configuration except those of the from (q, e) there is a uniquely determined next configuration.
A configuration of the form (q, e) signifies that M has consumed all its input, and hence its operation
ceases at this point.
We denote the reflexive, transitive closure of is read, (q,
w) yields (q', w') (after some number, possibly zero, of steps).
Example 1
Design a DFA that accepts the language L which has set of strings in {a, b } * that have even number of
b's .
Solution
Let required DFA M= (K, , , s, F)
where
K= {q0, q1 }
= { a ,b}
s=q0
F= { q0 } and is the function tabulated below
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
/ a b
q0 q0 q1
q1 q1 q0
e.g : If M is given the input aabba, its initial configuration is (qo, aabba). Then
Example 2:
Design a deterministic finite automaton M that accepts the language L( M) = {w {a, b} * : w does not
contain three consecutive b's}.
Solution
Let required DFA M= (K,, , s, F)
where
K= {q0, q1 , q2, q3 }
= { a ,b}
s=q0
F= { q0, q1, q2 } and is the function tabulated below
/ a b
q0 q0 q1
q1 q0 q2
q2 q0 q3
q3 q3 q3
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
State q3 is said to be a dead state, and if M reaches state q3 it is said to be trapped, since no further input
can enable it to escape from this state.
Example 3:
n
Design a DFA, the language recognized by the Automaton being L = {a b: n 0}
Solution
Let required DFA M= (K,, , s, F)
where
K= {q0, q1 , q2 }
= { a ,b}
s=q0
F= { q1 } and is the function tabulated below
/ a b
q0 q0 q1
*q1 q2 q2
q2 q2 q2
Example 4
Given = {a, b}, construct a DFA that shall recognize the language L = {bmabn :m, n > 0}.
Solution
Let required DFA M= (K,, , s, F)
where
K= {q0, q1 , q2, q3 , q4}
= { a ,b}
s=q0
F= { q3 } and is the function tabulated below
/ a b
q0 q4 q1
q1 q2 q1
q2 q4 q3
*q3 q4 q3
q4 q4 q4
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Example 5:
Construct a DFA which recognizes the set of all strings on = {a, b} starting with the prefix ab.
Solution
NFA has Ability to change states in a way that is only partially determined by the current state
and input symbol.
Permit several possible "next states" for a given combination of current state and input symbol.
The automaton, as it reads the input string, may choose at each step to go into anyone of these
legal next states; the choice is not determined by anything in our model, and is therefore said to
be nondeterministic.
Nondeterministic devices are not meant as realistic models of computers. They are simply a
useful notational generalization of finite automata, as they can greatly simplify the description of
these automata.
Nondeterministic finite automaton can be a much more convenient device to design than a
deterministic finite automaton,
Formal Definition : A nondeterministic finite automaton is a quintuple M = (K, , , s, F), where
K is a finite set of states,
is an alphabet,
s K is the initial state,
F K is the set of final states, and
, the transition relation, is a subset of K x ( {e}) x K.
NFA is a variant of finite automaton with two capabilities
a. e or transition : state transition can made without reading a symbol
b. non-determinism : zero or more than one possible value may exist for state transition
For each p K and each a { e} , (s, a) =R means
"Upon reading an 'a ' the automaton M may transition from state s to any state in R."
For e in particular, (s, e) =R means,
"Without reading the symbol the automaton M may transition from s to any state in R"
Configuration of NFA:
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Examples
To see that a nondeterministic finite automaton can be a much more convenient device to design than a
deterministic finite automaton, consider the language L = (ab U aba)*, which is accepted by the
deterministic finite automaton illustrated in below
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
When M is given the string bababab as input, several different sequences of moves may ensue. For
example, M may wind up in the non final state qo in case the only transitions used are (qO, a, q0) and (qO,
b, q0):
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
The same input string may drive M from state q0 to the final state q4, and indeed may do so in three
different ways. One of these ways is the following.
Since a string is accepted by a nondeterministic finite automaton if and only if there is at least one
sequence of moves leading to a final state, it follows that bababab L(M)
E-closure
Epsilon closure (- closure) of a state q is the set that contains the state q itself and all other states
that can be reached from state q by following transitions.
We use the term, ECLOSE( q0) to denote the Epsilon closure of state q0.
Suppose that the given NFA M= (Q,, , q0,F) . For any set s Q of states of M, we define the
e-closure of S, denoted by E(S) to be the set of states reachable from s via zero or more e
transition in a row.
Formally for I 0 ,we define Ei(S) inductively to be the set of states reachable from s via exactly
i many e-transitions, so that E0(s)=s and for I 0
Ei+1(s) = { r Q | Eq Ei(s) : r (q, e) }
= q ( ) (q, e)
E(s) = Ei(s)
Namely, the set of all states reachable from s via zero or more e-transitions.
e-closure(q0 ) denotes a set of all vertices p such that there is a path from q0 to p labeled .
Example
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
A DFA is just a special type of NFA . In a DFA it so happens that the transition relation is in
fact a function from K x to K i.e. NFA (K,, , s, F) is deterministic if and only if there are no
transition of the form (q, e, p) in and for each q K and a there is a exactly one p K
such that (q, a, p )
It is there fore evident that the class of languages accepted by DFA is a subset of the class of
languages accepted by NFA. Rather surprisingly these classes are in fact equal.
Despite the power and generality enjoyed by NFA, they are no more powerful than the NFA in
terms of the language they accept.
NFA can always be converted into an equivalent deterministic one.
Formally, we say that the two finite automata M1 and M2 (NFA and DFA) are equivalent if and
only if L(M1) = L(M2). Thus two automata are considered to be equivalent if they accept the
same language, even though they may use different methods to do so.
E(q)= { p K : (q , e) (p , e) }
To put it otherwise, E( q) is the closure of the set {q} under the relation
{ (p, r) : there is a transition (p, e, r) }.
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Solution:
Here
Given NFA M= (K,,, s, F)
E(q0)= { q0, q1, q2, q3}
E(q1) = {ql,q2,q3}, and
E(q2) = {q2}
We are now ready to define formally the deterministic automaton M'= (K',,', s', F') that
is equivalent to M, where
K' =2K
S'=E(s)
F'={ Q K : Q F } and
for each Q K and each symbol a ,define
'(Q, a) = U{E(p) : p K and ( q, a, p) for some q Q }
i. e. '(Q, a) is taken to be the set of all states of M to which M can go by reading input a
and possibly followed several e-moves.
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
F' , the set of final states contains each set of states of which q4 is member , since q4 is
the sole final member of F.
Example : Given the NFA as shown in fig. below, determine the equivalent DFA.
Solution
The given NFA has q2 and q4 as final states. It accepts strings ending in 00 or 11. The state table
is shown below.
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Proof.
Since L1 is regular, there is, an NFA M1 = (K1, , 1, q1, F1), such that L1 = L(M1). Similarly,
there is an NFA M2 = (K2,, 2, q2, F2), such that L2 = L(M2).
We may assume that K1 K2 = , (K1 and K2 are disjoint sets) because otherwise, we can give
new names to the states of K1 and K2.
From these two NFAs, we will construct an NFA M = (K, , , q0, F), such that L(M) = L1
L2. The construction is illustrated in Figure 2.1. The
NFA M is defined as follows:
1. K = K1 K2 {q0}, where q0 is a new state.
2. q0 is the start state of M.
3. F = F1 F2.
4. : 1 2 { (q0, e, q1), (q0, e, q2)} is defined as follows:
For any r K and for any a
Compiled By : Hari Prasad Pokhrel [hpokhrel24@gmail.com] Page 13 of 30
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
(r, a) = 1(r, a) if r K1
= 2(r, a) if r K2
= {q1, q2 } if r = q0 and a = e
= if r = q0 and a e
Steps in Union of L1 and L2
Create a new start state
Make a e-transition from the new start state to each of the original start states.
Concatenation
The set of regular languages is closed under the concatenation operation, i.e., if L1 and L2
are regular languages over the same alphabet , then L1L2 is also a regular language.
Proof. Since L1 is regular, there is, an NFA M1 = (K1, , 1, q1, F1), such that L1 = L(M1).
Similarly, there is an NFA M2 = (K2,, 2, q2, F2), such that L2 = L(M2).
We may assume that K1 K2 = , (K1 and K2 are disjoint sets) because otherwise, we can give
new names to the states of K1 and K2.
From these two NFAs, we will construct an NFA M = (K, , , q0, F), such that L(M) = L1 . L2.
The construction is illustrated in Figure 2.2. The
NFA M is defined as follows:
1. K = K1 K2 ,
2. q0 = q1 is the start state of M.
3. F = F2.
4. : is defined as follows:
For any r K and for any a
(r, a) = 1(r, a) if r K1 and r F1
= 1(r, a) if r F1 and a e
= 1(r, a) U {q2} if r F1 and a = e
= 2(r, a) if r K2
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Complementation
The set of regular languages is closed under the complement operations:
If L1 is a regular language over the alphabet , then the complement L1 = {w * : w L1}
is also a regular language.
If L is an Regular over , then Complement of L= * - L
Proof
To show Complement of L is also regular , Convert every final state into non final and every
non final state to final state.
Steps Complementation of L1
Start with a complete DFA, not with an NFA
Make every final state non final and every non final state final.
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Steps in Reverse of L1
Start with an automaton with just one final state.
Make the initial state final and final state initial.
Reverse the direction of every arc
Begin by creating a start state whose label is (start state of L1, start state of L2).
Repeat the following until no new arcs can be added:
(1) Find a state (A, B) that lacks a transition for some x in S.
(2) Add a transition on x from state (A, B) to state ( (A, x), (B, x)). (If this state does not
already exist, create it).
The same construction is used for both intersection and set difference. The distinction is in how the final
states are selected.
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
L1 L2 = * - (( *-L1 ) U ( *-L2 ))
Proof: Let A and B be DFAs whose languages are L1 and L2, respectively.
Construct C, the product automaton of A and B.
Make the final states of C be the pairs consisting of final states of both A and B.
Since L1 is regular, there is, an DFA M1 = (K1, , 1, q1, F1), such that L1 = L(M1). Similarly,
there is an DFA M2 = (K2, , 2, q2, F2), such that L2 = L(M2). We can construct a DFA
M1 M2 =M = (K, , , q0, F)
Where
K= K1 xK2,
= ,
q0= (q1, q2),
F= F1 x F2
and is defined Such a way that ,
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Set Difference
Proof:
Let A and B be DFAs whose languages are L and M, respectively.
Construct C, the product automaton of A and B.
Make the final states of C be the pairs where A-state is final but B-state is not.
Mark a state (A, B) as final if A is a final state in L1, but B is not a final state in L2.
State Minimization
Minimization of DFA
For a given language, many DFA may exist that accept it. The DFA we produce from a NFA
may contain many dead states, inaccessible states and indistinguishable states. All these
unnecessary states can be eliminated from the DFA through a process called minimization.
For practical applications, it is desirable that number of states in the DFA is minimum.
The Algorithm for minimizing a DFA as follows:
Compiled By : Hari Prasad Pokhrel [hpokhrel24@gmail.com] Page 19 of 30
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
1. Step 1: Eliminate any state that cannot be reached from the start state.
2. Step 2: partition the remaining states into blocks so that all the states in the same block
are equivalent and no pair of states from different blocks are equivalent.
Example
Minimise the following DFA
Current state input symbol
a b
q0 q5 q1
q1 q2 q6
*q2 q2 q0
q4 q5 q7
q5 q6 q2
q6 q4 q6
q7 q2 q6
q3 q6 q2
Step 1: Eliminate any state that can't be reached from the start state
In above, the state q3 can't be reached. So remove the corresponding to q3 from the transition
table. Now the new transition table is
Current state input symbol
a b
q0 q5 q1
q1 q2 q6
*q2 q2 q0
q4 q5 q7
q5 q6 q2
q6 q4 q6
q7 q2 q6
Step 2: Divided the rows of the table into 2 sets as
1. one set containing only rows which starts from non final states
Set 1
q0 q5 q1
q1 q2 q6
q4 q5 q7
q5 q6 q2
q6 q4 q6
q7 q2 q6
2. another set containing those rows which start from final states
* q2 q2 q0
Step 3a: Consider the set 1
q0 q5 q1 Row1
q1 q2 q6 Row2
q4 q5 q7 Row3
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
q5 q6 q2 Row4
q6 q4 q6 Row5
q7 q2 q6 Row6
Row 2 and Row 6 are similar since q1 and q7 transit to same states on inputs a and b so remove
one of them (for instance q7) and replace q7 with q1 in rest we get
Set 1
q0 q5 q1 Row1
q1 q2 q6 Row2
q4 q5 q1 Row3
q5 q6 q2 Row4
q6 q4 q6 Row5
Now Row 1 and Row 3 are similar. So remove one of them (for instance q4) and replace q4 with
q0 in the rest
we get
Set 1
q0 q5 q1 Row1
q1 q2 q6 Row2
q5 q6 q2 Row3
q6 q0 q6 Row4
Now there are no more similar rows
3b. Consider the set 2
Set 2
*q2 q2 qq0
Do the same process for set 2
But it contains only one row .It is already minimized
Step 4
Combine set 1 and set 2
we get
Current state input symbol
a b
q0 q5 q1
q1 q2 q6
q5 q6 q2
q6 q0 q6
*q2 q2 q0
Now this is minimized DFA
The transition diagram is
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Similarly do yourself
final Minimised DFA diagram is
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Only If Part
(a) Regular Expression to NFA Construction
Proof:
Recall that the class of regular languages is the smallest class of languages containing the empty
set and the singletons a, where a is a symbol, and closed under union concatenation, and
Kleene star. It is evident (see Figure below) that the empty set and all singletons are indeed
accepted by finite automata; and by Theorem the finite automaton languages are closed under
union, concatenation, and Kleene star. Hence every regular language is accepted by some finite
automaton.
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
1. For any x in , the regular expression denotes the language {x}. The NFA (with a single start state and
a single final state) as shown below, represents exactly that language.
2. The regular expression l denotes the language {} or {e} that is the language containing
only the empty string.
3. The regular expression denotes the language ; no strings belong to this language, not
even the empty string.
6. The star (*) denotes zero or more applications of the regular expression, hence a loop has
to be set up in the NFA.
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
IF Part
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Example 2
Example 3
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Second Example
Automata that accepts even number of 1s
ioenotes.edu.np
Class Notes on Theory of Computation [BCT II/I] Chapter 2: Finite Automata
Decision property
Decision property for a class of languages is an algorithm that takes a formal description of a
language (e.g., a DFA) and tells whether or not some property holds.
Example : Is language L empty?
1. The Membership Question
2. The Emptiness Problem
3. The Infiniteness Problem
ioenotes.edu.np