180 likes | 357 Views
Fall 2010. The Chinese University of Hong Kong. CSC 3130: Formal languages and automata theory. Nondeterminism. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. Example. Construct a DFA over alphabet {0, 1} that accepts those strings that end in 101 Sketch of answer:. 0. 0.
E N D
Fall 2010 The Chinese University of Hong Kong CSC 3130: Formal languages and automata theory Nondeterminism Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Example • Construct a DFA over alphabet {0, 1} that accepts those strings that end in 101 • Sketch of answer: 0 0 q000 1 0 q00 1 q001 q0 1 … 0 q01 … qe 1 q101 q10 0 … 1 q1 … 1 q11 1 q111 1
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 1 0 1 3 symbols left 0 0 1 qdie This is not a DFA!
Nondeterminism • Nondeterminism is the ability to make guesses, which we can later verify • How a nondeterministic automaton for strings that end in 101 works: • Guess if you are approaching end of input • If guess is yes, look for 101 and accept if you see it • If guess is no, read one more symbol and go to step 1
Nondeterministic finite automaton • This is a kind of automaton that allows you to make guesses • Each state can have zero, one, or more outgoing transitions labeled by the same symbol 0, 1 1 0 1 q0 q1 q2 q3
The ability to make choices • State q0 has two transitions labeled 1 • Upon reading 1, we have the choice of staying in q0 or moving to q1 0, 1 1 0 1 q0 q1 q2 q3
The ability to make choices • State q1 has no transition labeled 1 • Upon reading 1 in q1, we die; upon reading 0, we continue to q2 0, 1 1 0 1 q0 q1 q2 q3
The ability to make choices • State q3 has no transition going out • Upon reading 0 or 1 in q3, we die 0, 1 1 0 1 q0 q1 q2 q3
Meaning of NFA Guess if you are 3 symbols away from end of input 0, 1 1 0 1 q0 q1 q2 q3 If so, guess you will see thepattern101 Check that you are at the end of input
How to run an NFA , 0 1 1 0 1 q0 q1 q2 q3 0 1 1 0 1 input: The NFA can have several active states at the same time
Example • Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern001
Example • Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern 001 somewhere • Answer 0, 1 0, 1 0 0 1 q0 q1 q2 q3
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
Language of an NFA • Example • e, 00, 001, 101are accepted, but 11, 0110are not 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 e-transitions are taken for free). 0 , 1 q0 q1 q2 0
Example 0 , 1 q0 q1 q2 0 table of transition function d: alphabet S= {0, 1} statesQ = {q0, q1, q2} initial stateq0 accepting statesF = {q2} inputs 0 1 q0 {q1} Æ {q1} q1 {q2} {q0, q1} Æ states q2 Æ Æ Æ
Examples 0 , 1 1 e 2 q0 q1 q2 0 2 3 0 , 1 1 00 4 q0 q1 q2 0 2 0 , 1 1 4 or 5 q0 q1 q2 0 3 0 , 1 1 3 or 5 q0 q1 q2 0 2 4
Examples 2 0 , 1 4 1 5 001 q0 q1 q2 0 3 0 , 1 1 3 4 q0 q1 q2 101 0 2 0 , 1 1 q0 q1 q2 11 0 STOP 0 , 1 1 2 q0 q1 q2 0 STOP
Example of e-transitions • Construct an NFA that accepts all strings with an even number of 0s or an odd number of 1s 1 1 0 even number of 0s r1 r0 0 q0 0 0 1 odd number of 1s s1 s0 1 e-transitions can be taken for free (without reading input)