0% found this document useful (0 votes)
21 views12 pages

hw04 Soln

Uploaded by

Javeria Fazal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

hw04 Soln

Uploaded by

Javeria Fazal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CS 341: Foundations of Computer Science II

Prof. Marvin Nakayama

Homework 4 Solutions

1. Use the procedure described in Lemma 1.55 to convert the regular expression

(((00)∗ (11)) ∪ 01)∗

into an NFA.

Answer:

0
0

1
1

0 ε 0
00

1 ε 1
11

0 ε 1
01

ε 0 ε 0
(00)∗

1 ε 1
(00)∗(11) ε ε
0 ε 0
ε

1
1 ε 1
(00)∗(11) ∪ 01 ε ε
0 ε 0
ε ε

ε
ε 0 ε 1

1 ε 1
((00)∗(11) ∪ 01)∗ ε ε
0 ε 0
ε ε
ε
ε
ε 0 ε 1

While the resulting NFA is correct, the language has simpler NFAs (with fewer states
and transitions).
2. Use the procedure described in Lemma 1.60 to convert the following DFA M to a
regular expression.
a
a, b
1 2

a b b

Answer: First convert DFA M into an equivalent GNFA G.

2
a
s ε a∪b
1 2

ε b
a b

t ε 3

Next, we eliminate the states of G (except for s and t) one at a time. The order in which
the states are eliminated does not matter. However, eliminating states in a different
order from what is done below may result in a different (but also correct) regular
expression. We arbitrarily choose to first eliminate state 3. To do this, we define the
set C = {2} of states with edges directly into state 3, and set D = {1, 2, t} of states
with edges directly from state 3. We then need to account for paths that start in a
state in C, go immediately to state 3, and then go immediately to a state in D. Thus,
we need to account for the paths

• 2 → 3 → 1, which will create an arc from 2 to 1 labelled with ba;


• 2 → 3 → 2, which will create an arc from 2 to 2 labelled with bb; and
• 2 → 3 → t, which will create an arc from 2 to t labelled with bε = b.

We combine the previous arc from 2 to 2 labelled a with the new one labelled bb to
get the new label a ∪ bb.

a∪b a ∪ bb
s ε
1 2
ba
ε
b

We next eliminate state 1. To do this, we define C = {s, 2} as the set of states with
edges directly into state 1, and D = {2, t} as the set of states with edges directly
from state 1. Thus, need to account for the following paths from a state in C, going
immediately to state 1, and then immediately going to a state in D:

• s → 1 → 2, which will create an arc from s to 2 labelled with ε(a ∪ b) = a ∪ b.


• s → 1 → t, which will create an arc from s to t labelled with εε = ε.
• 2 → 1 → 2, which will create an arc from 2 to 2 labelled with ba(a ∪ b). We
combine this with the existing 2 to 2 arc to get the new label a ∪ bb ∪ ba(a ∪ b).
• 2 → 1 → t, which will create an arc from 2 to t labelled with baε = ba. We
combine this arc with the existing arc from 2 to t to get the new label b ∪ ba.

3
a∪b a ∪ bb ∪ ba(a ∪ b)
s 2

ε b ∪ ba

Finally, we eliminate state 2 by adding an arc from s to t labelled (a ∪ b)(a ∪ bb ∪


ba(a ∪ b))∗ (b ∪ ba). We then combine this with the existing s to t arc to get the new
label ε ∪ (a ∪ b)(a ∪ bb ∪ ba(a ∪ b))∗ (b ∪ ba).

ε ∪ (a ∪ b)(a ∪ bb ∪ ba(a ∪ b))∗ (b ∪ ba)

So a regular expression for the language L(M ) recognized by the DFA M is

ε ∪ (a ∪ b)(a ∪ bb ∪ ba(a ∪ b))∗ (b ∪ ba).

Writing this as

∪ (a ∪ b) (a ∪ bb ∪ ba(a ∪ b))∗ ∪ ba)


ε
|{z} | {z } | {z } (b
| {z }
stay in 1 1 to 2 (2 to 2)∗ end in 3 or 1

should make it clear how the regular expression accounts for every path that starts in
1 and ends in either 3 or 1, which are the accepting states of the given DFA.

a
a, b
1 2

a b b

3. Each of the following languages is either regular or nonregular. If a language is regular,


give a DFA and regular expression for it. If a language is nonregular, give a proof.

(a) A1 = { www | w ∈ {a, b}∗ }

4
Answer: A1 is nonregular. To prove this, suppose that A1 is a regular language.
Let p be the “pumping length” of the pumping lemma (Theorem 1.70). Consider
the string s = ap bap bap b. Note that s ∈ A1 since s = (ap b)3 , and |s| =
3(p + 1) ≥ p, so the pumping lemma will hold. Thus, we can split the string s
into 3 parts s = xyz satisfying the properties
i. xy i z ∈ A1 for each i ≥ 0,
ii. |y| > 0,
iii. |xy| ≤ p.
Since the first p symbols of s are all a’s, the third property implies that x and y
consist only of a’s. So z will be the rest of the first set of a’s, followed by bap bap b.
The second property states that |y| > 0, so y has at least one a. More precisely,
we can then say that

x = aj for some j ≥ 0,
y = ak for some k ≥ 1,
z = am bap bap b for some m ≥ 0.

Since ap bap bap b = s = xyz = aj ak am bap bap b = aj+k+mbap bap b, we must have
that j + k + m = p. The first property implies that xy 2 z ∈ A1 , but

xy 2 z = aj ak ak am bap bap b
= ap+k bap bap b

since j + k + m = p. Hence, xy 2 z 6∈ A1 because k ≥ 1, and we get a


contradiction. Therefore, A1 is a nonregular language.

(b) A2 = { w ∈ {a, b}∗ | w = w R }.


Answer: A2 is nonregular. To prove this, suppose that A2 is a regular language.
Let p be the “pumping length” of the pumping lemma (Theorem 1.70). Consider
the string s = ap bap . Note that s ∈ A2 since s = sR , and |s| = 2p + 1 ≥ p,
so the pumping lemma will hold. Thus, we can split the string s into 3 parts
s = xyz satisfying the properties
i. xy i z ∈ A2 for each i ≥ 0,
ii. |y| > 0,
iii. |xy| ≤ p.
Since the first p symbols of s are all a’s, the third property implies that x and y
consist only of a’s. So z will be the rest of the first set of a’s, followed by bap .
The second property states that |y| > 0, so y has at least one a. More precisely,
we can then say that

x = aj for some j ≥ 0,
y = ak for some k ≥ 1,
z = am bap for some m ≥ 0.

5
Since ap bap = s = xyz = aj ak am bap = aj+k+mbap , we must have that j +
k + m = p. The first property implies that xy 2 z ∈ A2 , but

xy 2 z = aj ak ak am bap
= ap+k bap

since j + k + m = p. Hence, xy 2 z 6∈ A2 because (ap+k bap )R = ap bap+k 6=


ap+k bap since k ≥ 1, and we get a contradiction. Therefore, A2 is a nonregular
language.

(c) A3 = { a2n b3n an | n ≥ 0 }.


Answer: A3 is nonregular. To prove this, suppose that A3 is a regular language.
Let p be the “pumping length” of the pumping lemma (Theorem 1.70). Consider
the string s = a2p b3p ap . Note that s ∈ A3 , and |s| = 6p ≥ p, so the pumping
lemma will hold. Thus, we can split the string s into 3 parts s = xyz satisfying
the properties
i. xy i z ∈ A3 for each i ≥ 0,
ii. |y| > 0,
iii. |xy| ≤ p.
Since the first p symbols of s are all a’s, the third property implies that x and y
consist only of a’s. So z will be the rest of the first set of a’s, followed by b3p ap .
The second property states that |y| > 0, so y has at least one a. More precisely,
we can then say that

x = aj for some j ≥ 0,
y = ak for some k ≥ 1,
z = am+p b3p ap for some m ≥ 0.

Since a2p b3p ap = s = xyz = aj ak am+p b3p ap = aj+k+m+pb3p ap , we must have


that j + k + m + p = 2p, or equivalently, j + k + m = p, so j + k ≤ p. The
first property implies that xy 2 z ∈ A3 , but

xy 2 z = aj ak ak am+p b3p ap
= a2p+k b3p ap

since j + k + m = p. Hence, xy 2 z 6∈ A3 because k ≥ 1, and we get a


contradiction. Therefore, A3 is a nonregular language.

(d) A4 = { w ∈ {a, b}∗ | w has more a’s than b’s }.


Answer: A3 is nonregular. To prove this, suppose that A4 is a regular language.
Let p be the “pumping length” of the pumping lemma (Theorem 1.70). Consider
the string s = bp ap+1 . Note that s ∈ A4 , and |s| = 2p + 1 ≥ p, so the pumping
lemma will hold. Thus, we can split the string s into 3 parts s = xyz satisfying
the properties

6
i. xy i z ∈ A4 for each i ≥ 0,
ii. |y| > 0,
iii. |xy| ≤ p.
Since the first p symbols of s are all b’s, the third property implies that x and y
consist only of b’s. So z will be the rest of the b’s, followed by ap+1 . The second
property states that |y| > 0, so y has at least one b. More precisely, we can then
say that
x = bj for some j ≥ 0,
y = bk for some k ≥ 1,
z = bm ap+1 for some m ≥ 0.
Since bp ap+1 = s = xyz = bj bk bm ap+1 = bj+k+map+1 , we must have that
j + k + m = p. The first property implies that xy 2 z ∈ A4 , but
xy 2 z = bj bk bk bm ap+1
= bp+k ap+1
since j + k + m = p. Hence, xy 2 z 6∈ A4 because it doesn’t have more a’s
than b’s since k ≥ 1, and we get a contradiction. Therefore, A4 is a nonregular
language.
(e) A5 = { w ∈ {a, b}∗ | nab (w) = nba (w) }, where ns (w) is the number of
occurrences of the substring s ∈ {a, b}∗ in w.
Answer: A5 is regular. A regular expression for the language is a(a ∪ bb∗ a)∗ ∪
b(b ∪ aa∗ b)∗ ∪ ε. Another regular expression is a(a ∪ b)∗ a ∪ b(a ∪ b)∗b ∪ a ∪ b ∪ ε.
A DFA for the language is

a b
b
q1 a q2 q3
a
b
b
q4

a
b
a
q5

There are infinitely many other correct regular expressions and DFAs for A5 .

7
(f) A6 = { w ∈ {a, b}∗ | na (w) 6= nb (w) }.
Answer: A6 is nonregular, which we will establish via a proof by contradiction.
Suppose that A6 were regular. Then its complement A6 is regular because the
class of regular languages is closed under complementation (HW 2, problem 3),
where

A6 = { w ∈ {a, b}∗ | na (w) = nb (w) }. (1)

By arguing as on slide 1-112 of the lecture notes, we can show that A6 is nonregular
(which we could also prove by applying the pumping lemma with the string s =
ap bp ∈ A6 ), giving a contradiction, so A6 is nonregular.
Alternatively, using an approach outlined at this link, we can also prove that A6
is nonregular via the pumping lemma (Theorem 1.70) by carefully constructing
a string s ∈ A6 to get a contradiction. For a contradiction, suppose that A6 is
regular, and let p be the pumping length of the pumping lemma. Consider the
string s = ap bp!+p , where p! = p(p − 1)(p − 2) · · · 1 is p factorial, so s ∈ A6
because na (s) = p 6= p! + p = nb (s). Also, |s| = 2p + p! ≥ p, so the
conclusions of the pumping lemma will hold. Thus, we can split the string s into
3 parts s = xyz satisfying the properties
i. xy i z ∈ A6 for each i ≥ 0,
ii. |y| > 0,
iii. |xy| ≤ p.
Since the first p symbols of s are all a’s, the third property implies that x and y
consist only of a’s. So z will be the rest of the a’s, followed by bp!+p. The second
property states that |y| > 0, so y has at least one a. More precisely, we can then
say that

x = aj for some j ≥ 0,
y = ak for some k ≥ 1,
z = am bp!+p for some m ≥ 0,

with j + k ≤ p, so

1 ≤ k ≤ p. (2)

Also, we must have that

j+k+m=p (3)

because ap bp!+p = s = xyz = aj ak am bp!+p = aj+k+mbp!+p .


The first property of the pumping lemma states that for all i ≥ 0, the pumped
string xy i z must be in A6 , and we will get a contradiction by showing that no
matter the value of k satisfying equation (2), there will exist an i ≥ 0 (depending
on k) so that the pumped string xy i z 6∈ A6 , which means that na (xy i z) =

8
nb (xy i z) for this value of i by equation (1). Now this pumped string is xy i z =
aj aik am bp!+p , with na (xy i z) = j + ik + m and nb (xy i z) = p! + p. To get our
contradiction, we must show that for each k satisfying equation (2), there exists
an i ≥ 0 such that
j + ik + m = p! + p (4)
so that the pumped string xy i z 6∈ A6 for this i because na (xy i z) = nb (xy i z).
Now equation (3) implies that the left side of equation (4) satisfies j + ik +
m = j + k + m + (i − 1)k = p + (i − 1)k, so equation (4) is equivalent to
p + (i − 1)k = p! + p, which is the same as (i − 1)k = p!. Thus, for any
k satisfying equation (2), taking i = 1 + (p!/k), which is an integer because
k ∈ {1, 2, . . . , p}, results in the pumped string xy i z 6∈ A6 , contradicting the
pumping lemma’s first property. Hence, A6 is nonregular.
(g) A7 = { aj bk cl | j, k, l ≥ 0 and if j = 1, then k = l }.
Answer: A7 is nonregular, which we will establish via a proof by contradiction.
Suppose that A7 is regular, and consider the language L having regular expression
ab∗ c∗ , so L is regular by Kleene’s Theorem. Let D = A7 ∩ L, which must then
be regular if A7 is regular because the class of regular languages is closed under
intersection (HW 2, problem 5). Now
D = A7 ∩ L = { abn cn | n ≥ 0 },
which we will show below is nonregular, giving a contradiction, so A7 must be
nonregular.
To show that D is nonregular, we will use the pumping lemma (Theorem 1.70).
For a contradiction, suppose that D is regular, and let p be the pumping length of
the pumping lemma. Consider the string s = abp cp ∈ D, where |s| = 2p + 1 ≥
p, so the conclusions of the pumping lemma will hold. Thus, we can split the
string s into 3 parts s = xyz satisfying the properties
i. xy i z ∈ A7 for each i ≥ 0,
ii. |y| > 0,
iii. |xy| ≤ p.
The second property implies that y 6= ε. Also, since the first p symbols of s are
abp−1 , the third property implies that y is a nonempty substring of abp−1 . We
can break down all of the nonempty possibilities for y into two cases: y contains
a, or y does not contain a. We next show a contradiction arises in each case.
• Suppose y contains a. The first conclusion of the pumping lemma implies
that xy 0 z = xz ∈ D, but since y has the only a in s, the pumped-down
string xz does not begin with a, so xz 6∈ D, which is a contradiction.
• Suppose that y does not contain a. Since y 6= ε and y is a nonempty
substring of abp−1 , we must have that y has at least one b. The first property
of the pumping lemma implies that xy 0 z = xz ∈ D, but since y has at least
one b, the pumped-down string xz has fewer b’s than c’s, so xz 6∈ D, which
is a contradiction.

9
The two above cases cover all possibilities for y satisfying the three properties of
the pumping lemma, and each gives a contradiction. Thus, the pumping lemma
does not hold, so D is nonregular.
An interesting property about A7 is that while it is nonregular, this cannot be
proven using the pumping lemma (Theorem 1.70). In particular, there exists some
p (specifically, we can take p = 3) such that for every string s ∈ A7 with |s| ≥ p,
we can split s = xyz such that (i) xy i z ∈ A7 for all i ≥ 0, (ii) y 6= ε, and (iii)
|xy| ≤ p. To see why, write A7 = E ∪ F with

E = { abn cn | n ≥ 0 },
F = { aj bk cl | j, k, l ≥ 0 and j 6= 1 },

so E ⊆ A7 and F ⊆ A7 . We can cover all possibilities for strings s ∈ A7 with


|s| ≥ p through two cases: either s ∈ E or s ∈ F (or both)
• Suppose that s ∈ E with |s| ≥ p = 3, so s = abn cn for some n ≥ 1. We
then split s as s = xyz, with x = ε, y = a, and z = bn cn . Note that
|xy| = |a| = 1 ≤ p = 3 and y 6= ε, so the second and third properties of
the pumping lemma hold. For each i ≥ 0, the pumped string xy i z is ai bn cn ,
which is in F ⊆ A7 for i 6= 1, and belongs to E ⊆ A7 for i = 1, so the
pumped string xy i z ∈ A7 for all i ≥ 0. In other words, each string s ∈ E
with |s| ≥ p = 3 can be split into s = xyz with all three conclusions of the
pumping lemma holding.
• Suppose that s ∈ F with |s| ≥ p = 3, so s = aj bk cl for some j, k, l ≥ 0
with j 6= 1. Then all possibilities for s belong to one of the following three
subcases: j = 0, j = 2, and j ≥ 3.
– If j = 0, then s = bk cl , with |s| ≥ p, so s = s1 s2 · · · sn for some
n ≥ p with each si ∈ {b, c}, and all b’s appear before any c. Split s
into s = xyz with x = ε, y = s1 , and z = s2 s3 · · · sn . Thus, y 6= ε
and |xy| = |s1 | = 1 ≤ p = 3, so the second and third properties
of the pumping lemma hold. For each i ≥ 0, the pumped string is
xy i z = si1 s2 · · · sn , which can be generated by the regular expression
b∗ c∗ , so the pumped string will always be in F ⊆ A7 .
– If j = 2, then the string is s = aabk cl . We then can split s into s = xyz
with x = ε, y = aa, and z = bk cl . Thus, y 6= ε and |xy| = |aa| =
2 ≤ p = 3, so the second and third properties of the pumping lemma
hold. Now for each i ≥ 0, the pumped string xy i z = (aa)i bk cl has 2i
a’s at the beginning followed by bk cl , so the number of a’s is never 1,
implying that xy i z ∈ F ⊆ A7 .
– If j ≥ 3, then the string s = aj bk cl has at least 3 a’s at the beginning.
We then can split s into s = xyz with x = ε, y = a, and z = aj−1 bk cl .
Thus, y 6= ε and |xy| = |a| = 1 ≤ p = 3, so the second and third
properties of the pumping lemma hold. Now for each i ≥ 0, the pumped
string xy i z = ai+j−1 bk cl has i + j − 1 ≥ 2 a’s at the beginning followed
by bk cl , implying that xy i z ∈ F ⊆ A7 .

10
Thus, both cases of s ∈ E and s ∈ F always can be pumped to get strings still in
A7 , so there is no contradiction. In other words, A7 is nonregular, but we cannot
prove this via the pumping lemma.
To understand this, recall that if the conclusions of the pumping lemma do not
hold for a given language, then the language is nonregular. While this gives
a sufficient condition to ensure a language is nonregular, language A7 shows
that the condition is not necessary for nonregularity. (A necessary and sufficient
condition for regularity is provided by the Myhill-Nerode theorem, which our class
doesn’t cover but appears as problem 1.52 of the Sipser book.)

4. Suppose that language A is recognized by an NFA N , and language B is the collection


of strings not accepted by some DFA M . Prove that A ◦ B is a regular language.

Answer: Since A is recognized by an NFA, we know that A is regular since a language


is regular if and only if it is recognized by an NFA (Corollary 1.20). Note that the
DFA M recognizes the language B, the complement of B. Since B is recognized by a
DFA, by definition, B is regular. We know from a problem on the previous homework
that B being regular implies that its complement B is regular. (B is the complement
of the complement of B.) But B = B, so B is regular. Since A and B are regular,
their concatenation A ◦ B is regular by Theorem 1.23.

5. (a) Prove that if we add a finite set of strings to a regular language, the result is a
regular language.
Answer: Let A be a regular language, and let B be a finite set of strings.
We know from class (see page 1-95 of Lecture Notes for Chapter 1) that finite
languages are regular, so B is regular. Thus, A ∪ B is regular since the class of
regular languages is closed under union (Theorem 1.22).

(b) Prove that if we remove a finite set of strings from a regular language, the result
is a regular language.
Answer: Let A be a regular language, and let B be a finite set of strings with B ⊆
A. Let C be the language resulting from removing B from A, i.e., C = A − B.
As we argued in the previous part, B is regular. Note that C = A − B = A ∩ B.
Since B is regular, B is regular since the class of regular languages is closed
under complement. We proved in an earlier homework that the class of regular
languages is closed under intersection, so A ∩ B is regular since A and B are
regular. Therefore, A − B is regular.

(c) Prove that if we add a finite set of strings to a nonregular language, the result is
a nonregular language.
Answer: Let A be a nonregular language, and let B be a finite set of strings.
We want to add B to A, so we may assume that none of the strings in B are
in A, i.e., A ∩ B = ∅. Let C be the language obtained by adding B to A, i.e.,

11
C = A ∪ B. Suppose for a contradiction that C is regular, and we now show
this is impossible. Since A ∩ B = ∅, we have that A = C − B. Since C and
B are regular (the latter because B is finite), the previous part of this problem
implies that C − B = C ∩ B must be regular, but we assumed that A = C − B
is nonregular, so we get a contradiction.

(d) Prove that if we remove a finite set of strings from a nonregular language, the
result is a nonregular language.
Answer: Let A be a nonregular language, and let B be a finite set of strings,
where B ⊆ A. Let C be the language obtained by removing B from A, i.e.,
C = A − B. Suppose that C is regular, and we now show this is impossible.
Since we removed B from A to get C, we must have that C ∩ B = ∅, so
A = C ∪ B. Now C is regular by assumption and B is regular since it’s finite,
so C ∪ B must be regular by Theorem 1.25. But we assumed that A = C ∪ B
is nonregular, so we get a contradiction.

6. Consider the following statement: “If A is a nonregular language and B is a language


such that B ⊆ A, then B must be nonregular.” If the statement is true, give a proof.
If it is not true, give a counterexample showing that the statement doesn’t always hold.

Answer: The statement is not always true. For example, we know that the language
A = { 0j 1j | j ≥ 0} is nonregular. Define the language B = {01}, and note that
B ⊆ A. However, B is finite, so we know that it is regular.

12

You might also like