Chapter 2 Finite State Automata Part 1
Chapter 2 Finite State Automata Part 1
ANUJ GHIMIRE
DISCLAIMER
This document does not claim any originality and
cannot be used as a substitute for prescribed textbooks.
The information presented here is merely a collection
from various sources as well as freely available material
from internet and textbooks.
The ownership of the information lies with the
respective authors or institutions.
Chapter 2
ON OFF
Press/ Device
ON
Normal States
Terminating States
Representation of DFA
Let us consider a DFA as
M= (Q, ∑, δ, q0, F)
Q= {q0, q1 q2 q3}
∑ ={ a,b}
δ : Transition Function of the form Q ∑ → Q are:
a a
b b b
q2 a q3
Representation of DFA
A Transition Table is a tabular representation of a
transition function of finite automata.
The rows of the table correspond to the states, and the
columns correspond to the input.
The intersection of each row and the column i.e. each
cell corresponding to the next state.
Here the initial state or starting state is marked with
arrow head and the final state are marked with
( * ) symbol
Representation of DFA
Let us consider a DFA as
M= (Q, ∑, δ, q0, F)
Q= {q0, q1 q2 q3}
∑ ={ a,b}
δ : Transition Function of the form Q ∑ → Q are:
Q/∑ a b
qo q1 q2
q1 q3 q1
q2 q1 q0
* q3 q2 q1
Processing of String
For processing any string by an automata, it has three
blocks: a a b a b …. ….. ….. ….
Input Tape Input Tape
Reading Head
Finite Control Finite Control
Reading Head q0, q1, q2,…..
Input Tape of automata is responsible for storing the
symbol of input string.
It is divided into number of square cell, each cell stores
the symbol of input string.
Processing of String
Reading Head scans the symbol from the input tape,
each cell at a time in one direction only, either from
left to right or right to left.
The processing of FA is re controlled by Finite
Control i.e. all the states, current state, next state
obtained after each transition function must be
defined in the finite control.
Finite control is also responsible for giving next state
regularly and the state obtained after each transition is
tracked by finite control.
Processing of String
Initially the reading head is placed at the left must cell
of input tape and then it scans each cell in the defined
direction.
When the input string becomes empty and it the finite
control gives one of the defined final states as the next
state, the string is said to be accepted otherwise
rejected.
Processing of String
Consider the following DFA and check whether the
string w=001110100 is accepted or rejected
0
1 q2
q1
1 1 0
q3
0
Processing of String
Solution,
The transition function of given DFA is written as:
δ(q1,0) → q1 δ(q2,0) → q3
δ(q1,1) → q2 δ(q2,1) → q1
δ(q3,0) → q3
δ(q3,1) → q2
q1 b q2
a
b
b a b q4
q1 q2 q3
Figure: DFA over the {a,b} that accepts the string ending with bab
Designing DFA (Example_2)
Now the required DFA is
M= (Q, ∑, δ, q0, F)
Q= {q1, q2 q3 q4}
∑ ={ a,b}
δ : Transition Function of the form Q ∑ → Q are:
1 1 0 q4
q1 q2 q3
1
0 0
q5
0,1
Figure: DFA over the {a,b} that accepts the string starting with 110
Designing DFA (Example_3)
Now the required DFA is
M= (Q, ∑, δ, q0, F)
Q= {q1, q2 q3 q4 ,q5}
∑ ={ 0,1}
δ : Transition Function of the form Q ∑ → Q are:
(q1,0) → q5 (q2,1) → q3 (q4,0) → q4
(q1,1) → q2 (q3,0) → q4 (q4,1) → q4
(q2,0) → q5 (q3,1) → q5 (q5,0) → q5
(q5,1) → q5
q0 : q1
F: q4
Designing DFA (Example_4)
Design DFA over {0,1} that accepts the string with even
number of 0 and odd number of 1.
Solution,
We need to construct the DFA as M= (Q, ∑, δ, q0, F) where:
Q: Set of finite number of states.
∑ : Set of finite input symbols.
q0 : Initial /Starting State (q0 Є Q)
F: Set of Final States (F Q)
δ : Transition Function of the form
Q∑→Q
Set of string that the DFA accepts are:
{1, 001, 111, 01011, 01101, 1001010, 110111000, 00101010110,……….}
Designing DFA (Example_4)
0
q1 q2
1 1 1 1
q4 0 q3
Figure: DFA over the {0,1} that accepts the string with even 0 and odd 1
Designing DFA (Example_4)
Now the required DFA is
M= (Q, ∑, δ, q0, F)
Q= {q1, q2 q3 q4 }
∑ ={ 0,1}
δ : Transition Function of the form Q ∑ → Q are:
(q1,0) → q2 (q2,1) → q3 (q4,0) → q3
(q1,1) → q4 (q3,0) → q4 (q4,1) → q1
(q2,0) → q1 (q3,1) → q2
q0 : q1
F: q4
Designing DFA (Example_5)
Design a deterministic finite automaton M that accepts the
language L= {w є {a, b} * : w does not contain three
consecutive b's }
Solution,
We need to construct the DFA as M= (Q, ∑, δ, q0, F) where:
Q: Set of finite number of states.
∑ : Set of finite input symbols.
q0 : Initial /Starting State (q0 Є Q)
F: Set of Final States (F Q)
δ : Transition Function of the form
Q∑→Q
Set of string that the DFA accepts are:
{a, bb, aab, aabb, aabba, babbab, bbabbaba,……….}
Designing DFA (Example_5)
a
a a
b b b q4
q1 q2 q3
q0 : q1
F: q4
Non-Deterministic Finite Automata
(NFA)
a
a
b a
a a b q4
q1 q2 q3
b
Figure: NFA
Non-Deterministic Finite Automata
(NFA)
A Transition Table of given NFA
Q/∑ a b
q1 {q1,q2} q3
q2 q3 ф
q3 ф {q3,q4}
* q4 {q2,q4} {q1,q3}
Non-Deterministic Finite Automata
(NFA)
Consider the language L = (ab+aba)*, and draw its DFA
a b q3 a q4
q1 q2
b
b a
a q5
b Figure: DFA that accepts the language
L = (ab+aba)*
Non-Deterministic Finite Automata
(NFA)
For this language L = (ab+aba)*, we can have the NFA as
b
q1 q2
b
a
q3
Figure: NFA that accepts the
language L = (ab+aba)*
Non-Deterministic Finite Automata
(NFA)_Example_1
Construct an NDFA for the language
(ba)* (bab)*.
Solution,
We need to construct the NDFA as M= (Q, ∑, δ, q0, F) where:
Q: Set of finite number of states.
∑ : Set of finite input symbols.
q0 : Initial /Starting State (q0 Є Q)
F: Set of Final States (F Q)
δ : Transition Function of the form
Q ∑ → 2Q
Set of string that the DFA accepts are:
{ε, ba, bab, babbabbab, babbabbabbab, bababa..……….}
Non-Deterministic Finite Automata
(NFA)_Example_1
For this language L = (ba)* (bab)*, we can have the NFA
as
a
q1 q2
a
b
q3
Figure: NFA that accepts the
language L = (ba)* (bab)*
Non-Deterministic Finite Automata
(NFA)_Example_1
Consider a NFA as:
M= (Q, ∑, δ, q0, F)
Q= {q1, q2 q3}
∑ ={ a,b}
δ : Transition Function of the form Q ∑ → 2Q are:
(q1,a) → ф (q2,b) → ф
(q1,b) → q2 (q3,a) → ф
(q2,a) →{q1, q3} (q3,b) → q1
q0 : q1
F: q1
Non-Deterministic Finite Automata
(NFA)
Here we can see that the complex DFA can be
described with the simple NFA.
Non-deterministic finite automata can be a much
more convenient device to design than a deterministic
finite automata.
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.
Extended Transition Function of
NFA
The extended transition function of NFA, denoted
by is a transition function that takes two arguments as
input, a state q Є Q & a string w and returns a set of
states that the NFA is in, if it starts in q & processes the
string w.
It describes what happens when we start in any state
and follow any sequence of inputs.
Extended Transition Function of
NFA_Example
Conversion of NFA to DFA
Remember that every DFA is an special NFA but not vice
versa.
But there is an equivalent DFA for every NFA.
We use sub-set construction method to convert NFA into
DFA
Steps are:
Construct a transition table of given NFA.
Identify all the new states from the transition table.
Find the transition for each new states in term of input
symbols.
The process is continued until transition for all the new states
are identified.
Finally draw a transition diagram by using all states obtained.
Conversion of NFA to DFA_Example(1)
Convert the following NFA into DFA
0 1
0 q2
q1
1 1
q3
0
Conversion of NFA to DFA_Example(1)
Solution,
The transition table of the given NFA is given by:
Q/∑ 0 1
q1 {q1,q2} q3
q2 ф {q2,q3}
*q3 q3 ф
{q1 ,q2 }
1 1
0
Q
0 1
P S
1
0
1 0 {q1 }=P
T R {q1 ,q2 }=Q
1
1
{q2 ,q3 }=S
0 {q3 }=R
{A}=T
Conversion of NFA to DFA_Example(2)
Convert the following NFA into DFA
1
1
0, 1 0, 1
A B C
1 0
D
Conversion of NFA to DFA_Example(2)
Solution,
The transition table of the given NFA is given by:
Q/∑ 0 1
A {B,D} B
B C {B,C}
C D A
*D Ф A
δ’ ({B,C,D},0)
→ δ (B,0) δ (C,0) δ (D,0)
→ {C} {D} {ф}
{C,D}
Conversion of NFA to DFA_Example(2)
δ’ ({B,C,D},1)
→ δ (B,1) δ (C,1) δ (D,1)
→ {B,C} {A} {A}
{A,B,C} (old state)
δ’ ({C,D},0)
→ δ (C,0) δ (D,0)
→ {D} {ф}
{D} (old state)
δ’ ({C,D},1)
→ δ (C,1) δ (D,1)
→ {A} {A}
{A} (old state)
Conversion of NFA to DFA_Example(2)
Final DFA is: 1
0 1
A {B,D} {A,B,C}
1 1 0 1
1 1 1
B {B,C} {B,C,D}
0 0 0 1
C {C, D} 0
0 0
0 Q 0, 1
D
Conversion of NFA to DFA_Example(3)
Convert the following NFA into DFA
Try Yourself
ε-Non-Deterministic Finite
Automata (ε -NFA)
If there is a transition for ε (empty input) then the
automata is called ε-NFA.
This is another extension of finite automata that
allows a transition on ε, the empty string which
means it can switches to new states without reading an
input symbol.
This capability does not expand the class of languages
that can be accepted by finite automata, but it does
give some added “programming convenience”.
ε-Non-Deterministic Finite
Automata (ε -NFA)
Formally, ε-NFA is defined by five tuples (similar to
DFA and NFA):
M= (Q, ∑, δ, q0, F) where:
Q: Set of finite number of states.
∑ : Set of finite input symbols.
q0 : Initial /Starting State (q0 Є Q)
F: Set of Final States (F Q)
δ : Transition Function of the form
Q ∑ {ε} → 2Q
ε-Non-Deterministic Finite
Automata (ε -NFA)
ε-NFA can be shown as:
1
q1 q2
ε 1 1
q4 ε q3
0
Figure: ε-NFA
ε-Closure of a State (ε*)
Epsilon 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.
Epsilon closure of a state q denoted by ε-closure (q) is
the set of states that can be reached without reading any
input symbol from state q.
To obtain ε-closure (q), follow all transitions out of q
that are labelled ε.
After we get to another state by following ε, follow the ε-
transitions out of those states & so on, eventually
finding every state that can be reached from q along any
path whose arcs are all labeled.
ε-Closure of a State (ε*)
Formally, we can define ε-closure of the state q as;
Basis: state q is in ε-closure (q).
Induction: If state p is reached with ε-transition from
state q, p is in ε-closure (q) and if there is an arc from p
to r labeled ε, then r is in ε-closure (q) and so on.
ε-Closure of a State (ε*) a
q2 ε q4 b q6
ε
q1 ε
a ε a
a
q3 q5 q7
a ε
ε-closure (q1)={q1,q2,q4,q5,q7,q6}
ε-closure (q2)={q2,q4,q5,q7,q6} ε-closure (q6)={q6}
ε-closure (q3)={q3} ε-closure (q7)={q7,q6}
ε-closure (q4)={q4,q5,q7,q6}
ε-closure (q5)={q5,q7,q6}
(ε -NFA) Representation
1
q1 q2
ε 1 1
q4 ε q3
0
Figure: ε-NFA
(ε -NFA) Representation
Given ε - NFA as can be defined as:
M= (Q, ∑, δ, q0, F)
Q= {q1, q2, q3, q4}
∑ ={ 0, 1}
δ : Transition Function of the form Q ∑ → 2Q are:
(q1,0) → q2 (q2,1) → {q1, q3}
(q1,1) → ф (q3, ε) → {q3, q4}
(q2, ε) → q2
(q1, ε) → {q1, q4} (q4,0) → q3
(q3,0) → ф
(q2,0) → ф (q4,1) → ф
(q3,1) → q2
(q4, ε) → q4
q0 : q1
F: q4
(ε -NFA) Representation
A Transition Table of given ε-NFA
Q/∑ 0 1 ε
q1 q2 ф {q1, q4}
q2 ф {q1,q3} q2
Q3 ф q2 {q3, q4}
* q4 q3 ф q4
• Keep in mind that all states contains ε and every state on getting ε
as input goes to itself. i.e. if ε is not given then then goes to itself.
• If ε is given then transition will be on itself as well as other state
where transition is given
Extended Transition Function of
(ε -NFA)
The extended transition function of ε -NFA denoted by 𝛿መ is
defined by;
መ ε) = ε -closure (q)
Basis Step: 𝛿(q,
Inductive Step: Let w = xa be a string, where x is substring
of w without last symbol a and a Є ∑ but a ≠ ε
Let 𝛿መ (q, x)= {p1,p2,p3………pk} i.e. pi's are the states that can be
reached from q following path labeled x which can end with
many ε & can have many ε.
Also let,
=𝑖𝑘ڂ1 δ(𝑝𝑖, 𝑎)= {r1,r2,……….rm}
Then, δ 𝑞, 𝑥 = 𝑚ڂ 𝑗=1 ε −closure (rj)
Extended Transition Function of
(ε -NFA)
To compute 𝛿መ (q, w), first find the ε -closure of starting
state.
Then read the first symbol of w and determine the states of
NFA after reading first symbol and take the union of ε -
closure of these states.
Again read the second symbol and determine the states of
NFA after reading second symbol from the union of ε -
closure of states found in previous step.
Continue this process until the last symbol is read and
finally compute the ε -closure of states that are reached
after reading last symbol.
Extended Transition Function of
(ε -NFA)_Example
ε b q6 ε
q2 q4 q8
a
ε
q1 q10
ε
ε
q3 q5 q7 q9
b a b
= ε-closure({B,C})
= ε-closure(B) ε-closure(C)
= {B,C} {C}
= {B,C}
Removing ε-Transition Function
from ε -NFA
δN(B, 0) = ε-closure(δ(ε-closure(B),0)
δ(B,0) δ(C,0)
= ε-closure(δ({B,C},0) ф {C}= {C}
= ε-closure(C)
= {C}
δN(B, 1) = ε-closure(δ(ε-closure(B),1)
δ(B, 1) δ(C, 1)
= ε-closure(δ({B,C},1) {B} {C}= {B, C}
= ε-closure({B, C})
= ε-closure(B) ε-closure(C)
= {B, C} {C}
= {B, C}
Removing ε-Transition Function
from ε -NFA
δN(C, 0) = ε-closure(δ(ε-closure(C),0)
= ε-closure(δ(C,0))
= ε-closure(C)
= {C}
δN(C, 1) = ε-closure(δ(ε-closure(C),1)
= ε-closure(δ(C, 1)
= ε-closure(C)
= {C}
Removing ε-Transition Function
from ε -NFA
Final NFA
0
1
0, 1
0, 1 0
A B C
1
0, 1
Here A, B and C all are final states in NFA because the final
states are those state from where we can reach to the final
state of ε –NFA only by ε – input.