Anda di halaman 1dari 3

Selected solutions cosc341 Tutorial 4 Sudkamp ch2 ex.

4: Give a recursive definition of the language Equal consisting of all strings over {a, b} having an equal number of a's and b's. Use concatenation as the operator. Solution: Let's call the set of strings we define L. From the next question it will follow that in fact L = Equal. Basis: L Recursive step: if u, v L then so are uv, aub, and bua L. Proof by induction that the answer to question 1 does indeed give the set of strings over {a, b} having exactly as many a's as b's. (Not requested as part of the tutorial, but you may be asked these things! So study carefully.): Suppose we call the language recursively defined in question 1 L, and we call the set of strings having the same number of a's as b's Equal. We want to show that L = Equal. First we show that the strings in L have an equal number of a's and b's. (This will not yet guarantee that all strings with equal numbers of a's and b's live in L. but will at least reassure us that the things in L are of the right sort.) Let X L consist of the strings in L that have at exactly as many a's as b's in them. Is X? Certainly, since it has 0 a's and 0 b's. Suppose u, v X. Are uv, aub and bua X? Well, u has exactly as many a's as b's, because u came from X, and so does v, so certainly uv has the same number of a's as b's. Similarly aub and bua will have exactly as many a's as b's, so that aub X and bua X. Since L is the smallest set containing the basis and closed under the operations, X = L. Next we show that every string with an equal number of a's and b's lives in L. This will allow us to conclude that L = Equal. (Up to now all we know is that L Equal.) Equal itself wasn't given a recursive definition, so we use induction on the set E of even natural numbers, which we know does have a recursive definition (atom 0, new members n+2). The connection between Equal and E is that every string in Equal has even length. Let Y be the subset of E consisting of even naturals n such that all the strings of length n in Equal also belong to L. We show Y = E. Is 0 Y? Yes, because the string is the only string in Equal of length 0, and L. Suppose 0, 2, 4, , k Y. Is k+2 Y? Well, take any string w in Equal of length k+2. We know that every shorter string in Equal belongs to L. If w is of form aub or bua, then u must be in L, so w is also. If w starts and ends with the same letter, then w must be of form uv where u (and thus v too) has equal a's and b's, so u, v L, so w = uv L.

Sudkamp 10. Let X = {aa, bb} and Y = { , b, ab}. a) List the strings in the set XY. b) List the strings of the set Y* of length three or less. c) How many strings of length 6 are there in X*? Solution: a) aa, bb, aab, bbb, aaab, bbab b) , b, ab, bb, bab, bbb, abb c) There are clever ways to count the possibilities, but it is not hard to generate all strings of length 6, and if we do so systematically, then we can be sure none have been omitted. Those consisting just of b's: bbbbbb Those with a single aa: aabbbb, bbaabb, bbbbaa Those with two aa's: aaaabb, aabbaa, bbaaaa Those consisting just of a's: aaaaaa. So there are 8 strings of length 6 in X*. Sudkamp 12. Give a regular expression for the set of strings over {a, b, c} in which all the a's precede all the b's, the b's precede the c's, and there might be no a's or no b's or no c's. Solution: a*b*c* Sudkamp 13. The same as the previous exercise, but without the empty string: Solution: a+b*c* a*b+c* a*b*c+ Sudkamp 15. Find a regular expression for the strings over {a, b } that contain aa and bb. Solution: (a b)*aa(a b)*bb(a b)* (a b)*bb(a b)*aa(a b)* Sudkamp 17. Find a regular expression for the strings over {a, b, c } that do not contain aa. Solution: (b c)*(a(b c)+)*( a) Reasoning: every a must immediately be followed by b's or c's or else must terminate the string. Sudkamp 22. Find a regular expression for the set of strings over {a, b, c} that begin with a, contain exactly two b's, and end with cc. Solution: a(a c)*b(a c)*b(a c)*cc Sudkamp 36. Find a regular expression for the strings over {a, b} with an even number of a's or an odd number of b's.

Solution: We take the union of two regular expressions, one for an even number of a's, the other for an odd number of b's: (b*ab*ab*)* (a*ba*b a*)*a*ba* Sudkamp 37. Find a regular expression for the language Even-Even over {a, b} consisting of strings with an even number of a's and an even number of b's. Solution: Strings in this language will have clumps of 3 kinds: Type 1 aa Type 2 bb Type 3 starting with ab (or with ba ) and eventually balancing with another ab or ba. So: ( aa bb ((ab ba)(aa bb)*(ab ba)) )*

Anda mungkin juga menyukai