CS 341: Foundtions of Computer Science II Prof. Mrvin Nkym Homework 3 Solutions 1. Give NFAs with the specified numer of sttes recognizing ech of the following lnguges. In ll cses, the lphet is Σ = {,1}. () The lnguge {w Σ w ends with } with three sttes.,1 1 2 3 () The lnguge {w Σ w contins the sustring 11, i.e., w = x11y for some x,y Σ } with five sttes.,1,1 1 1 1 2 3 4 5 (c) The lnguge {w Σ w contins t lest two s, or exctly two 1s} with six sttes.,1 1,1 ε 2 3 4 1 1 5 1 6 (d) The lnguge {ε} with one stte. 1
1 (e) The lnguge 1 with three sttes. 1 ε 1 2 3 2. () Show y giving n exmple tht, if M is n NFA tht recognizes lnguge C, swpping the ccept nd non-ccept sttes in M doesn t necessrily yield new NFA tht recognizes C. Answer: The NFA M elow recognizes the lnguge C = {w Σ w ends with }, where Σ = {,1}.,1 1 2 3 Swpping the ccept nd non-ccept sttes of M gives the following NFA M :,1 1 2 3 Note tht M ccepts the string 1 C = {w w does not end with }, so M does not recognize the lnguge C. () Is the clss of lnguges recognized y NFAs closed under complement? Explin your nswer. Answer: The clss of lnguges recognized y NFAs is closed under complement, which we cn prove s follows. Suppose tht C is lnguge recognized y some NFA M, i.e., C = L(M). Since every NFA hs n equivlent DFA (Theorem 1.39), there is DFA D such tht L(D) = L(M) = C. By prolem 3 on Homework 2, we then know there is nother DFA D tht recognizes the lnguge L(D). Since 2
every DFA is lso n NFA, this then shows tht there is n NFA, in prticulr D, tht recognizes the lnguge C = L(D). Thus, the clss of lnguges recognized y NFAs is closed under complement. 3. Use the construction given in Theorem 1.39 to convert the following NFA N into n equivlent DFA. ε 1 2 3, Answer: Let NFA N = (Q,Σ,δ,1,F), where Q =, Σ = {,}, 1 is the strt stte, F = {2}, nd the trnsition function δ s in the digrm of N. To construct DFA M = (Q,Σ,δ,q,F ) tht is equivlent to NFA N, first we compute the ε-closure of every suset of Q =. Then define Q = P(Q), so Set R Q ε-closure E(R) {1} {2} {2} {3} {3} {1,3} {2,3} {2,3} Q = {, {1}, {2}, {3},, {1,3}, {2,3}, }. The strt stte of M is then E({1}) =. The set of ccept sttes of M is F = { {2},, {2,3}, }. We define the trnsitions in the DFA M s in the following digrm: 3
{2,3}, Note tht we left out some of the sttes (e.g., {1}) in P(Q) from our digrm of the DFA M since they re not ccessile from the strt stte. Also, we hd to dd n rc from stte to itself lelled with, so tht this stte hs n rc leving it corresponding to ech symol in the lphet Σ, which is requirement for ny DFA. The lgorithm given in the notes nd textook will lwys correctly construct n equivlent DFA from given NFA, ut we don t lwys hve to go through ll the steps of the lgorithm to otin n equivlent DFA. For exmple, on this prolem, we egin y figuring out wht sttes the NFA cn e in without reding ny symols. In this cse, this is E({1}) = since 1 is the strting stte of the NFA, nd the NFA cn jump from 1 to 2 without reding ny symols y tking the ε-trnsition. Thus, we first crete DFA stte corresponding to the set : The stte is the strt stte of the DFA since this is where the NFA cn e without reding ny symols. The stte is lso n ccepting stte for the DFA since it contins 2, which is ccepting for the NFA. Now for DFA stte, determine where the NFA cn go on n from ech NFA stte within this DFA stte, nd where the NFA cn go on from ech NFA stte within this DFA stte. On n, the NFA cn go from stte 1 to stte 3; lso, the NFA cn go from stte 2 to 1, nd then it lso cn go further from 1 to 2 on the ε. So from NFA sttes 1 nd 2 on n, the NFA cn end up in sttes 1, 2, nd 3, so drw trnsition in the DFA from stte to new stte, which is n ccepting stte since it contins 2 F: 4
Similrly, to determine where the DFA moves on from DFA stte, determine ll the possiilities of where the NFA cn go from NFA sttes 1 nd 2 on. From stte 1, the NFA cn t go nywhere on ; lso, the NFA cn t go nywhere from stte 2 on. Thus, the NFA cn t go nywhere from sttes 2 nd 3 on, so we dd -edge in the DFA from stte to new DFA stte, which is not ccepting since it contins no ccept sttes of the NFA: Now every time we dd new DFA stte, we hve to determine ll the possiilities of where the NFA cn go on n from ech NFA stte within tht DFA stte, nd where the NFA cn go on from ech NFA stte within tht DFA stte. For DFA stte, we next determine where the NFA cn go on n from ech of the NFA sttes 1, 2 nd 3. From NFA stte 1, the NFA on n cn go to NFA stte 3; from NFA stte 2, the NFA on n cn go to NFA stte 1, nd then it cn lso further jump to 2 on ε; from NFA stte 3, the NFA on n cn go to NFA stte 2. Thus, if the NFA is in sttes 1, 2 nd 3, it cn go on n to sttes 1, 2 nd 3, so we dd to the DFA n -edge from to. Now we determine where the -edge from DFA stte goes to. To do this, we exmine wht hppens to the NFA from sttes 1, 2 nd 3 on. If the NFA is in stte 1, then there is nowhere to go on ; if the NFA is in stte 2, then there is nowhere to go on ; if the NFA is in stte 3, then the NFA cn go to 2 or 3 on. Hence, if the NFA is in sttes 1, 2 nd 3, the NFA on cn end in sttes 2 nd 3. Thus, in the DFA, drw n edge from stte to new stte {2,3}, which is ccepting since it contins 2 F: 5
{2,3} Now do the sme for DFA sttes {2,3} nd. If ny new DFA sttes rise, then we need to determine the nd trnsitions out of those sttes s well. We stop once every DFA stte hs n -trnsition nd -trnsition out of it. Accepting sttes in the DFA re ny DFA sttes tht contin t lest one ccepting NFA stte. We eventully end up with the DFA elow s efore: {2,3}, FortheDFAstte,thererenoversionsoftheNFAcurrentlyctive, i.e., ll threds hve crshed, so the NFA cnnot proceed nd the input string will not e ccepted. However, ccording to the definition of DFA, ech stte must hve edges leving it corresponding to ech symol in the lphet Σ. Thus, we dd loop from the DFA stte ck to itself leled with Σ, which in our cse is,. 4. Give regulr expressions tht generte ech of the following lnguges. In ll cses, the lphet is Σ = {,}. 6
() The lnguge {w Σ w is odd}. Answer: ( )(( )( )) () The lnguge {w Σ w hs n odd numer of s}. Answer: ( ) (c) The lnguge {w w contins t lest two s, or exctly two s}. Answer: ( ) (d) The lnguge {w Σ w ends in doule letter}. (A string contins doule letter if it contins or s sustring.) Answer: ( ) ( ) (e) The lnguge {w Σ w does not end in doule letter}. Answer: ε ( ) ( ) (f) The lnguge {w Σ w contins exctly one doule letter}. For exmple, hs exctly one doule letter, ut hs two doule letters. Answer: (ε )() () (ε ) (ε )() () (ε ) 5. Suppose we define restricted version of the Jv progrmming lnguge in which vrile nmes must stisfy ll of the following conditions: A vrile nme cn only use Romn letters (i.e.,,,..., z, A, B,..., Z) or Aric numerls (i.e.,, 1, 2,..., 9); i.e., underscore nd dollr sign re not llowed. A vrile nme must strt with Romn letter:,,..., z, A, B,..., Z The length of vrile nme must e no greter thn 8. A vrile nme cnnot e keyword (e.g., if). The set of keywords is finite. Let L e the set of ll vlid vrile nmes in our restricted version of Jv. () Let L e the set of strings stisfying the first 3 conditions ove; i.e., we do not require the lst condition. Give regulr expression for L. Answer: To simplify the regulr expression, we define Then regulr expression for L is = {,,...,z,a,b,...,z} Σ 2 = {,1,2,...,9}. ( Σ 2 ε) ( Σ 2 ε). } {{ } 7 times Note tht y including the ε in ech of the lst prts, we cn generte strings tht hve length strictly less thn 8. 7
() Prove tht L hs regulr expression, where L is the set of strings stisfying ll four conditions. Answer: We proved in Homework 1, prolem 4(), tht L is finite. Thus, L is regulr, so it hs regulr expression. Although the prolem didn t sk for it, we cn write regulr expression for L y listing ll of the strings in L nd putting in etween ech pir of consecutive strings. This works ecuse L is finite. (c) Give DFA for the lnguge L in prt (), where the lphet Σ is the set of ll printle chrcters on computer keyord (no control chrcters), except for prentheses to void confusion. Answer: Define nd Σ 2 s in prt (), nd let Σ 3 = Σ ( Σ 2 ) e ll of the other chrcters on computer keyord except for prentheses. Then DFA for L is s follows: Σ 2 Σ 2 1 2 3 4 9 Σ 2 Σ 3 Σ 3 Σ 3 Σ 3 Σ Σ 6. Define L to e the set of strings tht represent numers in modified version of Jv. The gol in this prolem is to define regulr expression nd n NFA for L. To precisely define L, let the set of digits e = {, 1, 2,..., 9}, nd define the set of signs to e Σ 2 = {+, -}. Then L = L 1 L 2 L 3, where L 1 is the set of ll strings tht re deciml integer numers. Specificlly, L 1 consists of strings tht strt with n optionl sign, followed y one or more digits. Exmples of strings in L 1 re 2, +9, nd -241. L 2 is the set of ll strings tht re floting-point numers tht re not in exponentil nottion. Specificlly, L 2 consists of strings tht strt with n optionl sign, followed y zero or more digits, followed y deciml point, nd end with zero or more digits, where there must e t lest one digit in the string. Exmples of strings in L 2 re 13.231, -28. nd.124. All strings in L 2 hve exctly one deciml point. L 3 is the set of ll strings tht re floting-point numers in exponentil nottion. Specificlly, L 3 consists of strings tht strt with string from L 1 or L 2, followed y E or e, nd end with string from L 1. Exmples of strings in L 3 re -8.1E-83, +8.E5 nd 1e+31. Assume tht there is no limit on the numer of digits in string in L. Also, we do not llow for the suffixes L, l, F, f, D, d, t the end of numers to denote types (long integers, flots, nd doules). Define Σ s the lphet of ll printle chrcters on computer keyord (no control chrcters), except for prentheses to void confusion. 8
() Give regulr expression for L 1. Also, give n NFA nd DFA for L 1 over the lphet Σ. Answer: A regulr expression for L 1 is R 1 = (+ - ε) Σ 1 where = {, 1, 2,..., 9} s previously defined. An NFA for L 1 is q 1 q 2 q 3 Define Σ 3 = Σ ( Σ 2 ) with Σ 2 = {-, +}, s efore. Then DFA for L 1 is 3 Σ 2 1 2 Σ Σ Σ 3 4 Σ Notice tht the DFA is more complicted thn the NFA. () Give regulr expression for L 2. Also, give n NFA for L 2 over the lphet Σ. Answer: A regulr expression for L 2 is R 2 = (+ - ε)( Σ 1.Σ 1.Σ 1 ) Note tht the regulr expression (+ - ε)σ 1.Σ 1 is not correct since it cn generte the strings., +. nd -., which re not vlid floting-point numers. An NFA for L 2 is r 1 r 2 r 3 r 5 r 4 9
(c) Give regulr expression for L 3. Also, give n NFA for L 3 over the lphet Σ. Answer: A regulr expression for L 3 is R 3 = (R 1 R 2 )(E e)r 1 where R 1 nd R 2 re defined in the previous prts. An NFA for L 3 is s 3 s 1 s 2 E, e s 4 s 5 s 6 s 7 s 8 (d) Give regulr expression for the lnguge L. Also, give n NFA for L over the lphet Σ. Answer: Note tht L = L 1 L 2 L 3, so regulr expression for L is R 4 = R 1 R 2 R 3 We cn construct n NFA for L y tking the union of the NFA s for L 1, L 2 nd L 3, s follows: 1
q 1 q 2 q 3 ε q ε r 1 r 2 r 3 r 5 r 4 ε s 3 s 1 s 2 E, e s 4 s 5 s 6 s 7 s 8 A simpler NFA for L is to tke the NFA for L 3 nd mke s 3 nd s 5 lso ccepting sttes in ddition to s 8. 11