Lecture 3 & 4 - Chapter 2
Lecture 3 & 4 - Chapter 2
Finite Automata
Contents
Finite Automata
Deterministic Finite accepters
String acceptance by DFA
Regular Languages
Nondeterministic Finite accepters
String acceptance by NFA
Equivalence of DFA and NFA
Implementation of DFA and NFA
Advanced topics in Finite State Automata
Finite State Automata
Finite state automata (AKA finite accepters ) is a mathematical
representation of a system, with discrete input and output.
Consists of a finite sets of states and a set of transitions from
state to state.
Finite accepters are simple special case of the general schema of
automata.
They have no temporary storage.
Hence, a finite automaton is limited in its capacity to remember
things during the computation, because its input file cannot be
rewritten.
There are two types of finite accepters
Deterministic finite state Automata (DFA)
Nondeterministic finite state Automata (NFA)
Deterministic Finite State automata
A Determinis tic finite accepters (DFA)
is a 5 - tupple(Q, , , q0 , F ), where
1. Q is a finite set called the states ,
2. is a finite set called the alphabet,
3. : Q Q is the transition function,
4. q0 Q is the start state , and
5. F Q is the set of accept states .
0
1
q0 q1 q2
0, 1
b a
q2
q2
a
String Acceptance by DFA…
b)
i) *(q0, abbb) = *(T(q0,a), bbb)
= *(q1, bbb)
= *(T(q1,b), bb)
= *(q1, bb)
= *(T(q1,b), b)
= (q1, b)
= q1
So, abbb is accepted by M because q1 e {q1, q2}
Following the same step as above it can be proved that (ii) abba is
accepted and (iii) bab is rejected
Languages accepted by DFA
Definition: The language accepted by DFA M = (Q, Â, , q0, F) is
the set of all strings on  accepted by M. Formally,
L(M) = {w e Â* : *(q0, w) e F} and the language which is not
accepted by M is given as : [L(M)]’ = {w e Â* : *(q0, w) ‰ F}
Example: Given the following DFA find L(M) and [L(M)]’
M = ({q0, q1, q2}, {a, b}, , q0, {q1} ) where (q0, a) = q0, (q0, b) = q1,
(q1, a) = q2, (q1, b) = q2, (q2, a) = q2, (q2, b) = q2
Answer: L(M) = {anb: n ≥0}, [L(M)] = {a,b}*a U λ
Exercise: What is the language accepted by the following DFA.
Theorem 2.1
Let M = (Q, Â, , q0, F) be a DFA and Gm be its associated transition
graph. Then for every qi, qj e Q and w e Â+, *(qi, w) = qj if and only if
there is a walk in Gm with label from qi to qj.
Proof: Proof by induction on the length of w
Base case: |w| = 1, that is w = a e Â
Clearly, (qi, a) = qj iff there is defined transition function , and then to
be represented as an edge (qi, qj ) labelled a in Gm
Inductive Step: Assume for A |v| n, the result holds
We want to show it holds true for w = va, where |a|=1
|w| = |v|+1 = n+1, because |uv| = |u| + |V|
Suppose (qi, v) = qk … i.e. from the inductive assumption there is a walk
from qi to qk in Gm labelled v (|v| = n)
If (qi, va) = qj then there must be a transition (qk, a) = qj, hence there is
an edge (qk, qj) labelled a in Gm
Consequently, there is a walk from qi to qj labelled with w in Gm
Examples
Example: Find a deterministic finite accepter that recognizes the
set of all strings on  = {a, b} starting with the prefix ab.
Answer:
Answer
Regular Language…
Example 2:
Given a language L = {awa: we{a, b}* } , Show that L2 is regular
language.
Answer
Nondeterministic Finite automata (NFA)
Unlike DFA, NFA has a set of possible transitions from a state
with an input.
Definition a nondeterministic finite accepter (NFA) is
defined by the quintuple M = (Q, Â, , q0, F) where Q, Â , q0,
F are defined as for DFA but :Q X (Â U {l}) f 2Q
The major difference b/n DFA and NFA
The range of the transition function is 2Q
Empty string, l , is allowed to be an argument of
Example
Given an NFA M = ({q0, q1, q2 , q3}, {a, b}, , q0, {q3})and
given in transition table indicated in the next slide, draw its
transition diagram
Nondeterministic Finite automata (NFA)…
State (q) Input(x) Next state ( (q, x))
q0 a {q1, q2}
q1 b {q1, q3}
q2 λ {q3}
q2 a, b 0
q3 a, b 0
q1 a {q3}
a b
q0 q1
Answer a
a b
q2
q3
λ
String acceptance by NFA
Definition: The language accepted by a NFA
M = (Q, Â, , q0, F) is defined L(M) = {w eÂ*: (q0, w) n FÎ 0}
Because of the difference of the definition of NFA from DFA
the transition function should be further extended.
1. Needs to operate on 2Q XÂ
: 2Q X Âf 2Q is defined as
(Q’, a) = U (q, a), Q’ z Q, a e Â, q e Q
Example, consider the previous example
({q0, q1}, a) = (q0, a) U (q1, a)
= {q1, q2} U {q2}
= {q1, q2}
String acceptance by NFA…
2. Needs to operate on 2Q X Â*
: 2Q X Â* f 2Q is given by
*(Q’, x) = Q’ if x = l
*((Q’, a), y) if x = ay, aeÂ, yeÂ*
Example: Consider the following NFA M =(Q, Â, , q0, F)
a b, a
Check if the following q0 q1
a a a
a
q0 q1 q0 q1
b b b
a, b
q2 q2
a, b D
Equivalence of DFA and NFA…
b a
q2 q3
Answer b
a
Find reachable states from q0
a b
• Define DFA M’ = (Q’, Â, ’, q0’, F’) where
{qo} {q1} {q2}
• Q’ = {{qo}, {q1}, {q2}, {q1, q3}, {q3}}
{q1} {q1, q3} -
• q0’ = {qo}
{q2} {q2} {q3}
• F = {{q1, q3}, {q3}}
{q1, q3} {q1, q3} - • ’ is given in the diagram in the next slide
{q3} - -
Equivalence of DFA and NFA…
a a {q2, q3}
a
{q0} {q1}
b
{q2}
b {q3}
a
Exercise: Convert the following NFA to its equivalent DFA.
Equivalence of DFA and NFA…
NFA can be converted to DFA using subset construction
Algorithm
let Dtran[A, a] = B
compute Dtran[A, b]. Among the states in A, only 4 has a
transition on b, and it goes to 5
Equivalence of DFA and NFA…
C=
If we continue this process with the unmarked sets B and C, we eventually
reach a point where all the states of the DFA are marked
Equivalence of DFA and NFA…
Exercises - Convert the following NFA to DFA
States 2 and 4 are final states
a
a
1 2
start
0 b
b
3 4
Implementation of DFA
Let us assume that the end of a string is marked with a special
symbol (say eos).
The algorithm for recognition will be as follows: (an efficient
implementation)
q = q0 { start from the initial state }
c = nextchar { get the next character from the input string }
while (c != eos) do { do until the end of the string }
begin
q = move(q,c) { transition function }
c = nextchar
end
if (q in F) then { if s is an accepting state }
return “yes”
else
return “no”
Implementing NFA
Q = -closure({q0})
{ set of all states that can be
c = nextchar
accessible from q0 by -transitions }
while (c != eos) {
begin { set of all states that can
Q = -closure(move(Q,c)) be accessible from a state
c = nextchar in q by a transition on c }
end
if (Q F != ) then { if Q contains an accepting state }
return “yes”
else
return “no”
This algorithm is not efficient.
DFA Minimization
Questions of DFA size:
Given a DFA, can we find one with fewer states that accepts the
same language?
What is the smallest DFA for a given language?
Is the smallest DFA unique, or can there be more than one
"smallest" DFA for the same language?
All these questions have neat answers…
a a
q0 q1 q2
b b
b
q3 q4
a,b a,b
b b
b
a,b
b
b
a,b
No more equivalencies
This is a minimum-state DFA for the language,
{xay | x {b}* and y {a}*}
DFA Minimization…
An Algorithm to Minimizing the number of states of a DFA
DFA Minimization…
DFA Minimization…
B
Two-Way Finite Automata
Is a generalized version of the finite automaton which can
revisit characters already processed
Just like ordinary finite state automata, there are a finite
number of states with transitions between them based on the
current character,
but each transition is also labeled with a value indicating whether the
machine will move its position in the input to the left, right, or stay at
the same position.
That is treat the input like a tape, and allow the automaton to
move its read head left or right on each move
FA is a special case of 2FA, they have equal power.
Two-Way Finite Automata…
However, the equivalent FA for a 2FA may have exponentially
more states
making 2FAs a much more practical representation for
algorithms for some common problems.
2FAsare also equivalent to read-only Turing machines
There two types of two-way finite automata
Two-way deterministic finite automata (2DFA)
Two-way nondeterministic finite automata (2NFA)
Two-Way Finite Automata…
A two way deterministic finite automata (2DFA) is a quintuple
M = (Q, Â, , q0, F) where:
Q, Â, q0, F are as before
: Q X Â QX{L, R}
If (q, a) = (P, L) then in state q, scanning the input symbol a , the
2DFA enters state p and moves its head left one square.
If (q, a) = (P, R) then in state q, scanning the input symbol a , the
2DFA enters state p and moves its head right one square.
x1 x2 ... x n-1 xn
Example