CS 373, Spring 29. Solutions to Mock midterm (sed on first midterm in CS 273, Fll 28.) Prolem : Short nswer (8 points) The nswers to these prolems should e short nd not complicted. () If n NF M ccepts the empty string (i.e., ɛ), does M s strt stte hve to e n ccepting stte? Why or why not? No it does not hve to e n ccepting stte. ecuse M is n NF, we cn ccept the empty string y hving non-ccepting strt stte which hs n ɛ-trnsition (or sequence of such ɛ-trnsitions) to n ccepting stte. () Is every finite lnguge regulr? Why or why not? Yes. Every finite lnguge is regulr. You cn uild n NF for it y uilding liner DF tht recognizes ech string individully. Then join them ll to common strt stte using ɛ-trnsitions. (c) Suppose tht n NF M = (Q, Σ, δ, q, F ) ccepts lnguge L. Crete new NF M y flipping the ccept/non-ccept mrkings on M. Tht is, M = (Q, Σ, δ, q, Q F ). Does M ccept L (the set complement of L)? Why or why not? This only works for DF. Consider the following NF. It ccepts the lnguge {}. If you flip the ccept mrkings, it recognizes {, ɛ}. q q q 2 (d) Simplify the following regulr expression ( ) ɛ. This is equl to ɛ( ) which is just. This cn lso e written s + +.
Prolem 2: DF design (6 points) Let Σ = {, }. Let L e the set of strings in Σ which contin the sustring or the sustring. For exmple, L nd L, ut / L. Strings shorter thn three chrcters re never in L. Construct DF tht ccepts L nd give stte digrm showing ll sttes in the DF. You will receive zero credit if your DF uses more thn sttes or mkes significnt use of non-determinism., F Prolem 3: Rememering definitions (8 points) () Define formlly wht it mens for DF (Q, Σ, δ, q, F ) to ccept string w = w w 2... w n. The DF ccepts w if there is stte sequence s s... s n such tht s = q s n F, nd s i = δ(s i, w i ) for every i [i, n]. () Let Σ nd Γ e lphets. Suppose tht h is function from Σ to Γ. Define wht it mens for h to e homomorphism. mpping h is homomorphism if h(xy) = h(x)h(y) for ny strings x nd y. Or, equivlently, h is homomorphism if h(c c 2... c n ) = h(c )h(c 2 )... h(c n ) for ny sequence of chrcters c c 2... c n. 2
Or, you could sy it in words: the output of h on string is the conctention of its outputs on the individul chrcters mking up the string. Or you could even sy: h is homomorphism if it opertes on strings chrcter-y-chrcter. Prolem 4: NF trnsitions (6 points) Suppose tht the NF N = (Q, {,, 2}, δ, q, F ) is defined y the following stte digrm: 2 ǫ D 2 ǫ C E Fill in the following vlues: () F = F = {D}. () δ(, ) = δ(, ) = (c) δ(c, ) = δ(c, ) = {D, E} (d) δ(d, ) = δ(d, ) = {} (e) List the memers of the set {q Q D δ(q, 2)} E. (f) Does the NF ccept the word 2? (Yes / No) Yes. The stte sequence is CDCDD. 3
Prolem 5: NF to DF conversion (6 points) Convert the following NF to DF recognizing the sme lnguge, using the suset construction. Give stte digrm showing ll sttes rechle from the strt stte, with n informtive nme on ech stte. ssume the lphet is {, }., C D D CD D C, Prolem 6: Short Construction (8 points) () Give regulr expression for the lnguge L contining ll strings in whose length is multiple of three. E.g. L contins ut does not contin or. () () () () () (). 4
() Let Σ = {,, c}. Give n NF for the lnguge L contining ll strings in Σ which hve n or c in the lst four positions. E.g. nd c re oth in L, ut c is not. Notice tht strings of length four or less re in L exctly when they contin n or c. You will receive zero credit if your NF contins more thn 8 sttes.,,c,c,,c,,c,,c 2 3 4 Prolem 7: NF modifiction nd tuple nottion (8 points) For this prolem, the lphet is lwys Σ = {, }. Given n NF M tht ccepts the lnguge L, design new NF M tht ccepts the lnguge L = {twt w L, t Σ}. For exmple, if is in L, then nd re in L. () riefly explin the ide ehind your construction, using English nd/or pictures. The new NF M hs two copies of M s sttes, plus new initil nd finl sttes. Copy is for the strings strting in nd copy is for the strings strting in. When we red the first chrcter, we trnsition to the strt stte of the pproprite copy. When we rech finl stte in our copy, there is trnsition from tht finl stte to the new finl stte, consuming the pproprite input chrcter. E.g. the finl sttes of copy hve trnsition on into the new finl stte. () Suppose tht M = (Q, Σ, δ, q, F ). Give the detils of your construction of M, using tuple nottion. M = (Q, Σ, δ, q S, {q F }) where Q = {q S, q F } {q q Q} {q q Q} nd δ is defined s follows: δ (q S, ) = q δ (q S, ) = q δ (q, t) = {r r δ(q, t)} if q Q F, or if q F nd t = δ (q, t) = {r r δ(q, t)} if q Q F, or q F nd t = δ (q, ) = {q F } {r r δ(q, t)} for every q F 5
δ (q, ) = {q F } {r r δ(q, t)} for every q F δ (q, t) = for ll other inputs Notice tht the old finl sttes in ech copy of M need to keep ll their old trnsitions nd lso dd the trnsition into the new finl stte. Full credit didn t require getting solutely every detil correct. The ig mistke mny people mde ws to use single copy of the originl set of sttes, to which they just dded new strt nd end sttes to M. This doesn t llow the NF to rememer the first chrcter of the string, so it cn t verify tht the lst chrcter is the sme. Resonly well-formed versions of this nswer were worth 2/8 points. 6