Nondeterminism and Epsilon Transitions Mridul Aanjaneya Stanford University June 28, 22 Mridul Aanjaneya Automata Theory / 3
Challenge Problem Question Prove that any square with side length a power of 2, and one square removed, is tileable with L s. Mridul Aanjaneya Automata Theory 2/ 3
Something to think about... Question Are such tilings always possible? Mridul Aanjaneya Automata Theory 3/ 3
Recap: Deterministic Finite Automata Definition A DFA is a 5-tuple (Q, Σ, δ D, q, F ) consisting of: A finite set of states Q, A set of input alphabets Σ, A transition function δ D : Q Σ Q, A start state q, and A set of accept states F Q. The transition function δ D : Takes two arguments, a state q and an alphabet a. δ D (q,a) = the state the DFA goes to when it is in state q and the alphabet a is received. Mridul Aanjaneya Automata Theory 4/ 3
Recap: Graph representation of DFA s Nodes correspond to states. Arcs represent transition function. Arc from state p to state q labeled by all those input symbols that have transitions from p to q. Incoming arrow from outside denotes start state. Accept states indicated by double circles. q q 2, q 3 Accepts all binary strings without two consecutive s. Mridul Aanjaneya Automata Theory 5/ 3
Recap: Transition table for DFA s q q q 2 q 2 q q 3 q 3 q 3 q 3 A row for each state, a column for each alphabet. Accept states are starred. Arrow for the start state. q q 2, q 3 Mridul Aanjaneya Automata Theory 6/ 3
Recap: Regular languages Definition A DFA M = (Q, Σ, δ D, q, F ) accepts w if there exists a sequence of states r, r,..., r n in Q with three conditions: r = q δ D (r i, w i+ ) = r i+ for i =,..., n, and r n F Condition says that M starts in the start state q. Condition 2 says that M follows δ D between two states. Condition 3 says that last state is an accept state. We say that M recognizes L if L = {w M accepts w}. Mridul Aanjaneya Automata Theory 7/ 3
Regular Languages: Examples Example Let L = {w w {,} and w, viewed as a binary integer, is divisible by 5.} q q 3 q 4 q q 2 Mridul Aanjaneya Automata Theory 8/ 3
Regular Languages: Examples Example Show that the language of all strings over {, } that do not contain a pair of s that are separated by an odd number of s is regular. a e o, b Mridul Aanjaneya Automata Theory 9/ 3
Regular Languages: Examples Example Show that the language of all strings over {, } that contain an even number of s and s is regular. ee oe eo oo Mridul Aanjaneya Automata Theory / 3
Nondeterminism Deterministic finite automata can only be in one state at any point in time. Recall the definition of the transition function δ D. In contrast, nondeterministic finite automata (NFA s) can be in several states at once! The transition function δ N is a one-to-many function.,,, q q q q 2 3 4 This NFA recognizes strings in {,} containing a in the third position from the end. Mridul Aanjaneya Automata Theory / 3
Transition table q q q,q 2 q 2 q 3 q 3 q 3 q 4 q 4 q 4 q 4 q 4 q is a nondeterministic state with a one-many transition on.,,, q q q q 2 3 4 Intuitively, the NFA always guesses right. Mridul Aanjaneya Automata Theory 2/ 3
Nondeterministic Finite Automata Example An NFA that accepts all strings of the form k where k is a multiple of 2 or 3. q 2 q 3 q q 4 q 5 q 6 Mridul Aanjaneya Automata Theory 3/ 3
Nondeterministic Finite Automata Definition An NFA is a 5-tuple (Q, Σ, δ N, q, F ) consisting of: A finite set of states Q, A set of input alphabets Σ, A transition function δ N : Q Σ P(Q), A start state q, and A set of accept states F Q. Here, Σ denotes the set Σ {}. P(Q) denotes the power set of Q. Mridul Aanjaneya Automata Theory 4/ 3
Transition function of an NFA δ N (q,a) is a set of states. Extend to strings as follows: Basis: δ N (q,) = q Induction: δ N (q,wa) = the union over all states of δ N (p,a), where p δ N (q,w). A string is accepted by an NFA if δ N (q,w) contains at least one state p F. The language of an NFA is the set of strings it accepts. Mridul Aanjaneya Automata Theory 5/ 3
Equivalence of DFA s and NFA s Every DFA is also an NFA by definition. There is simply no nondeterminism. Surprisingly, for every NFA, there is also an equivalent DFA! Two equivalent machines recognize the same language. Nonintuitive, as we d expect NFA s to be more powerful. Useful, as describing an NFA is much simpler. Proof is the subset construction. The number of states of the DFA can be exponential in the number of states of the NFA. Thus, NFA s accept exactly the regular languages. Mridul Aanjaneya Automata Theory 6/ 3
Equivalence of DFA s and NFA s DFA for recognizing strings with a in the third last position. q q q q 5 3 7 q q q q 2 6 4 8 Mridul Aanjaneya Automata Theory 7/ 3
Subset construction Given an NFA (Q,Σ,δ N,q,F ), construct equivalent DFA with: Note: States P(Q) (set of subsets of Q). Inputs Σ. Start state {q }. Final states = all those with a member of F. The DFA states have names that are sets of NFA states. But as a DFA state, an expression like {p,q} must be read as a single symbol. Mridul Aanjaneya Automata Theory 8/ 3
Subset construction Example: We ll construct the DFA for the following NFA which recognizes strings in {,} containing a in the second position from the end.,, a b c Mridul Aanjaneya Automata Theory 9/ 3
Subset construction,, a b c a a,b a,b,c a,c Mridul Aanjaneya Automata Theory 2/ 3
Proof of equivalence: subset construction Show by induction on length of w that δ N (q,w) = δ D ({q },w) Basis: w =. δ N (q,) = δ D ({q },) = {q }. Inductive step: Assume IH is true for all strings shorter than w. Let w = xa, then IH is true for x. Let δ N (q,x) = δ D ({q },x) = S. Let T = the union over all states p in S of δ N (p,a). Then δ N (q,w) = δ D ({q },w) = T (by definition). Mridul Aanjaneya Automata Theory 2/ 3
NFA s with -transitions State-to-state transitions on input. These transitions are spontaneous, and do not consider the input string. B C D A E F Mridul Aanjaneya Automata Theory 22/ 3
Closure of States CL(q) = set of states that can be reached from state q following only arcs labeled. B C D A E F CL(A) = {A}, CL(E) = {B, C, D, E}. Closure of set of states = union of closure of each state. Mridul Aanjaneya Automata Theory 23/ 3
Extended transition function Basis: δ E (q,) = CL(q). Induction: δ E (q,xa) is computed as follows: Start with δ E (q,x) = S. 2 Take the union of CL(δ(p,a)) for all p in S. Intuition: δ E (q,w) is the set of states you can reach from q following a path labeled w with s in between. Mridul Aanjaneya Automata Theory 24/ 3
Example: Extended transition function B C D A E F δ E (A,) = CL(A) = {A}. δ E (A,) = CL(E) = {B, C, D, E}. δ E (A,) = CL(C,D) = {C, D}. Mridul Aanjaneya Automata Theory 25/ 3
Example: Extended transition function B C D A E F Language of an -NFA is the set of strings w such that δ E (q,w) contains a final state. Mridul Aanjaneya Automata Theory 26/ 3
Equivalence of NFA, -NFA Every NFA is an -NFA. It just has no -transitions. Converse requires us to take an -NFA and construct an NFA that accepts the same language. This is done by combining -transitions with the next transition on a real input. Start with an -NFA (Q,Σ,q,F,δ E ) and construct an ordinary NFA (Q,Σ,q,F,δ N ). Mridul Aanjaneya Automata Theory 27/ 3
Equivalence of NFA, -NFA Compute δ N (q,a) as follows: Let S = CL(q). δ N (q,a) is the union over all p in S of δ E (p,a). F = set of states q such that CL(q) contains a state of F. Intuition: δ N incorporates -transitions before using a. Proof of equivalence is by induction on w that CL(δ N (q,w)) = δ E (q,w). Basis: CL(δ N (q,)) = CL(q ) = δ E (q,). Inductive step: Assume IH is true for all x shorter than w. Let w = xa. Then CL(δ N (q,xa)) = CL(δ E (CL(δ N (q,x)),a)) (by definition). But from IH, CL(δ N (q,x)) = δ E (q,x). Hence, CL(δ N (q,w)) = CL(δ E (δ E (q,x),a)) = δ E (q,w). Mridul Aanjaneya Automata Theory 28/ 3
Example B C D A E F B C D A E F Mridul Aanjaneya Automata Theory 29/ 3
Summary DFA s, NFA s and -NFA s all accept exactly the same set of languages: the regular languages. NFA types are easier to design and may have exponentially fewer states than a DFA. But only a DFA can be implemented! Mridul Aanjaneya Automata Theory 3/ 3