ch05andfsm

48
Nondeterministic Finite State Machines Chapter 5

Upload: adviful

Post on 11-Jan-2016

215 views

Category:

Documents


2 download

DESCRIPTION

llll

TRANSCRIPT

Page 1: Ch05aNDFSM

Nondeterministic Finite State Machines

Chapter 5

Page 2: Ch05aNDFSM

Imagine adding to a programming language the function choice in either of the following forms:

1. choose (action 1;;action 2;;

…action n )

2. choose(x from S: P(x))

Nondeterminism

Page 3: Ch05aNDFSM

before the first choice choose makes

first call to choose first call to choose choice 1 choice 2

second call to choose second call to choose choice 1 choice 2

Implementing Nondeterminism

Page 4: Ch05aNDFSM

Nondeterminism

• What it means/implies– We could guess and our guesses would lead

us to the answer correctly (if there is an answer).

4

Page 5: Ch05aNDFSM

Definition of an NDFSM

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 ( {}) ) Kstate input or empty string state

5

Cartesian product

Where you are Where

you goWhat

you see

Page 6: Ch05aNDFSM

Accepting by an NDFSM

M accepts a string w iff there exists some path along which w drives M to some element of A.

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

6

Page 7: Ch05aNDFSM

Sources of Nondeterminism

7

What differ from determinism?

Page 8: Ch05aNDFSM

Two approaches:

• Explore a search tree:

• Follow all paths in parallel

Analyzing Nondeterministic FSMs

8

Page 9: Ch05aNDFSM

Optional Substrings

L = {w {a, b}* : w is made up of an optional a followed by aa followed by zero or more b’s}.

9

Page 10: Ch05aNDFSM

Optional Substrings

L = {w {a, b}* : w is made up of an optional a followed by aa followed by zero or more b’s}.

10

Page 11: Ch05aNDFSM

Optional Substrings

L = {w {a, b}* : w is made up of an optional a followed by aa followed by zero or more b’s}.

11

M = (K, , , s, A) = ({q0, q1, q2 , q3}, {a, b}, , q0, {q3}), where

= {((q0, a), q1), ((q0, ), q1),

((q1, a), q2), ((q2, a), q3),

((q3, b), q3)}

How many elements does have?

Page 12: Ch05aNDFSM

Multiple Sublanguages

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

12

Page 13: Ch05aNDFSM

Multiple Sublanguages

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

13

Page 14: Ch05aNDFSM

Multiple Sublanguages

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

14

Page 15: Ch05aNDFSM

Multiple Sublanguages

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

15

M = (K, , , s, A) = ({q0, q1, q2, q3, q4, q5, q6}, {a, b}, , q0, {q4, q5}), where

= {((q0, ), q1), ((q0, ), q5),

((q1, a), q2), ((q2, b), q3), ((q3, a), q4),

((q5, a), q6), ((q5, b), q6), ((q6, a), q5), ((q6, b), q5)}

Do you start to feel the power of nondeterminism?

Page 16: Ch05aNDFSM

The Missing Letter Language

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

16

Recall the complexity of DFSM!

Page 17: Ch05aNDFSM

The Missing Letter Language

17

Page 18: Ch05aNDFSM

Pattern Matching

L = {w {a, b, c}* : x, y {a, b, c}* (w = x abcabb y)}.

A DFSM:

18

Page 19: Ch05aNDFSM

Pattern Matching

L = {w {a, b, c}* : x, y {a, b, c}* (w = x abcabb y)}.

A DFSM:

An NDFSM:

19

Page 20: Ch05aNDFSM

Pattern Matching with NDFSMs

L = {w {a, b}* : x, y {a, b}* : w = x aabbb y or

w = x abbab y }

20

Page 21: Ch05aNDFSM

Multiple Keywords

L = {w {a, b}* : x, y {a, b}*

((w = x abbaa y) (w = x baba y))}.

21

Page 22: Ch05aNDFSM

Checking from the End

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

22

Page 23: Ch05aNDFSM

Checking from the End

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

23

Page 24: Ch05aNDFSM

Another Pattern Matching Example

L = {w {0, 1}* : w is the binary encoding of a positive integer that is divisible by 16 or is odd}

24

Page 25: Ch05aNDFSM

Another NDFSM

L1= {w {a, b}*: aa occurs in w}L2= {x {a, b}*: bb occurs in x}L3= {y : L1 or L2 }L4= L1L2

M1 = M2=

M3=

M4= 25

Page 26: Ch05aNDFSM

A “Real” Example

26

Page 27: Ch05aNDFSM

Analyzing Nondeterministic FSMs

Does this FSM accept:

baaba

Remember: we just have to find one accepting path. 27

Page 28: Ch05aNDFSM

Two approaches:

• Explore a search tree:

• Follow all paths in parallel

Analyzing Nondeterministic FSMs

28

Page 29: Ch05aNDFSM

Dealing with 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) }.

How shall we compute eps(q)?

It simply means the states

reachable without consuming input.

29

Page 30: Ch05aNDFSM

An Algorithm to Compute eps(q)

result = {q}. While there exists some p result and

some r result and some transition (p, , r) do:

Insert r into result. Return result.

eps(q: state) =

30

Page 31: Ch05aNDFSM

An Example of eps

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

Page 32: Ch05aNDFSM

Simulating a NDFSM

ndfsmsimulate(M: NDFSM, w: string) = 1. current-state = eps(s).2. While any input symbols in w remain to be read do:

1. c = get-next-symbol(w).2. next-state = .3. For each state q in current-state do:

For each state p such that (q, c, p) do: next-state = next-state eps(p).

4. current-state = next-state.3. If current-state contains any states in A, accept. Else

reject.

32

Page 33: Ch05aNDFSM

Nondeterministic and Deterministic FSMs

Clearly: {Languages accepted by a DFSM} {Languages accepted by a NDFSM}

More interestingly: Theorem:

For each NDFSM, there is an equivalent DFSM.

33

Page 34: Ch05aNDFSM

Nondeterministic and Deterministic FSMs

Theorem: For each NDFSM, there is an equivalent DFSM.Proof: By construction:

Given a NDFSM M = (K, , , s, A), we construct 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}

34

May create many unreachable states

Page 35: Ch05aNDFSM

An Algorithm for Constructing the Deterministic FSM

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 }.

35

The algorithm1.Proves NDFSM DFSM2.Allows us to solve problems using NDFSM then construct equivalent DFSM

Page 36: Ch05aNDFSM

The Algorithm ndfsmtodfsmndfsmtodfsm(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 }.

36

Page 37: Ch05aNDFSM

An Example – Optional Substrings

L = {w {a, b}* : w is made up of 0 to 2 a’s followed by zero or more b’s}.

37

b

Page 38: Ch05aNDFSM

Another Example

38

Page 39: Ch05aNDFSM

The Number of States May Grow Exponentially

No. of states after 0 chars: = 1No. of new states after 1 char: = n

No. of new states after 2 chars: = n(n-1)/2

No. of new states after 3 chars: = n(n-1)(n-2)/6

Total number of states after n chars: 2n

n

n

1

n

n

2

n

n

3

|| = n

39

Page 40: Ch05aNDFSM

Another Hard Example

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

40

Page 41: Ch05aNDFSM

If the Original FSM is Deterministic

1. Compute the eps(q)s:2. s' = eps(q0) = 3. Compute '

({q0}, odd, {q1}) ({q0}, even, {q0})({q1}, odd, {q1}) ({q1}, even, {q0})

4. K' = {{q0}, {q1}}5. A' = { {q1} }

M' = M

M

41

Page 42: Ch05aNDFSM

The Real Meaning of “Determinism”

Is M deterministic?

An FSM is deterministic, in the most general definition of determinism, if, for each input and state, there is at most one possible transition.

• DFSMs are always deterministic. Why?• NDFSMs can be deterministic (even with -transitions and implicit dead states), but the formalism allows nondeterminism, in general.• Determinism implies uniquely defined machine behavior.

Let M =

42

Page 43: Ch05aNDFSM

Deterministic FSMs as Algorithms

L = {w {a, b}* : w contains no more than one b}:

43

Page 44: Ch05aNDFSM

Deterministic FSMs as Algorithms

until accept or reject do:S: s = get-next-symbol

if s = end-of-file then acceptelse if s = a then go to Selse if s = b then go to T

T: s = get-next-symbolif s = end-of-file then acceptelse if s = a then go to Telse if s = b then reject

end 44

Page 45: Ch05aNDFSM

Deterministic FSMs as Algorithmsuntil accept or reject do:

S: s = get-next-symbolif s = end-of-file then acceptelse if s = a then go to Selse if s = b then go to T

T: s = get-next-symbolif s = end-of-file then acceptelse if s = a then go to Telse if s = b then reject

end

Length of Program: |K| (|| + 2)Time required to analyze string w: O(|w| ||)

We have to write new code for every new FSM. 45

Page 46: Ch05aNDFSM

A Deterministic FSM Interpreterdfsmsimulate(M: DFSM, w: string) = 1. st = s. 2. Repeat

2.1 c = get-next-symbol(w). 2.2 If c end-of-file then

2.2.1 st = (st, c). until c = end-of-file.

3. If st A then accept else reject.

Input: aabaa 46

Page 47: Ch05aNDFSM

Nondeterministic FSMs as Algorithms

Real computers are deterministic, so we have three choices if we want to execute a NDFSM:

1. Convert the NDFSM to a deterministic one: • Conversion can take time and space 2|K|. • Time to analyze string w: O(|w|) 2. Simulate the behavior of the nondeterministic one by constructing sets of states "on the fly" during execution • No conversion cost • Time to analyze string w: O(|w| |K|2)

3. Do a depth-first search of all paths through the nondeterministic machine. 47

Page 48: Ch05aNDFSM

A NDFSM Interpreter ndfsmsimulate(M = (K, , , s, A): NDFSM, w: string) = 1. Declare the set st. 2. Declare the set st1. 3. st = eps(s). 4. Repeat

4.1 c = get-next-symbol(w). 4.2 If c end-of-file then do

4.2.1 st1 = .4.2.2 For all q st do

4.2.2.1 For all r (q, c) do 4.2.2.1.1 st1 = st1 eps(r).

4.2.3 st = st1.4.2.4 If st = then exit.

until c = end-of-file. 6. If st A then accept else reject. 48