Report

2.4 Nondeterministic Finite State Automata Definition 6 : A nondeterministic finite state automaton(NFA) M = (, Q, , q 0, F), where is a finite set alphabet, Q is a finite set of states, : Q {} 2 Q is a transition function, where 2 Q is a power set of Q, q 0 Q is the initial state, F Q is the set of final states. Example 1: A nondeterministic finite state automaton(NFA) M = (, Q, , q 0, F), where = {0, 1}, Q = {q 0, q 1, q 2, q 3}, q 0 Q is the initial state, F = {q 3}, : Q {} 2 Q is defined as follows. q0 q1 0 {q 0, q 1} 1 {q 0} {q 2} q2 {q 3} q3 0, 1 q0 0 q1 1 q2 0 q3 The transition (q 0, 0) = {q 0, q 1} says that at the state q 0 after reading the symbol 0 the machine can enter either the state q 0 or the state q 1. Each move of the machine may have more than one state to select. Now, we get a problem that how to determine wether an input string is accepted by the machine or not. Consider the example 1 as the machine reading the input 01010. The NFA M may have the following possible moves. 0, 1 q0 0 q1 1 q2 0 q3 Case 1: q 0 01010 |-- 0 q 1 1010 |-- 0 1 q 2 010 |-- 0 10 q 3 10 |--? Case 2: q 0 01010 |-- 0 q 0 1010 |-- 0 1 q 0 010 |-- 0 10 q 0 10 |-0 10 1 q 0 0 |-- 0 10 1 0 q 0 Case 3: q 0 01010 |-- 0 q 0 1010 |-- 0 1 q 0 010 |-- 0 10 q 0 10 |-0 10 1 q 0 0 |-- 0 10 1 0 q 1 Case 4: q 0 01010 |-- 0 q 0 1010 |-- 0 1 q 0 010 |-- 0 10 q 1 10 |-0 10 1 q 2 0 |-- 0 10 1 0 q 3 There are 4 different ways for M to read the input 01010. Although 3 ways do not lead to a final state, but there is one way, i.e., in case 4, that the machine enters a final state q 3. Since there is a way for M to enter a final state q 3 after reading the input 01010, we say that the machine M accepts 01010. Definition 7 : A string is accepted by an NFA M = (, Q, , q 0, F), if there is some way that q 0 |--* q f , where q f F. The language accepted by the machine M is L(M) = { * | if there is some way that q 0 |--* q f , where q f F. } The language L accepted by the following NFA M is the set of strings ending with 010. 0, 1 q0 0 q1 1 q2 0 q3 Compare the design of the DFA accepting language L in section 2.3 with the design of the above NFA. It is quite obvious that the design of an NFA is much easier than that of a DFA, since we take less care of interaction between states. Althogh the design of an NFA is much easier than that of a DFA, later we will show that NFA‘s and DFA’s are of same expressive power, i.e., if L = L(M) for some NFA M, then there is a DFA M‘ such that L = L(M’), and vise versa. Therefore, we may first design an NFA, then convert to a DFA. Example 2: Design an NFA M to accept strings of 0’s and 1’s such that the 3rd symbol from the right end is 0. Solution : 0, 1 0 q0 0, 1 q1 An equivalent DFA is as follows. 1 0 q0 1 q1 0 q4 1 q5 q2 q3 0 0 q3 q 0= q 111 q 1= q 110 1 q 2= q 100 q 3= q 000 1 1 0 q2 0, 1 0 0 q6 1 q7 0 1 q 4= q 011 q 5= q 101 q 6= q 010 q 7= q 001 An NFA with -moves is much easier to design and to understand, and more flexible and important to use. If we allow an NFA M to have -moves, then we allow M to change states without reading any input symbol. Because of -moves, it is quite easy to group a substructure to form a module. And the connection between modules is much easier to handle. Example 3: Design an NFA M to accept strings of 0’s and 1’s such that the strings either contain a substring 101 or end with the symbols 00. 0, 1 0, 1 q0 q1 1 q2 0 q3 q5 0, 1 0 q6 0 q7 1 q4 The transition function is as follows. 0 1 q0 {q 1, q 5} q1 {q 1} {q 1, q 2} q2 {q 3} q3 {q 4} q4 {q 4} {q 4} q5 {q 5, q 6} {q 5} q6 {q 7} q7 {q 7} {q 7} For the input 10100, M has the following ways to accept it. Case 1: q 0 10100 |-- q 110100 |--1q 20100 |-- 10q 3100 |-- 101q 400 |-- 1010q 40 |-- 10100q 4 Case 2: q 0 10100 |-- q 510100 |--1q 50100 |-- 10q 5100 |-- 101q 500 |-- 1010q 60 |-- 10100q 7 q0 0, 1 0, 1 q1 1 q5 0, 1 0 q2 0 q6 0 q3 1 q7 q4 Example 4: Design an NFA M to accept strings over {0, 1} so that the evaluation of the string according to the following operation is same as the evaluation of R. 0 1 0 0 0 1 1 0 Solution : Let M = ( ={0, 1}, Q, , q 0, F ), where each state q Q and q q 0, q is of the form [f, b, z], f, b, z {0, 1, }, F = { [z, , z] | z = 0 or 1}, if [g, b, z] ([f, c, z], a), then g = fa and c = ba, and (q 0, )={[, a, a] | a = 0 or 1}. Each state q Q and q q 0, q is of the form [f, b, z], where f is the evaluation of the substring from the initial state up to current state, z is a fixed value used to indicate the evaluation of the reversal of a string , and b is the value of the coming substring evaluated in reverse direction. The idea is as follows. (1) If [g, b, z] ([f, c, z], a), then g = fa, c = ba. In another word, g = fa is evaluated forward, and c = ba is evaluate backward. (2) [z, , z] is a final state which can not proceed any more. Since to evaluate forward equal to the backward evaluation z, it should be a final state. (3) The start state should be of the form [, z, z]. Although we do not know what will be the input , we can assume the evaluation of the string R is z. And it is possible to trace back. (4) ([1, 0, 0], 1) = {[0, 0, 0], [0, 1, 0]}, since considering the first component 0 of [0, 0, 0] and [0, 1, 0], the forward evaluation 11 = 0, and considering the second components of [0, 0, 0] and [0, 1, 0], the backward evaluation 10 R = 01 = 0, and 11R = 11 = 0. (5) ([, 0, 0], 0) = {[0, , 0], [0, 0, 0]}, where [0, , 0] is a final state. (6) An NFA M is listed below. 1 1 0, 1 [0,1,0] [0,0,0] 0 0 0 1 [0,,0] 1 0 0 [,0,0] [1,0,0] 1 1 [1,1,0] q0 0 0 [1,,1] 0 1 1 [,1,1] 0 [0,1,1] 1 [0,,1] [1,,0] The expressive power of NFA’s seems more powerful than that of DFA’s. It is easier to design an NFA than to design a DFA to accept the same language. Now we have a problem that whether or not NFA’s are really more powerful than DFA’s. Is there any language L accepted by an NFA but not DFA’s? By the following theorem, we shall see that NFA’s are equivalent to DFA’s in the sense that if there is a language L accepted by an NFA, then there is a DFA that accepts L, and vise versa. We are going to use constructive method to prove the following theorem , and that is the method we use to convert an NFA to an equivalent DFA. First, we need to talk about -closure of a set of states. Definition 8 : An -closure of a set P of states is the set -closure( P ) defined recursively by (1) P -closure( P ). (2) If q -closure( P ), then ( q, ) -closure( P ). Example 5: Considering the following transition diagram, find -closure( {q 0, q 2} ). 0, 1 0, 1 q0 q1 1 q2 q3 1 q4 q5 q6 0 q7 0, 1 Solution : ( q 0, ) = {q 1, q 5}, ( q 2, ) = {q 3, q 7} ( q 1, ) = = ( q 3, ) = ( q 7, ), ( p 5, ) = {q 6} Therefore, -closure( {q 0, q 2} ) = {q 0, q 2, q 1, q 5 , q 6 , q 3, q 7} Theorem 1: L = L( M 1 ) for some NFA M 1 iff L = L( M 2 ) for some DFA M 2. Proof : (1) Suppose that L = L( M 2 ) for some DFA M 2 . Since a DFA M 2 can be treated as an NFA M 1, where M 1 = M 2, we have that L = L( M 1 ) for some NFA M 1. (2) Suppose that L = L( M 1 ) for some NFA M 1 = ( , Q, , q 0, F). Construct a DFA M 2 = ( , Q’, ’, q 0 ’, F ’), where Q’=2 Q, q 0 ’ = -closure( {q 0} ), F ’ = { P Q’ | P F }, ’(P, a) = q P -closure((q, a)), Then for any input *, *(q 0 , ) = the set of all possible states that can be reached after reading the input by machine M 1= ’*(q 0 ’, )= the state for the machine M 2 to enter after reading the input . We can prove the above statement by induction. First, if = , we have that *(q 0 , ) = *(q 0 , ) = -closure( {q 0 } ) = ’*(q 0 ’, ) = q 0 ’. Second, if || = n and *(q 0 , ) = ’*(q 0 ’, ) = P, then *(q 0 , a) = q P -closure((q, a)) = ’(P, a) = ’*(q 0 ’, a) for a . And it is obvious that L = L(M 1) = L(M 2). Example 6: Design a DFA M 2 = ( , Q’, ’, q 0 ’, F ’) to accept the set L of strings of 0’s and 1’s such that the strings end with the symbols 010. Solution : The following is the design of an NFA M 1 = ( , Q, , q 0, F) accepting L. 0, 1 q0 0 q1 1 q2 0 q3 We now convert the NFA M 1 to an equivalent DFA M 2 . There is no -move in the machine. Therefore, we do not need to worry about -closure of a set of states. First, we have the state q 0 ’ = [q 0] and the transitions ’([q 0] , 0) = [q 0, q 1]) and ’([q 0] , 1) = [q 0]). 0, 1 q0 0 q1 1 M1 M2 1 [q 0] 0 [q 0, q 1] q2 0 q3 Second, consider the transitions ’([q 0, q 1] , 0) = [q 0, q 1]) and ’([q 0, q 1] , 1) = [q 0 , q 2]). 0, 1 q0 0 q1 1 q2 0 q3 M1 M2 0 1 [q 0] 0 [q 0, q 1] 1 [q 0, q 2] Third, consider the transitions ’([q 0, q 2] , 0) = [q 0, q 1 , q 3]) and ’([q 0, q 2] , 1) = [q 0]). 0, 1 q0 0 q1 1 q2 0 q3 M1 M2 0 1 [q 0] 0 [q 0, q 1] 1 1 [q 0, q 2] 0 [q 0,q 2,q3] Fourth, consider the transitions ’([q 0, q 1 , q 3] , 0) = [q 0, q 1]) and ’([q 0, q 1 , q 3] , 1) = [q 0 , q 2]), done! 0, 1 q0 0 q1 1 q2 0 q3 M1 M2 0 1 [q 0] 0 [q 0, q 1] 1 1 [q 0, q 2] 1 0 [q 0,q 1,q3] 0 Example 7: Design a DFA M 2 = ( , Q’, ’, q 0 ’, F ’) to accept the set L of strings of 0’s and 1’s such that the strings either contain a substring 101 or end with the symbols 00. Solution : The following is the design of an NFA M 1 = ( , Q, , q 0, F) accepting L. 0, 1 0, 1 q0 q1 1 q2 0 q3 1 q5 0 q6 0 q7 0, 1 We now convert the NFA M 1 to an equivalent DFA M 2 . q4 First, according to a part of transition diagram as follows we can get the state q 0 ’ = [q 0, q 1, q 5]. q0 M1 0, 1 0, 1 q1 1 q5 0, 1 M2 [q 0, q 1, q 5] 0 q2 0 q6 0 q3 1 q7 q4 Second, ’ (q 0 ’, 0) = [q 1, q 5 , q 6] and ’ (q 0 ’, 1) = [q 1, q 2, q 5]. q0 M1 0, 1 0, 1 q1 1 q5 0, 1 M2 0 q2 0 q6 0 q3 1 q7 0 [q 1, q 5 , q 6] 1 [q 1, q 2 , q 5] [q 0, q 1, q 5] q4 Third, ’ ( [q 1, q 5 , q 6], 0 )= [q 1, q 5 , q 6 , q 7], and ’ ([q 1, q 5 , q 6], 1) = [q 1, q 2, q 5], . q0 M1 0, 1 0, 1 q1 1 q5 0, 1 0 q2 0 q6 0 q3 1 q4 q7 0 0 M2 [q 1, q 5 , q 6] 1 [q 0, q 1, q 5] 1 [q 1, q 2 , q 5] [q 1, q 5 ,q 6 , q 7] Fourth, ’ ( [q 1, q 2 , q 5], 0 )= [q 1, q 3 , q 5 , q 6], and ’ ([q 1, q 2 , q 5], 1) = [q 1, q 2, q 5], . q0 M1 0, 1 0, 1 q1 1 q5 0, 1 0 q2 0 q6 0 q3 1 q4 q7 0 0 M2 [q 1, q 5 , q 6] 1 [q 0, q 1, q 5] 1 [q 1, q 5 ,q 6 , q 7] 0 [q 1, q 2 , q 5] 1 [q 1 , q 3, q 5 ,q 6] Fifth, ’ ( [q 1, q 3 , q 5 , q 6], 0 )= [q 1, q 5 , q 6 , q 7], and ’ ([q 1, q 3 , q 5 , q 6], 1) = [q 1, q 2, q 4 , q 5 ], . q0 0, 1 0, 1 q1 1 q2 0 q5 0, 1 M1 q6 0 q3 1 q7 0 0 M2 0 [q 0, q 1, q 5] [q 1, q 5 , q 6] [q 1, q 2 , q 5] 1 [q 1, q 5 ,q 6 , q 7] 0 1 1 q4 0 [q 1 , q 3, q 5 ,q 6] 1 [q1,q2,q4 ,q5] Sixth, ’ ( [q 1, q 5 , q 6 , q 7], 0 )= [q 1, q 5 , q 6 , q 7], and ’ ([q 1, q 5 , q 6 , q 7], 1) = [q 1, q 2, q 5 ], . q0 M1 0, 1 0, 1 q1 1 q5 0 0, 1 q2 0 q6 0 M2 0 [q 0, q 1, q 5] 1 [q 1, q 5 , q 6] 1 1 q4 q7 0 0 [q 1, q 5 ,q 6 , q 7] 1 0 [q 1, q 2 , q 5] q3 1 0 [q 1 , q 3, q 5 ,q 6] 1 [q1,q2,q4 ,q5] Seventh, ’ ( [q 1, q 2 , q 4 , q 5], 0 )= [q 1, q 3 , q 4 , q 5 , q 6], and ’ ([q 1, q 2 , q 4 , q 5], 1) = [q 1, q 2 , q 4, q 5 ], . q0 M1 0, 1 0, 1 q1 1 q5 0 0, 1 q2 0 q6 0 M2 0 [q 0, q 1, q 5] 1 [q 1, q 5 , q 6] 1 1 q4 q7 0 0 [q 1, q 5 ,q 6 , q 7] 1 0 [q 1, q 2 , q 5] q3 1 [q1,q3,q4 ,q5 ,q6] 0 [q 1 , q 3, q 5 ,q 6] 0 1 [q1,q2,q4 ,q5] 1 Eighth, ’ ( [q 1, q 3 , q 4 , q 5 , q 6], 0 )= [q 1, q 4 , q 5 , q 6 , q 7], and ’ ([q 1, q 3 , q 4 , q 5 , q 6], 1) = [q 1, q 2 , q 4, q 5 ], . q0 M1 0, 1 0, 1 q1 1 q5 0 0, 1 q2 0 q6 0 M2 0 [q 0, q 1, q 5] 1 [q 1, q 5 , q 6] 1 1 q4 q7 0 0 [q1,q4,q5 ,q6 ,q7] [q 1, q 5 ,q 6 , q 7] 1 0 [q 1, q 2 , q 5] q3 1 0 [q1,q3,q4 ,q5 ,q6] 0 [q 1 , q 3, q 5 ,q 6] 0 1 [q1,q2,q4 ,q5] 1 1 Ninth, ’ ( [q 1, q 4 , q 5 , q 6 , q 7], 0 )= [q 1, q 4 , q 5 , q 6 , q 7], and ’ ([q 1, q 4 , q 5 , q 6 , q 7], 1) = [q 1, q 2 , q 4, q 5 ], . q0 M1 0, 1 0, 1 q1 1 q5 0 0, 1 q2 0 q6 0 M2 0 [q 0, q 1, q 5] 1 [q 1, q 5 , q 6] 1 1 q4 q7 0 0 0 [q1,q4,q5 ,q6 ,q7] [q 1, q 5 ,q 6 , q 7] 1 0 [q 1, q 2 , q 5] q3 1 0 [q1,q3,q4 ,q5 ,q6] 0 [q 1 , q 3, q 5 ,q 6] 0 1 [q1,q2,q4 ,q5] 1 1 1 After rewrite the machine M2, we come up with the following result. 0 q0 0 q1 1 1 1 0 q3 q2 0 q4 0 0 0 q6 1 0 1 1 q5 1 The above machine can be simplified as follows. 0 q0 q1 1 1 1 q3 1 0 0 q2 0 q4 q7 0 0, 1 1 q5 Example 8: Design a DFA M to accept strings over {0, 1} so that the evaluation of the string according to the following operation is same as the evaluation of R. 0 1 0 0 0 1 1 0 Solution : Convert the previous NFA in example 4 to the following DFA. 0 0 q0 1 q4 0, 1 1 q1 q2 1 0 q5 1 0 0, 1 q3