----------------------------- Finite state automata (cont'd) ----------------------------- - Example: Come up with F.S.A. that recognizes L = { w in {a,b}* : w contains an even number of a's }. Use states that represent information about string processed so far. In this case, only need to remember if number of a's seen so far is even or odd, so only need two states "even" and "odd". Initial state should be "even" (since before reading any symbol, number of a's processed so far = 0 is even) and set of accepting states is simply {"even"}. To represent transition function, transition diagrams are a useful notation. Each state represented by a node (labeled with state), transitions represented by directed edges labeled with input symbol (i.e., d(q,a) = q' represented by edge from q to q' labeled with a). Initial state has "dangling" in-edge, accepting states have double circles for nodes (in ASCII picture below, accepting states will be boxed and non-accepting states will have no box or circle). a _ ---- -----> |/ \ -->|even| odd | b ---- <----- \_/ / |\ a \___/ b Proof that L(A) = L. By induction on |w|, prove equation (1) below: { "even" if w contains even number of a's, d*("even",w) = { { "odd" if w contains odd number of a's. Base case: (|w|=0) The only string of length 0 is e and d*("even",e) = "even" by definition. But e contains an even number of a's (0 is even) so equation (1) is satisfied. Ind. Hyp.: Let k >= 0 be an arbitrary integer and assume that equation (1) is satisfied for all strings w of length k. Ind. Step: Consider an arbitrary string w of length k+1. Since k>=0, k+1>=1 so w contains at least 1 symbol, i.e., w = w'x for some string w' of length k and some symbol x in {a,b}. Consider the possible values of x. Case 1: x = a Consider the possible values of d*("even",w'). Subcase A: d*("even",w') = "even" Then, d*("even",w) = d(d*("even",w'),a) = d("even",a) = "odd". But d*("even",w') = "even" means w' contains an even number of a's (by the ind. hyp.) so w = w'a contains an odd number of a's. Subcase B: d*("even",w') = "odd" Then, d*("even",w) = d(d*("even",w'),a) = d("odd",a) = "even". But d*("even",w') = "odd" means w' contains an odd number of a's (by the ind. hyp.) so w = w'a contains an even number of a's. In all subcases, w satisfies equation (1). Case 2: x = b Consider the possible values of d*("even",w'). Subcase A: d*("even",w') = "even" Then, d*("even",w) = d(d*("even",w'),b) = d("even",b) = "even". But d*("even",w') = "even" means w' contains an even number of a's (by the ind. hyp.) so w = w'b contains an even number of a's. Subcase B: d*("even",w') = "odd" Then, d*("even",w) = d(d*("even",w'),b) = d("odd",b) = "odd". But d*("even",w') = "odd" means w' contains an odd number of a's (by the ind. hyp.) so w = w'b contains an odd number of a's. In all subcases, w satisfies equation (1). Hence, equation (1) is satisfied for all strings of length k+1. Therefore, by induction, equation (1) is satisfied for all strings. - Now, use this information to prove L(A) = L: L(A) subset of L: Let w be an arbitrary string in L(A). This means that d*("even",w) is in F and since F = {"even"}, d*("even",w) = "even". By equation (1), this means w contains an even number of a's, i.e., w is in L. L subset of L(A): Let w be an arbitrary string in L. Then, w contains an even number of a's so d*("even",w) = "even", by equation (1). Since F = {"even"}, this means w is accepted by A, so w is in L(A). - To simplify transition diagrams, additional conventions: . multiple transitions from one state to another labeled with different input symbols can be combined into one edge with "compound" label; (e.g., for vending machine F.S.A., instead of having three edges from state 25 to state 30 -- one for each input symbol n, d, q -- have a single edge with label "n,d,q") . "dead" states (from which an accepting state can never be reached) are not drawn. - Careful! With these additional conventions, a "missing" transition in a diagram does *NOT* mean F.S.A. stays in that state: it means F.S.A. goes to dead state and rejects. Non-deterministic F.S.A. ------------------------ - Is a variation of DFSA. From each state, for an input symbol, say a, there may be more than one state that it may go. There is no restrictions on the choice of next state, if there are any. - It may even change state without reading any symbol. These are called e-transitions. - An input is accepted if there is **at least one** computation path that ends in an accepting state. - Example: Consider the following NFSA (picture 7.8 of the text) This NFSA accepts the language (1+0)*1. For input 011 there are at least 3 computations: q0 -> q0 -> q0 -> q1 or q0 -> q0 -> q0 -> q0 or q0 -> q0 -> q1 -> dead and only the first one ends in an accepting state. - A NFSA is a quintuple (Q,S,d, s, F) where . Q is a finite set of states . S is a finite alphabet . d: Q*(S U {e}) -> P(Q) is a transition function . s in Q is the start state . F subset Q is the set of accepting states - For any state q, E(q) is the set of states that are reachable from q by following *any* (including zero) number of e-transitions. - How do we define the extended transition function for a NFSA? Assume that x=ya for some string y. Intuitively, to find d^*(q,x), the set of states we may end in starting from q and reading input x, we must: . First, find each state q' that we can get to from q, by reading y which would be d*(q,y). . Then find each state q'' that can be reached from each q' by reading the last symbol (a). This is d(q',a). . Finally, we get to each state that can be reached from q'' by any number of e-transitions, which is E(q''). - The extended transition function of the NFSA is the function d*: Q*S^* -> P(Q) defined by structural induction: . Basis: if x=e then d*(q,x) = E(q) . Ind. Step: if x=ya for some y in S^* and a in S, then d*(q,x) = union ( union E(q'')) q' in d*(q,y) q'' in d(q',a) - It is clear that a DFSA is a special case of a NFSA. Interestingly, we can show that for any NFSA we can make a DFSA that accepts the same language. - Given a NFSA M=(Q,S,d,s,F) we construct a DFSA M'=(Q',S,d',s',F') as follows: . Q' = P(Q) . s' = E(s) (i.e the set of all states that are reachable from s by e-transitions) . F' = {q' in Q': q' intersection F is non-empty} (i.e. all states that contain an accepting state of M). . For any q' in Q' and a in S, d'(q',a) = union ( union E(q'')) r in q' q'' in d(r,a) - Example: A FSA A with three states {a,b,c} on alphabet S={0,1}, initial state is a. The transitions are: a -1-> b -1-> c -0-> c -1-> b -0,1-> b -e-> a -0-> c for this FSA, . E(a) = {a} . E(b) = {a,b} . E(c) = {c} The corresponding DFSA has 8 states {empty, a, b, c, ab, bc, ac, abc} The initial state is ac. The accepting states are {b, ab, bc, abc}. The transitions are: ac -1-> ab -1-> ab -0-> abc , a -1-> ab , a -0-> c -0-> c -1-> ab , b -0-> b -1-> abc , bc -0,1-> abc Closure properties of FSA-accepted languages ------------------------------------------ - If we apply an operation on a language that is accepted by a FSA and the result is also a language accepted by a FSA we say the operation has closure property. - Suppose L and L' are two languages accepted by two FSA M and M', respectively. The following operations have closure property: . Union: L union L'. We can construct a NFSA M'' that consist of M and M' by adding a state s which will be the initial state and has only two e-transitions to the initial states of M and M' (see fig 7.17 in the textbook). . Complement: ~L. Assume that M is a DFSA for L (if not make it a DFSA). ~M is exactly the same as M with the exception that the accepting states of ~M are precisely the non-accepting states of M. Remark: If M is not a DFSA this construction does NOT work (why?) . Intersection: L intersection L'. Since L intersection L' = ~(~L union ~L) and since these two operations are already proved to have closure property, so does intersection. . Concatination: L o L'. We can construct a NFSA M'' for L o L'. Take a copy of M and a copy of M' and add an e-transition from every accepting state of M to the initial state of M'. . Kleene Star: L^*. Take M and add an e-transition from every accepting state to its initial state.