CSCI3255: Math Foundations of CS
Homework Chapter 3, page 1
October 2002
3.1/1-11, 17, 18 1. Find Find all all stri string ngss in in L(( L((a a+b)*b )*b(a+ab)*) ab)*) of length less than four. Length 0: none, since there must be at least one b. Length 1: b Length 2: ab, ab, bb, bb, ba (aa is not possible) aab, aba, aba, abb, abb, baa, baa, bab, bab, bba, bba, bbb. bbb. (aaa is not possible) Length 3: aab, 2. Does the expres expression sion ((0+1)(0+ ((0+1)(0+1)*)* 1)*)*00(0+1) 00(0+1)* * denote the the language language of strings stringsw w such that w has at least one pair of consecutive zeros? This requires you to understand basic things about regular expressions. x x* x* represents one or more x more x’s. ’s. So (0+1)(0+1)* represents items, each one of which is either a 0 or a 1. So this is all strings of zeros zeros and ones of length 1 or greater. greater. But when you apply * to this expression, you are now including λ making the expression ((0+1)(0+1)*)* the same as (0+1)*. So the expression as a whole represents any string string that consists of 0’s and 1’s with a 00 somewhere in it. Therefore the answer to this question is YES. 3. Show that r = r = (1 + 01)*(0 + 1*) also denotes the language of strings w such that w has no pair of consecutive zeros. Find two other other expressions for for this language. It should be clear that (1 + 01)* forces every 0 to be followed by a 1, which is usually necessary for this language. The only exceptions are a 0 at the very end of the string, or any string that has no 0’s (all 1’s). So this expression DOES DOES represent the language in question. question. Other possibilities might might be (1 + 01)*(0 + λ ) + 1* or (1 + 01)* + (1 + 01)*0 + 1* 4. Find Find a regula regularr expr express ession ion for {anbm: n ≥ 3, m is even}. n ≥ 3 can be represented by the expression (aaa (aaa))a* or, more, simply aaaa* and m is even can be represented by the expression expression (bb)*. bb)*. So, overall, the the expression aaaa*( aaaa*(bb bb)* )* would represent this language. 5. Find Find a regula regularr expr express ession ion for {anbm: (n (n + m) is even}. n + m is even in only two cases: when both n and m are even, or when both are odd. Therefore the expression (aa (aa)*( )*(bb bb)* )* + a(aa)* aa)*b b(bb)* bb)* would represent this language. 6. Give a regular regular expression expression for each each of the following: following: n m a. L1 = {a {a b : n ≥ 4, m ≤ 3} (aaaa) aaaa)a*(λ +b+bb+ bb+bbb) bbb) b. L2 = {a {anbm: n < 4, m ≤ 3} (λ +a+aa+ aa+aaa) aaa) (λ +b+bb+ bb+bbb) bbb) c. The The comp compllemen ementt of L1 n < 4 or m or m > 3 or not in the proper form: an a can follow a b. (λ +a+aa+ aa+aaa) aaa)b* + a*(bbbb *(bbbb))b* + (a (a+b)*ba )*ba((a+b)* d. The The com compl plem emen entt of of L2 n ≥ 4 or m or m ≥ 4 or not in the proper form: (an a can follow a b). (aaaa) aaaa)a*b* + a*(bbbb *(bbbb))b* + (a (a+b)*ba )*ba((a+b)*
CSCI3255: Math Foundations of CS
Homework Chapter 3, page 2
October 2002
7. What languages do the regular expressions (Φ *)* and aΦ represent? According to the definition in the book, X * always contains λ . So (Φ *)* = {λ }, since it clearly can not contain anything else. aΦ represents the strings that can be formed by concatenating a string from {a} with a string from Φ . Since Φ doesn’t contain any strings, the answer is Φ .
8. Give a simple verbal description of the language L((aa)*b(aa)* + a(aa)*ba(aa)*). The set of all strings of a’s and b’s containing exactly one b and an even number of a’s. (Notice that the two alternatives correspond to having an even number of a’s both before and after the b, or having an odd number of a’s both before and after the b.) 9. Give a regular expression for L R, where L is the language L((a+b)*b(a+ab)*). Just reverse the order of the concatenated items: (a+ba)*b(a+b)* 10. Give a regular expression for L = {anbm: n ≥ 1, m ≥ 1, nm ≥ 3}. Since nm ≥ 3, this can happen when n ≥ 3 or when m ≥ 3 or when n = 2 and n = 2. So we divide the regular expression into its individual cases: aaaa*bb* + aa*bbbb* + aabb 11. Find a regular expression for L = {abnw: n ≥ 3, w ∈ {a, b}+}. abbbb*(a+b)(a+b)* 17. Write a regular expression for the following languages on {0,1}*: a. all strings ending in 01: (0+1)*01 b. all strings not ending in 01: λ + 0 + 1 + (0 + 1)*(00 + 10 + 11) (don’t forget those short strings that meet the definition) c. all strings containing an even number of 0’s: (1*01*01*)* + 1* d. all strings having at least two occurrences of the substring 00: e. all strings with at most two occurrences of the substring 00:
(0+1)*00(0+1)*00(0+1)* + (0+1)*000(0+1)*
(1+01)*(λ +00)(1+10)*1(λ +00)(1+10)* + (1+01)*000(1+10)*
f. all strings not containing the substring 101: 0*(11*000*)*1*0*
CSCI3255: Math Foundations of CS
Homework Chapter 3, page 3
October 2002
18. Find regular expressions for the following languages on {a, b}* a. L = {w: |w| mod 3 = 0} ((a+b)(a+b)(a+b))* b. L = {w: na(w) mod 3 = 0}
(b*ab*ab*a)*b*
c. L = {w: na(w) mod 5 > 0} (b*ab*ab*ab*ab*a)*(b*ab* + b*ab*ab* + b*ab*ab*ab* + b*ab*ab*ab*ab*) The first part generates multiples of 5 a’s. We tack on 1, 2, 3, or 4 more.
Chapter 3, Section 2 1. Use the construction shown in class and in the textbook to find an nfa that accepts L(ab*aa + bba*ab) λ
a
b
λ
λ
a
a
λ
λ
λ λ
λ
b
b
a
λ
λ
λ
b
a
λ
4. Find dfa’s that accept the following languages: a. L(aa* + aba*b*) a a a
a b
b
Much easier if you don’t try the algorithmic construction for an nfa as we did in problem 1. Convince yourself that the given machine is a dfa and that it recognizes the language.
b
b
a
b a,b
b. L(ab(a+ab)*(a + aa))
q0
a
q1
a b
a
q2
q3 b
a
a
Q1
Q0
b
a,b
b
Qφ
a
q5
Q234
b b
q1
{}
q2
{q2,q3, q4}
q3
a
Q2
a b
q4
q0
a {q1}
a
q4 q5 Q2345
b {} {q2 } {}
{q2 } {q5} {} Coming up with an {} nfa,{} though not by {}
a
algorithm, is much easier in this case.
CSCI3255: Math Foundations of CS
Homework Chapter 3, page 4
October 2002
5. Find dfa’s that accept the following languages: a. L = L(ab*a*) ∪ L((ab)*ba) a a
q2
q1
b b
q0 a
a
b
q3
q4
b
a
q5
q6
a
b. L = L(ab*a*) ∩ L((ab)*ba) The intersection of these two sets can be seen as follows. The only way a string could satisfy the conditions of the first language and the second language is if the string begins and ends with an a. The first language strings must begin with an a. The second language strings must end with an a. Furthermore, according to the first language the only symbols allowed in between are b's. All these conditions can only be met if the second language string is abba. So L = {abba}. b
a a
b a
a
b
a,b
b
Chapter 3, Section 3 1. Construct a dfa that accepts the language generated by the grammar S → abA, A → baB, B → aA | bb S
a b
b a a,b
A a
b
b
a
b
b
B a
a
a,b
CSCI3255: Math Foundations of CS
Homework Chapter 3, page 5
October 2002
2. Find a regular grammar that generates the language L(aa*(ab + a)*). S → aA, A → aA | B, B → abB | aB | λ Starting with the start symbol S , we can generate 1 or more a’s followed by the variable A. By changing the variable to B, we can now additionally generate zero or more ab’s or a’s. 3. Construct a left-linear grammar for the grammar in Exercise 1. The grammar in Exercise 1, generates the regular expression (abba)(aba)*bb. Working from right-toleft, we would get: S → Cbb, C → Caba | abba 5. Construct a right-linear grammar for L((aab*ab)*). S → aaA | λ , A → bA | abS . Generate aaA. Let A generate zero or more b’s. When done, generate the final ab and start over again. 6. Find a regular grammar that generates the language on Σ = {a, b} consisting of all strings with no more than three a’s. S → bS | aA | λ , A → bA | aB | λ , B → bB | aC | λ , C → bC | λ The four variables represent 0, 1, 2 and 3 a’s generated so far. The λ ’s allow us to stop early. 9. Find a left-linear grammar for the language in Excercise 5. S → Aab | λ , A → Ab | Saa
12. Find regular grammars for the following languages on {a, b}: a. L = {w: na(w) and nb(w) are both even}. S → aAoe | bAeo | λ , Aoe → aS | bAoo, Aeo → aAoo | bS , Aoo → aAeo | bAoe S stands for even a’s and even b’s. The other variables are self-explanatory. b. L = {w: (na(w) - nb(w)) mod 3 = 1}. S → aA1 | bA2, A1 → aA2 | bS | λ , A2 → aS | bA1 S represents the modulo value of 0, A1 a value of 1 and A2 a value of 2. c. L = {w: (na(w) - nb(w)) mod 3 ≠ 0}. (and therefore equals either 1 or 2) S → aA1 | bA2, A1 → aA2 | bS | λ , A2 → aS | bA1 | λ d. L = {w: |na(w) - nb(w)| is odd}. S → aAoe | bAeo, Aoe → aS | bAoo | λ ,
Aeo → aAoo | bS | λ ,
Aoo → aAeo | bAoe