CIS 262 Automata Computability and Complexity Spring 2019
CIS 262 Automata, Computability, and Complexity Spring 2019 http: //www. seas. upenn. edu/~cse 262/ Instructor: Aaron Roth aaroth@cis. upenn. edu Lecture: February 6, 2019
Course Logistics Homework 2 is out. Due Wednesday February 20 before class, turned in via Grade. Scope. 2
Nondeterministic Automata a, b q 0 a q 1 a, b q 2 In state q 0, on symbol a, choice of next state is not unique: either q 0 or q 1 In state q 2 cannot process additional symbols (no next state) Deterministic transition function: processing symbol s in state q leads to the unique next state d(q, s) Nondeterministic transition function D maps a state and a symbol to a set of states: if current state is q and symbol is s, next state is chosen from the set D(q, s) 3
Execution of Nondeterministic Automata a, b q 0 a q 1 a, b q 0 a q 2 q 0 b How does the machine process input string abab ? Many possible executions/runs corresponding paths in this tree Successful run: All of input processed, and Resulting state is accepting q 1 q 0 q 2 a q 0 q 1 q 2 b Input accepted if at least one of the runs is successful 4
Execution of Nondeterministic Automata a, b q 0 a q 1 a, b q 0 a q 2 q 0 How does the machine process input string aabb ? a q 0 q 1 q 2 b q 0 Input rejected if no run is successful q 1 q 2 b q 0 5
Language of a Nondeterministic Automaton a, b q 0 a q 1 a, b q 2 NFA M accepts a string w if and only if there exists a successful execution of M on w Which strings does the above machine accept ? L(M) = { w | second last symbol of w equal a } 6
NFA Example S = { A, C, G, T } A, C, G, T q 0 A A, C, G, T q 1 C q 2 C q 3 Nondeterministic transition function D : Q x S 2 Q D(q 0, A) = {q 0 , q 1}; D(q 0, C) = {q 0}; D(q 1, A) = { }; … What language does this NFA accept ? 7
NFA Example S = { A, C, G, T } A, C, G, T q 0 A A, C, G, T C q 1 C q 2 q 3 Execution of the machine on input ACTACCGA: Alternative to tree of all possible runs: compute the set of possible states after processing each input symbol { q 0 } A {q 0 , q 1 } C C {q 0 , q 2 } C T {q 0 , q 3 } {q 0 } G A {q 0 , q 1 } {q 0 , q 3 } A {q 0 , q 1 , q 3} Accept input if the set of possible states at the end contains a final state 8
Definition: NFA Syntax A nondeterministic finite automaton (NFA) M consists of Ø a finite set Q of states Ø a finite alphabet S Ø an initial state q 0 that belongs to Q Ø a subset F of Q, called accepting/final states Ø a transition function D : Q x S 2 Q (if M reads symbol s in state q, the resulting state is some state chosen from the set D(q, s) ) 9
Definition: NFA Semantics Let M = (Q, S, q 0, F, D) be an NFA. M accepts the string w = w 1 w 2 … wk if there exists a sequence of states q 0 q 1 q 2 … qk such that (1) q 0 is the initial state, (2) for each i=0, 1, … (k-1), state qi+1 is in D(qi, wi+1), and (3) state qk is in F. L(M) = set of strings w such that M accepts w 10
Alternative Definition of Acceptance Let M = (Q, S, q 0, F, D) be an NFA. Define extended transition function D* : Q x S* 2 Q D *(q, w) = set of all possible states where machine can be after processing the string w starting in state q D*(q, e) = { q } D*(q, x. s) = U q’ in D*(q, x) D(q’, s) M accepts the input w if and only if the intersection of the sets D*(q 0, w) and F is non-empty 11
NFA Example a, b q 0 a q 1 a, b q 2 a, b q 3 Which strings does the above machine accept ? L(M) = { w | second last symbol of w equal a } 12
NFA Example Draw an NFA that accepts { w | third last symbol of w equal a } Recall: A DFA for this language must have 8 states a, b q 0 a q 1 a, b q 2 a, b q 3 13
Parametric Example S = {a, b}. For each k, let Lk = { w | kth symbol from end in w equals a } How many states does an NFA need to accept Lk as a function of k ? a, b a, b k-1 transitions NFA with (k+1) states can accept Lk Recall: DFA must have 2 k states Nondeterminism can lead to succinct descriptions ! 14
Nondeterminism as a high-level construct a q 0 a q 1 In state q 0, on reading symbol a, how does the machine know whether to stay in state q 0 or go to state q 1? One answer: if only of the two possibilities is going to lead to success at the end, then it chooses that A real-world machine/program cannot figure out which possibility is going to succeed, so needs to track all possibilities by maintaining a set of possible current states Nondeterminism is a “logical” or “mathematical” construct with no direct analog in the real world 15
Why Study Nondeterminism ? If there is no “real-world” analog of nondeterminism construct, namely, make the choice that will lead to success, why bother? First, excellent test case for studying “models of computation” Ø Mathematically well-defined syntax and semantics Ø Valid mathematical question: how does this construct impact computational power Second, turns out to be very important concept Ø Useful in translation from regular expressions Ø Critical to theory of NP-complete problems which includes a large number of natural optimization problems 16
Does nondeterminism always help ? Draw an NFA that accepts L = { w | count(w, a) is even } DFA for this language: b q 0 a a b q 1 DFAs are a special case of NFAs Above machine is also an NFA for L Is there a smaller/simpler NFA for L ? 17
NFA Example S = { A, C, G, T } Draw NFA for { w | w contains “ACCAC” as a substring } A, C, G, T q 0 A q 1 C q 2 C q 3 A q 4 C q 5 If input w contains the substring ACCAC then some run does end up in final state q 5 If a run does end up in state q 5, then w must contain “ACCAC” 18
NFA Example S = { A, C, G, T } Draw NFA for { w | w contains “ACCAC” as a substring or ends with G } A, C, G, T q 0 A q 1 G C q 2 C q 3 A q 4 C q 5 q 6 19
Can we design NFAs when there is no DFA ? L = { w | count(w, a) = count(w, b) }, where S = { a, b } 20
Compiling an NFA to a DFA Let M = (Q, S, q 0, F, D) be an NFA. Goal: to construct a DFA M’ such that M’ accepts w if and only M does How can a deterministic machine track information about all possible executions of M ? 21
Recap: Set-based Definition of NFA Execution S = { A, C, G, T } A, C, G, T q 0 A A, C, G, T C q 1 q 2 C q 3 Execution of the machine on input ACTACCGA: { q 0 } A {q 0 , q 1 } C C {q 0 , q 2 } C T {q 0 , q 3 } {q 0 } G A {q 0 , q 1 } {q 0 , q 3 } A {q 0 , q 1 , q 3} To simulate an NFA M, the DFA M’ maintains, as its state, the set of states of M 22
From NFA to DFA: Subset Construction A, C, G, T q 0 C, G, T { q 0 } A, C, G, T A q 1 C A A G, T { q 0 , q 1 } C C q 2 q 3 A { q 0 , q 2 } G C T Only finitely many states can be generated: all possible subsets of {q 0 , q 1 , q 2 , q 3 } suffice 23
NFA => DFA Let M = (Q, S, q 0, F, D) be an NFA. Goal: to construct a DFA M’ such that M’ accepts w if and only M does A state of M’ is a set of states of M: Q’ = 2 Q Initial state q’ 0 of M’ = { q 0 } A state is final for M’ if it contains a final state of M: F’ = { S | intersection of S and F is non-empty } Transition function of M’: d’( S, s) = U q in S D(q, s) This construction is called “determinization” or “subset construction” 24
Correctness of Determinization Let M = (Q, S, q 0, F, D) be an NFA. Consider the DFA M’ = (2 Q, S , { q 0 } , F’, d’), where F’ = { S | intersection of S and F is non-empty } and d’( S, s) = U q in S D(q, s) Prove: L(M) = L(M’) To prove: for all strings w, NFA M accepts w iff DFA M’ accepts w What’s the suitable claim that can be proved by induction on w ? For all strings w, D*(q 0 , w) = d’ *({ q 0 }, w) that is, the set of states that NFA M can be after processing a string w coincides with the state of DFA M’ after processing w 25
Example of Determinization a, b q 0 a q 1 a, b b { } { q 0 } a, b { q 1 } a b { q 0 , q 1 } a In above automatically constructed DFA, states { } and {q 1} are redundant, since they are “unreachable” from the initial state {q 0} Possible optimization: Construct only those states that are reachable from the initial state {q 0} 26
Summary of Determinization For every NFA, there exists an equivalent DFA accepting same language The class of problems solved by DFAs and NFAs is identical, namely, regular languages It is straightforward to write a computer program that implements the NFA to DFA translation If NFA M has k states then the determinized machine M’ has 2 k states Some optimizations can result in smaller DFAs in some cases, but such an exponential blow-up is unavoidable in worst-case, for example, L k = {w | kth symbol from end equals a } has NFA with k+1 states, but every DFA for this language must have at least 2 k states 27
Nondeterministic Automata: Summary NFA model is a generalization of DFA: transition function maps a state and a symbol to a set of states Semantics: NFA accepts a string if one of the many possible executions leads to a final state Nondeterminism does not increase expressiveness: An NFA can be compiled into an equivalent DFA NFAs accept only regular languages NFAs allow more high-level or succinct descriptions 28
NFAs with e-transitions A number consists of one or more digits 0. . 9 (but first digit is not 0) and can be optionally preceded by + or - 0. . 9 q 0 e, +, - q 1 1. . 9 q 2 Meaning of e-transition: state may change without consuming any input symbol Acceptance as before: a string is accepted as long as some execution succeeds 29
e-NFAs Transition function D : Q x [ S U { e } ] 2 Q Such transitions do not increase expressiveness: An NFA with e-transitions can be compiled into an equivalent DFA by modifying the subset construction e-transitions can allow more natural/succinct description Read the textbook for details 30
- Slides: 30