22
1/29/02 CSE460 - MSU 1 Nondeterminism -NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan State University

Nondeterminism-NFA

  • Upload
    allene

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan State University. Nondeterminism-NFA. Introduction. An NFA is a more general FA Easier to construct  More useful in proving theorems - PowerPoint PPT Presentation

Citation preview

Page 1: Nondeterminism-NFA

1/29/02 CSE460 - MSU 1

Nondeterminism-NFASection 4.1 of Martin Textbook

CSE460 – Computability & Formal Language Theory

Comp. Science & Engineering

Michigan State University

Page 2: Nondeterminism-NFA

1/29/02 CSE460 - MSU 2

Introduction

An NFA is a more general FA Easier to construct More useful in proving theorems

In general less states Less cumbersome,more readable Relax some FA rules

Maps a symbol and a state to a set of 0, 1 or more next states NFA can be in several states at once vs. FA in exactly 1 state

NFAs, as a whole, accept same languages as FAs: Regular No more powerful than FAs

Page 3: Nondeterminism-NFA

1/29/02 CSE460 - MSU 3

Definition of FA - Reminder

Definition A finite automaton is a 5-tuple M=(Q,,q0,,A) Q is a finite set of states is a finite alphabet of input symbols q0 Q is the initial state A Q is the set of accepting states : Q x Q is the transition function, which maps every

state and input symbol to a next state.

A string x is accepted by an NFA, if after processing x, at least one of the active states is an accepting state.

Page 4: Nondeterminism-NFA

1/29/02 CSE460 - MSU 4

Outline

Example NFA Formal Definition of NFA

Extended Transition Function Exercise Equivalence of NFAs and FAs

Example Construction of Equivalent FA Theorem

Summary

Page 5: Nondeterminism-NFA

1/29/02 CSE460 - MSU 5

Example - NFA0,1

1 0q0 q1 q2

Language accepted by above NFA?

Set of strings that end with 10. L = (0+1)*10

1

1q0 q1 q2

0 0

0

1

Fig 3.3 (p.81)

Page 6: Nondeterminism-NFA

1/29/02 CSE460 - MSU 6

Example - Processing Input String

q0 q0 q0q0 q0 q0

q1

q2

q1 q1

q2

stuck

stuck

1 1 0 1 0

Input string: 11010

Adapted from J.E.Hopcroft 2001

Page 7: Nondeterminism-NFA

1/29/02 CSE460 - MSU 7

Formal Definition of NFA

Definition A nondeterministic finite automaton NFA is a 5-tuple M = (Q, , q0, A, ), where

Q is a finite set of states is a finite alphabet of input symbols q0 Q is the initial state A Q is the set of accepting states : Q x 2Q is the transition function, which maps every

state and symbol to a set (possibly empty) of next statesA string x is accepted by an NFA, if after processing x, at least

one of the active states is an accepting state.

Page 8: Nondeterminism-NFA

1/29/02 CSE460 - MSU 8

Extended Transition Function for NFAs

Definition The extended transition function is the function *: Q x * 2Q defined as follows.

For any q Q, *(q, ) = {q} For any y *, a , and q Q, let q1= *(q, y),

*(q, ya) = Upq1(p,a)

Union of sets (p,a) for each possible p in set *(q,y)

*(q,x) is the set of active states of NFA after starting in state q and processing the symbols of x.

Page 9: Nondeterminism-NFA

1/29/02 CSE460 - MSU 9

Example – Extended Transition Function

Input 11010, what is *(q0,11010)?

*(q0,) = {q0}

*(q0,1) = (q0,1) = {q0,q1}

*(q0,11) = (q0,1) U (q1,1) = {q0,q1} U = {q0,q1}

*(q0,110) = (q0,0) U (q1,0) = {q0} U {q2} = {q0,q2}

*(q0,1101) = (q0,1) U (q2,1) = {q0,q1}U ={q0,q1}

*(q0,11010) = (q0,0) U (q1,0) = {q0}U{q2} ={q0,q2} Is 11010 accepted by the NFA?

Page 10: Nondeterminism-NFA

1/29/02 CSE460 - MSU 10

Exercise 9.1.0

1

q0 q1 q2

0,10

00,1

0,1q3

(a) Why this finite automata is nondeterministic? (q0, 0) lead to three next states

(b) Does NFA accept a string as long as there is a path to accepting state? yes(c) What is the next state from state q0 on input 0? States: q0, q1, q3

(d) Give all possible paths for strings 01, 011, 00.For string 01 there are three paths: q0q0q0, q0q1q3, q0q3q3

(e) Is the string 000 accepted by the above NFA? Yes(f) Give all strings that are accepted by the above NFA. Those that end with 00

Page 11: Nondeterminism-NFA

1/29/02 CSE460 - MSU 11

Exercise 9.2.

a b

1 {1,2} {1}

2 {3} {3}

3 {4} {4}

4 {5} 5 {5}

(b) *(1,ab)?*(1,a) = {1,2}*(1,ab)= *(1,b) U *(2,b) = {1} U {3} = {1,3}

1 32 54

a,b

a a,ba,b

b

a

(a) Give the transition diagram

Page 12: Nondeterminism-NFA

1/29/02 CSE460 - MSU 12

Exercise 9.3.

0,1

0q0 q1

Draw the transition diagram of an NFA for the regular expression: (0+1)* 0 (0+1)*

0,1

Page 13: Nondeterminism-NFA

1/29/02 CSE460 - MSU 13

Example - Equivalent FA of NFA

Q1=2Q={,{q0},{q1},{q2},{q0,q1},{q0,q2},{q1,q2},{q0,q1,q2}}

Initial state: {q0} A1= {{q2}, {q0,q2}, {q1,q2}, {q0,q1,q2}} The transition function is defined as follows:

1(, 0)= 1(, 1) =

1({q0},0) = {q0} 1({q0},1) = {q0 ,q1}

1({q1},0) = {q2} 1({q1},1) =

1({q2},0) = 1({q2},1) =

1({q0,q1},0) = {q0,q2} 1({q0,q1},1) = {q0,q1}…

Page 14: Nondeterminism-NFA

1/31/02 CSE460 - MSU 14

Example - Transition Table

0 1

{q0} {q0} {q0,q1}

{q1} {q2}

{q2}

{q0,q1} {q0,q2} {q0,q1}

{q0,q2} {q0} {q0,q1}

{q1,q2} {q2}

{q0,q1,q2} {q0,q2} {q0,q1}

Page 15: Nondeterminism-NFA

1/31/02 CSE460 - MSU 15

Example - Equivalent DFA

1

1q0 q1 q2

0 0

0

1

1

1

{q0} {q0,q1} {q0,q2}

0 0

0

1

Compare to original DFA?

{q2}{q1}0

1 0,1 {q1,q2}

0

1

{q0,q1,q2}10

0,1

Page 16: Nondeterminism-NFA

1/31/02 CSE460 - MSU 16

Equivalence of NFAs and FAs

Theorem For any NFA M = (Q, , q0, A, ) accepting a language L, there is a deterministic FA M1=(Q1,, q1, A1,1) that also accepts L.

Proof idea: How would an FA simulate an NFA?

It needs to keep track of all branches by remembering all active states at given points in the input; add, remove states as NFA operates.

If the NFA has k states, there are 2k subsets of states that need to be considered.

Page 17: Nondeterminism-NFA

1/31/02 CSE460 - MSU 17

Proof – by Construction

For any NFA M=(Q, , q0, A, ), let’s construct an FA M1=(Q1, , q1, A1, 1): Set of states: Q1 = 2Q

Initial state: q1 = {q0}

Transition function: for q Q1, a: 1(q,a)=Upq(p,a)Union of the sets (p,a) for each possible p in q (subset of Q).

Set of accepting states: A1= {q Q1 | q A }

{q Q1 | q contains an accepting state of M}

To prove that FA M1 accepts the same language as NFA M, we need to prove that for any string x *, 1*(q1,x)=*(q0,x).

Page 18: Nondeterminism-NFA

1/31/02 CSE460 - MSU 18

Proof (cont.)

Basis step: 1*(q1, )= q1= {q0} = *(q0, )

Induction hypothesis: 1*(q1,x) = *(q0,x)

Statement to be shown: for any a , 1*(q1, xa) = *(q0, xa)

1*(q1,xa) = 1(1*(q1, x), a) by recursive def. of 1*() for FA

= 1(*(q0, x), a) by induction hypothesis

= Up*(q0, x) (p, a) by def. of 1() for equiv. FA

= *(q0, xa) by recursive def. of *() for NFA

Page 19: Nondeterminism-NFA

1/31/02 CSE460 - MSU 19

Exercise 9.4.

(a) Why does a single state in FA correspond to one or more states in NFA? To keep track of all states where NFA can be at a given point.

The initial state? {q0} (b) 1({q0}, 0) = ? {q0,q1} (c) An accepting state of FA must contain an

accepting state of the NFA. Why does it also allow non accepting states of NFA? If NFA ends up in an accepting state, then the string is accepted

even though other paths may lead to nonaccepting states.

Page 20: Nondeterminism-NFA

1/31/02 CSE460 - MSU 20

Exercise 9.5.

0{q0} {q0,q1}

1 0,1

0,1

0q0 q1

0,1

Q1=2Q={,{q0},{q1},{q0,q1}} Initial state: {q0} A1= {{q1}, {q0,q1}}

1(, 0) = 1(, 1) =

1({q0},0) = (q0,0) = {q0 ,q1} 1({q0},1) = (q0,1) = {q0}

1({q1},0) = (q1,0) = {q1} 1({q1},1) = (q1,1) = {q1}

1({q0,q1},0) = (q0,0) U (q1,0) = {q0,q1} U {q1} = {q0,q1}

1({q0,q1},1) = (q0,1) U (q1,1) = {q0} U {q1} = {q0,q1}

The transition diagram of the NFA is:

The transition diagram of the FA is then:

Page 21: Nondeterminism-NFA

1/31/02 CSE460 - MSU 21

Summary

NFAs are more general, and more practical than FAs Yet, NFAs recognize same class of languages as FAs

(regular languages) For every state and input symbol, the transition function

associates a set of states NFA can be in 1 or more active states at once NFA may get stuck (go to dead state)

A string x is accepted by an NFA, if after processing x, at least one of the active states is an accepting state

For every NFA, one can construct an equivalent FA, that recognizes the same language.

Page 22: Nondeterminism-NFA

1/31/02 CSE460 - MSU 22

What Next?

Extend NFA a little further NFA-: allow -transitions

Kleene’s Theorem