FORMAL LANGUAGES, AUTOMATA AND THEORY OF COMPUTATION EXERCISES ON REGULAR LANGUAGES
Introduction This compendium contins exercises out regulr lnguges for the course Forml Lnguges, Automt nd Theory of Computtion t the School of Innovtion, Design nd Technology, Mälrdlen University. The nottion used throughout this compendium is tken from the Swedish course ook, nd is descried on the next pge. All exercises hs solution in the end of the compendium, including those mrked with ; Those mrked with ; re test ssignments. The compendium is outlined s follows. There re five sections; Mthemticl preliminries, Regulr expressions, Finite Automt, Regulr grmmr nd non-regulrity. Ech section strts with some simple exercises intended to lern new concepts. The difficulty of the exercises then increses, nd in the end there re test ssignments. Study Technique Discuss your solutions in the group! The other students re your iggest support in this course, use tht resource! Try to solve the esy exercises in the eginning of ech section! Don t look t the solution until you hve tried to solve n exercise. If you cnnot solve it, look t the solution nd try to grsp the sic ide of the solution nd try to solve it gin. You won t lern nything from just copying solution! To ensure tht property holds for certin utomtion, grmmr, expression etc. it might help to elorte with different strings. Try to construct strings which re violting wht you re trying to prove! Good luck with the course! Lennrt Slling, Formell språk, utomter och eräkningr,
Tle of Content Nottion 4 Mthemticl Preliminries 5 SOLUTIONS........ 8 Regulr expressions Test Assignment. (Lesson ).... SOLUTIONS........ 4 Finite Automt 6 Test Assignment. (Lesson, 9 pril)... Test Assignment. (Lesson, 9 pril)... Test Assignment 4. (Lesson, 5 pril)......... SOLUTIONS........ 4 Regulr Grmmr 9 SOLUTIONS........ 4 5 Non-Regulrity 4 Test Assignment 5. (Lesson, 5 pril)......... 45 SOLUTIONS........ 46
Nottion Nottion Mening Exmple Other nottion The chrcter,,c,, c foo The string "foo" r, thing r, foo w, x, y, z Aritrry string w Σ ε The empty string λ k Numericl constnt k,,, 4 n, m Numericl vrile n =m L, S, A, B Lnguge, set L =Σ The empty set {} Σ An lphet Σ={, } {w foo} The set of ll w such tht foo holds {w w rev = w} k k repetitions of the chrcter 4 = k, 4 w k k repetitions of the string w w = www A k The set of ll strings with k ritrry elements {, } = {,,, } from the set A A Kleene str opertion on A {, } = {ε,,,,...}, = {ε,,,...}, (foo) = {ε, foo, foofoo,...} A + Plus opertor on the set A {, } + = {,,,,,...} w rev Reverse of the string w (foor) rev = roof w R L The complement lnguge to L (L),L C A The powerset of A {,} = {, {}, {}, {, } } A Numer of elements in A (sometimes {,, } =, utomtion =7 denoted crdinlity) w The numer of s in the string w rcdr =5 n (w), # (w) prefix (w) The set of strings x such tht w = xz prefix (c) ={ε,,, c} pprefix (w) The set of proper prefixes to w prefix (c) ={, } prefix (L) The set of strings w such tht w is prefix prefix (Σ )=Σ in some string in L suffix (w) The set of strings x such tht w = zx suffix (c) ={ε, c, c, c} psuffix(w) The set of proper suffixes to w psuffix (c) ={c, c} suffix(l) The set of strings w such tht w is suffix suffix (Σ )=Σ in some string in L A B The union of A nd B {,, } {,, 4} = {,,, 4} A + B A B The intersection of A nd B {,, } {,, 4} = {, } xy The conctention of the strings x nd x = foo,y = r,xy = foor y A B The set of ll elements which re in A {,, } {,, 4} = {} A \ B ut not in B A B The set of ll comintions of n element {, } {c, d} = {c, d, c, d} in A conctented with n element in B. # The end of string/stck N The set of nturl numers N = {,,,...} Z The set of integers Z = {...,,,,,,...} R The set of rel numers 47, R Ergo, hence, therefore End of proof 4
Mthemticl Preliminries Exercise.. Construct the powerset for the following sets. (i) {, } (o) (ii) {, } {, } (iii) {z} (iv) {,,,, 4} {,, 5,} (v) {,,, } {,, 5,} (vi) (the empty set) Exercise.. Determine the crdinlity of the following lnguges over the lphet Σ={, } (Tht is, re they finite, infinite nd countle, or infinite nd uncountle). Prove your nswers. (i) Σ (ii) Σ 4 (iii) Σ =Σ Σ Σ Σ (iv) Σ (v) Σ, tht is the powerset of ll lnguges over Σ. (Hint: use digonlistion rgument!) Exercise.. Find possile lphet Σ for the following lnguges. A word foor should e interpreted s string of chrcters f, o, o,, nd r. (i) The lnguge L = {oh, ouch, ugh} (ii) The lnguge L = {pple, per, 47} (iii) The lnguge of ll inry strings (o) (o) Exercise.4. Descrie wht the Kleene str opertion over the following lphets produces. (i) Σ={, } (o4) (ii) Σ={} (iii) Σ= (the empty lphet) Exercise.5. Descrie wht the + -opertion over the following lphets produces. (i) Σ={, } (o5) (ii) Σ={} (iii) Σ= (the empty lphet) 5
Exercise.6. Stte the lphet Σ for the following lnguges : (o6) (i) L =Σ = {ε,,,,,,,,,,,...} (ii) L =Σ + = {,,,...} (iii) L =Σ + = {ε} Exercise.7. Assuming tht Σ={, }, construct complement lnguges for the following. (o7) (i) {,, } (ii) Σ {} (iii) Σ + ε Exercise.8. Stte the following lnguges explicitly. (o8) (i) {,} {,c} (ii) {, } {,, } (iii) {x x N y N : y< (y +=x)} (N is the set of ll non-negtive integers) Exercise.9. Let Σ e n lphet, nd let ε e the empty string over Σ. (o9) (i) is ε in Σ? (ii) does it hold tht εεε = ε? does it hold for ε i where i? (iii) Let x nd y e two strings over Σ. Is the conctention of x nd y lwys the sme s the conctention of y nd x? Exercise.. A string is infinite when its length is infinite. Let Σ e n ritrry lphet. (o) (i) Does Σ contin ny infinite string? (ii) If Σ would e n infinite lphet (which it ctully my not e), would the nswer still e the sme? Exercise.. Prove tht 6 n (mod9)for ll integers n. (o74) Exercise.. (o75) Prove y induction tht S = S (tht is, tht the size of the powerset is equl to to the power of the size of the originl set) for ll finite sets S. 6
Exercise.. (o76) Let x e string nd let x rev e the sme string ut ckwrds. Prove tht (xy) rev = y rev x rev for ritrry strings x, y over n lphet Σ. (Hint: try to define x rev inductively) Exercise.4. Prove y induction tht A B = A B for ll sets A nd B. Exercise.5. Prove tht + + + + n = n+ for ll integers n. (o) (o) 7
Solutions Solution.. (i) S = {, {}, {}, {, } } (ii) S = {,, }, S = {, {}, {}, {}, {, }, {, }, {, }, {,, } } (iii) S = {, {z} } (iv) S = {, }, S = {, {}, {}, {, } } (v) S = {, }, S = {, {}, {}, {, } } (o) (vi) S = { } Solution.. (i) Σ = {ε} The set is finite. (o) (ii) Σ 4 = {,,,,,,,,,,,,,,, } The set is finite. (iii) Ech set in the infinite nd countle series is finite nd my therefore e counted y mpping it to the nturl numers. Therey, the full set cn e counted. The set is infinite nd countle. (iv) Σ = {{ε}, {}, {}, {, }} The set is finite. (v) The set is infinite nd uncountle. Solution.. (i) Σ={o, h, u, c, g} (o) (ii) Σ={, p, l, e, r,, 4, 7, } (iii) Σ={, } Solution.4. (i) All inry strings. (o4) (ii) All strings which contins nothing ut s (including the empty string). (iii) The lnguge which contins only the empty string. Solution.5. (i) Binry strings contining t lest one chrcter. (o5) (ii) Strings contining only s, with t lest one chrcter. (iii) The lnguge which contins only the empty string. 8
Solution.6. (i) Σ={, } (o6) (ii) Σ={} (iii) Σ= Solution.7. (i) L = {ε,,,,,,,,,,,,,...} (o7) (ii) L = {} (iii) L = {ε} Solution.8. (i) { {}, {, } } (o8) (ii) (iii) {,, 4, 5, 6, 7, 8, 9,, } Solution.9. (i) No. (o9) (ii) Yes. Yes. (iii) No, consider for instnce Σ ={, },x=,y =,xy =,yx=. Solution.. (i) No, even though the lnguge is infinite, ll strings hve finite length. (o) (ii) No, ech symol in the lphet still hs finite length, nd ech string hs finite length. Solution.. Clim: 6 n (mod9)for ll integers n. (o74) Verifiction: 6 =6=9 4 }{{} heltl, 6 = 6 = 9 4, 6 4 = 96 = 9 44 Bse step: We choose 6, tht is n =s se cse, since this is the lest n for which the clim must hold. Since 6 =6=9 4, the clim holds for the se cse. Induction step: We ssume tht the clim is true for certin cse nd try to show tht it is true for the following cse s well. Thus, ssume tht 9 divides 6 p, tht is 6 p =9k for some integer k. We wnt to show tht this implies tht 6 p+ =9l for some integer l. }{{} heltl }{{} heltl 9
If 6 p =9k for some p nd some integer k, then the following holds. 6 p+ = 6 6 p = 6 9k = 9(6k) }{{} integer = 9l where l is n integer Summry: The clim holds for n =, nd if it holds for some cse p, then it lso holds for the cse p +. Thus, it holds for ll cses, which concludes the proof. Solution.. (o75) Clim: S = S for ll finite sets S. Verifiction: = = = {x} = = = {x} {x, y} =4 = = {x,y} Bse step: We choose S = s se step, since this is the lest set for which the clim should hold. According to the verifiction ove, the clim should hold for the se step. Induction step: We ssume tht the clim is true for certin cse, nd try to prove it for the next cse. Thus, ssume tht S = S for some set S. We wnt to show tht this implies tht (S {x}) = S {x} for n ritrry element x S. If S = S for some set S, then the following holds. (S {x}) = S T where T = {t {x} t S } (S {x}) = S T = S + T since S nd T re disjoint (x S x S ) = S + {t {x} t S } = S + S = S = S (from the inductive hypothesis) = S + = S {x} since x S Summry: The clim holds for S =, nd if the clim holds for some set S then it lso holds for the set S {x} where x S. Thus, it holds in ll cses nd the proof is concluded. Solution.. (o76) ε rev = ε for the empty string ε. rev = for string with contining only single chrcter. A nonempty string x cn e prtitioned into two sustrings x = y where contins one or zero chrcters. We cn then define (y) rev = y rev where. Now we will show tht (xy) rev = y rev x rev. First ssume tht x is the empty string. If so, the clim is true, since (εy) rev = y rev = y rev ε. Now ssume tht x is string contining only one chrcter. If so, the clim is true s well, since (y) rev = y rev y definition. These re our se steps.
Now we ssume tht the clim (uv) rev = v rev u rev holds for two strings u, v. This is our inductive hypothesis. Now we wnt to show tht (uv) rev = v rev u rev. (uv) rev =(uv) rev y definition. Furthermore, (uv) rev = v rev u rev y inductive hypothesis, which yields (uv) rev = v rev u rev, which is wht we wnted to show. Since we hve shown tht the clim holds for (two) se cses, nd tht it holds for ny string which my e constructed y conctenting chrcter to the eginning of the first string, the clim holds for ll strings. Solution.4. (o) A B = A B for the empty string A = nd n ritrry set B. The clim lso holds for A = nd n ritrry B. Now ssume tht the clim holds for two sets A nd B (the induction hypothesis). We wnt to show tht A {x} B = A {x} B for n ritrry element x/ A (if x A the clim holds trivilly, since x A A {x} = A). We strt with the left hnd side. The cross product implies tht A {x} B = A B {x} B. Since A B nd {x} B re disjoint (since x/ A), it holds tht A B {x} B = A B + {x} B = A B + B. For the right hnd side it holds tht A {x} = A +. Thus, we hve A {x} B = ( A +) B = A B + B = A B + B. But then it lso holds tht A {x} B = A {x} B, which concludes the proof. Solution.5. Clim: + + + + n = n+ for ll integers n. (o) Verifiction: == = + == = + + =7= =7 Bse step: We choose n =s se cse, since this is the lest numer for which the clim should hold. According to the verifiction ove, the clim holds for the se step. Induction step: We ssume tht the clim holds for certin cse, nd try to prove tht it holds lso for the following cse. Thus, ssume tht + + + + k = k+ for some numer k. We wnt to show tht this implies tht + + + + k + k+ = k+. If + + + + k = k+ for some numer k, then the following holds. + + + + k + k+ = ( k+ ) + k+ (from the inductive hypothesis) = k+ + k+ = k+ = k+ Summry: The clim holds for the se cse k =, nd if it holds for numer k then it lso holds for the following numer k +. Thus it holds for ll cses, which concludes the proof.
Regulr expressions Exercise.. (o) Construct regulr expressions representing lnguges, over the lphet {,, c}, in which for every string w it holds tht: (i) The numer of s in w is even. (ii) There re 4i + s in w. (i ) (iii) w =i.(i ) Exercise.. (o4) A regulr expression is miguous when there exists string which cn e constructed in two different wys from the regulr expression. Which of the following regulr expressions re miguous? (i) ( () cd ) (c ) (ii) () (iii) Exercise.. Consider the regulr expression ( (cd) ). (o4) (i) Find string over {,, c, d} 4 which mtches the expression. (ii) Find string over {,, c, d} 4 which does not mtch the expression. Exercise.4. Find regulr expression for the lnguge consisting of lternting zeroes nd ones. (o4) Exercise.5. (o4) Find regulr expression for the lnguge L over Σ={, } consisting of strings which contin exctly two or exctly three s. Exercise.6. (o4) Find regulr expression over the lnguge L over the lphet Σ={, } consisting of strings where the numer of s cn e evenly divided y. Exercise.7. Descrie which lnguges the following regulr expressions represents, using common english. (i) ( ) (ii) (iii) () (iv) ( ) (v) ( ) ( ) (o44)
(vi) (vii) ( ) ( ) (viii) ( ) Exercise.8. Let E e the following regulr expression: (o) c ( ε ( c) ( c) ) c (i) is L(E) ={,, c}? (ii) is L(EE) ={,, c}? TEST ASSIGNMENT. (LESSON ) (o45) Find regulr expression which represents the set of strings over {, } which contins the two sustrings nd.
Solutions Solution.. (i) ( c) ( (( c) ) ) (o) (ii) ( c) ( c) ( (( c) ) 4 ) (iii) (Σ ) Solution.. (o4) (i) Amiguous. For instnce, the string cn e constructed y using ( () cd ) or (c ). (ii) Amiguous. The string cn e constructed either y or. (iii) Unmiguous. Solution.. (o4) (i) mtches. (ii) cd doesn t mtch, since every c must e preceded y n in order for the string to mtch. Solution.4. (o4) Alterntive :. () is the lnguge of zero or more.. ( ε)() is the lnguge of lternting zeroes nd ones which ends in.. ( ε)() ( + ε) is the lnguge of lternting zeroes nd ones. Alterntive : ( ε)() ( + ε) ccording to the ove. Solution.5. (o4). is the set of strings with exctly one.. is the set of strings with exctly two s.. is the set of strings with exctly three s. 4. The lnguge L is then. Solution.6. (o4). re ll strings with exctly three s.. ( ) is then the lnguge L. 4
Solution.7. (i) An ritrry numer of inry chrcters ( or ) precedes the sustring. (o44) (ii) Strings tht must contin zero ut which otherwise consists only of ones. (iii) Strings consisting only of ones nd which lengths re even. (iv) An ritrry numer of repetitions of string consisting two s nd n ritrry numer of zeroes in ritrry positions. (v) Strings contining the sustring. (vi) Strings on the form......, tht is, strings tht egins with zero or more ones followed y zero or more zeroes. (vii) ( ) is ll strings which doesn t contin the sustring. ( ) is ll strings which doesn t contin the sustring. so the conctention of these is ll strings where ech occurrence of precedes ll occurrences of. (viii) Al strings which doesn t contin the sustring. Solution.8. (i) No. See for instnce the string which cnnot e produced y the regulr expression. (ii) Yes. (o) SOLUTION TEST ASSIGNMENT. (o45) ( ) ( ( ( ) ) ( ( ) )) ( ) 5
Finite Automt Exercise.. Let M e the following DFA. (o6) strt 4 (i) Write down four string ccepted y M nd the sequence of configurtions tht shows this. (ii) Write down four strings not ccepted y M. Exercise.. Let M e the following DFA. (o8) strt 5 4 (i) Write down four strings ccepted y M nd the sequence of configurtions tht shows this. (ii) Write down four strings not ccepted y M. Exercise.. Construct DFA which ccepts the following lnguge: (o5) L = {w w Σ w contins the sustring } Tht is, w = xy for two ritrry strings x nd y. Exercise.4. (o6) Construct finite utomt, deterministic of non-deterministic for the following regulr expressions. (i) ( ) (ii) ( ) (iii) 6
(iv) ( ) ( ) (v) ( ε) (vi) ( )( )( ) (vii) ( ) ( ) Exercise.5. Formlly define the utomt you constructed in Exercise.4. Exercise.6. Which lnguges re ccepted y the following utomt: (o7) (o7), (i) strt,, 4 strt, (ii), 4 (iii) strt 4 (iv) strt, (v) strt 7
(vi) strt Exercise.7. Find DFA s which ccepts the following lnguges: (o) (i) Strings over {, } ending in. (ii) String over {, } contining three consecutive s (tht is, contins the sustring ). (iii) Strings over {, } not contining the sustring. (iv) All strings over {, } where ech string of length 5 contins t lest two s. (v) The set of strings over {, } where ech pir of s immeditely succeeds pir of s. (vi) The strings ε, nd. (vii) Strings over {, } eginning with nd which, if interpreted s inry numer, is multiple of 5. As n exmple, the string should e ccepted since =, which is multiple of 5. On the other hnd, the string should not e ccepted, since =4 which is not multiple of 5. (viii) {w {, } w =n, w =m, m, n N} (ix) {w {, } w =n, n N} nd w doesn t contin the sustring. Exercise.8. (o5) Construct finite utomt which ccepts the lnguges defined y the following regulr expressions. (i) (ii) ( ( 4 ) ) (() (iii) ( ) ) Exercise.9. Construct DFA which is equivlent to the following regulr expression: ( ( )( ) ) (o9) Exercise.. Prove tht the following utomtion ccepts the lnguge L = { i j, i c j i, j }. (o9) 8
,c c,,c strt,c 4 6 c c, 5 Exercise.. (o5) Convert the following finite utomt to GFA s nd find out which regulr expressions which descries the utomt s lnguges. (i) (ii), (iii) Exercise.. (o7) Construct regulr expressions which re equivlent to the following (deterministic) finite utomt: (i) strt 4 9
(ii) strt, 4, (iii), strt 4 (iv) strt Exercise.. Construct regulr expression representing the following utomtion. (o) strt Exercise.4. Convert ech of the NFA s elow to n equivlent DFA y suset construction. (o8) ε (i),
(ii) ε q, ε q q q Exercise.5. Convert the following NFA to DFA. (o65),, strt Exercise.6. Minimise the DFA elow y set prtitioning or doule complement. (o9) C, E A B φ D Exercise.7. Minimise the following DFA nd prove tht your new DFA relly is miniml. (o8) 7 8, 4 5 6
Exercise.8. (o64) Let L nd L e lnguges represented y the following utomt. Construct minimum DFA representing L L., strt L, 4 strt L 5 6 7 TEST ASSIGNMENT. (LESSON ) Construct regulr expressions for the utomtion defined in exercise.. TEST ASSIGNMENT. (LESSON ) Convert the following NFA to n equivlent DFA y suset construction. (o8) (o),,ε,ε 4 TEST ASSIGNMENT 4. (LESSON ) Construct the minimum DFA which is equivlent with the following regulr expression: (o7) ( ( ) ) (c) Hint: The DFA hs 8 sttes.
Solutions Solution.. (i). :, #,, #. :, #, 4, #, 4, #,, #. :, #, 4, #,, #,, # 4. :, #, 4, #,, #,, #,, # (o6) (ii). ε.. 4. Solution.. (i). :, #,, #,, #. :, #, 5, #, 4, #. :, #,, #,, #, 5, #, 4, # 4. :, #, 5, #, 4, #,, #,, # (o8) (ii). ε.. 4. losningo5, 4 Note tht if the input string w exists in the lnguge L, we eventully will find the sustring in w. We define the sttes corresponding to how much of the sustring hs een oserved: Stte is the initil stte. In this stte no prt of the sustring hs een oserved. In stte, the first zero of hs een oserved. If zero turns up, we continue to serch for the next one in the sustring. In stte, the sustring of hs een oserved. If the next chrcter is one, we return to the initil stte since is not sustring of. In stte, the string hs een oserved. If we dd nother zero, we return to stte this ecuse the new zero my e the strt of new sustring. In stte 4 we hve oserved the full sustring. Thus, it doesn t mtter wht other chrcters there re in the string, since we lredy hve concluded tht the string exists in the lnguge. Consequently, stte 4 is n ccepting stte.
Solution.4. (o6), (i), (ii) (iii),, (iv) (v),ε, (vi),,,, (vii), 4 Solution.5. (i) M =({,, }, {, },δ,{}, {}), σ ε, (o7) 4
(ii) M =({,, }, {, },δ,{}, {}), (iii) M =({, }, {, },δ,, {, }), (iv) M =({, }, {, },δ,{}, {, }), (v) M =({, }, {, },δ,{}, {, }), (vi) M =({,, }, {, },δ,{}, {}), σ ε, σ σ ε, σ ε σ ε,,,, (vii) M =({,,, 4}, {, },δ,{}, {}), σ ε,, 4, 4 4, 4 Solution.6. (i) The strings with n even numer of chrcters nd length of t lest. (ii) The strings which contins t lest one, followed y n odd numer of chrcters. (iii) The strings tht ends in.note: or? (iv) The strings on the form n m,n,m. (v) The strings tht ends in. (vi) The strings tht doesn t strt with. Solution.7. (o7) (o) (i) (ii), 5
(iii), (iv) 5, 6 7 8 4 9,, (v) 4, Note: stte should e n ccepting stte here. (vi) s, 7, 4 5 6 6
, (vii) s 4 (viii) 4 (ix) 5 6, Solution.8. (o5) (i) DFA:, 4 (ii) NFA: 7
(iii) NFA: 4 Solution.9. Construct n NFA given GFA. (o9). ( )( ) ( )( ). ( ) ( ). 4 5 6 4. 4 8
Convert the NFA to DFA (join the oviously equivlent sttes {5, } nd {4, 6}). 5 4 Solution.. (o9) We construct GFA from the finite utomtion.. Eliminte sttes nd 6, new trnsitions (, ), (, 4), (, 5), (, 4). +,c strt + c,c +,,c 4 + c c, 5. Eliminte stte 4. Since there re no pths from stte 4 (ded stte) we cn remove it entirely. + strt + c c 5. Crete new ccepting stte, numer 7, nd mke new epsilon-trnsitions from the previous ccepting sttes to 7. We do this to e le to eliminte stte nd 5. 9
+ ε strt + c c ε 7 5 4. Eliminer tillstånd och 5. strt + + 7 + c + 5. Join the two trnsitions from to 7. + + + c + strt 6 Thus, the utomtion ccepts the lnguge + + + c +, which is the sme s { i j, i c j i, j }, which concludes the proof. Solution.. (i) (o5) (ii) ( ) (iii) ( ) ( ε) Solution.. Construct GFA s to crete regulr expressions, see Slling pge 48 nd forwrd. (i) ( + ( ) ) ( + (ε ) ) (ii) ( () ) ( () (ε ) ) (o7) (iii) ( ) (iv) ( + ) Solution.. We strt y constructing the corresponding GFA. (o). New finl stte, remove stte. ε strt. Remove stte. ( ) ( ) strt We now otin the regulr expression ( ( ) ) ( ).
Solution.4. We solve the ssignments y the method from Slling, pge 4. (o8), R (i) {,} {,,} {,} Step y step:. Initil stte: {, } {,},. {, } {,, } {, } {}(ded stte) {,} R {,,},. {,, } {,, } {,, } {, } {,} R {,,} {,}
, R 4. {, } {, } {, } {, } {,} {,,} {,} {q,q} (ii) {q,q} {q}, φ {q,q,q} {q,q,q}. Initil stte: {q,q}. {q,q} {q}. {q,q} {q,q,q} 4. {q} {} (ded) 5. {q} {q,q} 6. {q,q} {q,q,q} 7. {q,q} {q,q,q} 8. {q,q,q} {q,q,q} 9. {q,q,q} {q,q,q}. {q,q,q} {q,q,q}. {q,q,q} {q,q,q} Solution.5. We use the suset construction. (o65) {} {,} {,,} {,,,} {,} {,,} Step y step:. Initil stte: {}. {} {, }
. {} {} 4. {, } {,, } 5. {, } {} 6. {,, } {,,, } 7. {,, } {} 8. {,,, } {,,, } 9. {,,, } {, }. {, } {,, }. {, } {, }. {,, } {,,, }. {,, } {, } Solution.6. (o9) We show how to solve the prolem oth y using set prtitioning nd y using the doule complement. Set prtitioning: We use the minimistion lgorithm from Slling, pge 6. Level {A, B, C, D, E, } Level {A, B, C, D, E} { } Level {A, C, D, E} {B} { } Level {C, D, E} {A} {B} { } The stte is non-ccepting nd the sttes A, B, C, D, E re ccepting. The stte B trnsfers to y the string, ut the sttes A, C, D, E trnsfers to sttes in {C, D, E}. A trnsfers to y the string, ut C, D, E trnsfers to sttes in {D, E}. Level 4 {C, D, E} {A} {B} { } No further sttes cn e distinguished., φ B, A CDE
Doule reversing: We use the doule reversing lgorithm from Slling, pge 6. First we reverse the utomtion nd otin n NFA M rev. D E, φ C B A The next step is to convert M rev todfam using suset construction.. First we crete new stte from the set of sttes in the NFA which re rechle y consuming ε from some initil stte. Note tht the new stte is n ccepting stte, since the stte A is ccepting. {A,B,C,D,E}. Then, we crete new stte representing the rechle sttes from {A, B, C, D, E} y consuming n. The rechle sttes re {A, C, D, E}. {A,C,D,E} {A,B,C,D,E}. Now we find the sttes rechle y consuming from stte in {A, B, C, D, E}. {A,B,C,D,E} {A,C,D,E} 4. We crry on in the sme mnner, ut from some stte in {A, C, D, E}. {A,B,C,D,E} {A,C,D,E} {C,D,E} 5. Over gin, ut from stte in {C, D, E}. 4
{A,B,C,D,E} {A,C,D,E} {C,D,E} 6. Now, we hve otined n equivlent DFA M since no more trnsitions need to e exmined. Now we reverse M to otin n NFA (M ) rev. From (M ) rev we crete DFA M s efore.. {,} {}. {,} {,,} {}. {,} {} {,,} {} 4. {}, {,} {,,} We re done! Note tht this DFA is the sme s the one we otined erlier, except tht the sttes hve different nmes. 5
Solution.7. We solve the prolem y suset construction, Slling pge 6. (o8) Level {,,, 4, 5, 6, 7, 8} Strt Level {, 4, 6, 8} {,, 5, 7} Level {, 6, 4, 8} {, } {5, 7} Level {, 6} {4, 8} {, } {5, 7} The sttes,4,6 nd 8 re ccepting nd the sttes,,5 nd 7 re not. The sttes nd trnsfers to rejecting sttes y the string, ut the sttes 5 nd 7 trnsfers into ccepting sttes. The sttes nd 6 trnsfers into cceptingt sttes y the string, ut the sttes 4 nd 8 trnsfers into rejecting sttes y the string. Level 4 {, 6} {4, 8} {, } {5, 7} No further distinguishing cn e done. The miniml DFA is shown elow., {4,8} {,6} {5,7} {,} Solution.8. Interpret the utomt s one NFA nd convert this to DFA using suset construction. (o64) {,6} {,7}, {4} {,5} {,5,6} {,5} {,5,6} {4,5,6} {4,6} {4,7} {4,5} 6
Step y step:. Initil stte: {, 5}. {, 5} {, 5, 6}. {, 5} {4} 4. {, 5, 6} {4, 5, 6} 5. {, 5, 6} {, 7} 6. {4} {4} 7. {4} {4} 8. {4, 5, 6} {4, 5, 6} 9. {4, 5, 6} {4, 7}. {, 7} {, 6}. {, 7} {, 5}. {4, 7} {4, 6}. {4, 7} {4, 5} 4. {, 6} {, 6} 5. {, 6} {, 7} 6. {, 5} {, 5, 6} 7. {, 5} {, 7} 8. {4, 6} {4, 6} 9. {4, 6} {4, 7}. {4, 5} {4, 5, 6}. {4, 5} {4, 7}. {, 5, 6} {, 5, 6}. {, 5, 6} {, 7} G, E H K C A B I D F J We renme the sttes ccording to elow. Minimistion: Level {A, B, C, D, E, F, G, H, I, J, K} Level {A, B, C, D, I, J} {E,F,G,H,K} Accepting or not Level {A, C} {B, D, I, J} {E, G, H, K} {F } foo Level {A} {C} {B,D,I,J} {E,G,H,K} {F } r Level 4 {A} {C} {B,D,I,J} {E,G,H,K} {F } No further distinguishing possile. The new DFA:, {C} {A}, {B,D,I,J} {E,G,H,K} SOLUTION TEST ASSIGNMENT. TBA. (o8) 7
SOLUTION TEST ASSIGNMENT. (o) {,} {,,} {,,,4} Step y step:. Initil stte: {, }. {, } {,, }. {, } {, } 4. {,, } {,,, 4} 5. {,, } {, } 6. {,,, 4} {,,, 4} 7. {,,, 4} {, } SOLUTION TEST ASSIGNMENT 4. TBA. (o7) 8
4 Regulr Grmmr Exercise 4.. (o46) Try to construct regulr grmmr which genertes the lnguge { n n n N}. Explin why you don t succeed! Exercise 4.. (o48) Crete regulr grmmr for the following lnguges. Use S s the first non-terminl. When you hve creted the grmmrs, construct the corresponding NFA s. (i) {w w {, } w contints the sustring or } (ii) Exercise 4.. (o49) Crete NFA s for the following lnguges nd construct the corresponding regulr grmmrs using these. Use S s the first non-terminl. (i) (ε )( ) (ii) ( ) (iii) {w w {, } w doesn t end in } Exercise 4.4. (o47) Crete regulr grmmr for the lnguge of string over {, } which contins the sustrings nd. Exercise 4.5. Construct regulr grmmrs for the following lnguges. (o5) (i) ( ) ( ) (ii) { n m n + m =k + n, m, k N} Exercise 4.6. (o77) Construct regulr grmmr which produces the lnguge over the lphet Σ={, } consisting of ll strings with t most s. Show how the string is produced. Exercise 4.7. Construct regulr grmmr producing the following lnguge over the lphet Σ={, }: L = {w ( w w (mod )} Then show how the string is produced. (o78) 9
Solutions Solution 4.. (o46) It is logicl to strt y S... s production rule. From the eginning we hve to e le to generte s to otin the sme numer of s nd s. However, no other terminls my pper in regulr production rule. The context-sensitive grmmr S S ε, on the other hnd, is generting the lnguge. Solution 4.. (o48) (i) S S S T V T U V U U U U ε, S, T U V (ii) S T U T T ε U U V V V S ε T U ε V Solution 4.. (i) (ε )( ) =( ), S T S S S T T ε (o49) (ii) ( ) = S S S ε (iii) S T U S T S ε T T U ε U T S 4
Solution 4.4. (o47) S S S S S S A A B B B B C C D D D D ε S S S E E F F F F G G H H H H ε Solution 4.5. (o5) (i) S S S S A ε A A B B C C C D D S S A A A B B (ii) Construct strings with odd length consisting of some numer of s followed y some numer of s. S A odd B odd A odd A even B even ε A even A odd B odd B odd B even ε B even B odd Solution 4.6. (o77) S S S ε A A B ε B B C ε C C ε Production of the string : S S S A A B C C 4
Solution 4.7. (o78) S A B A B S ε B S A The string is produced s follows. S A S A B S A S A 4
5 Non-Regulrity Exercise 5.. (o79) Show tht if the lnguges L nd L re regulr, then the following lnguge re regulr. You cn use ny proof technique you like, nd use ny proof from the course ook nd/or the lecture mteril. (i) L L (ii) L (iii) L L (iv) L, dvs. Σ L (v) L L (vi) L L (vii) {w w L if nd only if w/ L } (viii) prefix (L ) (see Nottion) (ix) suffix (L ) (see Nottion) (x) sustring(l ) where sustring(l) ={w x, y such tht xwy L} Exercise 5.. Which of the following lnguges re regulr? (o68) (i) { n m n>m n<m} (ii) { n m n m n m} (iii) { n m n>m n<m} (iv) { n m n m n m} Exercise 5.. Prove tht if L is regulr, then L rev is regulr. (o4) Exercise 5.4. (o7) Wht cn e sid out the lnguge L = {w {, } w = w}? is it the empty string? is it regulr? Exercise 5.5. (o) Does it hold tht lnguge cnnot e regulr if it is the union of lnguges which re non-regulr? Does it hold for the intersection? For conctention? If it is true, prove it. If not, give counter exmple. 4
Exercise 5.6. (o8) Determine if the following lnguges re regulr or non-regulr. Prove tht your nswers re correct. You my use properties of regulr lnguges, closure properties of regulr lnguges, things you know out DFA s, NFA s or regulr expressions nd grmmrs. (i) { n n } (ii) {w w {, } w w } (iii) {w w {, } w = w } (iv) {w rev w L( n m )} (v) {xwx rev y x, w, y {, } + } (vi) { n n+m c m m } Exercise 5.7. Prove tht the following lnguges re non-regulr: (o) (i) {, n m+n m, n } (ii) {ww w ( ) + } (iii) {() n () n n } (iv) The set of strings over {, } with the eqully mny zeroes s ones. (v) {w ( ) w = w rev } (vi) {xx rev w x, w ( ) + } (vii) { n n is prime} (viii) { n n is composite} (ix) { m n m n} (x) { m n gcd(m, n) =}, where the function gcd(x, y) is the gretest common divisor. Exercise 5.8. Prove tht the lnguge L = { n n },n N is non-regulr using the pumping lemm. Exercise 5.9. Prove tht the lnguge L = {w {, } u : www = uu} is non-regulr. Exercise 5.. Let L e regulr lnguge. Wht cn e sid out the following lnguge? (o8) (o6) (o67) Exercise 5.. L = {w ww rev ww rev L} (o7) x is root to w if n such tht w = x n. We define the root to lnguge L s rot(l) ={x n :x n L}. Prove tht for ech lnguge L Σ it holds tht ε L Σ =root(l). 44
TEST ASSIGNMENT 5. (LESSON ) Determine if the following lnguges re regulr or not. (o4) (i) L = { i j c k i + j = k} (ii) L = { i j c k i j k min(8,j )} 45
Solutions Solution 5.. (i) Regulr lnguges re y definition closed under conctention. (o79) (ii) Regulr lnguges re y definition closed under the Kleene str opertion. (iii) Regulr lnguges re y definition closed under union. (iv) If L is regulr, it is possile to construct DFA for the lnguge. Assume tht M =(Q, Σ,δ,s,F) is such DFA. Then it is possile to construct new DFA M =(Q, Σ,δ,s,Q F ) which rejects string w if nd only if M would ccept w. L(M )=Σ L(M )=Σ L = L. Since it exists DFA ccepting the lnguge L, then L is regulr if L is regulr. Regulr lnguges re closed under complement. ) (v) DeMorgns lw implies tht L L = (L L. Since regulr lnguges re closed under complement nd union, they re lso closed under intersection. (vi) Note tht L L = L L. Since regulr lnguges re closed under intersection nd complement, they re lso close under set difference. (vii) Note tht {w w L om och endst om w/ L } =(L L ) (L L ). Since regulr lnguges re closed under set difference nd union, this set is regulr. (viii) Assume tht M is NFA ccepting L. Construct new NFA M equivlent to M except tht we dd new stte q f, which is the only finl stte in M. We lso dd ε-trnsitions to q f from ll sttes from with finl stte is rechle in M.Ifwz L, then w is consumed y the regulr trnsitions in M, followed y one of the ε-trnsitions to q f to simulte the consumption of z, without ctully consuming something. Thus, L(M )=prefix (L(M )) = prefix(l ), tht is, if L is regulr, then prefix(l ) is lso regulr. (ix) Assume tht M is NFA ccepting L. Construct new NFA M equivlent to M except tht we dd new initil stte q. In ddition, we dd ε-trnsitions from q to ll sttes where n initil stte is rechle. If xw L, then the consumption of x is simulted y trnsition from the first set of ε-trnsitions nd w is consumed y the regulr trnsitions in M. Thus, L(M )=suffix(l(m )) = suffix(l ), nd so it holds tht if L is regulr, then suffix(l ) is lso regulr. (x) Assume tht M is n NFA ccepting L. Construct new NFA M equivlent to M except tht we dd new initil stte q nd new finl stte q f, which is the only finl stte in M. In ddition, we dd ε-trnsitions q to ll sttes from which n initil stte cn e reched, nd ε-trnsitions q f from ll sttes from which finl stte cn e reched in M.Ifxwy L, the consumption of x is simulted y trnsition from the first set of ε-trnsitions, w is then consumed y the regulr trnsitions of M nd the consumption of y is simulted y trnsition from the second set of ε-trnsitions. Thus, L(M )=delstrng(l(m )) = delstrng(l ), nd hence it holds tht if L is regulr, then delstrng(l ) is lso regulr. Solution 5.. (o68) Not regulr. It is the sme lnguge s { n m n m} which is the complement to { n m n = m} which is not regulr (which you should hve discovered erlier). Since regulrity is closed under complementtion, the considered lnguge cn not e regulr (since it would imply tht { n m n = m} is regulr). Regulr. The lnguge cn e descried y the regulr expression. Regulr. This lnguge contins no lnguge, nd is thus regulr. 46
Solution 5.. (o4) Since L is regulr, there exists DFA ccepting this lnguge. If we reverse ll trnsitions in this utomtion, nd turn the initil stte into n ccepting stte nd ll ccepting sttes to initil sttes, the result is tht we run the originl utomtion ckwrds, nd the new utomtion will ccept the lnguge L rev. Since there exists n finite utomtion ccepting L rev, the lnguge is regulr. Solution 5.4. (o7) It is not the empty set since, for instnce, w = nd w = stisfies w = w. If we look it closer t this condition we notice tht w hs to strt with since this is the prefix on the left hnd side. In the sme wy, the strings hs to end in since tht is the postfix on the right hnd side. However, we cn mke n exception for the string which my ct oth s initil nd finl nd orrows, so to spek, its s from the prefix nd postfix strings in the condition. Thus, the string stisfies the condition =. We now try to find some lrger exmples. The strings re supposed to strt with, so we hd to dd to our miniml string. In ddition, we hd the requirement tht the strings should end in, ut we cn let the finl e the sme s the initil one to otin the string. If we keep on dding chrcters to this string, the requirement tht they should end in yields the following strings,,,... As cn e seen, there is regulrity in the strings which cn e summrised s: the strings egins with n followed y zero or more s. This cn e formulted s the regulr expression (). Since there exists regulr expression descriing the lnguge, it is regulr. Solution 5.5. (o) Lnguges formed y the union of two non-regulr lnguges my e regulr. For instnce, this is the cse with the lnguge constructed y the union of the two non-regulr lnguges { n m n = m} nd { n m n m}, which is descried y the regulr expression. The intersection of two non-regulr lnguges my lso result in regulr lnguge, for instnce the non-regulr lnguges n n nd n n, which intersection only contins the empty string, which is regulr. Conctention of two non-regulr lnguges will however lwys result in non-regulr lnguge. If we ssume the contrry, tht is, tht it is possile to crete regulr lnguge y conctention of two non-regulr lnguges, there would e finite utomtion ccepting this lnguge. Conctention of lnguges corresponds to conctention of two utomt ccepting respective lnguge. Thus, it would e possile to split this finite utomtion into two seprte utomt ccepting ech of the originl lnguges, which is contrdiction since these lnguges re non-regulr which mens there cnnot exist finite utomt ccepting them. Solution 5.6. (o8) Non-regulr. Assume tht L = { n n } is regulr lnguge. Then there exists DFA M ccepting L. Crete new DFA M equivlent to M except tht ll pirs of trnsitions δ(q,)= q nd δ(q,)= q re chnged to single trnsition δ(q,)= q (two trnsitions in M consuming two s re replced y trnsition consuming one ). M consumes hlf s mny s s M, L(M )={ n n } which hs to e regulr, since there is DFA ccepting this lnguge. This is contrdiction since we know tht { n n } is non-regulr. Thus, L is lso non-regulr. Non-regulr. Assume tht L = {w w {, } w w } is regulr. Since regulr lnguges re closed under complementtion nd intersection, (L) L( ) hs to e regulr. However, (L) L( )={ n n }, which is not regulr, nd thus neither is L. Non-regulr. Assume tht L = {w w {, } w = w } is regulr. We know from the closure properties of regulr lnguges tht L L( ) is regulr. However, L L( )= { n n }, which is not regulr. Hence, L is cnnot e regulr. 47
Regulr. The regulr expression descries the lnguge. Regulr. The regulr expression ( ) + ( ) + ( ) + ( ) + descries this lnguge. Note tht w cn hve n ritrry length, so it my consume the tril of x nd the eginning of x rev. The only requirement is tht the first chrcter in x (which must contin t lest one chrcter) is present in the rest of the string. Non-regulr. This lnguge cn lso e expressed s L = { n n } { n n+ c} { n n+ c }. Assume tht L is regulr. Then, L { } hs to e regulr. However, L { } = { n n }, which is non-regulr, thus L cnnot e regulr. Solution 5.7. (o) { n n+m m, n } = {}{ n n }{ m }. Since we know tht { n n } nd { m } re nonregulr nd tht conctention of non-regulr lnguges re non-regulr, the lnguge cnnot e regulr. The lnguge is non-regulr. Since ech pir of the infinitely mny strings over ( ) + re distinguished y the lnguge, tht is, ech pir x, y ( ) + ) where x y is distinguished when xx elongs to the lnguge nd yx doesn t. The fct tht this lnguge is non-regulr might e simpler to understnd if we let =nd =resulting in the lnguge { n n n } which is known to e non-regulr. Cll this lnguge L nd ssume tht it is regulr. Then L ( ) is lso regulr since regulr lnguges re closed under intersection. However, this is contrdiction since L L( )= { n n }, which is non-regulr. Thus, L cnnot e regulr. This lnguge distinguishes n infinite numer of strings nd it therefore non-regulr. Every pir of the infinitely mny strings x, y ( ) + where x y re distinguished when xx rev elongs to the lnguge ut not yx rev. The lnguge hs to distinguish infinitely mny strings. The proof is similr to the solution ove. For ech of the infinitely mny strings x, y ( ) + where x y, there exists string z = x rev such tht xzw elongs to the lnguge, ut not yzw. Hence infinitely mny strings re distinguished, nd the lnguge is not regulr. Also here it is useful to use rgumenttion out distinguishing strings. However, we need to mke slightly stricter requirements on the strings since it otherwise would not e ovious tht the lnguge distinguishes etween them. Tke two strings x = n nd y = n+. The numer of such strings re infinite. Since there exists n infinite numer of prime numers, there exists string z = m such tht n + m is prime, ut not (n +)+m (except when n = m = where oth nd re prime). Then xz elongs to the lnguge, ut not yz. The lnguge then distinguishes n infinite numer of strings, thus mking it non-regulr. Composite numers re numers which re not prime. Thus, this lnguge is the complementry lnguge to the lnguge or prime strings in the previous exercise, which ws non-regulr. Considering this nd the closure properties of complementtion, we cn derive tht this lnguge cnnot e regulr. This lnguge is the complement lnguge to { n n } which is non-regulr. By the closure properties for complementtion we know tht lso this lnguge is non-regulr. Solution 5.8. (o8) We strt y tking look t the pumping lemm. The pumping lemm sttes properties tht must hold for ll strings in regulr lnguge (with n infinite numer of strings). The lemm sttes tht ll strings in such lnguge, which hs 48
length exceeding certin numer, contins non-empty sustring which my oth e pumped up (repeted) n ritrry numer of times or pumped out (removed) such tht the resulting string still is in the lnguge. In ddition it holds tht such sustring exists in ech sustring of string in the lnguge, if the sustring hs length equl to or exceeding the numer stted ove. In other words, it holds tht ll strings w L such tht w N cn e re-written s w = xyz where y, nd for ll i N it holds tht xy i z L. In ddition, it holds tht ll sustrings of w where w N hs such pumping lock y. To prove tht lnguge is non-regulr using the pumping lemm, it is esiest to try to construct counter exmple. Thus, we strt y ssuming tht the lnguge L = { n n } is regulr, nd try to chieve contrdiction. We will do this y oserving n individul string in L nd show tht it is not possile to pump it using the pumping lemm. Thus, ccording to the pumping lemm it holds tht ech string in L hving length greter th or equl to n unknown numer N should hve pumping lock y which cn e pumped. Since we hve no ide wht N would e, we hve to e creful nd select string which is gurnteed to hve length which is equl to or greter thn N. The only wy we cn do this is to choose string which is prmeterised in N. For instnce, we cn consider the string w = N N, for which it nturlly holds tht w N. The next step is to find out exct where in the string w = xyz the pumping lock y is. Here we could use the cct tht pumping lock hs to exist in ll sustrings of w where w N. Consider the sustring N, for which it holds tht N N. Thus, N must contin y such tht w = xy i z,i N.Sowehvew = N k k N where x = N k, y = k nd z = N. We ttempt to pump out the pumping lock y once, nd otin the string = N k N where k>. This string is supposed to elong to the lnguge L ccording to the pumping lemm (under the ssumption tht L is regulr). But since we the numer of s nd the numer of s re not the sme, the pumped string cnnot e in the lnguge! We hve found contrdiction, nd hve to reject our hypothesis tht L would e regulr. Hence, L is non-regulr. Solution 5.9. (o6) We use the pumping lemm lso in this proof. The string w = N n elongs to the lnguge, since www = uu with u = N N N. If the strting string of zeroes is pumped, which ccording to the pumping lemm should e possile since the length of this string is greter thn or equl to N, the string is no longer in the lnguge. Tht is, if w = N N then www uu. Thus, the lnguge is non-regulr. Solution 5.. (o67) The strings in the lnguge consist of conctented pir of the sustring ww rev. Thus, the strings re the sme ckwrds nd forwrds, i.e., they re plindromes. Plindrome lnguges re not regulr, nd s we sw erlier, it is not possile to otin regulr lnguge y conctenting two non-regulr lnguges. Solution 5.. (o7) We hve tht root(l) ={x n :x n L}. Assume tht ε L nd let n =. This results in root(l) ={x x L} nd since we no longer hve ny requirements on which strings x which should elong to root(l) we hve tht root(l) =Σ. Thus, we hve shown tht ε L Σ = root(l). Now ssume tht Σ = root(l). We wnt to show tht this implies ε L. Since ε Σ,iths to hold tht n :ε n L. No mtter how mny times we repet ε, the result is gin ε, i.e., n :ε n = ε. Thus, it must hold tht ε L. Consequently, we hve shown tht ε L Σ = root(l) nd this together with the prgrph ove yields tht ε L Σ = root(l). 49
SOLUTION TEST ASSIGNMENT 5. (o4) L is non-regulr. Assume tht L is regulr for contrdiction. Then there should e numer N such tht if w L nd w N, then w contins non-empty sustring y which cn e pumped such tht w stys in the lnguge. Such string exists, s mtter of fct, in every sustring of w with length of t lest N chrcters. Consider the string N N c N which elongs to the lnguge since N + N =N. Let the eginning sequence of s e the pumping string y, which stisfies the condition in the pumping lemm since y N. If we pump up y once, the string no longer elongs to the lnguge, since N + N N. Thus, we hve chieved contrdiction, nd L cnnot e regulr. L is regulr. Ech string in L my consist of mximum 8 of ech of the chrcters, nd c. This mkes the numer of strings in L finite, thus regulr. 5