Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nondeterminism The Chinese University of Hong Kong Fall 2011

Similar presentations


Presentation on theme: "Nondeterminism The Chinese University of Hong Kong Fall 2011"— Presentation transcript:

1 Nondeterminism The Chinese University of Hong Kong Fall 2011
CSCI 3130: Formal languages and automata theory Nondeterminism Andrej Bogdanov

2 Example from last time Construct a DFA over alphabet {0, 1} that accepts those strings that end in 101

3 Would be easier if… Suppose we could guess when the string we are reading has only 3 symbols left 1 1 3 symbols left 1 qdie This is not a DFA!

4 Nondeterministic finite automaton
This is a machine that allows us to make guesses Each state can have zero, one, or more outgoing transitions labeled by the same symbol 0, 1 1 1 q0 q1 q2 q3

5 Choosing where to go State q0 has two transitions labeled 1
0, 1 1 1 q0 q1 q2 q3 State q0 has two transitions labeled 1 Upon reading 1, we have the choice of staying in q0 or moving to q1

6 The ability to make choices
0, 1 1 1 q0 q1 q2 q3 State q1 has no transition labeled 1 Upon reading 1 in q1, we die; upon reading 0, we continue to q2

7 The ability to make choices
0, 1 1 1 q0 q1 q2 q3 State q3 has no transition going out Upon reading 0 or 1 in q3, we die

8 Meaning of NFA Guess if you are 3 symbols away from end of input
0, 1 1 1 q0 q1 q2 q3 If so, guess you will see the pattern 101 Check that you are at the end of input

9 How to run an NFA input: 1 1 1 The NFA can have several active states
, 1 1 1 q0 q1 q2 q3 input: 1 1 1 The NFA can have several active states at the same time It accepts if at the end, one of its active states is final

10 Example Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern 001 somewhere , , should be accepted e, 000, should not

11 Example Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern 001 somewhere Answer 0, 1 0, 1 1 q0 q1 q2 q3

12 Definition A nondeterministic finite automaton (NFA) is a 5-tuple (Q, S, d, q0, F) where Q is a finite set of states S is an alphabet d: Q × (S ∪ {e}) → 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 d can go into several states It allows e-transitions

13 Language of an NFA The NFA accepts string x Î S* if there is some path that, starting from q0, leads to an accepting state as the string is read left to right. The language of an NFA is the set of all strings that the NFA accepts.

14 e-transitions These can be taken for free: accepts: 
q1 accepts: a, b, aab, bab, aabab, … q0 a b rejects: a q2 , aa, ba, bb, …

15 Example table of transition function d: alphabet S = {0, 1}
, 1 q0 q1 q2 table of transition function d: alphabet S = {0, 1} states Q = {q0, q1, q2} initial state q0 accepting states F = {q2} inputs 1 q0 Æ {q1} {q1} q1 {q0, q1} Æ {q2} states q2 Æ Æ Æ

16 Examples e 00 or or , 1  q0 q1 q2 , 1  q0 q1 q2 , 1  q0 q1 q2
, 1 1 e 2 q0 q1 q2 2 3 , 1 1 00 q0 q1 4 q2 2 , 1 1 4 or q0 q1 5 q2 3 , 1 1 3 or q0 q1 5 q2 2 4

17 Examples 001 101 11  , 1  q0 q1 q2 , 1  q0 q1 q2 , 1  q0 q1 q2
 , 1 1 4 001 5 q0 q1 q2 3 , 1 1 3 101 q0 q1 4 q2 2 , 1 1 11 q0 q1 q2 STOP , 1 1 q0 q1 2 q2 STOP

18 Can you guess the language of this NFA?
, 1 q0 q1 q2 Can you guess the language of this NFA?

19 Example of e-transitions
Construct an NFA that accepts all strings with an even number of 0s or an odd number of 1s r0 r1 1 even number of 0s q0 e-transitions s0 s1 1 odd number of 1s can be taken for free (without reading input)


Download ppt "Nondeterminism The Chinese University of Hong Kong Fall 2011"

Similar presentations


Ads by Google