FLAT - Ch-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Chapter 2

Finite Automata

1
A Finite-State Machine
• Finite automata – is a machine which takes and reads an
input string one by one and then after the input is
completely read, it decides to whether the string is
accepted or not accepted.
• The automaton consists of states and transitions.
• As the automaton sees a symbol of input it makes a
transition or jump to another state according to its
transition function.
• A finite state machine is a mathematical model of a
system, with discrete inputs and outputs.

2
A Finite-State Machine…
• Finite Automata FA:
• a finite set of states
• a set of transitions (edges)
• a start state
• a set of final states
Defining a FA is a kind of programming.
• Problem definition
• Includes defining possible actions & accepting condition.
• States  structure of program
• Includes designating which are initial & final.
• Transitions  program

3
Types Finite Automata

Finite Automata : A recognizer that takes an input string &


determines whether it’s a valid sentence
of the language.

Deterministic : Has at most one action for every given


input symbol.

Non-Deterministic : Has more than one (or no) alternative


action for the same input symbol.

4
Deterministic Finite Accepters (dfa) Initial state q0
Input file
 1  2 3  4 5  6  7  8  9 …
Definition
A deterministic finite accepter or dfa Control unit
(transition function δ)
is defined by the quintuple
M = (Q, ,  , q0 , F ) δ(q0 ,σ1 ) = q1
where State q1
Q is a finite set of internal states, Input file
 is a finite set of symbols called the input  1  2 3  4  5  6  7  8  9 …

alphabet,
 : Q   → Q is a function called Control unit
(transition function δ)
th e transition function,
q 0  Q is the initial state,
F  Q is a set of final states. 5
Initial state q0
Input file
 1  2 3  4 5  6  7  8  9 … State qn −1
Input file
 1  2 3  4  5  6  7  8  9 … n
Control unit
(transition function δ)
Control unit
(transition function δ)
δ(q0 ,σ1 ) = q1
State q1
If the end of string is reached and
Input file
δ(q n −1,σ n ) = qn is a final state,
 1  2 3  4  5  6  7  8  9 …
then the string is accepted.
Otherwise, the string is rejected.
Control unit
(transition function δ)

6
Deterministic Finite Automata…

• DFA M
• Q = {q0, q1}
•  = {a, b}
• F = {q1}
• The transition function  is given in a tabular form called the
transition table.
• (q0, a) = q1 (q0, b) = q0
• (q1, a) = q1 (q1, b) = q0

7
Deterministic Finite Automata…
• A DFA M can be considered to be a language acceptor.

• The language of M, L(M), is the set of strings *


accepted by M.

• A DFA M reads an input string from left to right.

• The next state depends on the current state and the


unread (unprocessed) symbol.

8
Deterministic Finite Automata…
• The DFA M accepts the set of strings over {a, b} that contain the
substring bb.
• M : Q = {q0, q1, q2},  = {a, b}, F = {q2}
• The transition function  is given in a tabular form called the
transition table.
• (q0, a) = q0 (q0, b) = q1
• (q1, a) = q0 (q1, b) = q2
• (q2, a) = q2 (q2, b) = q2
• Is abba  L(M)? Yes, since the computation halts in state q2, which
is a final state.
• Is abab  L(M)? No, since the computation halts in state q1, which
is NOT a final state.

9
State Diagrams and Examples

• The state diagram of a DFA M = (Q, , ,q0, F) is a


labeled graph G defined by the following conditions:
• The nodes of G are the elements of Q
• The labels on the arcs of G are elements of 
a
• q0 is the start node, denoted by: q0

• F is the set of accepting nodes, denoted by:

• There is an arc from node qi to qj labeled a if (qi, a) = qj


• For every node qi and symbol a  , there is exactly one arc
labeled a leaving qi
10
State Diagrams and Examples…

• Deterministic Finite Automata DFA


• all outgoing edges are labelled with an input character
• no state has e- transition, transition on input e
• no two edges leaving a given state have the same label
• for each state s and input symbol a, there is at most one edge label a
leaving s.
• Therefore: the next state can be determined uniquely, given
the current state and the current input character.

11
• Transition graphs
Deterministic finite accepter
M = (Q,, δ, q0 , F)
Transition
0 0 1 function: δ(q1 ,1) =q 2
q0 q1 q2 0
1
1
Initial vertex Final vertex

Example
The above transition graph represents the dfa

M = ({q0 , q1 , q2 }, {0,1},δ , q0 ,{q1}),


whereδ is given by It accepts 01, 101,0111,11001,….
δ (q0 ,0) = q0, δ (q0 ,1) = q1, But not 00, 100,1100,….
δ (q1 ,0) = q0, δ (q1 ,1) = q2,
12
δ (q2 ,0) = q2, δ (q2 ,0) = q1,
State Diagrams and Examples…

Definition
The language accepted by
a dfa M = (Q,,  , q0 F ) is
the set of all string on Σ
accepted by M. In formal notation,
L(M) = {w  * :  * (q0 , w)  F}.

Let w = a1a2  an .
If  (q1 , a1 ) = q2 ,  (q2 , a2 ) = q3 ,,  (qn , an ) = qn +1 ,
then we write δ * (q1,w) = qn +1.
Especially,δ * (q,) = q.

13
Example: Consider the dfa in the following transition graph.

q0 q1 q2 a,b
b a,b
Solution: Trap state

L = {a nb: n  0}
Example: Find a dfa that recognizes the set of all strings on
 = {a, b} starting with the prefix ab.

Solution:

a q1 b q2
q0 a,b
b Trap state

q3 a,b 14
State Diagrams and Examples…

• strings over {a,b} with at least 3 a’s

b b b 

a a a
q0 q1 q2 q3

15
State Diagrams and Examples….

• strings over {a,b} without 3 consecutive a’s

b 
b
a a a q3
q0 q1 q2

16
State Diagrams and Examples…
• Draw a state diagram for DFA M that accepts the set of
strings over {a, b} that contain the substring bb

a a, b

b b
q0 q1 q2

• The string ababb is accepted since the halting state is the


accepting state q2

17
State Diagrams and Examples…

• The DFA
b 

a a
q0 q1 q2

b
• accepts (b|ab)*(a|e)
• the set of strings over {a, b} that do not contain the
substring aa

18
State Diagrams and Examples…

• Strings over {a, b} that contain the substring bb OR do


not contain the substring aa.
• This language is the union of the languages of the
previous examples. a

a a b
0a 1a 2a 2a
a
a b
b
b
b
1b 2b

 19
State Diagrams and Examples…

• Strings over {a, b} that contain an even number of a’s


AND an odd number of b’s

b
[ea, eb] [ea, ob]
b

a a a a

b
[oa, eb] [oa, ob]
b

20
State Diagrams and Examples…
• Let M be the DFA previous slide
• A DFA M’ that accepts all strings over {a, b} that do not contain an
even number of a’s AND an odd number of b’s is shown below
• L(M’) = {a, b}* - L(M) = * - L(M)
• Any string accepted by M is rejected by M’ and vice versa

b
[ea, eb] [ea, ob]
b

a a a a

b
[oa, eb] [oa, ob]
b
21
Definition: A language L is called regular if and only if there exists
some deterministic finite accepter M such that L = L(M).

Example b a
Show that language L={awa: w {a, b}* is regular.
a
q0 q2 a q3
b
b
a,b
Example q1
Let L be the language in Example above. Show that L2 = {aw1aaw2 a} is regular.
b a b a

a
q0 q2 a q3 a
q4 a q5
b b
b
a,b
q1 22
State Diagrams and Examples…
• The language {anbn, n0} is not regular, so we can not build a DFA
that accept this language.
• It needs an infinite number of states.

This DFA is NOT


• But {anbn, 1  n  3} is regular and its DFA is: Complete

a a a
q0 q1 q2 q3

b b b

q4 q5 q6
b b

23
State Diagrams and Examples…
• DFA M1 accepts (ab)*c
• M1 is incomplete determinism.
• The string abcc is rejected since M1 is unable to process the final c
from state q2
a
q0 q1
b
c

q2

M1

24
State Diagrams and Examples…

• M2 accepts the same language as M1 in previous example (ab)*c


• The state qe is the error state (dead end).

a
q0 q1
b
c a, c
b

q2 qe
a, b, c

M2

25
State Diagrams …Exercise

b
a
start a b b
0 1 2 3
a
b a

What Language is Accepted?

26
Deterministic Finite Automata…
• Extended transition function * of a DFA with transition
function  is a function from Q  * → Q defined
recursively on the length of the input string w.
• Basis: |w| = 0. Then w = e and *(qi, e) = qi
• Recursive step: Let |w| = 1. Then
• *(qi, av) = *((qi, a), v)
•  qi  Q,  a  ,  v  *

27
Deterministic Finite Automata…

• A string w is accepted if *(q0, w)  F.

• The language of a DFA M is


• L(M) = {w  * | *(q0, w)  F}

• DFA M = (Q, , , q0, F) accepts w  * 


• *(q0, w)  F

28
Deterministic Finite Automata...

• Two possibilities for DFA M running on w.


• M accepts w
• M accepts w iff the computation of M on w ends up (halts) in an
accepting configuration.
• *(q0, w)  F

• M rejects w
• M rejects w iff the computation of M on w ends up (halts) in a
rejecting configuration.
• *(q0, w)  F

29
Nondeterministic Finite Automata
NFA: Formal Definition
• NFA M = (Q, , , q0, F)

• Q = a finite set of states


•  = a finite set alphabet
•  = transition function
• total function Q   → (Q) = 2Q - power set of Q
• q0 = initial/starting state q0  Q
• F = final or accepting states FQ

30
Nondeterministic Finite Automata…

Q = { 0, 1, 2, 3 } a
start a b b
q0 = 0 0 1 2 3
F={3}
b
 = { a, b }
What Language is defined ?

input
What is the Transition Table ?
a b
s
t 0 { 0, 1 } {0}
a 1  {2}
t
e 2  {3}

31
Nondeterministic Finite Automata…

• Change in 
• For an DFA M, (q, a) results in one and only one state for all
states q and alphabet a.
• For an NFA M, (q, a) can result in a set of states, zero, one,
or more states:
qi
a
a
qn qi qn a qj qn

a
qk

(qn, a) = {qi} (qn, a) = {qi, qj , qk } (qn, a) = {} = 

32
Nondeterministic Finite Automata…

a,b a,b

a a b a
1 2 3 4 5

•Why is this only an NFA and not a DFA?

33
Nondeterministic Finite Automata…

Computing with NFA’s


• Computations are different
• We always start from start state. Call it the root of the
computation.
• Then we might go to different states on one symbol.
• Then from those states to new sets of states, creating a
tree-like computation.
• If one path ends up in a final state, then ACCEPT, else
REJECT.

34
Nondeterministic Finite Automata…
a
start a b b
0 1 2 3

b • Given an input string, we trace moves


• If no more input & in final state, ACCEPT
EXAMPLE:
Input: ababb

Path 1: 0 -> 0 -> 0 -> 0 -> 0 -> 0 (REJECT)


Path 2: 0 -> 0 -> 0 -> 1 -> 2 -> 3 (ACCEPT)

35
Nondeterministic Finite Automata…
• Extended transition function * of a NFA with transition
function  is a function from Q  * → 2Q (power set)
defined recursively on the length of the input string w.
• Basis: |w| = 0. Then w = e and *(qi, e) = {qi}
• Recursive step: Let |w| = 1. Then
• *(qi, av) = U*(qj, v), qj  (qi, a)
•  qi  Q,  qj  Q , a  ,  v *
• The language of a NFA M is
• L(M) = {w  * | *(q0, w)  F ≠ }
• The language consists of all strings w for which there is a walk
labeled w from the initial vertex of the transition graph to
some final vertex.

36
NFA…
• An NFA that accepts string over {a, b} with substring aa or bb.

a, b a, b

start a a
0 1 2

b a, b

3 b 4

• There are 2 distinct acceptance paths for the string abaaabb

37
Equivalence of DFA & NFA
• Is NFA more powerful than DFA? NO!
• NFA is inefficient to implement directly, so convert to a DFA
that recognizes the same strings.
• Is there a language accepted by an NFA that is not
accepted by any DFA? No
• There is an equivalent DFA for any NFA.
• Each state in DFA corresponds to a SET of states of the NFA.

• Two finite accepters M1 and M2 are said to be equivalent


if L(M1)=L(M2). That is, if they accept the same language.
Equivalence of DFA & NFA…

• The state diagram DFA M1 and NFA M2 accepts


(a|b)*bb(a|b)*

a a, b

M1 start b b
0 1 2

a, b a, b

start b b
M2 0 1 2

39
Equivalence of DFA & NFA…

• The state diagram DFA M1 and NFA M2 accepts


(a|b)*abba(a|b)*

b a a, b

M1 start a b b a
0 1 2 3 4
a
b

a, b a, b

M2 start a b b a
0 1 2 3 4

40
Equivalence of DFA & NFA…

• A DFA can be turned into an NFA that accepts the same


language.
• If δD(q, a) = p, let the NFA have δN(q, a) = {p}.
• Then the NFA is always in a set containing exactly one
state – the state the DFA is in after reading the same
input.

41
Equivalence of DFA & NFA…

• Surprisingly, for any NFA there is a DFA that accepts the


same language.
• Proof is the subset construction.
• The number of states of the DFA can be exponential in
the number of states of the NFA.
• Thus, NFA’s accept exactly the regular languages.

42
Subset Construction
• Given an NFA with states Q, inputs Σ, transition function
δN, start state q0, and final states F, construct equivalent
DFA with:
• States 2Q (Set of subsets of Q).
• Inputs Σ.
• Start state {q0}.
• Final states = all those with a member of F.

43
Subset Construction…

• The DFA states have names that are sets of NFA states.
• But as a DFA state, an expression like {p,q} must be read
as a single symbol, not as a set.
• Analogy: a class of objects whose values are sets of
objects of another class.

44
Subset construction…

• The transition function δD is defined by:


δD({q1,…,qk}, a) is the union over all i = 1,…,k of δN(qi, a).
• Example:
• Determine a deterministic Finite State Automaton from the
given Nondeterministic FSA.
M=
with the state table diagram for δ given below.

45
Subset construction…
• Let M′ = (Q′, Σ’, δ’, q’0, F′ ) be a DFA. Finite state
automaton (DFA),
where

• Let us now proceed to determine δ′ to be defined for


the DFA.

q0 ,q1

46
Convert nfa to dfa

Example a

q0 a q1  q2 nfa
b
 ({q0}, a} = {q1 , q2 }
 ({q0 }, b} = 
 ({q1, q2}, a} = {q1 , q2}
 ({q1, q2 }, b} = {q0 }
 ( , a} = 
 ( , b} =  a
b
{q0 } {q1 , q2 } dfa
a
b
 a, b
0
Example q1 0,1 q2
q0 1 nfa

 ({q0 },0} = 
 ({q0 },1} = {q1}
 ({q1},0} = {q0, q2 }
 ({q1},1} = {q2 }
 ({q0 , q2 },0} = 
 ({q0 , q2 },1} = {q1}
 ({q2 },0} = 
 ({q2 },1} = 

{q0 } 1 {q1}
0 {q0 , q2 }
1 dfa
1
0 {q2 }
0,1 0
0,1

Conversion from NFA to DFA
Procedure to convert nfa to dfa
1. Create a graph G(D) with vertex {q0}. Identify this vertex as the initial
vertex.
2. Repeat the following steps until no more edges are missing.
take any vertex{qi,qj,…,qk} of G(D) that has no outgoing edge for some
a  . Compute  N* (qi , a), N* (q j , a),...,  N* (qk , a).
If  N* (qi , a)   N* (q j , a)  ...   N* (qk , a) = {ql, qm,..., qn},

create a vertex for G(D) labeled {ql,qm,…,qn} if it is not already exist. Add
to G(D) an edge from {qi,qj,…,qk} to {ql,qm,…,qn} and label it with a.
3. Every state of D(D) whose label contains any q f  FN is identified as a final
vertex.
4. If M N accepts  , the vertex {q0} in G(D) is also made a final vertex.
Reading (Self Study)
• NFA with e-Transitions
• Reduction of Number of States in Finite Automata
• Two way Finite Automata

50

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