=========================================================================== CSC 236 Sample Solutions for Week 12 Tutorial Fall 2007 =========================================================================== Consider the following language. L = { s in {0,1}* : the third-last symbol of s is a 1 } A. Give a RE for L. Q: What's a RE for the last three symbols of a string in L? A: 1 (0+1) (0+1) Q: What can come before? A: Anything, so R = (0+1)* 1 (0+1) (0+1). B. Construct a NFA for L. Q: How can we use non-determinism to help? A: From initial state, have transitions for 1 followed by exactly two more symbols, to accepting state, and also transition to remain in initial state -- if third last symbol is 1, NFA will accept by taking transition out of initial state at the right point. Result (using ASCII conventions from class -- accepting states inside parentheses, loops implied by labels next to states): 0,1 1 0,1 0,1 --> q_0 ---> q_1 ---> q_2 --->(q_3) Q: How does this work? A: Example: 010110 C. Construct a DFA equivalent to your NFA from Question A, using the subset construction. (See if you can simplify the resulting DFA.) Q: What's the construction again? A: Start with initial state {q_0}, then repeat until all states have transitions for each symbol: pick a state with a missing transition, add that transition (creating new states as necessary). (Use ASCII convention from class, e.g., set of states {q_0,q_1,q_3} represented as q_013.) Initial: --> q_0 0 1 Add transitions to q_0: --> q_0 ---> q_01 { Continue step-by-step, taking suggestions as you go. } Final result, with transitions out of "bottom" level of states indicated but not drawn in (you should draw them in for yourself): (q_03) -0-> q_0, -1-> q_01 0/| / q_02 --->(q_013) -0-> q_02, -1-> q_012 0/| 1 0 1 / --> q_0 ---> q_01 \_ 1\| 0 q_012 --->(q_023) -0->(q_03), -1->(q_013) \_ 1\| (q_0123) -0->(q_023), -1->(q_0123) No simplification possible: no rejecting state from which it is impossible to reach accepting state, and no accepting state from which it is impossible to reach rejecting state. D. Construct a RE equivalent to your DFA from part C, using the state elimination construction. (See if you can simplify the resulting RE using equivalences from class.) Q: What's the construction again? A: For each accepting state q, eliminate all but q and initial state to obtain RE R_q; overall RE = union of R_q over accepting q. { Warning! This is very messy. Go over one of the eliminations in more detail, then just do a few more quickly, with suggestions from the students, taking time to go over anything the students ask you about. It would be good to get to the regular expression for one of the accepting states (like q_03), but don't even try to cover everything! } Start (redrawn): _______________0_______________ | | | 1-------------------(q_03) | | 0/| |\ | | / | | | q_02 <------0 | | | /| \_ | | | | / 1\| | | | | 0/ (q_013) |0 | | / / /| | \| 1 \| / / | | --> q_0 ---> q_01 / |1 | 0 \ 1/ | | \ / | / 1\ / (q_023) \ / 0/| /| \| |/ / | q_012 |0 \_ | 1\| | (q_0123) 1 Eliminate q_01: __________0__________ | | | 10------(q_03)----- | | 0/| |\ | | \| / | | | q_02 <------0 | | | /| \_ | | | | / 1\| | | | | 10/ (q_013) |0 | | / / /| | | \| / / | | | --> q_0 / |1 | |11 0 \ 1/ | | | \ / | / | 11\ / (q_023) | \ / 0/| /| | \| |/ / | | q_012 |0 | /| \_ | | | 1\| | | | (q_0123) 1 | |__________________| Eliminate q_02: 100 0-------------------(q_03)-------- | /| /|| |\_ | | / || \_ | | / 00||101 | | | ___100____/ || | | | / ||/ | | | / _____101_____>(q_013) 01 |0 | | / / / /| | | \| / / / | | | --> q_0 / |1 _| |11 0 \ 1/ | _/ | \ / | / | 11\ / (q_023) | \ / 0/| /| | \| |/ / | | q_012 |0 | /| \_ | | | 1\| | | | (q_0123) 1 | |_____________________| Eliminate q_012: _____________ 100 / | 0---------(q_03)-------- | | /| /|| |\_ | | | / || \_ | | | / 00||101 | | | | 100/ || | | | | / ||/ | | | | / (q_013) 01 |0 |110 | | / _101/| /|| \ | | | \| / / || \ | | | --> q_0 1||10 \_| | |111 0 \ \____ || _/\ | | \ 110\| ||/ / | | | \ (q_023)<---+-- | \ /| | | 111\ | | | \ 0| /11 | \ | / | \| | |/ | (q_0123)<---------- 1 Now we have to start eliminating accepting states to keep only one at a time. Let's do them bottom to top. Eliminate q_0123: 100 0---------(q_03)-------- | /| /|| |\___ | | / || | | | / 00||101 | | | 100/ || | | | / ||/ | | | / (q_013) 01 |0 |110+1111*0 | / _101/| /|| | | \| / / ||10+ | | --> q_0 1||111*0 | | 0 \____ || ___| | 1111*0+110\| ||/ / | (q_023)<------ Simplify: 11* == 1*1 so 1111*0 == 11*110 and 11*110+110 == (11*+e)110 == 11*10. Similarly, 10+111*0 == 11*0 100 0---------(q_03)-------- | /| /|| |\___ | | / || | | | / 00||101 | | | 100/ || | | | / ||/ | | | / (q_013) 01 |0 |11*10 | / _101/| /|| | | \| / / || | | --> q_0 1||11*0 | | 0 \____ || ___| | 11*10\| ||/ / | (q_023)<------ Eliminate q_023, and simplify (as above, R+11*R == 1*R): 0-----------------(q_03) 100+11*100 | _/| /|| | _/ || | _/ || |100+11*100/ ||101+11*101 | _/ 11*00+00|| | _/ || | _/ || \| / 101+11*101 ||/ --> q_0 ------------->(q_013) 01+11*01 0 0-----------------(q_03) 11*00 | _/| /|| | _/ || | _/ || | 11*00/ ||11*01 | _/ 1*00|| | _/ || | _/ || \| / 11*01 ||/ --> q_0 ------------->(q_013) 1*01 0 Eliminate q_013: <--------------------------- --> q_0 0 (q_03) 11*00 + 11*01(1*01)*1*00 0 ---------------------------> 11*00 + 11*01 (1*01)* 1*00 Simplify: 11*00 + 11*01 (1*01)* 1*00 == 1 (e + 1*01 (1*01)* ) 1*00 == 1 (1*01)* 1*00 <--------------- --> q_0 0 (q_03) 1 (1*01)* 1*00 0 ---------------> 1 (1*01)* 1*00 So R_03 = 0*1 (1*01)* 1*00 ( 1 (1*01)* 1*00 + 00*1 (1*01)* 1*00 )* == 0*1 (1*01)* 1*00 ( 0*1 (1*01)* 1*00 )* Now, we would have to go back and eliminate q_03, then go further back to eliminate q_03 and q_013 to single out q_023 and q_0123. Finally, we would take the union of each RE... Compare this with the RE that we designed at the very beginning!