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

Department of Computer Science & Software Engineering

Comp335 Introduction to Theoretical Computer Science


Winter 2024

Assignment 1. Due date: February 5, by midnight

1. Let L1 , L2 and L3 be languages over some alphabet Σ. In each part below, state the relationship
between the given two languages. Are they equal, is one a subset of the other, do they have
an empty or non-empty intersection? Give reasons (proof!) for your answers, and counter-
examples where needed.

(a) L1 (L2 ∩ L3 ) versus L1 L2 ∩ L1 L3 .


Solution: L1 (L2 ∩ L3 ) = L1 L2 ∩ L1 L3 .
Proof:
w ∈ L1 (L2 ∩ L3 ) ⇔
w = xy where x ∈ L1 and y ∈ L2 ∩ L3 ⇔
w = xy where x ∈ L1 and y ∈ L2 and y ∈ L3 ⇔
w ∈ L1 L2 and w ∈ L1 L3 ⇔
w ∈ L1 L2 ∩ L1 L3 ⇔

(b) L∗1 ∩ L∗2 versus (L1 ∩ L2 )∗ .


Solution: (L1 ∩ L2 )∗ ⊂ L∗1 ∩ L∗2 .
Proof:
w ∈ (L1 ∩ L2 )∗ ⇒
w = x1 x2 . . . xk where each xi ∈ L1 ∩ L2 ⇒
w = x1 x2 . . . xk where each xi ∈ L1 and each xi ∈ L2 ⇒
w ∈ L∗1 and w ∈ L∗2 ⇒
w ∈ L1 ∩ L2
∗ ∗

If L1 = {a} and L2 = {aa} then aa ∈ L∗1 ∩ L∗2 but aa ∉ (L1 ∩ L2 )∗ .

(c) L∗1 L∗2 versus (L1 L2 )∗ .


Solution: {} ∪ L1 L2 ∪ (L1 ∩ L2 )∗ ⊂ L∗1 L∗2 ∩ (L1 L2 )∗ .
Proof:
Note that we always have  ∈ L∗1 L∗2 ∩ (L1 L2 )∗ .
If L1 = {a} and L2 = {b} then L∗1 L∗2 = {an bm ∶ n, m ≥ 0} and (L1 L2 )∗ = {(ab)n , n ≥ 0},
so L∗1 L∗2 ∩ (L1 L2 )∗ = {, ab}.
On the other hand, if L1 = {a, ab} and L2 = {b, ab} then
L∗1 L∗2 ∩ (L1 L2 )∗ = {(ab)n ∶ n ≥ 0}.

1
(d) L∗1 (L2 L∗1 )∗ versus (L∗1 L2 )∗ L∗1 .
Solution: L∗1 (L2 L∗1 )∗ = (L∗1 L2 )∗ L∗1 .
Proof:
If w ∈ L∗1 (L2 L∗1 )∗ then w = (x1,1 . . . x1,k1 )(y1 x2,1 . . . x2,k2 ) . . . (yp−1 xp,1 . . . xp,kp ) where the
xi,j ’s are in L1 and the yi ’s are in L2 .
Since concatenation is associative, we have (x1,1 . . . x1,k1 )(y1 x2,1 . . . x2,k2 ) . . . (yp−1 xp,1 . . . xp,kp )
= (x1,1 . . . x1,k1 y1 ) . . . (xp−1,1 . . . xp−1,kp−1 yp−1 )(xp,1 . . . xp,kp ), so w ∈ (L∗1 L2 )∗ L∗1 .
Similarly, if w ∈ (L∗1 L2 )∗ L∗1 then w ∈ L∗1 (L2 L∗1 )∗ .

2
2. Construct a DFA for each of the following languages

(a) {w ∈ {a, b}∗ ∶ bb appears at most once as a substring of w}

Solution:
a,b

qa b qb b qc b qd
start
a
a a b

qe b qf
a
a

(b) {w ∈ {a, b}∗ ∶ bab is not a substring of w}

Solution:

a b a,b

q0 b q1 a q2 b q3
start

(c) The set of strings over {a, b}∗ that either begin or end (or both) with ab.

Solution:

qa a qb b qc
start

b a a,b
a

qd a qe b qf

a
b
b

3
(d) The set of strings over {a, b}∗ that contain an odd number of a’s and end in at least
two b’s.

Solution:
b

q0 a q1 b q2 b q3
start
a
a
b
a

4
3. Let L = {w ∈ {0, 1}∗ ∶ w contains at least one 1}, and let A be the DFA with tabular represen-
tation:

A 0 1
→p p q
⋆q q q

Prove that L = L(A). Hint: Do the L(A) ⊆ L part of the proof by induction on the the length
of the string processed by A. You need a mutual induction with a claim for state p and a claim
for state q. Then use the contrapositive L(A) ⊆ L to show that L ⊆ L(A).

Solution:
To prove w ∈ L(A) ⇒ w ∈ L we use an induction on length of w. We claim that

(Sp ) If δ̂(p, w) = p, then w does not contain a 1.


(Sq ) If δ̂(p, w) = q, then w contains at least one 1.

Basis: If ∣w∣ = 0 then w = , so obviously (Sp ) is true. Also, it is clear that δ̂(p, ) = p ≠ q, so
(Sq ) is vacuously true.
Inductive Hypothesis: (Sp ) and (Sq ) are true for any string w of length k.
Inductive Step: Now prove (Sp ) and (Sq ) for w = xa, where ∣x∣ = k and a ∈ {0, 1}.

(Sp ) If δ̂(p, xa) = p then δ̂(p, x) is p and a = 0, as can be seen from the transition diagram of A.
When δ̂(p, x) = p then by the IH (Sp ), the string x does not contain a 1. Therefore w = x0
doesn’t contain a 1 either.
(Sq ) If δ̂(p, xa) = q then either δ̂(p, x) = p and a = 1 or δ̂(p, x) = q and a ∈ {0, 1}, as can be seen
from the transition diagram of A.
When δ̂(p, x) = p then by the IH (Sp ), the string x does not contain a 1, but δ̂(p, xa) =
δ̂(p, x1) and x1 clearly contains a 1.
When δ̂(p, x) = q then by the IH (Sq ), the string x contains a 1, so clearly xa still contains
a 1, for a ∈ {0, 1}.

To show that L(A) ⊆ L, let w ∉ L(A). From the transition diagram we see that δ̂(p, w) = p, so
by (Sp ) that we just showed, the string w does not contain a 1. Consequently w ∉ L.

5
4. Construct an NFA for each of the following languages.

(a) The set of strings over {0, 1, . . . , 9}, such that the final digit has not appeared before.

Solution:
0,1,2,3,4,5,6,7,8

q2

0,1,2,3,4,5,6,7,8 9
0,1,2,3,4,5,6,7,9

0,1,2,3,4,5,6,7,9 8
start q0 q1 q4

1,2,3,4,5,6,7,8,9 ⋮ 0

q3

1,2,3,4,5,6,7,8,9

(b) The set of strings over {0, 1}, such that there are two 0’s separated by a number of positions
that is a multiple of 4. Note that 0 is an allowable multiple of 4.

Solution:
0,1 0,1

q0 0 q1 0 q2
start
0,1 0,1

q3 q4

0,1 0,1
q5

6
5. Let L1 = {11, 110}∗ and L2 = {0}

(a) Give an NFA that accepts L1 L2 .


Solution:

q1

1 1

q0 0 q4
start
1 0

q2 1 q3

(b) Construct a DFA equivalent to your NFA of (a).


Solution:

start {q0 }

0 1

0,1 0
{q4 } ∅ {q1 , q2 }

0 1
0,1 1 1

0
{q0 , q4 } {q0 , q3 }

7
6. Let L1 = {0}∗ and L2 = {, 01, 11}.

(a) Give an NFA that accepts L1 L2 .


Solution:

q1
0
0 1

start q0 q3

1 1
q2

(b) Construct a DFA equivalent to your NFA of (a).


Solution:
0

1
{q0 , q1 } {q2 , q3 }

0
0
0

start {q0 } 0,1 ∅ 1

1
0

{q2 } {q3 }
1

8
7. Let A = ({q0 , q1 , q2 , q3 }, {a}, δ, q0 , {q3 }) be an -NFA with transition function

δ  a
→ q0 {q1 } {q1 }
q1 {q2 } {q2 }
q2 {q3 } {q3 }
⋆q3 ∅ ∅

Write down the transition diagram of A. Convert the -NFA A to an -free NFA B, using
the method on slide 5 of slides ”Epsilon-NFA’s” posted on Moodle. Give your NFA B as a
transition diagram. Then apply the subset method to B to obtain an equivalent DFA C. Give
the transition diagram of C.
Solution: Transition diagram of A

, a , a , a
start q0 q1 q2 q3

Equivalent NFA B

a
a

q0 a q1 a q2 a q3
start

Equivalent DFA C

a a a a
start {q0 } {q1 , q2 , q3 } {q2 , q3 } {q3 } ∅

You might also like