You are on page 1of 9

COS2601/202/2/2014

Tutorial letter 202/2/2014


Theoretical Computer Science 2

COS2601
Semester 2
School of Computing

IMPORTANT INFORMATION:
This tutorial letter contains the solutions to
assignment 02.

Question 1(a)
(a)

A recursive definition for the language EVENB over the alphabet = {a,b} must be
compiled where EVENB consists of all words of even length that do contain the
substring b.
Give (i)
(ii)
(iii)
(iv)

an appropriate universal set,


the generator(s) of EVENB, and
an appropriate function on the universal set, and then
use these concepts to write down a recursive definition of the language
EVENB.

Note: Questions 1(b) and 1(c) are a self-assessment questions. The solutions are provided in
tutorial letter 102.
Answer 1(a)
(i)

The set {a, b}* will be suitable because it contains, along with other words, all the words
that are in the language of EVENB.

(ii)

The generators should be of even length and should contain the substring b. Thus ab,
ba, and bb are suitable generators. Also, although is of even length, it does not contain
the b substring, and so cannot be a generator.
Note that the generator(s) is/are always the shortest word(s) in the language.

(iii)

The function CONCAT as defined on page 23 of the study guide, will be suitable.

(iv)

We give two possible recursive definitions. Note that all words in EVENB should have an
even number of letters. That is why two letters at a time are concatenated to the
generators (of even length) or already generated words. (Remember that even + even =
even.)
EVENB is the smallest subset of {a, b}* such that
ab, ba, bb EVENB
and if w EVENB, 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) EVENB.
or
Rule 1: ab, ba, bb EVENB.
Rule 2: If w EVENB, 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) EVENB.
Rule 3: Only words generated by rules 1 and 2 are in EVENB

COS2601/202

Question 2
This question has three parts and tests mathematical induction.
(i)
(ii)
(iii)

Give a recursive definition of the set P of all positive integers greater than 0,
formulate the appropriate induction principle, and then
use mathematical induction to prove that
11 + 15 + 19 + + (4n + 7) = 2n2 + 9n
for all positive integers n > 0.

Answer 2
(i)

P is the smallest subset of such that 1 P and if k P then also


k+1 P.
Another correct recursive definition for P is:
Rule 1: 1 P
Rule 2: If k P, then also k+1 P
Rule 3: Only elements generated by the above rules are in P.

(ii) The applicable induction principle is:


If a subset A of P is such that 1 A and if k A then also k+1 A, then A = P.
(iii) Define A P as follows:
A = {n | n P and

( 4 j + 7) = 2n

+ 9n }

j =1

We want to prove that this subset A of P is actually equal to P. The first step is to find out
whether the element 1 is in A. We do it as follows:
1

LHS:

(4 j + 7) = 4(1) + 7 = 11

RHS: 2n 2 + 9n = 2(1) 2 + 9(1) = 11

j =1

Because LHS = 11 = RHS, the element 1 does indeed have the necessary property that
qualifies it to belong to A.
Thus 1 A.
We have achieved our first goal. Secondly, we assume that k A. If k A and we can
prove that k + 1 A then it follows that A = P. We therefore assume we have some k A
such that
k

( 4 j + 7) = 2k

+ 9k

j =1

Now we should try to show that k+1 A, i.e. we must prove that

k +1

(4 j + 7) = 2(k + 1)

+ 9( k + 1)

j =1

PROOF
k +1

( 4 j + 7) = ( 2k

+ 9k ) + ( 4( k + 1) + 7)

from our assumption

j =1

= 2k 2 + 9k + 4k + 4 + 7

multiplying out

= 2k 2 + 13k + 11

= 2k 2 + 4k + 2 + 9k + 9

grouping differently

= 2( k 2 + 2k + 1) + 9( k + 1)

taking out common factors

= 2( k + 1) 2 + 9( k + 1)

factorising

We have shown that k+1 A.


From the induction principle, provided in (ii), it follows that A = P.
n

Thus

( 4 j + 7) = 2n

+ 9n for all integers n > 0.

j =1

Question 3
Construct a regular expression that defines the language of all words that do NOT end and
begin with the same letter. The two shortest words in the language are ab and ba.
Answer 3
One way of answering this question is to consider the type of words which should be generated
by the required language, say L. We know that the shortest words are ab and ba, and that all
other words will begin and end with an a or a b, and in between these two letters will be all
possible strings of concatenations of as and bs, together or separately, or .
A regular expression which ensures the generation of all words that do not begin and end with
the same letter is
a(a + b)*b + b(a + b)*a
where (a + b)* will generate all words over the alphabet = {a, b} including .

COS2601/202

Question 4
Construct a regular expression that defines the language of all words that contain either the aasubstring or the bb-substring but NOT BOTH the aa-substring and the bb-substring in the same
word.
Answer 4
It is clear that the shortest words that should be generated are: aa and bb. There can be no
words of less than two letters, and the ab and ba strings do not qualify. We can distinguish
between two groups of words: words that contain an aa-substring (Group 1); and words that
contain a bb-substring (Group 2).
Group 1
Types of words that should be generated include all possible strings of as and bs before and
after an aa substring but do not contain bb. A possible regular expression that defines all these
words is
(b + )(a*aba*)*aaa*(a*baa*)*(b + ),
where the (b + ) part allows for the concatenation of a single b to the start or end of the word.
Group 2
Similarly for words that contain a bb substring, the following regular expression is suitable:
(a + )(b*bab*)*bbb*(b*abb*)*(a + )
Thus a regular expression that generates all the words in the required language is:
(b + )(a*aba*)*aaa*(a*baa*)*(b + ) + (a + )(b*bab*)*bbb*(b*abb*)*(a + )

Question 5
Build an FA that accepts the language consisting of all words containing the aa-substring.
Answer 5
It is a good idea to list all the different types of words in the language, and types of words not in
the language, so that you can test whether your final FA accepts all those possible types of
words that it should, and rejects the others.
Types of words that should be accepted by the FA: aa, aaa, aaaa, baa, aab, aabbbbb, bbbaaa,
bbbaa, baabab
Types of words that should not be accepted by the FA: , a, ab, abb, ba, bba, abab, baba,
bbbabb, abba
Throughout this question well refer to the language L as the language that consists of words
containing the aa substring. Well consider a number of NFAs to illustrate our thinking towards
an FA which accepts L. We note that is not a word in L as it does not contain the aa substring.
5

The shortest word belonging to L is aa.

Now, words of the form aa(a + b)* are also accepted.

The NFA above should now be converted to an FA. An FA has exactly one outgoing edge for
each letter of the alphabet at each state.
We start at state X1, which has only an a outgoing edge. If we are in the start state and a b is
read, we remain in the start state.

State X2 only has an outgoing edge for a. Should we read a b in this state, we return to the start
state since we still have to read an aa substring before we can go to the end state.

This FA then represents the final answer to the question. Check that all the types of words that
should be accepted by this FA finish in an end state, and those that are to be rejected end in a
non-final state.
Question 6
Build an FA that accepts the language consisting of only those words that do NOT contain the
baa-substring and that do contain an odd number of as in total.
6

COS2601/202

Answer 6
This question requires that we do not only take into consideration which words should be
accepted but we should also make provision for specific words that should be rejected.
It is a good idea to list all the different types of words in the language, and types of words not in
the language, so that you can test whether your final FA accepts all those possible types of
words that it should, and rejects the others.
Types of words that should be accepted by the FA: a, ab, aaa, aaab, ababa, abbabba, aaba,
aaabbbababa, aaaaaabbbabb
Types of words that should not be accepted by the FA: , a, aa, baa, aaaa, baaa, aabaa,
ababaa, baaba, abaab, bbaab
The shortest word that is accepted is a (as 1 is an odd number). An NFA which accepts a is the
following.

We need to consider an odd number of as. State X3 is reached if we read an even number of
as, and then another a takes us back to a new end state X4 Note also that if an a is read in
state X4 that we now have an even number of as again, and we return to state X3.

Words that contain the baa substring should be rejected. Consequently, we need a dead-end
state, X7. Note that if we read only ba, this has an odd number of as, and needs to be accepted
(in state X6).

At this stage we have a basic framework for the required FA. We recall that an FA has exactly
one outgoing edge for each letter of the alphabet at each state.
We now need to make provision for the other outgoing edge for states where both letters in the
alphabet have not yet been included in the FA. There is one extra end state included for strings
that end on a b and have an odd number of as) see state X8.

This FA then represents the final answer to the question. Check that all the types of words that
should be accepted by this FA finish in an end state, and those that are to be rejected end in a
non-final state.

Question 7
Build a TG which accepts the language of all strings consisting of at least 4 characters such that
the next-to-last letter is identical to the second letter. Use as few states as possible.
Answer 7
A Transition Graph (TG) differs from an FA in various ways. These differences include the facts
that TGs are not deterministic and may have strings from the alphabet as labels on their edges.
The shortest words that are in this language are 4-letter words where either

the first two letters could be aa or ba and the last two letters ab or aa in other words,
the second and third letters are as, or

the first two letters could be ab or bb and the last two letters ba or bb in other words,
the second and third letters are bs
8

COS2601/202

There can be any number of as and bs between the double aas and bbs in the middle of each
of these. A possible TG accepting the required language is presented below.

------------------------------------------------------------------------------------------------------------------------------
UNISA 2014

You might also like