Exercise Collection
Exercise Collection
SELECTED EXERCISES
WITH SOLUTIONS
The exercises and the solutions presented in this collection are based on
the material listed below.
1
Notation
What follows are some notational conventions related to automata and for-
mal languages, which have not been introduce by the class textbook but
which are quite common in the literature and will be used throughout the
following chapters.
2
Contents
1 Regular Languages 4
2 Context-Free Languages 18
3
Chapter 1
Regular Languages
• For every x ∈ {0, 1}∗ , Pq0 (x) holds if and only if #1 (x) is even.
• For every x ∈ {0, 1}∗ , Pq1 (x) holds if and only if #1 (x) is odd.
(i) For every x ∈ {0, 1}∗ , Pq0 (x) holds if and only if δ̂(q0 , x) = q0 .
4
0 0
1
Start
q0 q1
(ii) For every x ∈ {0, 1}∗ , Pq1 (x) holds if and only if δ̂(q0 , x) = q1 .
Informally, (i) states that string x has an even number of occurrences of 1 if
and only if, starting from the initial state and reading x, A reaches state q0 .
Similarly, (ii) states that string x has an odd number of occurrences of 1 if
and only if, starting from the initial state and reading x, A reaches state q1 .
Intuitively, for a state q of A the property Pq indicates what language A
would accept if A had q as the only one final state.
At this point we observe that statement (ii) can be used to prove the
statement in the exercise. In fact we have the following chain of equivalences
x ∈ L(A) iff δ̂(q0 , x) = q1 (L(A)’s definition)
iff Pq1 (x) (statement (ii))
iff #1 (x) is odd (Pq1’s definition)
iff x∈L (L’s definition)
which leads us to the conclusion L(A) = L.
To complete the exercise, we must therefore demonstrate the state-
ments (i) and (ii). Since (i) and (ii) are of the form “if and only if”, we
need to prove four statements, and for each statement we need to prove
the base case and the inductive case. As already suggested, we use mutual
induction.
(Part (i), only if) We have to prove that, for every x ∈ {0, 1}∗ , if Pq0 (x)
then δ̂(q0 , x) = q0 . We proceed by induction on the length of x.
Base. We have |x| = 0, which implies x = ε. Since #1 (ε) is even, Pq0 (ε)
holds. Also, by definition of δ̂ we can write δ̂(q0 , ε) = q0 . We conclude then
that the implication holds true.
5
Induction. Let |x| = n > 0. If Pq0 (x) is false, then the implication is always
true. Let’s consider instead the case in which Pq0 (x) is true, that is, #1 (x)
is even. Let us write x = ya with a ∈ {0, 1} and y ∈ {0, 1}∗ , |y| = n − 1.
We need to distinguish two cases, depending on whether a = 0 or a = 1.
• If a = 0, then #1 (y) = #1 (x) and therefore Pq0 (y) holds true. We then
apply the inductive hypotheses for the only if part of statement (i),
and conclude that δ̂(q0 , y) = q0 . From the definition of the transition
function of A, we have that from state q0 and by reading symbol 0 we
can reach state q0 . So we can write
δ̂(q0 , x) = δ(δ̂(q0 , y), 0) = δ(q0 , 0) = q0 .
This concludes the inductive part of the proof showing the only if part of
the statement (i).
(Part (i), if) We have to prove that, for every x ∈ {0, 1}∗ , if δ̂(q0 , x) = q0
then Pq0 (x) holds. Again, we proceed by induction on the length of string x.
Base. We have |x| = 0, that is, x = ε. The relation δ̂(q0 , ε) = q0 is always
true, by definition of δ̂. Furthermore, Pq0 (ε) is also true, since #1 (ε) is even.
We therefore conclude that the implication is true.
Induction. Let |x| = n > 0. If δ̂(q0 , x) = q0 is false, then the implication we
want to prove is always true. Let’s consider now the case where δ̂(q0 , x) = q0
is true. As before, let us write x = ya with a ∈ {0, 1} and y ∈ {0, 1}∗ ,
|y| = n − 1. We distinguish two cases, depending on whether a = 0 or a = 1.
• Let a = 0. Since δ̂(q0 , x) = q0 , after reading x the automaton must be
in state q0 . The last symbol of x is 0, and from the definition of A we
see that by reading 0 we can reach state q0 only if the automaton is
already in q0 after reading y. Therefore we conclude that δ̂(q0 , y) = q0 .
We can now apply the inductive hypothesis for part if of statement (i),
and conclude that Pq0 (y) is true, that is, y has an even number of
occurrences of 1. Then x also has an even number of occurrences of 1,
and we can assert that Pq0 (x) is also true.
6
• Let a = 1. Since δ̂(q0 , x) = q0 , the automaton is in state q0 after
reading x. The last symbol of x is 1, and from the definition of A we
see that, by reading 1, state q0 can be reached only if the automaton
is in state q1 after reading y. We then conclude that δ̂(q0 , y) = q1 .
We now apply the inductive hypothesis for part if of statement (ii),
and conclude that Pq1 (y) holds true, that is, y has an odd number of
occurrences of 1. Since #1 (y) = #1 (x) − 1, we conclude that x has
an even number of occurrences of 1, and we can assert that Pq0 (x) is
true.
This concludes the inductive part of the proof, and we have then completed
the if part of statement (i).
The if and the only if parts of statement (ii) can be proved in a very
similar way to the proof of the same parts for statement (i) reported above.
These two proofs are therefore omitted.
Note In this end note to the above exercise, we discuss the need for
the use of mutual induction in the proof of statements (i) and (ii). Let us
consider the inductive case of the only if part of statement (i). We have
discussed two different scenarios, depending on whether string x ends with
0 or 1. In the latter case, we have x = y1, so if x has an even number
of occurrences of 1, then y has an odd number of occurrences of the same
symbol. For this reason, we can’t invoke on y the inductive hypothesis of
the same statement (i) we are proving, which concerns strings with an even
number of occurrences of 1: we must instead invoke on y the inductive
hypothesis of statement (ii), which concerns strings with an odd number of
occurrences of 1. This requires the technique of mutual induction.
7
δN 0 1
→ q0 {q0 , q1 } {q1 }
q1 {q1 , q2 } ∅
∗q2 ∅ {q2 }
Figure 1.2: Transition function in tabular form for the NFA of Exercise 1.2.
We recall the reader that the formula to get the next state from a state
{q1 , q2 , . . . , qr } of the DFA, by reading symbol a ∈ Σ, is as follows:
r
[
δD ({q1 , q2 , . . . , qr }, a) = δN (qi , a).
i=1
We report below some among the first states of D, in order of their reacha-
bility from the initial state {q0 }:
In a similar way, we can obtain the remaining states (the intermediate steps
are omitted):
δD ({q0 , q1 , q2 }, 0) = {q0 , q1 , q2 }
δD ({q0 , q1 , q2 }, 1) = {q1 , q2 }
δD ({q1 , q2 }, 0) = {q1 , q2 }
δD ({q1 , q2 }, 1) = {q2 }
δD ({q2 }, 0) = ∅
δD ({q2 }, 1) = {q2 }
8
0
Start 0 0
{q0 } {q0 , q1 } {q0 , q1 , q2 }
1 1 1
0
{q1 } {q1 , q2 } 0
1 1
0
0, 1 ∅ {q2 } 1
Figure 1.3: DFA D equivalent to the NFA with transition table reported in
Figure 1.2.
It’s not difficult to convince oneself that L contains all of the strings whose
third to last symbol is b. Even if the exercise requires to exhibit a determinis-
tic automaton that recognizes L, it is convenient to build a nondeterministic
automaton first. Afterwards, it will be possible to derive a deterministic ver-
sion of the automaton by applying the construction already exploited by the
solution to Exercise 1.2, that is, the construction used in the proof of the
equivalence of languages recognized by the DFA and the NFA classes. The
choice of this procedure stems from the fact that, as often happens, the non-
deterministic automaton has a reduced number of states: this simplifies the
9
specification of the automaton itself, as well as the proof that the recognised
language is the required one.
We therefore specify the NFA M = (Q, {a, b}, δ, q0 , F ), with Q = {q0 , q1 , q2 , q3 }
and F = {q3 }. The transition function δ : Q × {a, b} 7→ 2Q is defined in
Figura 1.4, both in the table and in the graphical form.
stato q0 q1 q2 q3
a {q0 } {q2 } {q3 } ∅
b {q0 , q1 } {q2 } {q3 } ∅
a, b
Start b a, b a, b
q0 q1 q2 q3
10
Induction. Assume |z| > 3; by definition of L, we can write z = xby, with
|x| ≥ 1. Since x contains at least one symbol, we can also write z = Xx′ by,
where X ∈ {a, b} represents the first symbol of x and x′ belongs to {a, b}∗ .
Starting from the initial state, and independently of the specific symbol
X, M can stay in state q0 , since q0 ∈ δ(q0 , a) e q0 ∈ δ(q0 , b). Let us now
observe that the string x′ by also belongs to L, and we have |x′ by| < |z|. By
the inductive hypothesis, then, we derive that x′ by belongs to L(M ), and
therefore there must be some path from q0 to q3 labeled by x′ by. Overall,
we’ve thus shown the existence of a path labeled Xx′ by = z from the initial
state to some final state, and we can conclude that z ∈ L(M ).
(Part L(M ) ⊆ L) For a generic string z ∈ L(M ), we define n = |z| and we
show that z ∈ L(M ) implies z ∈ L. We proceeds by induction on n.
Base. According to the definition of the transition function δ, in order
to move from the initial state to the final state of M we need to visit in
sequence all of the intermediate states of the automaton. Since M has a
total of four states, it takes at least three steps to accept any string. It is
easy to see that the paths of length 3 from q0 to q3 are labeled baa, bab, bba,
bbb. In all cases, b is the third to last symbol, and we can conclude that all
strings accepted by the automaton in three steps belong to L.
Induction. Assume now that n > 3. We observe that, in the first computa-
tion step, M must necessarily stay in state q0 : if it does not, the automaton
would reach in two steps the final state, from which no more moves are
possible. We can then write z = Xx, where X ∈ {a, b} represents the first
symbol of z. String x is accepted by the automaton in less than n steps.
Using the inductive hypothesis, we derive that x belongs to L, and therefore
its third to last symbol is a b. But the third to last symbol of x is also the
third to last symbol of Xx = z. We can then conclude that z ∈ L.
We have concluded our proof that the nondeterministic automaton M
exactly recognizes the language L. To complete the exercise, we now build
an equivalent deterministic automaton M ′ = (Q′ , A′ , B ′ , F ′ ), using the same
technique already exploited in Exercise 1.2. The states in Q′ are subsets of
Q and represent the sets of states in which “different copies” of M can be at
the same time, after reading the same input string. To compute the elements
of Q′ we use the following definition:
r
[
′
δ ({q1 , q2 , . . . , qr }, X) = δ(qi , X), X ∈ {a, b}.
i=1
With this definition, we start with the initial state {q0 } in M ′ , and and
11
compute all reachable states.
12
all states in Q′ that contain at least one state in F ; in our case,
Exercise 1.4 State whether the following languages are regular or not,
and justify your answer:
13
L0 would also be regular, against what we just have shown. Therefore we
must conclude that L1 is not a regular language.
The language L2 is a regular language. In fact, we will show that L2 =
{a, b}+ , which is a well-known regular language. The proof of this fact is
split into two parts.
(Part L2 ⊆ {a, b}+ ) Let z be a generic string in L2 . According to the
definition of L2 , we can write z = u · v, with u ∈ {a, b}∗ and v ∈ L1 . Since
L1 does not contain the null string, we have v ̸= ε, and therefore z ̸= ε. The
statement of this part now follows immediately, since {a, b}+ contains by
definition each string that is composed of occurrences of a and b, excluding
the null string.
(Part {a, b}+ ⊆ L2 ) Let z be a generic string in {a, b}+ . According to the
definition of L2 , to show z ∈ L2 we have to find strings u and v such that
z = u · v, with u ∈ {a, b}∗ and v ∈ L1 . Since the null string is not included
in {a, b}+ , z contains at least one occurrence of a symbol. Therefore it is
legitimate to place in v the last symbol occurrence in z, and to place in u
all of the remaining symbol occurrences in z. Let us observe that we can
have v = a or v = b, but in either case #a (v) ̸= #b (v), and therefore v ∈ L1
according to the definition of this language. Since u ∈ a, b∗, this concludes
our proof.
14
L2 = Σ∗ , which is regular. The result is again L1 , which we have assumed
to be a non-regular language.
Exercise 1.6 State whether the following languages, defined over the al-
phabet Σ = {a, b}, are regular or not, and motivate your answer:
15
to our original assumption about L< being regular, it sufficis to prove that
the pumping lemma is violated for at least one value of k.
(iii) The language L> is not regular. As in previous cases, let us assume
that L> is regular and let n be the pumping lemma constant for the language.
We now choose the string w = an bn−1 , which has length |w| ≥ n. Then we
can write w = xyz, where |xy| ≤ n and |y| = m ≥ 1 and we have that, for
each k value with k ≥ 0, the string wk = xy k z = an−m akm bn−1 must still
be a string of L> . Choosing k = 0, we get the string w0 = an−m bn−1 which
does not belong to L> , since we have m ≥ 1, and therefore in w0 the number
of occurrences of symbol a is not greater than the number of occurrences of
symbol b, as required by L> ’s definition. This allows us to conclude that
L> is not regular.
(iv) Finally, the language L̸= is not regular. However, in this case a
direct application of the pumping lemma would be problematic; the reader
is encouraged to make one attempt in this direction to realize this fact. We
then take a different route with respect to cases (i), (ii) and (iii) above, More
specifically, assuming that L̸= is regular, we will apply operators defined on
regular languages that guarantee that the result is still a regular language,
to arrive at a new language that we will show not to be regular using the
pumping lemma. This allows us to conclude that even L̸= is not a regular
language.
Complementing L̸= with respect to the alphabet Σ = {a, b}, we obtain
the language L̸= formed by all strings that have a certain number of a
followed by the same number of b, as well as all strings that have a and b in
various other positions that do not respect the ai bj pattern. Since we have
assumed that L̸= is regular, L̸= should also be regular. This follows from
the closure property of the regular languages under the complementation
operator.
To make the next part of the proof easier, it is very useful to “filter” the
language L̸= using the auxiliary language
Lab = {ai bj | i, j ≥ 0} .
L̸= ∩ Lab = L= .
This last equality follows from our observation above that the strings in L̸=
are all strings formed by a certain number of a followed by the same number
of b, but also all strings that have occurrences of symbols a and b mixed
16
together, in a way that violates the pattern ai bj imposed by Lab . Since we
are assuming that L̸= is regular, and since we know that Lab is also regular,
language L= must also be regular: this follows from the closure property
of the regular languages under the intersection operator. However, we have
already shown in part (i) of this same exercise that L= is not regular, so we
have obtained a contradiction. We must therefore conclude that L̸= is not
a regular language.
17
Chapter 2
Context-Free Languages
S → 0S1 | A
A → 1A | 1
In the general case, proving that L(G) = L for a CFG G and a language
L is very complex, the textbook mentions this problem in a special frame
in Chapter 5. The technique used for such proofs is mutual induction, and
the basic idea is the same we have studied for the case of DFAs; see for
instance Exercise 1.1 in Chapter 1. In case of a DFA M , it is necessary
to define properties Pq for each state q of M , and relate each property Pq
with computations of M starting from the initial state and reaching q. In
a similar way, in the case of a CFG G it is necessary to define a property
PA for each variable A of G and relate each property PA to the derivations
of G that start with A and generate a string entirely composed of terminal
18
symbols. In case of the present exercise, G is very simple and uses only two
variables, and therefore the proof of property L(G) = L is relatively simple.
We carry out the proof in the remaining part of this exercise.
Let us start with the definition of the properties associated with the two
variables of G.
• For every x ∈ {0, 1}∗ , PA (x) holds true if and only if x is a sequence
composed by n ≥ 1 occurrences of symbol 1.
• For every x ∈ {0, 1}∗ , PS (x) holds true if and only if x is a sequence
composed by n ≥ 0 occurrences of symbol 0, followed by m > n
occurrences of symbol 1.
19
+
A ⇒ y we have PA (y), that is, y is a sequence composed by n ≥ 1 occurrences
of symbol 1. It follows that x = 1y is a sequence composed by n ≥ 2
occurrences of symbols 1, and therefore PA (x) holds true.
+
(Part (i), only if) We must prove that if PA (x) holds true then A ⇒ x, for
every x ∈ {0, 1}∗ . We proceed by induction on the length of x.
Base. Let |x| = 1. If x = 1, then PA (1) holds true. The required derivation
1
for x is A ⇒ 1. If x = 0, then PA (0) is false. It follows that the statement
+
“if PA (0) is true then A ⇒ 0” holds true, since the antecedent is false.
Induction. Assuming |x| > 1, we consider the hypothesis that PA (x) holds
+
true and we prove A ⇒ x. We have that x is a sequence composed by n > 1
occurrences of symbols 1. We write x = 1y, with y ̸= ε. Since y contains at
least one occurrence of 1, PA (y) holds true. Since |y| ≤ |x|, we can apply
+
the inductive hypothesis and conclude that A ⇒ y. Using the production
1 +
A → 1A, we can finally write A ⇒ 1A ⇒ 1y = x.
+
(Part (ii), if) We must show that if S ⇒ x then PS (x) holds true, for
x ∈ {0, 1}∗ . We proceed by mutual induction on the length of the derivation.
+ 1 1
Base. The shortest derivation of the form S ⇒ x is S ⇒ A ⇒ 1 and has
length of two. We have x = 1 ∈ L, and therefore PS (1) holds true.
+
Induction. Let S ⇒ x be a derivation of length greater than two. We
+
distinguish two cases, based on the first production in S ⇒ x
+
• If the derivation S ⇒ x starts with the production S → 0S1, then
1 + +
we can write S ⇒ 0S1 ⇒ 0y1 = x. We thus have S ⇒ y. Applying
+
the inductive hypothesis to S ⇒ y we obtain that PS (y) holds true,
that is, y is a sequence composed by n ≥ 0 occurrences of symbol 0,
followed by m > n occurrences of symbol 1. It follows that x = 0y1 is
a sequence composed by n ≥ 1 occurrences of symbol 0, followed by
m > n occurrences of symbol 1, and therefore PS (x) holds true.
+
• If the derivation S ⇒ x starts with the production S → A then we can
1 + +
write S ⇒ A ⇒ x. Since the derivation A ⇒ x has one step less than
+ +
the derivation S ⇒ x, we can apply the mutual induction to A ⇒ x,
deriving that PA (x) holds true, that is, x is a sequence composed by
n ≥ 1 occurrences of symbol 1. This also means that x is a sequence
composed by n ≥ 0 occurrences of symbol 0, followed by m > n
occurrences of symbol 1, and therefore we have that PS (x) holds true.
20
+
(Part (ii), only if) We must prove that if PS (x) holds true then S ⇒ x, for
x ∈ {0, 1}∗ . We proceed by mutual induction on the length of x.
Base. Let |x| = 1. If x = 1, then PS (x) holds true. The required derivation
1 1
for x is S ⇒ A ⇒ 1. If instead x = 0, we have #0 (0) > #1 (0) = 0. Since the
property PS (x) requires #1 (x) > #0 (x), we conclude that PS (0) is false. It
+
follows that the statement “if PS (0) holds true then S ⇒ 0” is true, since
the antecedent is false.
Induction. Let |x| > 1. We assume the hypothesis that PS (x) holds true,
+
and we prove S ⇒ x. We know that x is a sequence composed by n ≥ 0
occurrences of symbol 0, followed by m > n occurrences of symbol 1. we
distinguish two cases, based on the first symbol in string x
• If x starts with 0, then the last symbol of x must be 1, otherwise we
would have a violation of PS (x). Thus we can write x = 0y1 for some
string y ∈ {0, 1}∗ . Since #0 (y) = #0 (x)−1 and #1 (y) = #1 (x)−1, the
string y is still a sequence composed by n ≥ 0 occurrences of symbol
0, followed by m > n occurrences of symbol 1, and then PS (y) holds
true. Since |y| < |x|, we can apply the inductive hypothesis to y and
+
obtain the relation S ⇒ y. Using the production S → 0S1, we can
1 +
write S ⇒ 0S1 ⇒ 0y1 = x.
L = {ai bj ck | i, j, k ≥ 1}.
21
generate a run of b’s, and finally switch to variable C to generate a run of
c’s. The required context-free grammar is therefore
G = ({S, B, C}, {a, b, c}, P, S),
where P consists of the following rules
S → aS | aB
B → bB | bC
C → cC | c
To prove L(G) = L we need to use mutual induction, as in Exercise 2.1.
Let us start with the definition of the properties associated with the three
variables of G. For every w ∈ {a, b, c}∗
• PC (w) holds true if and only if w is a string of k ≥ 1 occurrences of
symbol c;
• PB (w) holds true if and only if w is a string of j ≥ 1 occurrences of
symbol b, followed by k ≥ 1 occurrences of symbol c;
• PS (w) holds true if and only if w is a string of i ≥ 1 occurrences of
symbol a, followed by j ≥ 1 occurrences of symbol b, followed by k ≥ 1
occurrences of symbol c.
We now need to prove the following statements. For every w ∈ {a, b, c}∗
+
(i) PC (w) if and only if C ⇒ w;
+
(ii) PB (w) if and only if B ⇒ w;
+
(iii) PS (w) if and only if S ⇒ w.
Since each of the above statements has the form “if and only if”, we
must prove six statements overall, and for each statement we must provide
the base case and the inductive case. Finally, we observe that PS matches
the definition of language L, thus statement (iii) can be used to prove the
desired property L(G) = L.
+
(Part (i), if) We must prove that if C ⇒ w then PC (w) holds true, for every
w ∈ {a, b, c}∗ . We proceed by induction over the length of the derivation.
+
Base. The shortest derivation of the form C ⇒ w in our grammar G is
1
C ⇒ c and has length one. We have w = c, and it is immediate to see that
PC (w) holds true.
22
+
Induction. Let us now consider a derivation C ⇒ w with length two or
more. This derivation must start with the production C → cC, and we can
1 +
write C ⇒ cC ⇒ cw′ . We can now use factorization and obtain the shorter
+ +
derivation C ⇒ w′ . By applying the inductive hypothesis to C ⇒ w′ we have
PC (w′ ), that is, w′ is a sequence composed by k ≥ 1 occurrences of symbol
c. It follows that w = cw′ is a sequence composed by k ≥ 2 occurrences of
symbol c, and therefore PC (w) holds true.
+
(Part (i), only if) We must prove that if PC (w) holds true then C ⇒ w,
for every w ∈ {a, b, c}∗ . We proceed by induction on the length of w.
Base. Let |w| = 1. If w = c, then PC (w) holds true. The required
1
derivation for w is then C ⇒ c. If w = a or w = b, then PC (w) is false and
the implication is always true.
Induction. Assuming |w| > 1, we consider the hypothesis that PC (w) holds
+
true and we prove C ⇒ w. Under the above hypothesis, w is a sequence
composed by k > 1 occurrences of symbol c. We can write w = cw′ , with
|w′ | > 0. Since w′ contains at least one occurrence of c, we have that
PC (w′ ) holds true. Since |w′ | ≤ |w|, we can apply the inductive hypothesis
+ 1
and conclude that C ⇒ w′ . Using the derivation C ⇒ cC and composition,
1 +
we can finally write C ⇒ cC ⇒ cw′ = w.
+
(Part (ii), if) We must show that if B ⇒ w then PB (w) holds true, for any
w ∈ {a, b, c}∗ . We proceed by induction on the length of the derivation.
+ 1 1
Base. The shortest derivation of the form B ⇒ w in G is B ⇒ bC ⇒ bc and
has length of two. We can easily see that w = bc and PB (w) holds true.
+
Induction. Let B ⇒ w be a derivation of length greater than two. We
distinguish two cases, based on the choice of the first rule in the derivation.
+
• If the derivation B ⇒ w starts with the rule B → bB, then we can write
1 + +
B ⇒ bB ⇒ bw′ = w. Using factorization we conclude that B ⇒ w′ .
Since the latter derivation is one unit shorter than our initial deriva-
+
tion, we can apply to B ⇒ w′ the inductive hypothesis for statement
“(ii), if”, and derive that PB (w′ ) holds true, that is, w′ is a sequence
composed by j ≥ 1 occurrences of symbol b followed by k ≥ 1 occur-
rences of symbol c. Therefore we have that w = bw′ has the same
structure and PB (w) holds true as well.
+
• If the derivation B ⇒ w starts with the rule B → bC, then we can write
23
1 +
B ⇒ bC ⇒ bw′ = w. Again, we can use factorization and conclude that
+
C ⇒ w′ . Since the latter derivation is one unit shorter than our initial
+
derivation, we can apply to C ⇒ w′ mutual induction for statement
“(i), if”, and derive that PC (w′ ) holds true, meaning that w′ is a
sequence composed by k ≥ 1 occurrences of symbol c. This in turn
means that w = bw′ is a sequence composed by j ≥ 1 occurrences of
symbol b followed by k ≥ 1 occurrences of symbol c, and thus PB (w)
holds true.
+
(Part (ii), only if) We must prove that if PB (w) holds true then B ⇒ w,
for any x ∈ {a, b, c}∗ . We proceed by mutual induction on the length of w.
Base. The shortest string w such that PB (w) holds true is w = bc. The
1 1
required derivation in G for w is B ⇒ bC ⇒ bc. It is easy to see that no
other string of length 2 satisfies property PB ().
Induction. Let |w| > 2. We work under the hypothesis that PB (w) holds
+
true, and we prove B ⇒ w. From our hypothesis we have that w is a sequence
composed by j ≥ 1 occurrences of symbol b followed by k ≥ 1 occurrences
of symbol c. We distinguish two cases, based on whether the second symbol
in string w is b or c.
• If w has a prefix bb, then we can write w = bw′ and claim that w′ is
a sequence composed by j ≥ 1 occurrences of symbol b followed by
k ≥ 1 occurrences of symbol c. This means that PB (w′ ) holds true.
Since |w′ | < |w|, we apply on w′ induction for statement “(ii), only
+
if”, and we conclude that B ⇒ w′ . Then we can compose the desired
1 +
derivation B ⇒ bB ⇒ bw′ = w.
• If w has a prefix bc, then we can write w = bw′ and claim that w′ is a
sequence composed by k ≥ 1 occurrences of symbol c. This means that
PC (w′ ) holds true. Since |w′ | < |w|, we apply on w′ mutual induction
+
for statement “(i), only if”, and we conclude that C ⇒ w′ . Then we
1 +
can compose the desired derivation B ⇒ bC ⇒ bw′ = w.
+
(Part (iii), if) We must show that if S ⇒ w then PS (w) holds true, for any
w ∈ {a, b, c}∗ . We use mutual induction on the length of the derivation.
+ 1 1 1
Base. The shortest derivation of the form S ⇒ w is S ⇒ aB ⇒ abC ⇒ abc,
with length three. We can easily see that PS (w) holds true for w = abc.
24
+
Induction. Let S ⇒ w be a derivation of length greater than three. We
distinguish two cases, based on the first rule in the derivation.
+
• If the derivation S ⇒ w starts with rule S → aS, then we can write
1 +
S ⇒ aS ⇒ aw′ = w. Factorizing the derivation we obtain a shorter
+
derivation S ⇒ w′ . We can thus apply the inductive hypothesis, and
conclude that PS (w′ ) holds true, that is, w′ is a sequence composed by
i ≥ 1 occurrences of symbol a, followed by j ≥ 1 occurrences of symbol
b, and followed by k ≥ 1 occurrences of symbol c. From w = aw′ , we
can easily see that w has the same property, and then PS (w) holds
true.
+
• If the derivation S ⇒ w starts with the production S → aB, then we
1 +
can write S ⇒ aB ⇒ aw′ = w. Again, we factorize the derivation, and
+
obtain a shorter derivation B ⇒ w′ . We can then apply to deriva-
+
tion B ⇒ w′ the mutual induction hypothesis for statement “(ii), if”,
deriving that PB (w′ ) holds true. This means that w is a sequence com-
posed by j ≥ 1 occurrences of symbol b followed by k ≥ 1 occurrences
of symbol c. Then w = aw′ is a sequence composed by one occurrence
of symbol a, followed by j ≥ 1 occurrences of symbol b, and followed
by k ≥ 1 occurrences of symbol c. This in turn means that PS (w)
holds true.
+
(Part (iii), only if) We now prove that if PS (w) holds true then S ⇒ w,
for w ∈ {a, b, c}∗ . We proceed by mutual induction on the length of w.
Base. The shortest string in {a, b, c}∗ such that PS (w) holds true is w =
1 1 1
abc. The required derivation for w is S ⇒ aB ⇒ abC ⇒ abc. For all other
strings w ∈ {a, b, c}∗ with length 3 we have that PS (w) is false, and then
the desired implication is true.
Induction. We now consider strings in {a, b, c}∗ with |w| > 3 such that
+
PS (w) holds true, and we prove S ⇒ w. From our hypothesis we have that
w is a sequence composed by i ≥ 1 occurrences of symbol a, followed by
j ≥ 1 occurrences of symbol b, followed by k ≥ 1 occurrences of symbol c.
We distinguish two cases, based on whether the second symbol in string w
is a or b.
• If w has a prefix aa, then we can write w = aw′ and claim that w′
is a sequence composed by i ≥ 1 occurrences of symbol a, followed
by j ≥ 1 occurrences of symbol b, followed by k ≥ 1 occurrences
25
of symbol c. This means that PS (w′ ) holds true. Since |w′ | < |w|,
+
we apply induction on w′ , and conclude that S ⇒ w′ . Then we can
1 +
compose the desired derivation S ⇒ aS ⇒ aw′ = w.
• If w has a prefix ab, then we can write w = aw′ and claim that w′ is
a sequence composed by j ≥ 1 occurrences of symbol b, followed by
k ≥ 1 occurrences of symbol c. This means that PB (w′ ) holds true.
Since |w′ | < |w|, we apply on w′ mutual induction for statement “(ii),
+
only if”, and we conclude that B ⇒ w′ . Then we can compose the
1 +
desired derivation S ⇒ aB ⇒ aw′ = w.
S → ABC
A → aA | a
B → bB | b
C → cC | c
Although G′ has some more variables and rules than G, it is somehow simpler
than G since the variables A, B, and C are all mutually independent, and
the rules associated with these variables all work in the same way.
To prove the relation L(G′ ) = L, we define the properties associated
with the variables of G′ . For every w ∈ {a, b, c}∗
26
• PC (w) holds true if and only if w is a string of k ≥ 1 occurrences of
symbol c;
27
1 1 1 1
S ⇒ ABC ⇒ ABc ⇒ Abc ⇒ abc .
28
Induction. We now consider strings in {a, b, c}∗ with |w| > 3 such that
+
PS (w) holds true, and we show S ⇒ w. From the definition of PS (w), we
have that w is a sequence composed by i ≥ 1 occurrences of symbol a,
followed by j ≥ 1 occurrences of symbol b, followed by k ≥ 1 occurrences of
symbol c. It follows that we can factorize w as w = w1 w2 w3 , such that w1
is a sequence composed by i ≥ 1 occurrences of symbol a, w2 is a sequence
composed by j ≥ 1 occurrences of symbol b, and w3 is a sequence composed
by k ≥ 1 occurrences of symbol c. By definition, we have that PA (w1 ),
PB (w2 ), and PC (w3 ) are all true.
We observe that |w1 | + |w2 | + |w3 | = |w| and |wi | ≥ 1, 1 ≤ i ≤ 3. We
can easily conclude that |wi | < |w|, 1 ≤ i ≤ 3. This means that we can
apply mutual induction to each string wi . Since PA (w1 ), we apply mutual
+
induction for statement “(i), if” and obtain A ⇒ w1 . Similarly, from PB (w2 )
+
and statement “(ii), if” we get B ⇒ w2 , and from PC (w3 ) and statement
+
“(ii), if” we get C ⇒ w3 . Composing these three derivations with derivation
1
S ⇒ ABC, we obtain the derivation
1 + + +
S ⇒ ABC ⇒ w1 BC ⇒ w1 w2 C ⇒ w1 w2 w3 = w
+
which has the desired form S ⇒ w. This concludes the proof that L(G′ ) = L.
Exercise 2.3 Using the pumping lemma for CFL, prove that the language
L = {0i 1j | i, j ≥ 1, j = i2 }
is not a CFL.
Solution Let us suppose that L is a CFL. Then L must satisfy the pump-
ing lemma for context-free languages, and there exits a number n such that,
given a string z ∈ L with |z| ≥ n, we can write z = uvwxy and assert that:
(ii) |vwx| ≤ n;
29
following we viwe the string z as composed by two blocks: a block with n
occurrences of symbol 0, and a block with n2 occurrences of symbol 1. We
now analyze four possible cases, according to the position of vwx relative to
these two blocks.
• The last case takes into consideration the possibility that v consists
only of occurrences of 0, and x consists only of occurrences of 1, and
both of these strings are different from the empty string ε. For a
generic k > 1, let us consider the string zk = uv k wxk y. The number
of 0 in zk is n + (k − 1)|v| and the number of 1 in zk is n2 + (k − 1)|x|.
In order for zk to belong to L we must have
(n + (k − 1)|v|)2 = n2 + (k − 1)|x|,
that is
30
Produce a PDA that recognizes L or else rigorously prove that L is not a
context-free language.
Solution Following the first suggestion of the exercise, the first thing we
could try is the construction of a PDA that recognizes L. Considering such
construction, however, we soon realize that a nondeterministic PDA could
store w in the stack and then read wR , matching the latter against the stack
content. However, after this process the stack is emptied and the information
about w is no longer available for the third part of the computation. In fact,
the language L is not context-free, as shown below.
Let us suppose that L is a context-free language. Then the pumping
lemma for context-free languages should be satisfied. This in turn means
that there exists a positive integer n such that, given a string z ∈ L with
|z| ≥ n, we can write z = uvwxy such that:
(ii) |vwx| ≤ n;
There are several places within string z in which vwx can be located.
However, keeping in mind that z consists of 6 blocks of length n, and that
the length of vwx cannot be larger than the length of anyone of these blocks,
we need to consider only 2 possible scenarios, discussed below.
31
(i) vwx is located within a single block. If we choose i = 0, that is, if
we delete the strings v and x from our factorization of z, then the
resulting string z ′ differs from z by only a single block. We have the
following possible configurations
z ′ = 0j 1n 1n 0n 1n ,
z ′ = 0n 1j 1n 0n 0n 1n = 0n 1j 0n 0n 1n ,
z ′ = 0n 1n 1n 0j 0n 1n = 0n 1n 1n 0j 1n ,
z ′ = 0n 1n 1n 0n 1j ,
(ii) vwx is located between 2 blocks. In this case, since |vwx| ≤ n, vwx
can only span over two consecutive blocks. If we choose i = 0, we can
have the following possible configurations for the resulting string z ′′ ,
depending on the position of vwx:
z ′′ = 0j 1k 1n 0n 1n ,
z ′′ = 0n 1j 1k 0n 1n ,
z ′′ = 0n 1j 0k 0n 1n ,
z ′′ = 0n 1n 1j 0k 1n ,
z ′′ = 0n 1n 1n 0j 1k ,
where j and k are natural numbers such that j + k < 2n. Again, for
any of the above choices, we have z ′′ ̸∈ L.
Exercise 2.5 Assess whether the following languages are context-free, and
justify your answer:
32
Solution The language L1 is not context-free. An intuitive justification
of this fact can be derived by considering how a PDA works. We observe
that a PDA can store one or more non-negative integers by recording in its
stack an appropriate number of symbols, in some order. Later on, the PDA
can access the stored integers by extracting the associated representation
from the stack, in the reverse order. In the case under consideration, p and
q are stored in the stack, but p is inaccessible to the PDA because it is stored
“below q”.
A mathematical proof that L1 is not context-free can be provided using
the pumping lemma for context-free languages. As usual, we start by assum-
ing that L1 is a context-free language, and then we derive a contraddiction.
If L1 is a context-free language, there must be a number n, dependent on
L1 , such that, for an arbitrary string z ∈ L1 with |z| ≥ n, we can write
z = uvwxy and assert that:
(ii) |vwx| ≤ n;
| ·{z
z = a · · a} b| ·{z
· · }b a
| ·{z
· · a} b| ·{z
· · }b
I II III IV
33
• The string vwx consists only of occurrences of symbol b. This case
is similar to the previous one: deleting v and x, the resulting string
z ′ = uwy has blocks II and IV of different lengths, and cannot belong
to L1 .
Exercise 2.6 State whether the following languages are context-free, and
motivate your answer:
(i) L1 = {ww | w ∈ {a, b}∗ };
34
the sequence of a’s and b’s representing w, but this sequence can then be
retrieved from the stack only in the reverse order; in other words, you can
write w on the stack, but you can only read wR from the stack.
We now provide a rigorous proof of the fact that L1 is not a context-
free language. We initially assume that L1 is a context-free language, and
then apply the pumping lemma for this class of languages, showing that we
reach a contradiction. To apply the pumping lemma, we could for instance
choose the string z = a2n b2n a2n b2n , which belongs to L1 since z = ww with
w = a2n b2n . Following this line, however, we can very soon realize that
the factorization of z according to the pumping lemma requires the analysis
of many possible cases, and for some cases the analysis is quite complex.
It is also unclear which other strings of the language L1 to choose, as an
alternative, in order to simplify the overall analysis. The problem with the
complexity of the analysis of L1 according to the pumping lemma is due to
the fact that the structure of the strings in L1 is not strongly restricted, in
the sense that for a string ww ∈ L1 we do not have specific restrictions on
the substring w, that may then be used to simplify the analysis. To prove
that L1 is not a context-free language, we then choose a different strategy,
combining the application of the pumping lemma with some of the closure
properties of context-free languages, in order to avoid the direct application
of the pumping lemma to L1 .
Again, let us assume that L1 is a context-free language. The lan-
guage must therefore satisfy the closure property with the regular languages.
In particular, let L′ be the language generated by the regular expression
a∗ b∗ a∗ b∗ . Then the language L′1 = L1 ∩ L′ must still be a context-free
language. It is not difficult to see that we can define the language L′1 as
L′1 = {ap bq ap bq | p, q ≥ 0} .
Now the strings of L′1 have a much more restricted form than the strings
of L1 , and the application to L′1 of the pumping lemma for context-free
languages is simpler than in the case of L1 . Actually, we have already
encountered language L′1 at item (i) of Exercise 2.5, and we have already
shown using the pumping lemma that such language is not context-free.
To summarize, we have assumed that language L1 is a context-free lan-
guage, we have applied the closure property of context-free languages with
respect to the intersection with regular languages, and finally we have ap-
plied the pumping lemma for the class of context-free languages. All of this
has led us to a contraddiction. We must therefore conclude that L1 cannot
be context-free.
35
The language L2 = L1 ·{a, b}∗ is instead a regular language, and therefore
also a context-free language: this answers the question in item (ii) of the
exercise. To show that L2 is a regular language, we now provide a rigorous
proof that L2 = {a, b}∗ .
First of all, we definitely have L2 ⊆ {a, b}∗ , since {a, b}∗ contains all
possible strings over alphabet Σ = {a, b} and, among them, all those in L2 .
Let us now show that {a, b}∗ ⊆ L2 : we will then be able to conclude
that L2 = {a, b}∗ . Let w be an arbitrary string in {a, b}∗ . Since ε ∈ L1 , we
can factorize w as w = ε · w,, that is, w is the concatenation of a string in
L1 and a string in {a, b}∗ . Then we have w ∈ L2 .
• The string vwx is placed entirely within one of the three blocks. We
consider here only the case in which vwx is placed within the block of
a’s, as shown in Figure 2.3: the remaining two cases can be treated
in a similar way. If we set i = 0, we have that the resulting string
uwy will have fewer occurrences of a than occurrences of b and c, and
therefore it will not belong to L.
36
aaaaa
| {z } aaaaaaaa
| {z } aabbbbbbbbbbbbbbbccccccccccccccc
| {z }
u vwx y
aaaaaaaaaaaaaaabb
| {z } bbbbbbbbbb
| {z } bbbccccccccccccccc
| {z }
u vwx y
aaaaaaaaaaaaaaabbbbbbbbbbbbbbbccc
| {z } cccccc
| {z } cccccc
| {z }
u vwx y
aaaaaaa
| {z } aaaaaaaabbbb
| {z } bbbbbbbbbbbccccccccccccccc
| {z }
u vwx y
aaaaaaaaaaaaaaabbbbbbbbbb
| {z } bbbbbcccccc
| {z } ccccccccc
| {z }
u vwx y
Figure 2.4: Placement of string vwx across the first two blocks of an bn cn .
• The string vwx is placed across two adjacent blocks. We observe that
vwx cannot be placed across all of the three blocks, because otherwise
vwx would have length greater than n. We consider here only the case
in which vwx is placed across the block of a’s and the block of b’s, as
shown in Figure 2.4: the remaining case can be treated in a similar
way. Again, let us set i = 0. As we can see from Figure 2.4, even if
our factorization preserves the balance between the occurrences of a
and b in the resulting string uwy, there will be a mismatch with the
the occurrences of c, and the string will not belong to L.
The fact that we have not been able to find a way to factorize the string
z in the form uvwxy in such a way that the conditions imposed by the
pumping lemma are satisfied leads us to the conclusion that L cannot be a
context-free language.
37
(i) Given two non-regular languages L1 and L2 , the language L1 ∪ L2
cannot be regular.
L1 = {w | w = an bn , n ≥ 0}
L2 = {w | w = an bm , n, m ≥ 0, n ̸= m}
L1 ∪ L2 = {w | w = an bm , n, m ≥ 0}
L = {w | an bn cn , n ≥ 0}
L1 = {w | an bm , n, m ≥ 0}
L2 = Σ ∗
38
L1 is a regular language, as already seen in point (ii), and therefore also a
context-free language. L2 is definitely a regular language. We also have the
relationship L1 ∩ L2 = L1 , which proves statement (iii).
Statement (iv) is true. To show this, we again provide a simple example.
Consider the alphabet Σ = {a, b} and the two languages
L1 = {w | an bn , n ≥ 0}
L2 = Σ∗
L1 is a well-known context-free language but not a regular language, and L2
is definitely a regular language. It is easy to see that we have L1 ∩ L2 = L1 ,
which proves statement (iv).
Exercise 2.9 Consider the operator P defined as follows. For each lan-
guage L
P (L) = {x | x ∈ L, |x| = 2n, n ∈ N} .
Is the class of context-free languages closed with respect to operator P ?
Solution To solve this exercise we have to show that, for each context-
free language L, P (L) is still a context-free language. To prove this property
we can take advantage of the fact that the intersection of a context-free
language with a regular language is still a context-free language. In our
case, the language P (L) consists of all the strings from language L having
even length. We can then use as language L′ in the intersection the regular
language formed by all the strings of even length in Σ∗ , where Σ is the
alphabet of the language L.
The fact that L′ is a regular language can easily be verified noting that
′
L = L(M ), where M is the DFA defined as
M = ({q0 , q1 }, Σ, δ, q0 , {q0 })
where the transition function δ is specified as
δ(q0 , a) = q1 , a∈Σ;
δ(q1 , a) = q0 , a∈Σ.
Since L ∩ L′ = P (L), we can concluded that the class of context-free lan-
guages is closed under the operator P .
39
Chapter 3
L = {w | w ∈ Σ∗ , w = an bban , n ≥ 0}.
Specify a Turing machine that accepts L and stops for every possible input
in Σ∗ .
40
a/a → a/a ←
b/b → b/b ←
B/B →
b/b →
b/b → B/B →
q4 q5 q6
Exercise 3.2 Let Σ = {0, 1} and consider the language over Σ defined as
L = {w | w ∈ Σ∗ , #0 (w) = #1 (w)}.
Specify a Turing machine that accepts L and halts for every possible input
in Σ∗ .
41
Solution The required TM M uses two different computation phases. In
the first phase, M scans the tape from left to right, looking for the first
occurrence of a 0, and then replaces this occurrence with the special symbol
X. At this point M repositions its head at the beginning of the tape. The
second phase is similar to the first: M scans the tape again from left to right,
looking for the first occurrence of a 1, and replaces it with the special symbol
Y . Then M repositions its head at the beginning of the tape. Altogether, a
computation of M if formed by a cycle in which phases one and two above
alternate. The cycle ends when there are no more occurrences of 0 or 1 in
the tape. In such condition, M halts in a final state, therefore accepting the
input string. In all other cases, M stops without accepting.
More precisely, M uses the following strategy.
• In state q1 , M moves to the left leaving the tape unchanged. Note that
the encountered symbols can only be 1, X, Y , since the occurrences of
0 have all been replaced. When M reaches the first B (blank), it moves
to the right by one tape cell, and positions itself at the beginning of
the tape, entering state q2 .
• In state q3 , M moves to the left leaving the tape unchanged. Note that
the encountered symbols can only be 0, X, Y , since the occurrences of
1 have all been replaced. When M reaches the first B, it moves to
the right by one tape cell, and positions itself at the beginning of the
tape, entering state q0 and restarting the previous cycle.
• In state q4 , M runs the tape to the left to verify that there are no
unprocessed occurrences of 1. If this is the case, M enters the final
state q5 , indicating acceptance of the input string, and halts. In all
42
1/1 → 1/1 ← 0/0 → 0/0 ←
X/X → X/X ← X/X → X/X ←
Y /Y → Y /Y ← Y /Y → Y /Y ←
B/B →
B/B ←
X/X ← B/B →
q4 q5
Y /Y ←
Solution The required TM M must write on its tape the string wcw.
Initially M writes a c at the end of the input string, and then it positions
its tape head at the beginning of the input string. At this point M enters a
cycle in which it performs the following actions. M reads the first character
43
a/a → a/a ← a/a →
b/b → b/b ← b/b →
44
entering state q2 . At this point M is placed at the beginning of the
input string w.
P = {L | L ∈ RE, L ⊆ L1 }.
45
states that if the property P is non-trivial, that is if P ̸= RE and P ̸= ∅,
then P is not decidable. Recall that a property P of the RE languages is
decidable if the language LP is recursive, where LP consists of strings that
are codings of Turing machine that accept languages in P.
We certainly have P = ̸ ∅: in fact L1 is recursive, and therefore it is also
in RE, and it belongs to P since L1 ⊆ L1 . We also have P = ̸ RE, although
this verification is slightly more complex. We observe that L1 ̸= Σ∗ , because
otherwise L1 would be regular, and therefore also context-free, violating the
hypothesis. Then there exists at least one string w ∈ Σ∗ that does not
belong to L1 . We can then choose L2 = {w} as a RE language that is not
a subset of L1 and therefore does not belong to P.
Applying now Rice’s theorem, we conclude that P is not decidable, so
LP is not recursive.
P = {L | L ∈ RE, w ∈ L}.
46
• MP receives as input a string z and checks if it is a valid encoding
enc(M ) of some TM M . If not, then MP halts in a non-final state.
Note that, at the second item above, M could not halt on input w. In this
case also MP would not halt. Since LP is not recursive, this scenario can
not be avoided.
The following chain of logical equivalence relations formally proves that
L(MP ) = LP :
Exercise 3.6 Let Σ be some alphabet and let a ∈ Σ. Consider the follow-
ing property of recursively enumerable (RE) languages:
47
Exercise 3.7 Let L be a generic language defined over an alphabet Σ. Let
us define the prefix operator
pref(L) = {w | wx ∈ L, w ∈ Σ∗ , x ∈ Σ+ }.
In words, pref(L) contains all proper prefixes of strings from L, that is, all
strings that when prolonged by means of one or more occurrences of certain
symbols from Σ can form a string in L.
Show that the class RE of all recursively enumerable languages is closed
under the prefix operator.
Solution To solve the exercise we show that, for any language L in RE,
we can construct a nondeterministic Turing machine N such that L(N ) =
pref(L). We know that we can always convert N into a deterministic Turing
machine, and therefore we can conclude that pref(L) is a language in RE.
Let M be a deterministic Turing machine such that L(M ) = L; M exists
since we have assumed that L is an RE language. To accept all and only the
strings in pref(L), the nondeterministic Turing machine N uses the following
strategy.
48
wx to M as an input. However, this idea hides a severe mistake. Consider
xi , the i-th string in Σ+ assuming the lexicographic order. Computation of
M on wxi might never stop, according to the definition of languages in RE.
In this case, any strings wxj ∈ L with j > i would never be examined, and
therefore the machine Md would not have the correct behavior.
The problem can be solved by inserting into Md a pair generator G. The
latter produces as output all possible pairs of natural numbers (i, j) with
i, j > 0. G can easily be realized by increasing some counter k, starting from
2, and printing for each value of k all pairs of positive integers satisfying
i + j = k, sorted by growing values of i. The integer pairs are therefore
generated by G in the following order
(1, 1), (1, 2), (2, 1), (1, 3), (2, 2), (3, 1), . . .
P = {L | L ∈ RE, L1 ∩ L ̸= ∅}.
49
recursively enumerable language can satisfy the condition L1 ∩ L ̸= ∅ in
the definition of property P, and therefore P = ∅. In this case LP = ∅,
and it is certainly a recursive language, since it is recognized by a TM that,
regardless of the given input, always halts without accepting. Of course, LP
is also a recursively enumerable language.
L1 ̸= ∅. We start again from the verification of the hypotheses of Rice’s
theorem. First, we observe that P = ̸ ∅. To see this observe that L1 is a finite
language, and therefore also a regular language. Since the class of regular
languages is included in the class of recursively enumerable languages, we
have that L1 is a recursively enumerable language. Furthermore, we have
L1 ∈ P, since L1 ∩ L ̸= ∅ for L = L1 .
Second, P does not include all recursively enumerable languages. To see
this, we use the fact that L1 is a finite language. Therefore there exists a
string w ∈ Σ∗ such that w ̸∈ L1 . Then we can define L2 = {w}, which
is a recursively enumerable language. Furthermore, we have L2 ̸∈ P, since
L1 ∩ L = ∅ for L = L2 . Applying now Rice’s theorem, we have that LP is
not recursive.
Under the assumption that L1 ̸= ∅, the language LP is a recursively
enumerable language. To see this, assume that the strings in the finite
language L1 are sorted according to the lexicographical ordering. For each
j with 1 ≤ j ≤ |L1 |, let wj be the j-th string of L1 . Consider then the TM
MP specified by means of the following steps.
• MP sets i = 1.
50
As already done for some previous exercises, we now show that L(MP ) =
LP through a chain of equivalences:
51
yes -
w - M
x - M
1
52
Let enc(M ) be an instance of the decision proble associated with Lne ,
that is, enc(M ) is the encoding of a TM for which we have to test whether
L(M ) ̸= ∅. The reduction produces an instance enc(M, M ) of L, that is, a
string representing the encoding of two copies of M . The following chain of
logical equivalences shows that the proposed construction represents a valid
reduction:
enc(M, M ) ∈ L iff L(M ) ∩ L(M ) ̸= ∅ (definition of L)
iff L(M ) ̸= ∅ (definition of ∩)
iff enc(M ) ∈ Lne (definition of Lne ) .
Since there is no TM that always halts and that recognizes Lne , we can
conclude that L is not a recursive language.
53
• Next, N simulates M1 on the input string x, and simulates M2 on the
input string y.
54
• We set M = M ′′
L = {enc(M, w) | w ̸∈ L(M )}
Ld = {wi | wi ∈
/ Mi }
where wi is the i-th string in the language {0, 1}∗ , according to the indexing
studied in the textbook, and Mi is the Turing machine whose encoding
enc(Mi ) is wi .
To define our reduction, let us consider a string w which represents an
instance of the decision problem associated with Ld . We specify a Turing
55
machine that, on input w, finds the index i such that wi = w, and returns
as output the string enc(Mi , wi ) where, as already mentioned, Mi is the TM
whose binary code is wi .
To conclude our proof we must show that the construction is a valid
reduction, that is, we must show that w ∈ Ld implies enc(Mi , wi ) ∈ Lu , and
enc(Mi , wi ) ∈ Lu implies w ∈ Ld . We have the following chain of logical
equivalences
56
This shows that our construction is a valid reduction, and concludes the
solution of the exercise.
As an alternatively solution, we can reduce Lu to L as follows. Let
enc(M, w) be an instance of the decision problem associated with Lu . Let
also M∅ be a TM that recognizes the empty language, and let Mw be a TM
that recognizes the language {w}, i.e. a language consisting only of the string
w. We can map the instance enc(M, w) into the instance enc(M, Mw , M∅ ).
It is immediately evident that
{w}, if w ∈ L(M )
L(M ) ∩ L(Mw ) =
∅, if w ̸∈ L(M )
57