=========================================================================== CSC 236 Sample Solutions for Week 11 Tutorial Fall 2007 =========================================================================== A. For each regular expression R below, state whether or not the string 010 belongs to L(R), and why. { For each reg.exp., ask the students what they think the answer is, and why. After the first one, go over the general technique for showing s in L(R): break up s into parts and describe how each part belongs to language of corresponding part of R. After the second one, go over general technique for showing s notin R: describe a property of each string in L(R) and show s does not have that property. You can go fairly quickly over these, except maybe the last one (or if there are questions about any of the others). } - 010 in L((e+1)0*1*0): pick e in L(e+1), 0 in L(0*), 1 in L(1*), 0 in L(0). - 010 notin L(0(11)*0): each string in L(0(11*)0) has an even number of 1's, but 010 has an odd number of 1's. - 010 in L(e+0)(1*1*)*(0+1)): pick 0 in L(e+0), 1 in L((1*1*)*) (because e in L(1*) and 1 in L(1*)), and 0 in L(0+1). - 010 in L((0*1*)*): pick 01 in L(0*1*) (0 in L(0*), 1 in L(1*)) and 0 in L(0*1*) (0 in L(0*), e in L(1*)) - 010 notin L((0+1)(0*+1*)): by distributivity, (0+1)(0*+1*) == 0(0*+1*) + 1(0*+1*) == 00* + 01* + 10* + 11*, and 010 notin L(00*) (no string in L(00*) contains a 1), 010 notin L(01*) (no string in L(01*) contains a 1 followed by a 0), 010 notin L(10*) (no string in L(10*) contains a 0 followed by a 1), and 010 notin L(11*) (no string in L(11*) contains a 0). B. For each language L_i below, give a reg.exp. R_i such that L(R_i) = L_i and a F.S.A. A_i such that L(A_i) = L_i. Briefly explain why your answers are correct. { Try to go through the first two reasonably quickly to leave time for the last one. } - L_1 = { s in {a,b}* : s contains the substring bb } . FSA: Q: What information do we need to keep track of? A: Only need to remember last symbol read, more precisely, whether or not last symbol read was 'b'. Q: OK, how do we get started? A: Put down initial state (with meaning "last symbol not b"), then add transitions one by one, creating new states as needed. Repeat for each new state. Then, figure out which states should be accepting. { Carry this out step-by-step with them. } Transition diagram in ASCII, using parentheses around accepting states, and with self-loops not indicated explicitly (labels simply written next to state). a b b a,b -> q_e ---> q_b --->(q_bb) |\_a__/ Transition table (to be sure there is no confusion with my ASCII art above) -- initial q_e, accepting {q_bb}: ___|_ q_e __ q_b __ q_bb _ a | q_e q_e q_bb b | q_b q_bb q_bb . RE: Easy: (a+b)*bb(a+b)*. - L_2 = { s in {a,b}* : s does not contain the substring bb } . FSA: Same as above, except swap accepting/rejecting status of each state! . RE: RE's can only describe patterns that strings *have*, not patterns that strings do not have. Must figure out how to turn negative into positive, i.e., figure out how to phrase "no substring bb" into what the string *can* contain. Q: If 'bb' cannot appear, what does that mean for each 'b' in the string? A: It must be followed by 'a' (or be the last symbol in the string). Q: How do we describe this using RE's? A: String made up of 'a' and 'ba', with optional last b: (a + ba)* (b + e) - L_3 = { s in {a,b}* : s contains the substring bb exactly once } . FSA: Q: Approach? A: Start with FSA for L_1: must check s contains bb. a b b a,b -> q_e ---> q_b --->(q_bb) |\_a__/ Q: What next? A: Must add states to make sure bb does not appear again, i.e., keep track of last one or two symbols read using new states (to represent information about string *following* first bb). a b b a a b b a,b -> q_e ---> q_b --->(q_bb)--->(q_a)--->(q_ab)---> q_B |\_a__/ \ |\_a__/ /| \__________b__________/ As a transition table: ___|_ q_e __ q_b __ q_bb __ q_a __ q_ab __ q_B _ a | q_e q_e q_a q_a q_a q_B b | q_b q_bb q_B q_ab q_B q_B . RE: Q: Aren't we done? Can't we just combine the RE's from last two languages to get (a+ba)*(b+e)bb(a+ba)*(b+e)? A: Problem: this allows more than one bb, in multiple ways: . because bbb is in L((b+e)bb) . because bbba is in L(bb(a+ba)*) Q: How to fix first problem? A: Remove first "(b+e)": there must be either nothing or a string that ends with a in front of bb. Q: How to fix second problem? A: Redo expression for L_2 in a different way: string without bb means each b either first or preceded by a, which gives RE (b+e)(a+ab)* -- without (b+e) part, as above. End result: (a + ba)* bb (a + ab)*