Mth Circles Finite Automt Question Sheet 3 (Solutions) Nickols Rollick nrollick@uwterloo.c Novemer 2, 28 Note: These solutions my give you the nswers to ll the prolems, ut they usully won t tell you how to get the nswer. All the fun nd profit lies in finding the nswers for yourself... Also e wre tht some questions hve more thn one solution this will only provide you with one of them! Questions from Lesson. Descrie ll strings ccepted y ech of the following NFAs: () ; Solution: This NFA ccepts ll the strings tht strt with.
() ; 2 Solution: This NFA ccepts the string, nd nothing else. (c) 4 5 ; 2 3 ; Solution: This NFA ccepts the strings tht either strt with, or end with. 2
2. Drw n NFA stisfying ech set of conditions: () It hs four sttes, nd ccepts the lnguge of strings tht hve no s, or else hve one or more s followed y single. Solution: 2 3 () It hs three sttes nd llows three possile symols:,, nd c. It ccepts the lnguge defined y the following conditions: The empty string is in the lnguge. Given string in the lnguge, dding or c to the end gives nother string in the lnguge. (For exmple,, c, c,, cc ll elong to this lnguge.) Solution: c 2 3
3. Given the following NFAs, construct DFAs tht ccept the sme lnguge: () ; ; 2 Solution: As discussed during the session, designing DFA tht keeps trck of ll possile universes leds to: fg fg ; f2g f;g f;;2g B ; ; f;2g 4
() 2 Solution: Agin, we cn follow the sme possile universes construction tht we discussed during the session. The ccepting sttes in the new DFA re the ones with in them, since stte is the only ccepting stte of the NFA. fg f;2g B ; 5
Extr Questions 4. Here, we will see tht every finite lnguge uilt out of strings of s nd s is regulr. In other words, given ny finite collection of strings, there is some DFA tht ccepts exctly those strings nd no others. () Drw n NFA with four sttes nd three rrows tht ccepts the string nd nothing else. Now drw n NFA with six sttes nd five rrows tht ccepts the string nd nothing else. Solution: For : 2 3 For : 2 3 5 4 () Building on the pttern from the previous prt, suppose we re given ny string of s nd s (mye it s ). Descrie how to uild n NFA tht ccepts tht string, nd nothing else. Solution: If the string hs n symols in it, we uild n NFA tht hs n + sttes, lelled up to n, nd n rrows. There is one rrow from stte to stte lelled with the first letter of the string, one from stte to stte 2 lelled with the second letter, nd so on. Only the lst stte n is n ccepting stte. Unless the input to the NFA is exctly the specified string, the NFA will either not mke it to stte n nd reject the string, or it will red letter tht it cn t hndle, nd rek down. 6
(c) Given ny single string, explin why there is DFA (not just n NFA) ccepting tht string, nd nothing else. Solution: In the previous prt, we showed there ws n NFA tht ccepts the given string, nd nothing else. Applying the prllel universes construction, we cn tke tht NFA nd get DFA ccepting the sme lnguge (so exctly tht string, nd nothing else). (d) Lst time, we sw tht for ny two DFAs, ccepting ny two lnguges, we cn uild nother DFA ccepting the strings in either lnguge. Using this, explin how we cn tke ny two strings nd uild DFA ccepting those two strings nd nothing else. Solution: Given ny two strings, we cn pply the previous prt to find two DFAs, ech of which ccepts exctly one of the strings, nd nothing else. Applying the construction from lst time, we cn uild new DFA ccepting only the strings ccepted y either DFA. In other words, this new DFA ccepts the two given strings, nd nothing else. (e) Building on the ide from the previous prt, explin how we cn tke ny finite numer of strings nd uild DFA ccepting those strings nd nothing else. Solution: Suppose we hve finite collection of strings, which we ll cll w,..., w n. By prt (c), we cn find DFAs ccepting w nd nothing else, w 2 nd nothing else, w 3 nd nothing else, etc. Tking the DFA ccepting w nd nothing else nd the one ccepting w 2 nd nothing else, we uild new DFA ccepting the strings tht either of these DFAs ccept. In other words, this new DFA ccepts w, w 2, nd nothing else. Next, we tke this new DFA nd the one ccepting w 3 nd pply the sme construction, giving DFA ccepting w, w 2, w 3, nd nothing else. Repeting this over nd over gin, we end up with DFA tht ccepts exctly w,..., w n. 7
5. Given ny NFA, explin how to construct new NFA with exctly one ccepting stte tht ccepts the sme lnguge. (Hint: Try this on few exmples first the NFA in Question (c) is good plce to strt.) Solution: Here is how the construction works. Tke the originl NFA nd dd in one new stte (this will e the ccepting stte of the new NFA). Tke ll the ccepting sttes of the originl NFA nd dd lnk rrow from those sttes to the new stte. Then mke ll sttes of this new NFA rejecting sttes, except for the new one we just dded in. Why does this ccept the sme lnguge? We hve to check tht it ccepts nd rejects exctly the sme strings s the originl NFA. First, suppose string is ccepted y the originl NFA. This mens there is some collection of choices the originl NFA cn mke when reding the string, where it ends up t n ccepting stte. Since we hven t tken wy ny rrows in the new NFA, the new NFA cn mke the sme choices nd end up t the sme stte. The only prolem is tht this is not n ccepting stte ny more. But in the new NFA, there must e lnk rrow from this stte to the new ccepting stte, so this new NFA cn choose to follow the lnk rrow fter reding the string nd ccept it. Now suppose we hve string tht is rejected y the originl NFA. This mens no mtter wht choices the originl NFA mkes when reding the string, it will lwys e rejected. Note tht the only extr choices the new NFA cn mke re to follow the lnk rrows to the new ccepting stte. But since there re no rrows coming out of tht stte, if the NFA still hs letters left to red when it does this, it will rek nd reject the string. So the only wy the new NFA cn ccept string tht the old NFA rejected is if it follows one of the new lnk rrows fter reding ll the symols. The only wy it cn do tht is if it psses through n ccepting stte for the old NFA fter reding ll the symols, which would cuse the string to e ccepted y the originl NFA too. An exmple might e useful in mking this clerer. Here is wht we get when pplying this construction to the NFA from Question (c). The new ccepting stte hs een lelled N (for new ). 4 5 ; 2 N 3 ; 8
6. For ny string, the reverse of tht string is otined y reversing the order of ll the symols. For exmple, the reverse of is, nd the reverse of is gin. Given lnguge, we cn define the reverse lnguge to e wht you get if you reverse ll the strings in the originl lnguge. If lnguge is regulr, explin why the reverse lnguge is lso regulr. (Hint: Strting with DFA ccepting the originl lnguge, turn it into n NFA with single ccepting stte tht ccepts the sme lnguge, using the previous question. Then convert tht NFA into new NFA ccepting the reverse lnguge.) Solution: Since every NFA cn e converted into DFA ccepting the sme lnguge, it is enough to uild n NFA ccepting the reverse lnguge. If lnguge is regulr, we cn find DFA tht ccepts the lnguge. Since DFA is specil kind of NFA, we cn pply the previous question to turn this DFA into n NFA with single ccepting stte tht lso ccepts the lnguge. Finlly, we turn this NFA into n NFA ccepting the reverse lnguge. All we need to do is chnge the initil (strting) stte of the NFA into n ccepting stte, chnge the unique ccepting stte of the NFA into n initil stte, nd reverse ll of the rrows. Then, given string in the originl lnguge, there is some wy the originl NFA cn ccept it, y following collection of rrows from the initil stte to the single ccepting stte. By strting t the ccepting stte nd following ll those rrows ckwrds, we end up ck t the initil stte, which mens tht our new NFA ccepts the reverse string. The rgument works the other wy too: if the new NFA ccepts string, then in the originl NFA, there is some wy to strt t the ccepting stte, follow the rrows ckwrds, nd end up t the initil stte fter reding the string. This mens the originl NFA ccepts the reverse of this string, so tht the string we strted with is in the reverse lnguge. 9