1/23 Nondeterministic Finite Automata CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Chinese University of Hong Kong Fall 2017
2/23 Example from last lecture with a simpler solution 1 0 0 1 1 0 or 0 1 0 1 1 0 1 0 0 1 0 Three weeks later: DFA minimization 1 1 0 Construct a DFA over alphabet { 0 , 1 } that accepts all strings ending in 01 q 00 q 0 q 01 q ε q 0 q 1 q 2 q 10 q 1 q 11
3/23 Another example from last lecture or 1 0 1 0 1 0 0 1 Construct a DFA over alphabet { 0 , 1 } that accepts all strings ending in 101 q 0 q 1 q 2 q 2
4/23 String matching DFAs Boyer–Moore (BM) and Knuth–Morris–Pratt (KMP) and execute the DFA: Fast string matching algorithms to turn a pattern into a string matching DFA 1 0 0 1 0 1 0 1 (won’t cover in class) 1 0 0 1 1 0 Ending in 01 q 0 q 1 q 2 q 0 q 1 q 2 q 2 Ending in 101
5/23 Nondeterminism
6/23 0 We’ll answer this question in the next few lectures What problems can finite state machines solve? 1 0 0 1 1 In a few lectures 0 1 q 0 q 1 q 2 q 2 Useful to consider hypothetical machines that are nondeterministic
7/23 Even easier with guesses Suppose we could guess when the input string has only 3 symbols lefu Accept strings ending in 101: 3 symbols lefu 1 0 1 0 1 0 This is not a DFA!
8/23 Nondeterministic finite automata A machine that allows us to make guesses 0,1 1 0 1 same symbol q 0 q 1 q 2 q 3 Each state can have zero, one, or more outgoing transitions labeled by the
9/23 Choosing where to go 0,1 1 0 1 q 0 q 1 q 2 q 3 State q 0 has two transitions labeled 1 Upon reading 1, we have the choice of staying at q 0 or moving to q 1
10/23 Ability to choose 0,1 1 0 1 q 0 q 1 q 2 q 3 State q 1 has no transition labeled 1 Upon reading 1 at q 1 , die; upon reading 0, continue to q 2
11/23 Ability to choose 0,1 1 0 1 q 0 q 1 q 2 q 3 State q 1 has no transition going out Upon reading 0 or 1 at q 3 , die
12/23 Meaning of NFA Guess if we are 3 symbols away from end of input If so, guess we will see the pattern 101 Check that we are at the end of input 0,1 1 0 1 q 0 q 1 q 2 q 3
13/23 How to run an NFA 0,1 1 0 1 input: 01101 NFA accepts if at the end, one of its active states is accepting q 0 q 1 q 2 q 3 The NFA can have several active states at the same time
14/23 Example pattern 001 somewhere 11001010, 001001, 111001 should be accepted should not Construct an NFA over alphabet { 0 , 1 } that accepts all strings containing the ε , 000, 010101
15/23 Example pattern 001 somewhere 0,1 0 0 1 0,1 Construct an NFA over alphabet { 0 , 1 } that accepts all strings containing the q 0 q 1 q 2 q 3
16/23 Definition where Difgerences from DFA: A nondeterministic finite automaton (NFA) is a 5 -tuple ( Q , Σ , δ, q 0 , F ) ◮ Q is a finite set of states ◮ Σ is an alphabet ◮ δ : Q × (Σ ∪ { ε } ) → subsets of Q is a transition function ◮ q 0 ∈ Q is the initial state ◮ F ⊆ Q is a set of accepting states ◮ transition function δ can go into several states ◮ allows ε -transitions
17/23 Language of an NFA an accepting state as x is read from lefu to right The language of an NFA is the set of all strings accepted by the NFA The NFA accepts string x if there is some path that, starting from q 0 , ends at
18/23 a b a accepts a, b, aab, bab, aabab, … rejects ε -transitions ε -transitions can be taken for free: q 1 ε q 0 ε , aa, ba, bb, … q 2
19/23 0 states 1 Example inputs 0 0 ε ,1 ε q 0 q 1 q 2 table of transition function δ alphabet Σ = { 0 , 1 } ε states Q = { q 0 , q 1 , q 2 } ∅ { q 1 } { q 1 } q 0 initial state q 0 { q 0 , q 1 } ∅ { q 2 } accepting states F = { q 2 } q 1 ∅ ∅ ∅ q 2
20/23 0 4 Running NFA 1 4 0 3 2 2 5 1 3 0 2 0 4 3 5 0 00 1 0 0 2 0 1 ε ,1 ε ε q 0 q 1 q 2 ε ,1 ε q 0 q 1 q 2 ε ,1 ε q 0 q 1 q 2 ε ,1 ε q 0 q 1 q 2
21/23 101 0 0 1 0 0 1 11 4 0 2 Running NFA 3 1 0 5 3 001 1 4 0 2 0 2 ε , 1 ε q 0 q 1 q 2 ε ,1 ε q 0 q 1 q 2 ε ,1 ε q 0 q 1 q 2 ε ,1 ε q 0 q 1 q 2
22/23 Language of this NFA 0 0 What is the language of this NFA? ε ,1 ε q 0 q 1 q 2
23/23 odd number of 1s 0 1 1 1 1 0 0 even number of 0s s s r r q odd number of 1s Construct an NFA that accepts all strings with an even number of 0s or an 0 Example of ε -transitions
23/23 0 0 1 1 1 1 0 odd number of 1s even number of 0s odd number of 1s Construct an NFA that accepts all strings with an even number of 0s or an 0 Example of ε -transitions r 0 r 1 ε q 0 ε s 0 s 1
Recommend
More recommend