Instructor: Aaron Roth aaroth@cis.upenn.edu CIS 262 Automata, Computability, and Complexity Spring 2019 http://www.seas.upenn.edu/~cse262/ 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 GradeScope.
Nondeterministic Automata In state q0, on symbol a, choice of next state is not unique: either q0 or q1 In state q2 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) a,b a a, b q0 q1 q2
Execution of Nondeterministic Automata a,b q0 a a, b a q0 q1 q2 q0 q1 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 Input accepted if at least one of the runs is successful q0 q2 a q0 q1 b q0 q2
Execution of Nondeterministic Automata a,b q0 a a, b a q0 q1 q2 q0 q1 a How does the machine process input string aabb ? Input rejected if no run is successful q0 q1 q2 b q0 q2 b q0
Language of a Nondeterministic Automaton 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 } a,b a a, b q0 q1 q2
NFA Example S = { A, C, G, T } A, C, G,T A,C, G,T A C C q0 q1 q2 q3 Nondeterministic transition function D : Q x S 2Q D(q0, A) = {q0 , q1}; D(q0, C) = {q0}; D(q1, A) = { }; … What language does this NFA accept ?
NFA Example S = { A, C, G, T } A, C, G,T A,C, G,T A C C q0 q1 q2 q3 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 A {q0 , q1 } C {q0 , q2 } T {q0 } A {q0 , q1 } { q0 } C {q0 , q2 } C {q0 , q3 } G {q0 , q3 } A {q0 , q1 , q3} Accept input if the set of possible states at the end contains a final state
Definition: NFA Syntax A nondeterministic finite automaton (NFA) M consists of a finite set Q of states a finite alphabet S an initial state q0 that belongs to Q a subset F of Q, called accepting/final states a transition function D : Q x S 2Q (if M reads symbol s in state q, the resulting state is some state chosen from the set D(q, s) )
Definition: NFA Semantics Let M = (Q, S, q0, F, D) be an NFA. M accepts the string w = w1 w2 … wk if there exists a sequence of states q0 q1 q2 … qk such that (1) q0 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
Alternative Definition of Acceptance Let M = (Q, S, q0, F, D) be an NFA. Define extended transition function D* : Q x S* 2Q 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 D(q’, s) M accepts the input w if and only if the intersection of the sets D*(q0, w) and F is non-empty q’ in D*(q,x)
NFA Example Which strings does the above machine accept ? L(M) = { w | second last symbol of w equal a } a,b a a, b a, b q0 q1 q2 q3
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 q0 q1 a a, b q2 a,b q3
Parametric Example = {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 a,b a, b k-1 transitions NFA with (k+1) states can accept Lk Recall: DFA must have 2k states Nondeterminism can lead to succinct descriptions !
Nondeterminism as a high-level construct q0 q1 In state q0, on reading symbol a, how does the machine know whether to stay in state q0 or go to state q1? 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
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
Does nondeterminism always help ? Draw an NFA that accepts L = { w | count(w,a) is even } DFA for this language: DFAs are a special case of NFAs Above machine is also an NFA for L Is there a smaller/simpler NFA for L ? q0 q1 a b
NFA Example S = { A, C, G, T } Draw NFA for { w | w contains “ACCAC” as a substring } A, C, G,T A,C, G,T A C A C C q0 q1 q2 q3 q4 q5 If input w contains the substring ACCAC then some run does end up in final state q5 If a run does end up in state q5, then w must contain “ACCAC”
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 A,C, G,T C A C A C q0 q1 q2 q3 q4 q5 G q6
Can we design NFAs when there is no DFA ? L = { w | count(w,a) = count(w,b) }, where S = { a, b } We know that L is not regular, that is, there is no DFA for L Can we construct an NFA for L ? Why did DFA construction fail? Strings 𝜖, a, aa, aaa, a4, … all need to lead to “distinct” states Can non-determinism help? More generally, are NFAs “more expressive” than DFAs? Is there an NFA M such that L(M) is not regular?
Compiling an NFA to a DFA Let M = (Q, S, q0, 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 ?
Recap: Set-based Definition of NFA Execution S = { A, C, G, T } A, C, G,T A,C, G,T A C C q0 q1 q2 q3 Execution of the machine on input ACTACCGA: { q0 } A {q0 , q1 } C {q0 , q2 } T {q0 } {q0 , q3 } G {q0 , q1 , q3} To simulate an NFA M, the DFA M’ maintains, as its state, the set of states of M
From NFA to DFA: Subset Construction A, C, G,T A,C, G,T C A C q0 q1 q2 q3 C, G,T A A A C { q0 } { q0 , q1 } { q0 , q2 } C G, T T G Only finitely many states can be generated: all possible subsets of {q0 , q1 , q2 , q3 } suffice
NFA => DFA Let M = (Q, S, q0, 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’ = 2Q Initial state q’0 of M’ = { q0 } 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 D(q, s) This construction is called “determinization” or “subset construction” q in S
Correctness of Determinization Let M = (Q, S, q0, F, D) be an NFA. Consider the DFA M’ = (2Q, S , { q0 } , F’, d’), where F’ = { S | intersection of S and F is non-empty } and d’( S, s) = U 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*(q0 , w) = d’ *({ q0 }, 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 q in S
Example of Determinization a,b b { } { q0 } a,b a,b a b a q0 q1 { q1 } { q0 , q1 } a In above automatically constructed DFA, states { } and {q1} are redundant, since they are “unreachable” from the initial state {q0} Possible optimization: Construct only those states that are reachable from the initial state {q0}
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 2k states Some optimizations can result in smaller DFAs in some cases, but such an exponential blow-up is unavoidable in worst-case, for example, Lk = {w | kth symbol from end equals a } has NFA with k+1 states, but every DFA for this language must have at least 2k states
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
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 - q0 q1 e, +, - 1..9 q2 0..9 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
e-NFAs Transition function D : Q x [ S U { e } ] 2Q 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