Finite Automata and Regular Languages (part II) Prof. Dan A. Simovici UMB 1 / 25
Outline 1 Nondeterministic Automata 2 / 25
Definition A nondeterministic finite automaton (ndfa) is a quintuple M = (A, Q, δ, q 0, F ), where A is the input alphaet of M, Q is a finite set of states, δ : Q A P(Q) is the transition function, q 0 Q is the initial state, and F Q is the set of final states of M. We assume A Q =. 3 / 25
Example Consider the ndfa M = ({a, }, {q 0, q 1, q 2, q 2, q 4 }, δ, q 0, {q 1, q 3 }), whose transition function is defined y the tale: State Input q 0 q 1 q 2 q 3 q 4 a {q 1, q 2 } {q 3 } {q 0 } {q 3 } {q 4 } Note the presence of pairs (q, a) such that δ(q, a) =. We refer to such pairs as locking situations of M. For instance, (q 1, a) is a locking situation of M. 4 / 25
Extending the transition function for an ndfa As in the case of the dfa, we can extend the ndfa s transition function δ, defined on single characters, to δ, defined on words. Starting from the transition function δ, we define the function δ : Q A P(Q) as follows: δ (q, λ) = {q} δ (q, xa) = q δ (q,x) δ(q, a) 5 / 25
Graphs of ndfas If M = (A, Q, δ, q 0, F ) is an ndfa, then the graph of M is the marked, directed multigraph G(M), whose set of vertices is the set of states Q. The set of edges of G(M) consists of all pairs (q, q ) such that q δ(q, a) for some a A; an edge (q, q ) is laeled y the input symol a, where q δ(q, a). The initial state q 0 is denoted y an incoming arrow with no source, and the final states are circled. If q δ (q, x), then there is a path in the graph G(M) laeled y x that leads from q to q. 6 / 25
q 1 q 3 a a q 0 a q 2 q 4 7 / 25
Comparing dfas and ndfas In the graph of a dfa M = (A, Q, δ, q 0, F ) you must have exactly one edge emerging from every state q and for every input symol a A. a q 37 c d In the graph of an ndfa M = (A, Q, δ, q 0, F ) you may have states where no arrow emerges, or states where several arrow laeled with the same symol emerge. Also, not every symol needs to appear as a lael of an emerging edge. q No arrow emerges from q a q 7 3 a 8 / 25
Definition The language accepted y the ndfa M = (A, Q, δ, q 0, F ) is L(M) = {x A δ (q 0, x) F }. In other words, x L(M) if there exists a path in the graph of M laeled y x that leads from the initial state into one of the final states. Note that it is not necessary that all paths laeled y x lead to a final state; the existence of one such path suffices to put x into the language L(M). 9 / 25
Example q 1 q 3 a a q 0 a q 2 q 4 Note that a L(M) ecause of the existence of the path (q 0, q 1, q 3 ) laeled y this word and the fact that q 3 is a final state. On the other hand, (q 0, q 2, q 4 ) is another path laeled y x ut q 4 F. 10 / 25
Example (cont d) This ndfa is simple enough to allow an easy identification of all types of words in L(M): 1 The final state q 1 can e reached y applying an aritrary numer of s followed y an a. 2 The final state q 3 can e reached y a path of the form (q 0,..., q 0, q 1, q 3 ), that is y a word of the form k a for k N. 3 The same final state q 3 can e reached via q 2. Input words that allow this transition have the form k aa for k N. Thus, we have L(M) = {} a {} a {} aa = {} {a, a, aa}. 11 / 25
Example Consider an alphaet A = {a 0,..., a n 1 } and a inary relation ρ A A. The language L ρ = {a i0 a ip p N, (a ij, a ij+1 ) ρ for 0 j p 1} is accepted y the ndfa M ρ = (A, Q, δ, q, F ), where Q = {q, q 0,..., q n 1 }, F = {q 0,..., q n 1 }, and δ is given y δ(q, a i ) = {q i } for 0 i n 1; for every i, j such that 0 i, j n 1, δ(q i, a j ) = {q j Q (a i, a j ) ρ}. 12 / 25
Example (cont d) Note that if (a i, a j ) ρ, then (q i, a j ) is a locking situation. The existence of these locking situations is precisely what makes this device a nondeterministic automaton. q 0 a 0. q i a i a j. a j q q j a n 1. q n 1 13 / 25
We show that L ρ = L(M ρ ). Let x = a i0 a ip e a word in L ρ with p 0. We prove y induction on p = x 1 that x L(M ρ ) and that δ (q, x) = {q ip }. The ase case, p = 0, is immediate, since the condition (a ij, a ij+1 ) ρ for 0 j p 1 is vacuous. Suppose that the statement holds for words in L ρ of length at most p and let x = a i0 a ip e a word in L ρ of length p + 1. It is clear that the word y = a i0 a ip 1 elongs to L ρ. By the inductive hypothesis, y L(M ρ ) and δ (q, y) = {q ip 1 }. Since (a ip 1, a ip ) ρ (y the definition of L ρ ), we have δ(q ip 1, a ip ) = {q ip }, so q ip Therefore, x L(M ρ ). q δ(q,y) δ(q, a ip ) = δ (q, ya ip ) = δ (q, x). 14 / 25
To prove the converse inclusion L(M ρ ) L ρ we use an argument y induction on x 1, where x is a word from L(M ρ ), to show that if x = a i0 a ip L(M ρ ), then δ (q, x) = {q ip } and x L ρ. Again, the ase case is immediate. Suppose that the statement holds for words in L(M ρ ) of length less than p + 1 that elong to L ρ, and let x = a i0 a ip e a word in L(M ρ ) of length p + 1. If y = a i0... a ip 1, it is easy to see that y L(M ρ ) ecause no locking situation may arise in M ρ while the symols of y are read. Therefore, y the inductive hypothesis, y L ρ and δ (q, y) = {q ip 1 }. Further, since δ(q ip 1, a ip ), it follows that (a ip 1, a ip ) ρ, so x L ρ, and δ (q, x) = q ip. Thus, L ρ is accepted y the ndfa M ρ. 15 / 25
Let M = (A, Q, δ, q 0, F ) e a nondeterministic automaton, and let : P(Q) A P(Q) e defined y (S, a) = q S δ(q, a) (1) for every S Q and a A. Starting from, we define : P(Q) A P(Q) in the manner used for the transition functions of deterministic automata. Namely, we define for every S Q and a A. (S, λ) = S (2) (S, xa) = ( (S, x), a) (3) 16 / 25
Lemma The functions and defined aove satisfy the following properties: 1 For every family of sets {S 0,..., S n 1 } and every a A, we have: 0 i n 1 S i, a = 2 For every set S Q and x A we have (S, x) = q S 0 i n 1 δ (q, x). (S i, a). 17 / 25
Proof The first part of the lemma is immediate, ecause ( 0 i n 1 S i, a) = {δ(q, a) q 0 i n 1 S i} = 0 i n 1 {δ(q, a) q S i} = 0 i n 1 (S i, a). 18 / 25
The argument for the second part of the lemma is y induction on x. For the asis step, we have x = 0, so x = λ, and (S, λ) = S, q S δ (q, λ) = q S {q} = S. Suppose that the argument holds for words of length n, and let x = za e a word of length n + 1. We have (S, x) = (S, za) = ( (S, z), a) = ( q S δ (q, z), a)(y ind. hyp.) = (δ (q, z), a) q S = δ(r, a) = δ (q, za) = δ (q, x). q S r δ (q,z) q S q S 19 / 25
Nondeterministic automata can e regarded as generalizations of deterministic automata in the following sense. If M = (A, Q, δ, q 0, F ) is a deterministic automaton, consider a nondeterministic automaton M = (A, Q, δ, q 0, F ), where δ (q, a) = {δ(q, a)}. It is easy to verify that for every q Q and x A we have δ (q, x) = {δ (q, x)}. Therefore, L(M ) = {x A δ (q0, x) F } = {x A {δ (q 0, x)} F } = {x A δ (q 0, x) F } = L(M). In other words, for every deterministic finite automaton there exists a nondeterministic one that recognizes the same language. 20 / 25
Theorem For every nondeterministic automaton, there exists a deterministic automaton that accepts the same language. 21 / 25
Proof Let M = (A, Q, δ, q 0, F ) e a nondeterministic automaton. Define the function as in the equality (S, a) = q S δ(q, a), and consider the deterministic automaton M = (A, P(Q),, {q 0 }, {S S Q and S F }). For every x A we have the following equivalent statements: 1 x L(M); 2 δ (q 0, x) F ; 3 ({q 0 }, x) F ; 4 x L(M ). This proves that L(M) = L(M ). 22 / 25
Example Consider the nondeterministic finite automaton whose graph is given elow. M = ({a, }, {q 0, q 1, q 2 }, δ, q 0, {q 2 }) a q 0 q 1 q 2 a 23 / 25
It is easy to see that the language accepted y this automaton is A A, that is the language that consists of all words that contain two consecutive symols. The graph of the nondeterministic automaton is simpler than the graph of the previous deterministic automaton; this simplification is made possile y the nondeterminism. 24 / 25
Graph of the Equivalent ndfa a a {q 0 } {q 0, q 1 } a a {q 1 } {q 1, q 2 } {q 0, q 1, q 2 } a a {q 2 } {q 0, q 2 } a a 25 / 25