You are on page 1of 11

COS2601/202/1/2014

Tutorial letter 202/1/2014


Theoretical Computer Science 2

COS2601
Semester 1
School of Computing

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

Question 1(a)
(a)
A recursive definition for the language EVENAAnotbeginB over the alphabet = {a, b}
must be compiled where EVENAAnotbeginB consists of all words of even length containing the
substring aa but that do not begin with a substring b.
Give(i) an appropriate universal set,
(ii) the generator(s) of EVENAAnotbeginB, and
(iii) an appropriate function on the universal set, and then
(iv) use these concepts to write down a recursive definition of the language
EVENAAnotbeginB.
Note: Questions 1(b) and 1(c) are 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 EVENAAnotbeginB.

(ii)

The generators should be of even length and should contain the substring aa. Thus aa is
a suitable generator.
Note that the generator(s) is/are always the smallest 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 EVENAAandnotbeginB
should have an even number of letters. That is why two letters at a time are
concatenated to the generator (of even length) or already generated words. (Remember
that even + even = even.)
EVENAAandnotbeginB is the smallest subset of {a,b}* such that
aa EVENAAandnotbeginB
and if w EVENAAandnotbeginB, then also
CONCAT(w,bb), CONCAT(ab,w), CONCAT(w,ab),
CONCAT(w,ba), CONCAT(aa,w), CONCAT(w,aa) EVENAAandnotbeginB.
or
Rule 1:aa EVENAAandnotbeginB.
Rule 2:If w EVENAAandnotbeginB, then also
CONCAT(w,bb), CONCAT(ab,w),
CONCAT(w,ab), CONCAT(w,ba),
CONCAT(aa,w), CONCAT(w,aa)
Rule 3:Only words generated by rules 1 and 2 are in CONCAT EVENAAandnotbeginB

COS2601/202

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

Provide a recursive definition for the set P of all positive integers greater than 0,
formulate the appropriate induction principle, and then
apply the induction principle to prove that for each integer n > 0,
n

j 1

n(n 1)(2n 1)
.
6

Answer 2
(i)

P is the smallest subset of R 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

j
j 1

n(n 1)(2n 1)
}
6

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

LH:

j
j 1

RH: 1(1 1)(2(1) 1)


6
=1

Because LH = RH = 1 - 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
2 k ( k 1)(2k 1)
j

6
j 1

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

6
j 1
PROOF
k 1

j 1

k (k 1)(2k 1)
(k 1) 2
6

from our assumption

k (k 1)(2k 1) 6(k 1) 2
6
6 as denominator for all terms

(k 1)[k (2k 1) 6(k 1)]


6
(k + 1) is a common factor

(k 1)[(2k 2 k ) 6k 6]
6

.simplify

(k 1)[(2k 2 7k 6]
6

.simplify

(k 1)(k 2)(2k 3)
6

.factorise trinomial

(k 1)((k 1) 1)((2k 1) 1)
6
re-arrange - equation equal to the required equation

We have shown that k+1 A.


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

Thus

j
j 1

n(n 1)(2n 1)
for all integers n > 0.
6

COS2601/202

Question 3
Construct a regular expression that defines the language containing all words with at least one
aabsubstring or all words with at least one ba-substring.
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. If we consider the type of words to be generated then it is
simplest to start with the shortest words. Thus all the words over the alphabet {a,b} where 0
|w| 3 will be considered next. (Note that |w| indicates the length of a general word w.)
Words with 0 |w| 3 are as follows:

|w| = 0 --- should not be generated since it does not contain the ba substring or the aab
substring at all.
|w| = 1 --- a, b neither the ba-substring nor the aab-substring is contained in the word a
or the word b.
|w| = 2 --- aa, ab, ba, bb of these words only ba should be generated. The other words
aa, ab and bb do not belong to the requested language, L.
|w| = 3 --- aaa, aab, aba, abb, baa, bab, bba, bbb. Of all the three-letter words only aab,
bba, baa, bab and aba should be generated. The other three letter words should not be
generated.

A regular expression that generates L should make provision for all the shortest words to be
generated. We note that ba is a substring of bba, baa, bab and aba. Thus if we make provision
for the ba-substring then, if we present a correct regular expression, we should include the
generation of bba, baa, bab and aba. A regular expression which ensures the generation of all
the words containing the ba-substring is:
(a + b)*(ba)(a + b)* or (a*b*)*(ba)(a*b*)*
Thus all words with a ba-substring including bba, baa, bab and aba will be generated. (a + b)* is
namely equivalent to (a*b*)* and generates all the words over the alphabet = {a,b}, including
. The ba-substring is ensured by the inclusion of (ba) in both the regular expressions. Note
that (ba) and not (ba)* is included in the given regular expressions since should not be
generated by the required regular expression.
The same kind of argument holds for a regular expression containing the aab-substring. A
regular expression generating all words that contain the aab-substring is
(a + b)*(aab)(a + b)* or (a*b*)*(aab)(a*b*)*
We provide a regular expression generating all words with at least one aabsubstring or all
words with at least one ba-substring. Thus we obtain:
(a + b)*(aab)(a + b)* + (a + b)*(ba)(a + b)*
or

(a*b*)*(aab)(a*b*)* + (a*b*)*(ba)(a*b*)*.
5

Question 4
Construct a regular expression that defines the language L containing all the words with either
exactly one aba-substring or exactly one bab-substring but not both aba- and bab-substrings.
(Hint: For example, the word abab does not belong to L.)
Answer 4
Question 4 is more difficult than its predecessor. It is clear that the shortest words that should
be generated are: bab and aba. We note that no words where 0 |w| 2 belong to the
language. If we consider the words that the requested regular expression should generate then
it is trivial that we can distinguish between words that contain the aba-substring (Group 1) and
words that contain the bab-substring (Group 2).
Group 1
Types of words that should be generated:
aaba defined by the regular expression: aa*ba.
abaaaa defined by the regular expression: abaa*.
a(bb)b(aa)ba - note that the aa-substring and the bb-substring prevent words not
belonging to the language to be generated. These words can be generated by the regular
expression: abbb*aa
If all of these types of words are combined then we have:
a(a* + bbb*aa)*baa*
Group 2
Types of words that should be generated:
bbab defined by the regular expression: bb*ab.
babbbb defined by the regular expression: babb*.
a(aa)a(bb)ab - note that the aa-substring and the bb-substring prevent words not
belonging to the language from being generated. These words can be generated by the
regular expression: a(aa)a*(bb)ab.
If all of these types of words are combined then we have:
b(b* + aaa*bb)*abb*
Thus a regular expression that generates all the words in the required language is:
a(a* + bbb*aa)*baa* + b(b* + aaa*bb)*abb*
Question 5
Build an FA that accepts only those words in which the ab-substring occurs an even number of
times and words end in a b-substring.

Answer 5
Throughout this question well refer to the language L as the language that consists of words
with an even number of occurrences of ab and that end in b. 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.
Although is of even length it does not end in b. The shortest word belonging to L is b. The
number of ab occurrences in b is zero which is an even number b clearly ends in b. In fact
b(b)* should be accepted. Thus we can start our FA by drawing an FA that accepts b(b)*. See
6

COS2601/202

Figure 1. It is clear that the word a should not be accepted neither should a(a)* be accepted as
these cases end in a and not in b.

Figure 1
The shortest word accepted that also contains as is abab. Note that ab should not be accepted
as it contains an odd number of occurrences of the ab-substring. We modify the NFA presented
in Figure 1 to make provision for abab to also be accepted. See Figure 2. In Figure 2 well start
to distinguish between states with an odd number of occurrences and an even number of
occurrences of the ab-substring respectively.

b
X1even

X2+
even

a
a
Figure 2

X3

X4
odd

X5
odd

even

The NFA provided in Figure 2 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 and note that
we have two outgoing edges in state 1 one for each letter of the alphabet. In state X2 we
need to make provision for an a being read. If we are in state X2 and we read an a then we
know the word should not be accepted. Although there is still an even number of aboccurrences words ending in an a should not be accepted. If we consider the NFA in Figure 2
then state X3 has the properties of the words that are read if an a is read in state X2. Therefore if
we read an a in state X2 then we go to state X3. See Figure 3.

Figure 3
7

The only two states without two outgoing edges one for each letter of the alphabet - are states
X4 and X5. If we are in state X4 and we read a b then well remain there as a b in state X4 does
not have an influence on the number of ab occurrences. Similarly if we are in state X5 and we
read an a we remain in state X5. Thus we can draw the requested FA. See Figure 4. It is trivial
that the odds and evens can be removed from all the respective states. Figure 5 represents the
final answer to the question.

1even

2+
even

odd

odd

even

Figure 4

12+

Figure 5

COS2601/202

Question 6
Build an FA that accepts the language L of all words that do not contain the aab-substring and end
in a ba-substring.

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
clear that the shortest word to be accepted is ba. Other words that are accepted are aba, bba,
bababa, ababbbbaba, bbba, abbbababa,. An NFA which accepts ba is presented in Figure 6.

Figure 6
Words containing the aab-substring should be rejected. Consequently we require a dead end
state. In Figure 7 we introduce a dead end state, state X4. It is clear that if one enters state X4
then, irrespective of reading either an a or a b well remain in state X4. See Figure 7.

Figure 7
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 have made provision for
both reading an a and a b in state X1. If we are in state X2 and we read a b then we loop back
into state X2. A word should namely end in ba to be accepted. The current NFA is displayed in
Figure 8.

Figure 8
State X3 does not have an outgoing edge for b. We cannot stay in state X3 if a b is read - words
are not allowed to end in ab but only in ba. Therefore if we are in state X3 and we read a b then
we return to state X2 where we should read another a for a word to be accepted. If we are in
state X3 and we read an a then we have now read two as in a row. Words may not end on a
string of more than one a and two as followed by a b should also not be accepted thus, no
matter which letter is read after this a is read in state X3, the words should not be accepted.
(Words such as ababaaa or abbbaab are not accepted.) Thus, if we read an a in state X3, we go
to the dead end state X4.

Figure 9
Our FA is nearly completed. State X5 still requires an outgoing edge making provision for a b
being read. If a b is read in state X5 then we go to state X2. Finally if we are in state X6 and we
read some as then we remain in state X6. Figure 10 presents the requested FA.

Figure 10
10

COS2601/202

Question 7
Build a TG that accepts the language of all strings that contain at least one baa-substring or at
least one bab-substring or both the baa- and bab-substrings. 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 simplest TG to accept the required language can be constructed by drawing one edge from
the start state (X1) to the final state (X2) labelled with the two required substrings namely bab
and baa. We realise that any combination of as and bs can precede bab and baa. Therefore
we add a loop labelled a,b at state X1. A similar principle holds for characters or strings read
after one of the prerequisite strings has been read. Thus we add a loop, labelled a,b at state X2.
A possible TG accepting the required language is presented in Figure 11.

Figure 11
------------------------------------------------------------------------------------------------------------------------------
UNISA 2014

11

You might also like