0% found this document useful (0 votes)
106 views60 pages

Understanding Finite State Machines

Uploaded by

nida61325
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)
106 views60 pages

Understanding Finite State Machines

Uploaded by

nida61325
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

PART 1:

Automata:
Finite State Machines (Finite Automata)

Formal Language:
Regular Languages
Non-regular Languages

Grammar:
Regular Expressions
Regular Grammars

CS612 1
Finite State Machines
(Finite Automata)

CS612 2
Languages, Grammars & Automata

CS612 3
Languages, Grammars & Automata

CS612 4
Languages, Grammars
SD
& Automata
D

Context-sensitive

Context-free

DCF

Regular
FSMs

DPDAs

NDPDAs

LBAs

CS612 5
Turing Machines
Regular Languages

Generates Regular
Language

Regular Expression Accepts

Finite State
CS612 Machine 6
FSM (Finite Automaton)

• A computational model as an idealized


small computer with limited memory
– finite and small amount of memory!

• The simplest model of computation!

CS612 7
Deterministic FSM (DFSM or DFA)

CS612 8
Definition of a Deterministic FSM

A DFSM (DFA) M = (K, , , s, A) where:

K is a finite set of states

 is an alphabet of symbols

s  K is the initial (starting) state

A  K is the set of accepting states, and

 is the transition function from (K  ) to K


CS612 9
Example: DFSM

K=
 =
sK=
AK=

 = Transition Diagram!

CS612 10
Configurations of DFSMs

A configuration of a DFSM M is an element of:


K  *

It captures the two things that can make a


difference to M’s future behavior:

• its current state


• the input that is still left to read.

The initial configuration of a DFSM M, on input w,


is: (sM, w)
CS612 11
The Yields Relations

The yields-in-one-step relation |-M:

(q, w) |-M (q', w') iff


• w = a w' for some symbol a  , and
•  (q, a) = q'

|-M * is the reflexive, transitive closure of |-M.

CS612 12
Computations Using DFSMs

A computation by M is a finite sequence of


configurations C0, C1, …, Cn for some n  0
such that:

• C0 is an initial configuration,

• Cn is of the form (q, ), for some state q  K,

• C0 |-M C1 |-M C2 |-M … |-M Cn.

CS612 13
Accepting and Rejecting

A DFSM M accepts a string w iff:

(s, w) |-M * (q, ), for some q  A.

A DFSM M rejects a string w iff:

(s, w) |-M* (q, ), for some q  A.

The language accepted by DFSM M, denoted


L(M), is the set of all strings accepted by M.

CS612 14
Examples and Designing DFSMs

CS612 15
Example 5.2

L = {w  {a, b}* : every a is immediately


followed by a b}.
DFSM?

CS612 16
Example 5.3

L = {w  {a, b}* : every a region in w is of even


length}

DFSM?

CS612 17
Example 5.4

L = {w  {0, 1}* : w has odd parity - odd # of 1’s}.


DFSM?

CS612 18
Example 5.5
L = {w  {a, b}* : w contains no more than one b}.
DFSM?

CS612 19
Example 5.6

L = {w  {a, b}* : no two consecutive characters


are the same}.
DFSM?

CS612 20
Example 5.7

L = FLOAT = {w: w is the string representation


of a floating point number}.

+3.0, 3.0, 0.3E1, 0.3E+1, -0.3E+1, -3E8

DFSM?

CS612 21
Example 5.9

L = {w  {a, b}* : w contains an even number


of a’s and an odd number of b’s}

DFSM?

CS612 22
Example 5.10

L = {w  {a - z}* : all five vowels, a, e, i, o,


and u, occur in w in alphabetical order}.
DFSM?

CS612 23
Example 5.11

L = {w  {a, b}* : w does not contain the


substring aab}.

DFSM?

DFSM for L:

CS612 24
Example 5.12

 = {a, b, c, d}.
LMissing = {w : there is a symbol ai   not
appearing in w}.

DFSM?

CS612 25
More Examples

L = {} = 

DFSM?

L= {}

DFSM?

CS612 26
DFSMs Halt

Theorem 5.1 Every DFSM M, on any input s


of finite length, halts in |s| steps.

CS612 27
Regular Languages

A language is regular iff it is accepted


by some DFSM.

RL = DFSM

CS612 28
Nondeterministic FSM
(NDFSM or NDFA or NFA)

CS612 29
Determinism and Nondeterminism

• Deterministic computation on a
deterministic machine

• Nonderetministic computation on a
nondeterministic machines

CS612 30
Determinism of DFSM

 is the transition function from (K  ) to K

• Only one unique next state!


• No choices!
• No randomness!

CS612 31
Sources of Nondeterminism

• Multiple edges
• Epsilon edges

CS612 32
Nondeterminism of NDFSM

 is the transition relation from (K  ) to K

• The next state is chosen at random!


• All next states are chosen in parallel and
pursued simultaneously!

CS612 33
Definition of an NDFSM

A NDFSM (NDFA) M = (K, , , s, A) where:


K is a finite set of states

 is an alphabet

s  K is the initial state

A  K is the set of accepting states, and

 is the transition relation. It is a finite subset of


(K  (  {}))  K
CS612 34
Example: NDFSM

K=
 =
sK=
AK=
=

CS612 35
Accepting by an NDFSM

• M accepts a string w iff at least one of its


computations accepts, i.e., there exists
some path along which w drives M to some
element of A.

• M rejects a string w iff none of its


computations accepts.

The language accepted by NDFSM M,


denoted L(M), is the set of all strings accepted
by M.
Analyzing Nondeterministic FSMs

Two approaches:

 Explore a search tree:

 Follow all paths in parallel!

CS612 37
Example 5.13

L = {w  {a, b}* : w is made up of an optional


a followed by aa followed by zero or more
b’s}.
NDFSM?

CS612 38
Example 5.14

L = {w  {a, b}* : w = aba or |w| is even}.


NDFSM?

CS612 39
Example 5.15

 = {a, b, c, d}.
LMissing = {w : there is a symbol ai   not
appearing in w}

NDFSM?

CS612 40
Example 5.16

L = {w  {a, b, c}* : x, y  {a, b, c}* (w = x


abcabb y)}.
NDFSM?

CS612 41
Example 5.16

L = {w  {a, b, c}* : x, y  {a, b, c}* (w = x


abcabb y)}.
DFSM?

CS612 42
Example 5.17

L = {w  {a, b}* : x, y  {a, b}* ((w = x abbaa y)


 (w = x baba y))}.
NDFSM?

CS612 43
Example 5.18

L = {w  {a, b}* : the fourth to the last character


is a}

NDFSM?

CS612 44
Analyzing Nondeterministic FSMs

Does this NDFSM accept baaba ?

CS612 45
Handling -Transitions via -Closure

 -closure:
• eps(q) = The set of states that are
reachable from q by following 0 or more –
transitions.

• eps(q) = { p  K : (q, w) |-*M (p, w) }

• eps(q) is the closure of {q} under the


relation {(p, r) : there is a transition (p, , r)
 }.

CS612 46
An Algorithm to Compute eps(q)

eps(q: state) =

result = {q};
while
there exists some p  result and
some r  result and some transition (p, , r)  
do
Insert r into result;
return result;

CS612 47
Example 5.19

NDFSM:

eps(q0) =
eps(q1) =
eps(q2) =
eps(q3) =

CS612 48
Equivalence of NDFSMs and
DFSMs

NDFSM = DFSM

RL = DFSM = NDFSM

CS612 49
Equivalence of NDFSMs and
DFSMs
Theorem 5.2 If there is a DFSM for L, there is
an NDFSM for L.

CS612 50
Equivalence of NDFSMs and
DFSMs
Theorem 5.3 If there is an NDFSM for L,
there is a DFSM for L.

Proof Idea: Proof by Construction

The Subset Construction


Given a NDFSM M = (K, , , s, A), we construct DFSM M' = (K', , ', s', A'),
where
K' = P(K)
s' = eps(s)
A' = {Q  K' : Q  A  }
'(Q, a) = {eps(p): p  K and (q, a, p)   for some q  Q}
CS612 51
An Algorithm for Constructing the
DFSM from NDFSM
1. Compute the eps(q)’s.

2. Compute s' = eps(s).

3. Compute ‘.

4. Compute K' = a subset of P(K).

5. Compute A' = {Q  K' : Q  A  }.

CS612 52
The Algorithm ndfsmtodfsm

ndfsmtodfsm(M: NDFSM) =
1. For each state q in KM do:
1.1 Compute eps(q).
2. s' = eps(s)
3. Compute ':
3.1 active-states = {s'}.
3.2 ' = .
3.3 While there exists some element Q of active-states for
which ' has not yet been computed do:
For each character c in M do:
new-state = .
For each state q in Q do:
For each state p such that (q, c, p)   do:
new-state = new-state  eps(p).
Add the transition (Q, c, new-state) to '.
If new-state  active-states then insert it.
4. K' = active-states.
5. A' = {Q  K' : Q  A   }.
CS612 53
Example 5.20

NDFSM:

DFSM?

CS612 54
Example 5.20

NDFSM:

1. Compute the eps(q)’s.


2. Compute s' = eps(s).
3. Compute ‘.
4. Compute K' = a subset of P(K).
5. Compute A' = {Q  K' : Q  A  }.

CS612 55
Example 5.20

DFSM:

CS612 56
State Minimization

Step (1): Get rid of unreachable states.

State 3 is unreachable.

Step (2): Get rid of redundant states.

States 2 and 3 are redundant.


CS612 57
Minimizing DFSM

A DFSM M is minimal iff there is no other DFSM M’ st


L(M) = L(M’) and M’ as fewer states than M does.

• Given any regular language L, there exists a


minimal DFSM M that accepts L.

• M is unique up to the naming of its states.

• Given any DFSM M, there exists an algorithm


minDFSM that constructs a minimal DFSM that also
accepts L(M).

CS612 58
Reading Assignment
Chapter 5:

Sections
5.1
5.2
5.3
5.4

CS612 59
In-Class Exercises
Chapter 5:

2- f & g
3
4
5
6-b
7
9-a

CS612 60

You might also like