CS2MJ3 – Fall 2012. Sample solutions to the assignment 1. Total of this assignment is 92 pts. 100% of this assignment is 88 pts. Each assignment is worth 7%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. 1.[7] a.[2] a C b b a q2 b.[4] i.[1] ii.[1] iii.[1] vi.[1] c.[1] bababa, bbbaa q0 q1 a b q0 → a → q0 → b → q1 → a → q2 → a → q2 q0 → b → q1 → b → q1 → b → q1 → a → q2 → b → q0 → b → q1 q0 → b → q1 → a → q2 → b → q0 → a → q0 → b → q1 → a → q2 q0 → b → q1 → b → q1 → b → q1 → a → q2 → a → q2 1 2.[4] a[2] b C q0 a b q1 b q2 a a b.[1] q0 → b → q0 →a → q1 → b → q2 → a → q1 → a → q1 → b → q2 c.[1] abb - accepted, ba - rejected 2 3.[8] a.[5] if the solution is non-deterministic, a[8] if it is deterministic. The one below is deterministic. C0 nickel 5 dime nickel quarter dime 10 quarter quarter nickel dime 15 dime nickel quarter 20 quarter nickel 25 quarter dime $30 quarter dime nickel dime nickel b.[0] if (a) is deterministic, b[3] if (a) is non-deterministic and (b) is derived from (a). In both cases total for (a)+(b) is 8. bonus:[4] If the deterministic version returns change, so there might be more than 100% from this problem. Similarly as (a) above. The state ‘≥30' has to be replaced by the states ‘30 and no change’, ‘35 - change 5', ‘40 - change 10', ‘45 -change 15', ‘50 change 20' and ‘55 change ≥25'. For instance from the state ‘20', nickel goes to ‘30', dime to ‘35', quarter to ‘45'. 3 4.[8] b.[2] a C q0 c g q2 q1 c b a.[2] E({q0}) = {q0,q1}, E({q1}) = {q1}, E({q2}) = {q2}. c.[4] a C q0 b c q2 b q1 sink 4 c 5.[8] b.[2] C q0 a g a q3 b b a q1 g b b q2 a.[2] E({q0}) = {q0,q3}, E({q1}) = {q1}, E({q2}) = {q2,q3}, E({q3}) = {q3}. c.[4] Note that after standard removal of ε-moves, the automaton has δ’(q2,a) = δ’(q2,a) = Ø, so we can remove the state q2 and the language accepted remains the same. Then the standard procedure (restricted to the states that can be reached from the initial state lead to the following solution: C {q 0} a b {q 2} {q1,q 3} b a a b 5 6.[10] The states should be numbers {0,1,...,5}, each represent a number modulo 6. In other words, 0 represents {0,6,12,18,...}, 1 represents {1,7,13,19,...}, 2 represents {2,8,14,20,...,}, 3 represents {3,9,15,21,...}, 4 represents {4,10,16,22,...}, 5 represents {5,11,17,23,...}. The transition function represents addition modulo 6, i.e. δ(k,l)= (k+l) mod 6, where k=0,...,5, l=1,2,3. All together: M = ({0,1,2,3,4,5}, {1,2,3}, δ, 0, {0}), i.e. initial state is zero, and F={0}, δ 1 2 3 0 1 2 3 1 2 3 4 2 3 4 5 3 4 5 0 4 5 0 1 5 0 1 2 7.[15] An automaton analyses the string and once a substring of four without b or with more than one b is found, automaton moves to the garbage/sink state. The problem is underspecified since it does not say what to do with the strings of length smaller than 4, so both solutions (they are accepted or not) are o.k. So Σ = {a,b,c}, Q = {s0,[b],[x],[bx],[xb],[xx],[xxx],[bxx],[xbx],[xxb],[xxxx],[bxxx],[xbxx],[xxbx],[xxxb]}, and s0 is an initial state, F = { [bxxx], [xbxx], [xxbx], [xxxb]} - if we assume that all strings of length smaller than 4 are rejected, or F = {s0,[b],[x],[bx],[xb],[xx],[bxx],[xbx],[xxb],[xxx],[bxxx], [xbxx], [xxbx], [xxxb]} - if we assume that all strings of length smaller than 4 are accepted. In the second case only the state [xxxx] is non-final, in both cases it is a sink state. The states are interpreted as follows, for {[b],[x],[bx],[xb],[xx],[xxx],[bxx],[xbx],[xxb]} it means that the all paths from s0 to s are described by s, for instance δ(s0,w)=[xxb] means that w=xxb, where x is either a or c, i.e. w ∈ {aab,acb,cab,ccb}. For the states from {[bxxx],[xbxx],[xxbx],[xxxb]}, for instance xxbx means that the last four symbols satisfy the pattern xxbx, where x is either a or c. 6 The state [xxxx] means either that the last 4 symbols do not contain b, or that they contain more than one b. The function δ is now quite obvious, for instance δ([xxbx], a) = δ([xxbx],c) = [xbxx], δ([xxbx,b) = [xxxx] (as we assume that [xxxx] also represent [xbbx], see above). Formally: δ a b c s0 [x] [b] [x] [b] [bx] [xxxx] [xb] [x] [xx] [xb] [xx] [bx] [bxx] [xxxx] [bxx] [xx] [xxx] [xxb] [xxx] [xb] [xbx] [xxxx] [xbx] [bxx] [bxxx] [xxxx] [bxxx] [xbx] [xbxx] [xxxx] [xbxx] [xxb] [xxbx] [xxxx] [xxbx] [xxx] [xxxx] [xxxb] [xxxx] [bxxx] [xxxx] [xxxb] [xxxx] [xbxx] [bxxx] [xxxx] [bxxx] [xxbx] [xbxx] [xxxx] [xbxx] [xxxb] [xxbx] [xxxx] [xxbx] [xxxx] [xxxx] [xxxx] [xxxx] 7 8.[15] [10] Let M be any non-deterministic automaton such that L=L(M). Note that any deterministic automaton can be treated as a special case of a non-deterministic automaton. Intuition is given by the figure below: a a a qF a a a Automaton M Automaton M’ Formal proof. Let M=(Q,Σ,δ,q0,F). Define M’=(Q∪{qF},Σ,δ’,q0, {qF}), where qF ∉ Q. Let S be the following set of states: q∈S iff ∃a∈Σ. δ(q,a)∩F≠Ø. In other words, from S, one may reach a final state in one step. The mapping δ’ is defined as follows: for all q∈Q, all a∈Σ: δ’(q,a) = if q∉S then δ(q,a) else δ(q,a)∪{qF}, and δ’(qF,a)=Ø. Let x≠ε, i.e. x=ya. Hence: x=ya ∈ L(M) ⇔ δ(q0,ya)∩F≠Ø ⇔ δ(q0,y)∩S≠Ø ⇔ δ’(q0,y)∩S≠Ø ⇔ qF ∈ δ’(q0,ya) ⇔ x=ya ∈ L(M’). [5] Yes, the assumption that the language does not contain the empty string is essential. The results is false if ε∈L(M). Consider the automaton below: C a It accepts/generates the language {ε,a}. The initial state must be a final state since ε is accepted. If this is the only final state, and a is accepted than there must be an arrow from this state to itself labelled by a. But then every ak , all k, is also accepted, a contradiction. 8 9.[11] Solutions below are not unique as there are other equivalent regular expressions for each problem. 1.[4] a*bb*a(a*ba*bb*a)*. Proof: a*bb*a is the set of all sequences from q0 to q2, a*ba*bb*a is the set of all sequences from q2 to q2. 4.[3] a*cc* ∪ a*b* 5.[4] a(a∪b)* ∪ bb*a(a∪b)* = (a∪bb*a)(a∪b)* 9
© Copyright 2024