Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Department of Computer Science & Software Engineering

Comp335 Introduction to Theoretical Computer Science


Winter 2024

Assignment 2. Due date: February 23, by midnight

1. Give a regular expression for each of the languages below.

(a) {w ∈ {a, b, c} ∶ na (w) ≥ 1 and nb (w) ≥ 2}


Solution:
(a + b + c)∗ a(a + b + c)∗ b(a + b + c)∗ b(a + b + c)∗ +
(a + b + c)∗ b(a + b + c)∗ a(a + b + c)∗ b(a + b + c)∗ +
(a + b + c)∗ b(a + b + c)∗ b(a + b + c)∗ a(a + b + c)∗
(b) {ak bm cn ∶ k + m + n is odd}.
Solution:
a(aa)∗ (bb)∗ (cc)∗ +
(aa)∗ b(bb)∗ (cc)∗ +
(aa)∗ (bb)∗ c(cc)∗ +
a(aa)∗ b(bb)∗ c(cc)∗

2. Consider the two regular expressions

R = 0∗ + 1∗
S = 01∗ + 10∗ + 1∗ 0 + (0∗ 1)∗

(a) Find a string in L(R) ∖ L(S).


Solution: 00 ∈ L(R) ∖ L(S)
(b) Find a string in L(S) ∖ L(R).
Solution: 01 ∈ L(S) ∖ L(R)
(c) Find a string in L(R) ∩ L(S).
Solution: 1 ∈ L(R) ∩ L(S)
(d) Find a string in L(R) ∪ L(S).
Solution: 010 ∈ L(R) ∪ L(S).

1
3. For each of the DFA’s A, B, C below, use the state elimination technique to obtain an equivalent
regular expression.

A a b B a b C a b c
→ q0 q1 q2 → q0 q1 q2 → q1 q6 q2 q4
q1 q1 q2 q1 q0 q2 q2 q3 q6 q6
q2 q3 q4 ⋆q2 q1 q0 q3 q4 q5 q6
⋆q3 q5 q5 q4 q2 q6 q5
⋆q4 q5 q5 ⋆ q5 q6 q6 q6
q5 q5 q5 q6 q6 q6 q6

Solution:
DFA A as a transition diagram with transitions labelled by regex’s (trap state q5 omitted):

q3
a

q2 b q4
b
b
q0 a q1
start

For the next step eliminate q1 :

q3

q0 aa∗ b + b q2 b q4

Now remove q2 :

q3

(a+ b + b)a

start q0

(a+ b + b)b

q4

Regular expression : ((a+ b + b)a + (a+ b + b)b)

2
DFA B as a transition diagram with transition labelled by regex’s

q1

a b
a a
b
start q0 q2
b

eliminate state q1 :

ab + b

start q0 q2

aa + b
aa ab

regular expression : (aa + ((ab + b)(ab)⋆ (aa + b))⋆ )⋆ (ab + b)(ab)⋆

3
DFA C as a transition diagram with transitions labelled by regex’s (after eliminating trap
state q6 ):

q2 a q3

start q1 a b
a

q4 c q5

Eliminate state q2

q1 ba q3
start

c aa b
a

q4 c q5

Eliminate state q3

start q1
bab
c + baa

q4 c + aab q5

aaa

Eliminate state q4

bab + (c + baa)(aaa)∗ (c + aab)


start q1 q5

Regular expression: bab + (c + baa)(aaa)∗ (c + aab)

4
4. Convert the following regular expressions to -NFA’s.

(a) (000)∗ ( + 011 + 001)(111)∗


Solution:

 0  0  0   q
start

 

q  1  1  1  p


0  0  1

p   1  1  1 

5
(b) (01 + 10)∗ + (00 + 11)∗ + (1 + 10 + 100)∗
Solution:

0  1
 

 
 1  0

0  0
  
p     
start
 
 1  1

1 

  
 1  0  
 
1  0  0

6
5. Using the method of Theorem 3.14 to prove of disprove each of the following statements about
regular expressions.

(a) R(S + T ) = RS + RT .
Solution:
Identity is true. We consider a(b + c) and ab + ac
L(a(b + c)) = {a}({b} ∪ {c}) = {a}{b, c} = {ab, ac} = ab + ac
(b) (R + S)∗ = (R∗ S ∗ )∗
Solution:
Identity is true. We consider (a + b)∗ and (a∗ b∗ )∗
If w ∈ L((a + b)∗ ), then
w = x1 x2 . . . x k ,
where each xi ∈ {a, b} and k ≥ 0. Consider u = y1 y2 . . . yk , where yi = a1 b0 if xi = a, and
yi = a0 b1 if xi = b. Clearly u = w and u ∈ L(a∗ b∗ )∗
To see inclusion in the other direction, note that L((a + b)∗ ) = {a, b}∗ (= the set of all
strings over {a, , b}). Consequently L((a∗ b∗ )∗ ) ⊆ L((a + b)∗ )
(c) (R + S)∗ = R∗ + S ∗ .
Solution:
Identity is false. We consider (a + b)∗ and a∗ + b∗
We have for instance ababab ∈ L((a + b)∗ ), but ababab ∉ L(a∗ + b∗ ).
(d) (RS + R)∗ RS = (RR∗ S)∗ .
Solution:
Identity is false. We consider (ab + a)∗ ab and (aa∗ b)∗ .
We have for instance  ∈ L((aa∗ b)∗ ), but  ∉ L((ab + a)∗ ab).

7
6. For each of the following languages apply the Pumping Lemma to show that the language is
not regular.
In all cases start with the following:
Suppose to the contrary that L is regular
⇒ ∃ DFA A, s.t. L(A) = L. Let p be the number of states in A.
⇒ ∀w ∈ L(A), if ∣w∣ ≥ p, then w = xyz, where ∣xy∣ ≤ p, ∣y∣ = k ≥ 1, and xy i z ∈ L(A) for all i ≥ 0.

(a) The set of all binary strings with an equal number of occurrences of substrings 01 and
10 where overlapping substrings are not allowed. For instance 01010 is not accepted, but
011010001 is accepted.

Solution:
Solution that assumes non-ovelapping substrings:
Choose
w = 0101 . . . 01 00 1010 . . . 10
´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¶ ´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¶
p p

Then
xz = . . . 01010 . . . 00 1010 . . . 10
´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹¸¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¶ ´¹¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¹ ¶
p−k p

and clearly xz ∉ L, but xz ∈ L(A). Consequently L cannot be regular.


If we allow overlapping substrings the proof fails
In this case the language is actually regular, since it is accepted by the following DFA:
0 1
→ ⋆q0 q1 q3
⋆q1 q1 q2
q2 q1 q2
⋆q3 q4 q3
q4 q4 q3
n
(b) {a2 ∶ n ≥ 0}.
p+1
Solution: Choose w = a2 , that is

w = aa . . . a aa . . . a
´¹¹ ¹ ¹ ¸
p
¹ ¹ ¹ ¹ ¶ ´¹¹ ¹ ¹ ¸
p
¹¹¹¹¶
2 2

Then
xz = aa . . . a aa . . . a
´¹¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¶ ´¹¹ ¹ ¹ ¸
p
¹¹¹¹¶
m 2

where m = − k. Since 1 ≤ k ≤ p, we have 0 < m < 2p , so xz ∉ L, since the next power of


2p
two smaller than 2p+1 is 2p . Since xz ∈ L(A) it follows that L cannot be regular.

8
(c) {an bm ak ∶ n = m or m = k}.

Solution: Choose
w = aa . . . a bb . . . b
´¹¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¶ ´¹¹ ¹ ¸¹ ¹ ¹¶
p p

Then
xz = aa . . . a bb . . . b
´¹¹ ¹ ¹ ¸ ¹ ¹ ¹ ¹ ¶ ´¹¹ ¹ ¸¹ ¹ ¹¶
p−k p

and clearly xz ∉ L. Since xz ∈ L(A) it follows that L cannot be regular.

9
7. (a) Construct NFA’s A and B, such that L(A) = L(ab∗ a∗ ), and L(B) = L(a∗ b∗ a).
Then use these NFA’s to construct an NFA C, such that L(C) = L(A) ∩ L(B).
b a
Solution:

q0 a q1 a q2
NFA A: start

a b

p0  p1 a p2
NFA B: start

NFA C:  a
start (q0 , p0 ) (q0 , p1 ) (q0 , p2 )

b
a a a

 a
(q1 , p0 ) (q1 , p1 ) (q1 , p2 )

a a a a

 a
a (q2 , p0 ) (q2 , p1 ) (q2 , p2 )

(b) Suppose h is the homomorphism from {0, 1, 2} to {a, b}∗ defined by h(0) = a, h(1) = b, and
h(2) = ba.
i. What is the string h(21120)?
Solution: h(21120) = h(2).h(1).h(1).h(2).h(0) = babbbaa
ii. What is the language h(L(0 + 1∗ 2))?
Solution: h(L(0 + 1⋆ 2)) = L(h(1 + 1⋆ 2)) = L(h(1) + (h(1))⋆ h(2)) = L(b + (b⋆ ba))
iii. Let L = {ba}. What is the language h−1 (L)?
Solution: h−1 (L) = {10, 2}.

10
8. Draw the table of distinguishabilities for the DFA below (i.e., run the TF algorithm), and then
construct the minimum state equivalent DFA.

0 1
→ A B E
B C F
*C D H
D E H
E F I
*F G B
G H B
H I C
*I A E

Solution:

1
0,1
start {A, D, G} {B, E, H} {C, F, I}
0,1

11

You might also like