azfat2.ppt

19
Nondeterminism

Upload: paksmiler

Post on 02-Feb-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: azFAT2.ppt

Nondeterminism

Page 2: azFAT2.ppt

Example from last time

0

1

……

q

q

q

q

q

q

q

q

q

q

q

0

1

0

1

0

1

1

1

1

1

0

• Construct a DFA over alphabet {0, 1} that accepts those strings that end in 101

• Sketch of answer:

Page 3: azFAT2.ppt

Would be easier if…

• Suppose we could guess when the string we are reading has only 3 symbols left

• Then we could simply look for the sequence 101and accept if we see it

qdie

1 0 13 symbols left

This is not a DFA!

Page 4: azFAT2.ppt

Nondeterminism

• Nondeterminism is the ability to make guesses, which we can later verify

• Informal nondeterministic algorithm for strings that end in 101:

1. Guess if you are approaching end of input2. If guess is yes, look for 101 and accept if you

see it3. If guess is no, read one more symbol and go

to step 1

Page 5: azFAT2.ppt

Nondeterministic finite automaton

• This is a kind of automaton that allows you to make guesses

• Each state can have zero, one, or more transitions out labeled by the same symbol

1 0 1

0, 1

q q q q

Page 6: azFAT2.ppt

Semantics of guessing

1 0 1

0, 1

q q q q

• State q0 has two transitions labeled 1

• Upon reading 1, we have the choice of staying in q0 or moving to q1

Page 7: azFAT2.ppt

Semantics of guessing

1 0 1

0, 1

q q q q

• State q1 has no transition labeled 1

• Upon reading 1 in q1, we die; upon reading 0, we continue to q2

Page 8: azFAT2.ppt

Semantics of guessing

1 0 1

0, 1

q q q q

• State q3 has no transition going out

• Upon reading anything in q3, we die

Page 9: azFAT2.ppt

Meaning of automaton

1 0 1

0, 1

q q q q

Guess if you are 3 symbols away from

end of input

If so, guess you will see the

pattern 101

Check that you are at the end of input

Page 10: azFAT2.ppt

Example

• Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern 001

Page 11: azFAT2.ppt

Example

• Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern 001 somewhere

• Answer

0 0 1

0, 1

q q q q

0, 1

Page 12: azFAT2.ppt

Example

• Construct an NFA that accepts all strings with an even number of 0s or an odd number of 1s

r0 r1

1 10

0even number of 0s

s0 s1

0 01

1odd number of 1s

q0

-transitionscan be taken for free (without reading input)

Page 13: azFAT2.ppt

Formal definition

• A nondeterministic finite automaton (NFA) is a 5-tuple (Q, , , q0, F) where– Q is a finite set of states– is an alphabet– : Q × (∪) → subsets of Q is a transition

function

– q0 Q is the initial state

– F Q is a set of accepting states (or final states).

• Differences from DFA:– transition function can go into several states– It allows -transitions

Page 14: azFAT2.ppt

Example

alphabet = {0, 1}states Q = {q0, q1, q2}initial state q0

accepting states F = {q2}

state

s

inputs

0 1q0

q1

q2

{q1}

table oftransition function

{q0, q1}

q0

, 1 0

0q1 q2

{q1}{q2}

Page 15: azFAT2.ppt

Language of an NFA

The language of an NFA is the set of all strings for which there is some path that, starting from q0, leads to an accepting state as the string is read left to right (and -transitions are taken for free).• Example

, 00, 001, 101 are accepted, but 11, 0110 are not

q0

, 1 0

0q1 q2

Page 16: azFAT2.ppt

Examples

q0

, 1

0q1 q2

q0

, 1

0q1 q2

1

2

00

0

01

2

3

4

5

q0

, 1

0q1 q2

01

2 3

4

q0

, 1

0q1 q2

01

2

3

54

or

or

Page 17: azFAT2.ppt

Examples

q0

, 1

0q1 q2

15

001

0

q0

, 1

0q1 q2

01

4

q0

, 1

0q1 q2

01

2

3

4

1012

3

11

q0

, 1

0q1 q2

01

2

STOP

STOP

Page 18: azFAT2.ppt

NFAs are as powerful as DFAs

• Obviously, an NFA can do everything a DFA can do

• But can it do more?

Page 19: azFAT2.ppt

NFAs are as powerful as DFAs

• Obviously, an NFA can do everything a DFA can do

• But can it do more?

• Theorem

If a language L is accepted by some NFA, thenit is also accepted by some DFA.

NO!