hw04 Soln
hw04 Soln
Homework 4 Solutions
1. Use the procedure described in Lemma 1.55 to convert the regular expression
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
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
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:
3
a∪b a ∪ bb ∪ ba(a ∪ b)
s 2
ε b ∪ ba
Writing this as
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
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
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
x = aj for some j ≥ 0,
y = ak for some k ≥ 1,
z = am+p b3p ap for some m ≥ 0.
xy 2 z = aj ak ak am+p b3p ap
= a2p+k b3p ap
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
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)
j+k+m=p (3)
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 },
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.)
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.
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