Вы находитесь на странице: 1из 13

COS2601/103/3/2011

COS2601
THEORETICAL COMPUTER SCIENCE 2 TUTORIAL LETTER 103 Discussion class examples & solutions

School of Computing

2 Dear Student, This tutorial provides the questions and solutions that were discussed in the discussion class. The questions form part of the past examination paper provided in Tutorial Letter 102. Everything of the best for the examinations! Regards, Mrs. D Becker

EXAMPLE EXAMINATION PAPER & SOLUTIONS

QUESTION 1 (a)

[10]

Let S = {a, bb, bab, abaab}. For each of the following strings, state whether or not it is a word in S*: (i) (ii) abbabaabab abaabbabbbaabb (2)

(b) (c)

Give an example of a set S such that S* only contains all possible strings of combinations of as and bs that have length divisible by three. (4) Give an example of two sets S and T of strings such that S* = T* but S T and T S. (4)

ANSWER TO QUESTION 1 (a)(i) No, because there are no concatenations of a, bb, bab and abaab that will yield the word abbabaabab. (a)(ii) Yes, concatenations of a, bb, bab and abaab will yield the word (abaab)(bab)(bb)(aa)(bb) in S*. (b) (c) There is only one possible set S: S = {aaa, aab, aba, abb, baa, bab, bba, bbb}. All the words in S* are multiples of three as all the words of S are of length exactly 3. We give two possible answers, there are many other possibilities: S = {} and T = { }, note that S contains the empty word, but that T is the empty set; S = {a, b, ab} and T = {a, b, ba}.

3 QUESTION 2

COS2601/103/3/2011 [10]

A recursive definition for the language ODDAB must be compiled. Consider the alphabet = {a, b} and the language ODDAB, where ODDAB consists of all words of odd length that contain the substring ab. Provide (i) (ii) (iii) (iv) an appropriate universal set, the generator(s) of ODDAB, an appropriate function on the universal set, and then use these concepts to write down a recursive definition for the language ODDAB. (1) (2) (1) (6)

ANSWER TO QUESTION 2 (i) (ii) The set {a, b}* will be suitable because it contains, along with other words, all the words that are in the language ODDAB. The generators should be the smallest words in ODDAB of odd length and should contain the substring ab. Thus, aba, aab, abb, bab will be suitable generators.

(iii) The function CONCAT as defined in the study guide will be suitable. (iv) We give two possible recursive definitions. Note that all words in ODDAB should have an odd number of letters. The generators all have an odd number of letters; therefore, to keep the length of new words odd, we concatenate two letters at a time. ODDAB is the smallest subset of {a,b}* such that aba, aab, abb, bab ODDAB, and if w ODDAB, then also CONCAT(w,aa), CONCAT(w,bb), CONCAT(w,ab), CONCAT(w,ba), CONCAT(aa,w), CONCAT(bb,w), CONCAT(ab,w), CONCAT(ba,w) ODDAB. Or an alternative definition: Rule 1: aba, aab, abb, bab ODDAB. Rule 2: If w ODDAB, then also CONCAT(w,aa), CONCAT(w,bb), CONCAT(w,ab), CONCAT(w,ba), CONCAT(aa,w), CONCAT(bb,w), CONCAT(ab,w), CONCAT(ba,w) ODDAB. Rule 3: Only words produced by rules 1 and 2 are in ODDAB.

4 QUESTION 3 (i) (ii) (iii) Give a recursive definition of the set P of all positive integers greater than or equal to 5, formulate an appropriate induction principle, and then apply the induction principle to prove that 2n 3 2n-2 for all integers n 5. ANSWER TO QUESTION 3 (i) P is the smallest subset of Z (the set of integers) such that 5 P and if n P, then also n + 1 P. If a subset A of P is such that 5 A and if n A, then also n + 1 A, then A = P. (7) [10] (1) (2)

(ii)

(iii) Define A P as follows: A = {n P | 2n 3 2n-2} Is 5 A? 2(5) - 3 25 - 2 i.e. 7 8 Thus, 5 A is true. Assume k A, i.e. we assume that (1) 2k 3 2k 2 Required to prove that k + 1 A, i.e. 2(k + 1) 3 2(k + 1) - 2 LHS = 2(k + 1) 3 = 2k + 2 3 = (2k 3) + 2 2k 2 + 2 22 k 2 = 21 + k 2 = 2 (k +1) 2 = RHS Thus k + 1 A Hence, A = P and we conclude that 2n - 3 2n
-2

(from induction assumption (1)) (because the smallest value for 2 k 2 with k = 5 is 2 5 2 = 8 and 28 = 16 > 10 = 25 2 + 2)

for all integers n 5.

5 QUESTION 4 (a) Does the regular expression [b*+ (abb*)* + (aabb*)*]* bbb [b* + (abb*)* + (aabb*)*]*

COS2601/103/3/2011 [10]

define the language of all words in which the substring bbb appears at least once, but the substring aaa does not appear at all? If not, give a counter-example. (b)

(5)

Give a regular expression generating the language consisting of all words containing exactly one occurrence of the substring aa and no occurrence of the substring bb. (5)

ANSWER TO QUESTION 4 (a) It is indeed the case that all words generated by the given regular expression contain the substring bbb. It is furthermore the case that no word generated by the given regular expression contains the substring aaa. However, not all such words (i.e. words containing the substring bbb but not the substring aaa) can be generated by the given regular expression. Some examples are: babbb aabbb bbba. The answer to this question is thus no, and the words above serve as counterexamples. (b) The regular expression that we need to give, must make provision for words that start (and end) with either an a or a b. All occurrences of the letter b (except if it is the last letter of the word) must be followed by an a. Furthermore, only one a may be followed by another a - in order to ensure only one occurrence of the substring aa. A possible regular expression is: (b + )(ab)*aa(ba)*(b + ) This is of course not the only possibility. QUESTION 5 Build an FA (finite automaton) that accepts the language of all words that satisfies both of the following conditions: NO word contains the substring bba, and ALL words end with a double letter, thus all words end with either aa or bb. Note: Only one FA must be build. ANSWER TO QUESTION 5 We do not need to keep track of the number of letters that have been read at any stage - even or odd is irrelevant for this question. A dead-end state is necessary to make provision for all words containing the bba-substring. You should further keep in mind that words ending on aaa or bbb are, of course, also permissible. [10]

6 From the initial state 1, we move to state 2 with an a and to state 3 with a b. From state 2, we move to a final state 4 with an a and if we read an a in the final state, we stay in the final state, because words such as aaa and aaaa are permissible words in the language. From state 3, we move to a final state, state 5, with a b and if we read a b in the final state, we stay in the final state, because words such as bbb and bbbb are permissible words in the language. If, however, we read an a in state 5, we move to a dead-end state, state 6, from which we cannot leave because then the word contains the impermissible substring bba. The incomplete FA is given below:

-1 b 3 b b

+4
a

+5

a, b

If we read a b in state 2, we go to state 3 and if we read an a in state 3, we move to state 2. What happens if a b is read in state 4? We move to state 3.

-1 b 3 b b

a a b

2
b

+4
a

+5

a, b

7 QUESTION 6

COS2601/103/3/2011 [10]

By using Kleenes theorem, find a regular expression that generates the language accepted by the following TG (transition graph):

ANSWER TO QUESTION 6 Step 1 - Create a unique start state (note: this is actually redundant for this question because there is no incoming edges to the start state) and a unique final state:

a 2 a b 1 b ba 3 aa 4 bb a +

8 Step 2 - Eliminate state 3:


a 2 a 1 bab*aa b 4 bb

Step 3 - Eliminate state 4:

a 2 a (b + bab*aa)(bb)*a 1 (b + bab*aa)(bb)* +

Step 4 - Eliminate state 2:


[a + (b + bab*aa)(bb)*a]a*

(b + bab*aa)(bb)*

Step 5 Eliminate state 1:


1 [a + (b + bab*aa)(bb)*a]a* + (b + bab*aa)(bb)* +

A possible regular expression is: [a + (b + bab*aa)(bb)*a]a* + (b + bab*aa)(bb)* Note that one can follow a different order in applying Kleenes theorem to the states of the FA, and the regular expression may look somewhat different.

9 QUESTION 7 Consider the following FA with the corresponding regular expression r1:

COS2601/103/3/2011 [10]

-x1
a b

+x3

a, b

x2

Build an FA for the regular expression r1* by applying Kleenes theorem. (Do not formulate any regular expression.) ANSWER TO QUESTION 7

New state z1 = x1 z2 = x1 z3 = x2 +z4 = x1 or x3 +z5 = x2 or x3 or x1

Read an a z3 z3 z3 z5 z5

Read a b z2 z2 z4 z4 z4

z1

b a

z2
a

b +z4 b a b

z3
a

+z 5

10 QUESTION 8 (a) Convert the following Mealy machine to a Moore machine: [10]

a/0 q0 b/1 q1

a/1

b/1 q2

b/0

a/0
(b) Consider the following FA: (5)

a b b b +
Find an NFA (non-deterministic FA) with four states that accepts the same language. ANSWER TO QUESTION 8 (a)
a q0 1/ 0 b q 1/ 1 a

a a a b a b

(5)

b a

q0 / 1 b

q 2/ 0

(b)

a,b a a

a,b b +

11 QUESTION 9

COS2601/103/3/2011 [10]

Use the Pumping Lemma with length to prove that the following language is nonregular: L = {anbnam, where n = 0,1,2,3, and m = 0,1,2,3,}. ANSWER TO QUESTION 9 Assume L = {anbnam, where n {0, 1, 2, 3, ...} and m {0, 1, 2, 3, ...}} is regular. Then there exists an FA with, say k states, that accepts L. Let w = akbkap be a word in L. According to the pumping lemma, w may be written as w = xyz such that length(x) + length(y) k AND length(y) > 0 This means that both the strings x and y can only consist of as. According to the pumping lemma, xyyz must also be an element of L. This is, however, not the case because the pumped word, xyyz has more as (in the first group of as) than bs. We found a contradiction. Our assumption that L is regular is incorrect and we conclude that L is not regular. QUESTION 10 Assume we have an alphabet = {a, b}. Complete the following table: ANSWER TO QUESTION 10 (Note: We provide the table and solution.) FA Number of start states Number of final states Permissible edge labels 1 some or none letters from TG 1 or more some or none letters from * arbitrary no NFA 1 some or none letters from arbitrary no [10]

Number of lines leaving one edge for each letter in each state Deterministic or not yes

We add one question that was not in the paper:

12 QUESTION 11 (a) (b) (c) (d) FA1 Define a decision procedure. Use the definition in (a) to define decidability. [10] (1) (1)

Describe an effective procedure to decide whether a given FA accepts a finite or infinite language. (2) Using the decision procedure described in (c) above, determine for each FA1 and FA2 below whether it accepts a finite or an infinite language. (6)

b a a b a a,b a + b b

FA2

a a,b +

(a) (b) (c)

An effective solution to a problem with a Yes or No as answer is called a decision procedure. A problem for which a decision procedure can or may be found is called decidable. Refer to Theorem 20, page 216 of Cohen: If some FA has N states and there are m letters in its alphabet, then there exist mN + mN + 1 + mN + 2 + + m2N - 1 different input strings in the range N length of string < 2N. All these words can be tested by executing the FA on them. If any of these words are accepted, then the language is infinite. If none of these words is accepted, then the language is finite. Note: The Blue Paint algorithm cannot be applied here.

13 ANSWER TO QUESTION 11 (d) For FA1:

COS2601/103/3/2011

In this case N = 5. The only words accepted by FA1 are b and ab. These words are of length 1 and 2. Words that start with b or ab and that contain other letters will leave the final state and will not return. Hence they will not be accepted by FA1. Thus, words of length N = 5 AND length < 2N = 2(5) = 10 i.e. 5 length of string < 10. will not be accepted. We can therefore conclude that the language accepted is finite. For FA2: In this case N = 3. Now consider the word bbaa: Length(bbaa) = 4 > N = 3 and bbaa is accepted by FA2 where N = 3 length(bbaa) = 4 < 2N = 6. Hence the language accepted is infinite.
UNISA 2011

Вам также может понравиться