0% found this document useful (0 votes)
30 views52 pages

Lecture 3 & 4 - Chapter 2

Uploaded by

wondimelese2012
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)
30 views52 pages

Lecture 3 & 4 - Chapter 2

Uploaded by

wondimelese2012
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/ 52

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 .

T(q0, a) = q1 => a finite automata changes its state


from q0 to q1 by reading a
Transition graph
 A labeled directed graph with designed node as a start node
and one or more final/accept nodes describes a finite
automata.
 Notations
Nodes (States) are represented by

Initial node/state is represented by

Final node(s) is/are denoted by

Transitions from state q to p is a


q p
denoted by
The labels on the edges are input symbols
Transition graph
 Example. Draw transition diagram for the following DFA
 M = ({q0, q1, q2}, {0, 1}, , q0, {q1}) where  is given as
 (q0, 0) = q0, (q0, 1) = q1, (q1, 1) = q1, (q1, 0) = q2, (q2, 0) =
q1, (q2, 1) = q1 0 1

0
1
q0 q1 q2
0, 1

 Exercise. Draw transition diagram for the following DFA


 M=({q0, q1, q2, q3 }, , {a, b}, q0, {q3}) where  is given as (q0, a)=
q1, (q0, b) =q2, (q1, a) = q1, (q2, b) = q2, (q1, a) = q3, (q2, b) =
q3
String Acceptance by DFA
 A DFA accepts a string x if the sequence of transitions
corresponding to the symbols of x leads from the start state to
an accepting/final state.
 The transition function has to be extended to determine a string
acceptance by DFA.
  * : Q X Â+ as defined as follows
 *(q, x) =  *((q, a), y) if x = ay, a e Â, y e Â+
 Note that:
 DFA must have a definite input symbol to move from one state to
another state
  is total function. If  is not defined for a terminal a then  (q, a) = D,
if D is dummy state
String Acceptance by DFA…
 Example: Consider the following DFA
M = ({q0, q1, q2}, {a, b}, , q0, {q1, q2} ) where (q0, a) = q1,
(q0, b) = q2, (q2, a) = q2, (q1, b) = q1, (q1, a) = q2
a) Draw transition diagram of M
b) Check if the following strings are accepted by M
i ) x = abbb ii) x = abba iii) x = bab
Answers
a
a) q q11 b
0

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:

 Exercise: Find a DFA for the following language on  = {a,


b}, L = {w: |w|a mod 3 = 0}
 Answer
Regular Language
 Definition: A language L is called regular if and only if there
exists some deterministic finite accepter m such that L =
L(M).
 Example1:
 Show that the language L = {awa: we{a, b}* } is regular.

 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

strings are accepted by M b a


i. baa q2 a
q3
ii. abba a
a
iii. aab
String acceptance by NFA…
 Answer
 i ) baa
*({q0}, baa) = *(({q0}, b), aa) = *(({q2},aa)
= *(({q2}, a), a) = (({q2, q3},a) =  (q2, a) U (q3, a)
= {q2, q3} U {q3} = {q2, q3}
But, {q2, q3} I {q3} = {q3} Î 0, Hence, baa is accepted
 iii ) aab
*({q0}, baa) = *(({q0}, a), ab) = *(({q1},aa)
= *(({q1}, a), b) = (({q1, q3},b) =  (q2, b) U (q3, b)
= {q1} U 0 = {q1}
But {q1}I {q3} = 0, Hence, aab is rejected
Equivalence of DFA and NFA
 Definition:
 Two finite accepters M1 and M2 are said to be equivalent if
L(M1) = L(M2), i.e. if they accept the same language.
 Theorem: L can be accepted by a DFA if and only if L can be
accepted by NFA.
 Two algorithms is seen. The first one is using direct proof and
the second one using subset construction
 Lets see the first one
 Proof
1. () L is accepted by DFA  L is accepted by NFA
2. (‹) L is accepted by NFA L is accepted by DFA
Equivalence of DFA and NFA…
1. () L is accepted by DFA  L is accepted by NFA
 Since DFA is a special kind of NFA, it can be concluded that
every language accepted by some DFA will also be accepted by
NFA.
 Given a DFA with a partial function, it is always possible to
convert it to a total function as follows

a a a
a
q0 q1 q0 q1

b b b
a, b
q2 q2
a, b D
Equivalence of DFA and NFA…

 Set (q, a) = D, dummy state, where (q, a) is undefined


 (D, a) = D, Aa eÂ

 Now assume that DFA M = (Q, Â, , q0, F) with total


transition function and construct NFA M’ = (Q, Â, ’, q0, F)
such that L(m) = L(m’)
 Define ’(q, x) = {(q, x) }
 Done!
Equivalence of DFA and NFA…
2. (‹) L is accepted by NFA L is accepted by DFA
 Let M = (Q, Â, , q0, F) be NFA, Construct DFA M’ such that L(M)
= L(M’)

 This can be done as follows


 Start with {q0} and calculate ({q0}, a), AaeÂ. That is, obtain all
possible states reachable from q0 , say k
 Calculate (k, a), AaeÂ, may give new states
 Continue this process until no new states are created.
 Thus M’ = (Q, Â, ’, q0’, F’) where
 Q’ z Q, q0’ = {q0}, F’ = K’z Q’ such that K’ I F Î0, ’ is the
extension of t
Equivalence of DFA and NFA…
 Example: Given the following NFA, construct its equivalent
DFA q0
a a
q1

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

 Conversion algorithm: subset construction


 The idea is that each DFA state corresponds to a set of NFA states.
 After reading input a1a2…an, the DFA is in a state that represents the
subset Q of the states of the NFA that are reachable from the start state.
 INPUT: An NFA N
 OUTPUT: A DFA D accepting the same language as N.
 ALGORITHM: …
Equivalence of DFA and NFA…
Dstates := -closure (q0) //and they all are unmarked
While (there is an unmarked state Q in Dstates ) do
begin
mark Q;
for (each input symbol a) do
begin
U :=  -closure ( move(Q,a) );
if (U is not in Dstates ) then
add U as an unmarked state to Dstates;
Dtran [Q, a] := U;
end
end
Equivalence of DFA and NFA…
Example NFA to DFA

 The start state A of the equivalent DFA is -closure(0),


 A = {0,1,2,4,7},
 Since these are exactly the states reachable from state 0 via a
path all of whose edges have label .
 Note that a path can have zero edges, so state 0 is reachable from
itself by an  -labeled path.
Equivalence of DFA and NFA…
 The input alphabet is {a, b). Thus, our first step is to mark A and
compute
Dtran[A, a] =  -closure(move(A, a)) and
Dtran[A, b] =  - closure(move(A, b)) .
 Among the states 0, 1, 2, 4, and 7, only 2 and 7 have transitions
on a, to 3 and 8, respectively.
 Thus, move(A, a) = {3,8). Also,  -closure({3,8} )= {1,2,3,4,6,7,8),
so we call this set B,

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…

 Unreachable states, like some of those introduced by the subset


construction, can obviously be eliminated
 Even some of the reachable states may be redundant…
DFA Minimization…
Example a

a a
q0 q1 q2

b b
b

q3 q4

a,b a,b

 In both q3 and q4, the machine rejects, no matter what


the rest of the input string contains
 They're equivalent and can be combined…
DFA Minimization…
a
Example (cont’d)
a a
q0 q1 q2

b b
b

a,b

 In both q1 and q2, the machine accepts if and only if the


rest of the string consists of 0 or more as
 They're equivalent and can be combined…
DFA Minimization…
a
Example (cont’d)
a
q0

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…

1. Initially partition consists of the two groups


 non-final states {A, B, C, D},
 final state{E}
2. To Construct IInew , group {E} cannot be split
3. group {A, B, C, D} can be split into {A, B, C}{D}, and IInew for this
round is {A, B, C}{D}{E}.
DFA Minimization…
 In the next round, split {A, B, C} into {A, C}{B}, since A and C
each go to a member of {A, B, C) on input b, while B goes to a
member of another group, {D}. Thus, after the second round, new
= {A, C} {B} {D} {E).
 For the third round, we cannot split the one remaining group with
more than one state, since A and C each go to the same state (and
therefore to the same group) on each input.  final = {A,
C}{B){D){E). The minimum-state of the given DFA has four states.
b a
a B
b
A a
a D
b b
E
DFA Minimization…
 Minimize states of the following DFA

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

 Input string x1x2…xn-1xn


 Special endmarker symbols frame the input 2DFA
 The head can't move past them
Two-Way Finite Automata…
 Difference with DFA
 Transition function returns a pair of values:
 The next state
 The direction (L or R) to move the head
 Computation ends, not at the end of the string, but when a
special state is entered
 One accept state: halt and accept when entered
 One reject state: halt and reject when entered
 Can these define more than the regular languages?

 There is a 2DFA for a language L if and only if L is regular.

 There is a 2NFA for a language L if and only if L is regular.


Two-Way Finite Automata- 2DFA Example
 Consider a 2DFA M that behaves as follows: starting in state
q0, M repeats a cycle of moves wherein the tape head moves
right until two ones have been encountered then left until
encountering a zero at which point state 0 q is reentered and
the cycle repeated. More precisely M has three states all of
which are final state.  is given as follows:
Finite-State Transducers
 Our DFAs and NFAs have a single bit of output: accept/reject
 For some applications we need more output than that
 Finite-state machines with string output are called finite-state
transducers
 A finite state transducer (FST) is a finite state machine with
two tapes:
 an input tape
 an output tape.
 This contrasts with an ordinary finite state automaton (or finite
state acceptor), which has a single tape.
Finite-State Transducers…

Example

 A transition labeled a,x works like a DFA transition labeled a,


but also outputs string x
 Transforms input strings into output strings
Finite-State Transducers…

Action For Input 10#11#


Finite-State Transducers…
 A Finite-State Transducer is a 7-tuple:
 A finite set of states: Q
 A finite set of input symbols: Σ
 A finite set of output symbols: Γ
 A finite set of initial states: I
 A finite set of final states: F
 A set of transition relations between states:
 δ subset Q x (Σuε) x (ΓU ε) x Q

 FSAs are a special case of FSTs


Finite-State Transducers…
 Finite-state transducers have signal-processing applications:
 Natural language processing
 Speech recognition
 Speech synthesis

 They come in many varieties


 deterministic / nondeterministic
 output on transition / output in state
 software / hardware

You might also like