MA/CSSE 474

33
MA/CSSE 474 Theory of Computation Nondeterminism NFSMs

description

MA/CSSE 474. Nondeterminism NFSMs. Theory of Computation. Your Questions?. HW1 solutions HW3 or HW4 problems Anything else. Previous class days' material Reading Assignments. More DFSM Examples. Examples: Programming FSMs. Cluster strings that share a “future”. - PowerPoint PPT Presentation

Transcript of MA/CSSE 474

Page 1: MA/CSSE 474

MA/CSSE 474

Theory of Computation

Nondeterminism

NFSMs

Page 2: MA/CSSE 474

Your Questions?• Previous class days'

material• Reading Assignments

• HW1 solutions• HW3 or HW4 problems• Anything else

Page 3: MA/CSSE 474

MORE DFSM EXAMPLES

Page 4: MA/CSSE 474

Examples: Programming FSMs

Cluster strings that share a “future”.

L = {w {a, b}* : w contains an even number of a’s and an odd number of b’s}

Page 5: MA/CSSE 474

Vowels in Alphabetical Order

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

occur in w in alphabetical order}.

u

THIS EXAMPLE MAY BE facetious!

O

Page 6: MA/CSSE 474

Negate the condition, then …

L = {w {a, b}* : w does not contain the substring aab}.

Start with a machine for the complement of L:

How must it be changed?

Page 7: MA/CSSE 474

The Missing Letter Language

Let = {a, b, c, d}.

Let LMissing = {w * : there is a symbol ai that does not

appear in w}.

Try to make a DFSM for LMissing:

Expressed in first-order logic, LMissing = {w* : a∃ ( x,y∀ *(w ≠ xay))}

Page 8: MA/CSSE 474

NONDETERMINISM

Page 9: MA/CSSE 474

Nondeterminism

• A nondeterministic machine in a given state, looking at a given symbol (and with a given symbol on top of the stack if it is a PDA), may have a choice of several possible moves that it can make.

• If there is a move that leads toward acceptance, it makes that move.

Page 10: MA/CSSE 474

• As you saw in the homework, a PFA is a FSM plus a stack.

• Given a string in {a, b}*, is it in PalEven = {wwR : w {a,b}*}}?

• PDA

• Choice: Continue pushing, or start popping?

• This language can be accepted by a nondeterministic PDA but not by any deterministic one.

Necessary Nondeterminism?

Page 11: MA/CSSE 474

Nondeterministic value-added?

• Ability to recognize additional languages?– FSM: no– PDA : yes– TM: no

• Ease of designing a machine for a particular language– Yes in all cases

We will prove these later

Page 12: MA/CSSE 474

1. choose (action 1;;action 2;

…action n )

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

A Way to Think About Nondeterministic Computation

First case: Each action will return True, return False, or run forever.

If any of the actions returns TRUE, choose returns TRUE.If all of the actions return FALSE, choose returns FALSE.If none of the actions return TRUE, and some do not halt, choose does not halt.

Second case: S may be finite, or infinite with a generator (enumerator).

If P returns TRUE on some x, so does chooseIf it can be determined that P(x) is FALSE for all x in P, choose returns FALSE.Otherwise, choose fails to halt.

Page 13: MA/CSSE 474

NONDETERMINISTIC FSM

Page 14: MA/CSSE 474

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 ( {})) K

Another way to present it: : (K ( {})) 2K

Page 15: MA/CSSE 474

Acceptance by an NDFSM

An NDFSM M accepts a string w iff there exists some path along which w can take M to some element of A.

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

Page 16: MA/CSSE 474

Sources of Nondeterminism

Page 17: MA/CSSE 474

Optional Substrings

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

Page 18: MA/CSSE 474

Multiple Sublanguages

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

Page 19: MA/CSSE 474

The Missing Letter LanguageLet = {a, b, c, d}. Let LMissing = {w : there is a

symbol ai that does not appear in w}

Page 20: MA/CSSE 474

Pattern Matching

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

A DFSM:

An NDFSM:

Page 21: MA/CSSE 474

Pattern Matching: Multiple Keywords

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

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

Page 22: MA/CSSE 474

Checking from the End

L = {w {a, b}* : the fourth character from the end is a}

Page 23: MA/CSSE 474

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}

Page 24: MA/CSSE 474

Another NDFSM

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

M1 =

M2=

M3=

This is a good example for practice later

Page 25: MA/CSSE 474

Analyzing Nondeterministic FSMs

Does this FSM accept:

baaba

Remember: we just have to find one accepting path.

Page 26: MA/CSSE 474

Two approaches:

• Explore a search tree:

• Follow all paths in parallel

Simulating Nondeterministic FSMs

Page 27: MA/CSSE 474

Dealing with Transitions

The epsilon closure of a state: 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) }.

Algorithm for computing 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.

Page 28: MA/CSSE 474

Calculate eps(q) for each state q

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

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

do:result = result

{ r } Return result.

Page 29: MA/CSSE 474

Simulating a NDFSMndfsmsimulate(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.

Do it for the previous 8-state machine, with input bbabc

Page 30: MA/CSSE 474

Nondeterministic and Deterministic FSMs

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

More interesting:

Theorem:

For each NDFSM, there is an equivalent DFSM.

"equivalent" means "accepts the same language"

Page 31: MA/CSSE 474

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}

More precisely, each state in K' is the string that encodes a subset of K, using the standard set notation. Example: "{q0, q2}" We omit the quotation marks.

Think of the simulator as the "interpreted version" and this as the "compiled version".

Page 32: MA/CSSE 474

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

Page 33: MA/CSSE 474

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

Draw part of the transition diagram for the DFSM constructed from the NDFSM that appeared a few slides earlier.

Later we may prove that this works.