This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

for

Languages and Machines:

An Introduction to the Theory of

Computer Science

Third Edition

Thomas A. Sudkamp

Preface

This solution manual was written to accompany the third edition of Languages and Machines: An

Introduction to the Theory of Computer Science. It contains complete solutions to approximately

200 exercises from the text, including the “starred” exercises.

Acquiring a thorough background in and mastery of the foundations of computer science is not

a spectator sport, active participation is required. This participation takes several forms; putting

forth the eﬀort to understand the theorems and their proofs, following the techniques used to solve

the problems in the examples, and solving problems on your own. It is the ability to do the latter

that exhibits proﬁciency with the material. These solutions have been provided as a tool to assist

students in developing their problem solving skills.

The exercises in this manual were selected to complement the presentation in the text. The

solutions illustrate general techniques for solving the particular type of problem or extend the the-

oretical development of a topic. Understanding the strategies employed in these solutions should

provide valuable insights that can be used in solving many of the other exercises in the text.

Thomas Sudkamp Dayton, Ohio

Copyright 2005: Thomas Sudkamp

All rights reserved. No part of this is to be reproduced, stored in a retrieval system, or transmitted

in any form without the prior written permission of the author or the Addison-Wesley Publishing

Co.

i

ii

Contents

1 Mathematical Preliminaries 1

2 Languages 7

3 Context-Free Grammars 11

4 Normal Forms for Context-Free Grammars 19

5 Finite Automata 25

6 Properties of Regular Languages 31

7 Pushdown Automata and Context-Free Languages 37

8 Turing Machines 45

9 Turing Computable Functions 53

10 The Chomsky Hierarchy 59

11 Decision Problems and the Church-Turing Thesis 63

12 Undecidability 67

13 µ-Recursive Functions 73

14 Time Complexity 79

15 {, ^{, and Cook’s Theorem 83

16 NP-Complete Problems 87

17 Additional Complexity Classes 95

18 Parsing: An Introduction 99

19 LL(k) Grammars 101

20 LR(k) Grammars 107

iii

iv

Chapter 1

Mathematical Preliminaries

5. To prove that X ∩ Y = (X∪ Y) requires establishing both of the inclusions X ∩ Y ⊆ (X∪ Y)

and (X∪ Y) ⊆ X∩ Y.

i) Let a ∈ X ∩ Y. By the deﬁnition of intersection, a ∈ X and a ∈ Y. Thus a ∈ X and a ∈

Y or, in terms of union, a ∈ (X∪ Y). It follows that a ∈ (X∪ Y).

ii) Now assume that a ∈ (X∪ Y). Then a ∈ X ∪ Y. This implies that a ∈ X and a ∈ Y.

Consequently, a ∈ X∩ Y.

Part (i) shows that X∩Y is a subset of (X∪ Y), while (ii) establishes the inclusion (X∪ Y) ⊆

X∩ Y.

A completely analogous argument can be used to establish the equality of the sets (X∩ Y)

and X∪ Y.

6. a) The function f(n) = 2n is total and one-to-one. However, it is not onto since the range

is the set of even numbers.

b) The function

f(n) =

0 if n = 0

n −1 otherwise

is total and onto. It is not one-to-one since f(0) = f(1) = 0.

c) The function

f(n) =

1 if n = 0

0 if n = 1

n otherwise

is total, one-to-one, and onto but not the identity.

d) The function

f(n) =

n/2 if n is even

↑ otherwise

maps the even natural numbers onto the entire set of natural numbers. It is not a total

function from N to N, since it is not deﬁned for odd natural numbers.

13. To prove that ≡

p

is an equivalence relation, we must show that it is reﬂexive, symmetric, and

transitive. This is shown using the same argument given in Example 1.3.1, which explicitly

considers the case when p = 2.

i) Reﬂexivity: For every natural number n, n mod p = n mod p.

1

2 CHAPTER 1. MATHEMATICAL PRELIMINARIES

ii) Symmetry: If n mod p = m mod p, then m mod p = n mod p.

iii) Transitivity: If n mod p = m mod p and m mod p = k mod p, then n mod p = k mod p.

The equivalence classes of ≡

p

are the sets consisting of natural numbers that are equal mod p:

[0]

≡

p

= ¦0, p, 2p, 3p, . . .¦

[1]

≡

p

= ¦1, p + 1, 2p + 1, 3p + 1, . . .¦

[2]

≡

p

= ¦2, p + 2, 2p + 2, 3p + 2, . . .¦

.

.

.

[p −1]

≡

p

= ¦p −1, 2p −1, 3p −1, 4p −1, . . .¦.

15. i) Reﬂexivity: To demonstrate reﬂexivity, we must show that every ordered pair [m, n] is

related to itself. The requirement for [m, n] to be related to [m, n] by ≡ is m+n = n+m,

which follows from the commutativity of addition.

ii) Symmetry: If [m, n] ≡ [j, k], then m+k = n+j. Again, by commutativity, j +n = k +m

and [j, k] ≡ [m, n].

iii) Transitivity: [m, n] ≡ [j, k] and [j, k] ≡ [s, t] imply m + k = n + j and j + t = k + s.

Adding the second equality to the ﬁrst, we obtain

m+k +j +t = n +j +k +s.

Subtracting j + k from each side yields m + t = n + s, showing that [m, n] ≡ [s, t] as

desired.

18. The set of non-negative rational numbers is deﬁned by

¦n/m [ n ∈ N, m ∈ N−¦0¦¦

A rational number n/m can be represented by the ordered pair [n, m]. This representation

deﬁnes a one-to-one correspondence between the rational numbers and the set N(N−¦0¦).

The latter set is known to be countable by Theorem 1.4.4.

22. Diagonalization is used to prove that there are an uncountable number of monotone increasing

functions. Assume that the set of monotone increasing functions is countable. Then these

functions can be listed in a sequence f

0

, f

1

, f

2

, . . . , f

n

, . . . .

Now we will show that there is at least one (in fact there are inﬁnitely many) monotone

increasing function that is not in the listing. Deﬁne a function f as follows:

f(0) = f

0

(0) + 1

f(i) = f

i

(i) + f(i −1)

for i > 0. Since f

i

(i) > 0, it follows that f(i) > f(i −1) for all i and f is monotone increasing.

Clearly f(i) = f

i

(i) for any i, contradicting the assumption that f

0

, f

1

, . . ., f

n

, . . . exhaus-

tively enumerates the monotone increasing functions. Thus the assumption that the monotone

increasing functions comprise a countable set leads to a contradiction and we conclude that

the set is uncountable.

25. To show that the set of real numbers in the interval [0, a] is uncountable, we ﬁrst observe that

every real number in (0, 1] can be expressed by an inﬁnite decimal .x

0

x

1

x

2

. . .x

n

. . . . With such

a representation, the number

1

2

is represented by both .50000 . . . and .49999 . . . . To obtain a

SOLUTIONS TO EXERCISES 3

unique representation, we consider only decimal expansions that do not end with an inﬁnite

sequence of zeros.

Assume the set of real numbers in (0, 1] is countable. This implies that there is a sequence

r

0

, r

1

, r

2

, . . . , r

n

, . . .

that contains all of the real numbers in the interval (0, 1]. Let the decimal expansion of r

n

be denoted .x

n0

x

n1

x

n2

. . . . The enumeration given above is used to construct an inﬁnite

two-dimensional array, the i

th

row of which consists of the expansion of r

i

.

r

0

= x

00

x

01

x

02

. . .

r

1

= x

10

x

11

x

12

. . .

r

2

= x

20

x

21

x

22

. . .

.

.

.

.

.

.

.

.

.

.

.

.

With the unique decimal representation, two numbers are distinct if they diﬀer at any position

in the decimal expansion. A real number r = x

0

x

1

. . . is deﬁned using the diagonal elements

in the array formed by the x

ii

’s as follows:

x

i

=

2 if x

ii

= 1

1 otherwise.

Clearly r = r

i

for any i since the i

th

position of r, x

i

, is not identical to the i

th

position of r

i

.

Therefore the assumption that the enumeration contains all real numbers in (0, 1] fails, and

we conclude that the set is uncountable.

28. Before proving the Schr¨oder-Bernstein Theorem, we consider the special case where card(B) ≤

card(A), card(A) ≤ card(B), and B ⊆ A.

The relationship card(B) ≤ card(A) follows immediately from the inclusion B ⊆ A since the

identity function id : B → A is a one-to-one function from B into A.

By deﬁnition, card(A) ≤ card(B) means there is a one-to-one function f from A into B. We

will use f to construct a one-to-one function h : A → B from A onto B. The function h

demonstrates that A and B have the same cardinality.

The diagram

B

A

f

4 CHAPTER 1. MATHEMATICAL PRELIMINARIES

illustrates the mapping f. The function f is deﬁned for all elements in A (which includes all

elements of B) and the values of f, indicated by the heads of the arrows, must all be in B.

For each x ∈ A−B, we deﬁne the set

ch(x) = ¦x, f(x), f(f(x))), . . . , f

i

(x), . . .¦.

Every element in ch(x) is in B, except for x itself which is in A−B. Let

C =

¸

x∈ A−B

ch(x).

Now deﬁne the function h : A → B as follows:

h(z) =

f(z), if z ∈ C;

z, otherwise.

To show that h is a one-to-one, we must prove that h(x) = h(y) implies that x = y. There are

four cases to consider.

Case 1: x, y ∈ C. Then x = h(x) = h(y) = y.

Case 2: x ∈ C and y ∈ C. Since x ∈ C, h(x) = f(x) is in C. But h(x) = h(y) = y. This implies

that y ∈ C, which is a contradiction. Thus h(x) cannot equal h(y) in this case.

Case 3: x ∈ C and y ∈ C. Same argument as case 2.

Case 4: x, y ∈ C. Let f

i

denote the composition of f with itself i times, and f

0

denote the

identity function. The proof uses the fact that the composition of one-to-one functions is one-

to-one. Although you will be familiar with functional composition from previous mathematical

studies, a description and formal deﬁnition are given in Section 9.4 of the text if you need a

reminder.

Since x and y are both in C, x = f

m

(s) and y = f

n

(t) for some s, t ∈ A−B. Then

h(x) = f(f

m

(s)) = h(y) = f(f

n

(t)).

If m = n, then s = t and x = y and we are done. Assume that m > n. Then f

m−n

(s) = t.

Applying the function f

n

to both sides we get f

m

(s) = f

n

(t), or equivalently, x = y. A similar

argument shows x = y when m < n.

We now show that h maps A onto B. For each x ∈ B but not in C, h(x) = x and x is covered

by the mapping. If x ∈ B and x ∈ C, then x = f(t) for some t ∈ C because each element in C

is either in A − B or obtained by the result of applying f to an element in C. Consequently,

each element of B is ‘hit’ by the mapping h. Because h is a one-to-one function from A to B,

we conclude that card(A) = card(B).

To prove the Schr¨oder-Bernstein Theorem in its generality, we must show that card(X) ≤

card(Y) and card(Y) ≤ card(X) implies card(X) = card(Y) for arbitrary sets X and Y. By

the assumption, there are one-to-one functions f : X → Y and g : Y → X. Let

Im(Y) = ¦x ∈ X [ x = g(y) for some y in Y¦

be the image of Y in X under g. Now

- Im(Y) is a subset of X,

- Im(Y) has the same cardinality as Y (g is one-to-one and onto), and

- the composition f ◦ g is a one-to-one mapping from X into Im(Y).

SOLUTIONS TO EXERCISES 5

By the preceding result, card(X) = card(Im(Y)). It follows that card(X) = card(Y) by

Exercise 27.

31. Let L be the set of the points in N N on the line deﬁned by n = 3 m. L can be deﬁned

recursively by

Basis: [0, 0] ∈ L.

Recursive step: If [m, n] ∈ L, then [s(m), s(s(s(n)))] ∈ L.

Closure: [m, n] ∈ L only if it can be obtained from [0, 0] using ﬁnitely many applications of

the recursive step.

33. The product of two natural numbers can be deﬁned recursively using addition and the successor

operator s.

Basis: if n = 0 then m n = 0

Recursive step: m s(n) = m + (m n)

Closure: m n = k only if this equality can be obtained from m 0 = 0 using ﬁnitely many

applications of the recursive step.

37. The set T of ﬁnite subsets of the natural numbers can be deﬁned recursively as follows:

Basis: ∅, ¦0¦ ∈ T

Recursive step: If ¦n¦ ∈ T, then ¦s(n)¦ ∈ T.

If X, Y ∈ T, then X ∪ Y ∈ T.

Closure: A set X is in T only if it can be obtained from the basis elements by a ﬁnite number

of applications of the recursive step.

The ﬁrst rule in the recursive step generates all sets containing a single natural number. The

second rule combines previously generated sets to obtain sets of larger cardinality.

39. We prove, by induction on n, that

n

¸

i=0

2

i

= 2

n+1

−1

Basis: The basis is n = 0. We explicitly show that the equality holds for this case.

0

¸

i=0

2

i

= 2

0

= 1 = 2

1

−1

Inductive hypothesis: Assume, for all values k = 1, 2, . . . , n, that

k

¸

i=0

2

i

= 2

k+1

−1

Inductive step: We need to show that

n+1

¸

i=0

2

i

= 2

(n+1)+1

−1

To utilize the inductive hypothesis, the summation is decomposed into the sum of the ﬁrst n

powers of 2 and 2

n+1

.

6 CHAPTER 1. MATHEMATICAL PRELIMINARIES

n+1

¸

i=0

2

i

=

n

¸

i=0

2

i

+ 2

n+1

= 2

n+1

−1 + 2

n+1

(inductive hypothesis)

= 2 2

n+1

−1

= 2

(n+1)+1

−1

43. The set R of nodes reachable from a given node x in a directed graph is deﬁned recursively

using the adjacency relation A.

Basis: x ∈ R.

Recursive step: If y ∈ R and [y, z] ∈ A, then z ∈ R.

Closure: y ∈ R only if y can be obtained from x by ﬁnitely many applications of the recursive

step.

46. a) The depth of the tree is 4.

b) The set of ancestors of x

11

is ¦x

11

, x

7

, x

2

, x

1

¦. Recall that by our deﬁnition is node is an

ancestor of itself, which is certainly not the case in family trees.

c) The minimal common ancestor of x

14

and x

11

is x

2

; of x

15

and x

11

is x

1

.

d) The subtree generated by x

2

is comprised of the arcs [x

2

, x

5

], [x

2

, x

6

], [x

2

, x

7

], [x

5

, x

10

],

[x

7

, x

11

], and [x

10

, x

14

].

e) The frontier is the set ¦x

14

, x

6

, x

11

, x

3

, x

8

, x

12

, x

15

, x

16

¦.

48. Induction on the depth of the tree is used to prove that a complete binary tree T of depth n

has 2

n+1

−1 nodes. Let nodes(T) and leaves(T) denote the number of nodes and leaves in a

tree T.

Basis: The basis consists of trees of depth zero; that is, trees consisting solely of the root.

For any such tree T, nodes(T) = 1 = 2

1

−1.

Inductive hypothesis: Assume that every complete binary tree T of depth k, k = 0, . . . , n,

satisﬁes nodes(T) = 2

k+1

−1.

Inductive step: Let T be a complete binary tree of depth n + 1, where n ≥ 0. We need to

show that nodes(T) = 2

(n+1)+1

− 1. T is obtained by adding two children to each leaf of a

complete binary tree T

of depth n. Since T

**is complete binary, it is also strictly binary and
**

leaves(T

) = (nodes(T

) + 1)/2

by Exercise 47. Thus

nodes(T) = nodes(T

) + 2 leaves(T

)

= nodes(T

) + 2 [(nodes(T

) + 1)/2] (Exercise 47)

= 2 nodes(T

) + 1

= 2 (2

n+1

−1) + 1 (inductive hypothesis)

= 2

(n+1)+1

−1

Chapter 2

Languages

3. We prove, by induction on the length of the string, that w = (w

R

)

R

for every string w ∈ Σ

∗

.

Basis: The basis consists of the null string. In this case, (λ

R

)

R

= (λ)

R

= λ as desired.

Inductive hypothesis: Assume that (w

R

)

R

= w for all strings w ∈ Σ

∗

of length n or less.

Inductive step: Let w be a string of length n + 1. Then w = ua and

(w

R

)

R

= ((ua)

R

)

R

= (a

R

u

R

)

R

(Theorem 2.1.6)

= (au

R

)

R

= (u

R

)

R

a

R

(Theorem 2.1.6)

= ua

R

(inductive hypothesis)

= ua

= w

8. Let L denote the set of strings over Σ = ¦a, b¦ that contain twice as many a’s as b’s. The set

L can be deﬁned recursively as follows:

Basis: λ ∈ L

Recursive step: If u ∈ L and u can be written xyzw where x, y, z, w ∈ Σ

∗

then

i) xayazbw ∈ L,

ii) xaybzaw ∈ X, and

iii) xbyazaw ∈ X.

Closure: A string u is in L only if it can be obtained from λ using a ﬁnite number of appli-

cations of the recursive step.

Clearly, every string generated by the preceding deﬁnition has twice as many a’s as b’s. A proof

by induction on the length of strings demonstrates that every string with twice as many a’s

as b’s is generated by the recursive deﬁnition. Note that every string in L has length divisible

by three.

Basis: The basis consists of the null string, which satisﬁes the relation between the number

of a’s and b’s.

Inductive hypothesis: Assume that all strings with twice as many a’s as b’s of length k,

0 ≤ k ≤ n, are generated by the recursive deﬁnition.

7

8 CHAPTER 2. LANGUAGES

Inductive step: Let u be a string of length n + 3 with twice as many a’s as b’s. Since u

contains at least three elements, x can be written xayazbw, xaybzaw, or xbyazaw for some

x, y, w, z ∈ Σ

∗

. It follows that xyzw has twice as many a’s as b’s and, by the inductive

hypothesis, is generated by the recursive deﬁnition. Thus, one additional application of the

recursive step produces u from xyzw.

12. Let P denote the set of palindromes deﬁned by the recursive deﬁnition and let W = ¦w ∈ Σ

∗

[

w = w

R

¦. Establishing that P = W requires demonstrating that each of the sets is a subset

of the other.

We begin by proving that P ⊆ W. The proof is by induction on the number of applications of

the recursive step in the deﬁnition of palindrome required to generate the string.

Basis: The basis consists of strings of P that are generated with no applications of the

recursive step. This set consists of λ and a, for every a ∈ Σ. Clearly, w = w

R

for every such

string.

Inductive hypothesis: Assume that every string generated by n or fewer applications of

the recursive step is in W.

Inductive step: Let u be a string generated by n+1 applications of the recursive step. Then

u = awa for some string w and symbol a ∈ Σ, where w is generated by n applications of the

recursive step. Thus,

u

R

= (awa)

R

= a

R

w

R

a

R

(Theorem 2.1.6)

= aw

R

a

= awa (inductive hypothesis)

= u

and u ∈ W.

We now show that W ⊆ P. The proof is by induction on the length of the strings in W.

Basis: If length(u) = 0, then w = λ and λ ∈ P by the basis of the recursive deﬁnition.

Similarly, strings of length one in W are also in P.

Inductive hypothesis: Assume that every string w ∈ W with length n or less is in P.

Inductive step: Let w ∈ W be a string of length n + 1, n ≥ 1. Then w can be written ua

where length(u) = n. Taking the reversal,

w = w

R

= (ua)

R

= au

R

Since w begins and ends with with same symbol, it may be written w = ava. Again, using

reversals we get

w

R

= (ava)

R

= a

R

v

R

a

R

= av

R

a

Since w = w

R

, we conclude that ava = av

R

a and v = v

R

. By the inductive hypothesis, v ∈ P.

It follows, from the recursive step in the deﬁnition of P, that w = ava is also in P.

SOLUTIONS TO EXERCISES 9

13. The language L

2

consists of all strings over ¦a, b¦ of length four. L

3

, obtained by applying the

Kleene star operation to L

2

, contains all strings with length divisible by four. The null string,

with length zero, is in L

3

.

The language L

1

∩ L

3

contains all strings that are in both L

1

and L

3

. By being in L

1

, each

string must consist solely of a’s and have length divisible by three. Since L

3

requires length

divisible by four, a string in L

1

∩ L

3

must consist only of a’s and have length divisible by 12.

That is, L

1

∩ L

3

= (a

12

)

∗

.

15. Since the null string is not allowed in the language, each string must contain at least one a

or one b or one c. However, it need not contain one of every symbol. The +’s in the regular

expression a

+

b

∗

c

∗

∪ a

∗

b

+

c

∗

∪ a

∗

b

∗

c

+

ensure the presence of at least one element in each

string in the language.

21. The set of strings over ¦a, b¦ that contain the substrings aa and bb is represented by

(a ∪ b)

∗

aa(a ∪ b)

∗

bb(a ∪ b)

∗

∪ (a ∪ b)

∗

bb(a ∪ b)

∗

aa(a ∪ b)

∗

.

The two expressions joined by the ∪ indicate that the aa may precede or follow the bb.

23. The leading a and trailing cc are explicitly placed in the expression

a(a ∪ c)

∗

b(a ∪ c)

∗

b(a ∪ c)

∗

cc.

Any number of a’s and c’s, represented by the expression (a∪c)

∗

, may surround the two b’s.

24. At ﬁrst glance it may seem that the desired language is given by the regular expression

(a ∪ b)

∗

ab(a ∪ b)

∗

ba(a ∪ b)

∗

∪ (a ∪ b)

∗

ab(a ∪ b)

∗

ba(a ∪ b)

∗

.

Clearly every string in this language has substrings ab and ba. Every string described by the

preceding expression has length four or more. Have we missed some strings with the desired

property? Consider aba, bab, and aaaba. A regular expression for the set of strings containing

substrings ab and ba can be obtained by adding

(a ∪ b)

∗

aba(a ∪ b)

∗

∪ (a ∪ b)

∗

bab(a ∪ b)

∗

to the expression above.

26. The language consisting of strings over ¦a, b¦ containing exactly three a’s is deﬁned by the reg-

ular expression b

∗

ab

∗

ab

∗

ab

∗

. Applying the Kleene star to the preceding expression produces

strings in which the number of a’s is a multiple of three. However, (b

∗

ab

∗

ab

∗

ab

∗

)

∗

does not

contain strings consisting of b’s alone; the null string is the only string that does have at least

three a’s. To obtain strings with no a’s, b

∗

is concatenated to the end of the preceding expres-

sion. Strings consisting solely of b’s are obtained from (b

∗

ab

∗

ab

∗

ab

∗

)

∗

b

∗

by concatenating

λ from (b

∗

ab

∗

ab

∗

ab

∗

)

∗

with b’s from b

∗

. The regular expression (b

∗

ab

∗

ab

∗

a)b

∗

deﬁnes the

same language.

28. In the regular expression

(ab ∪ ba ∪ aba ∪ b)

∗

,

a’s followed by a b are produced by the ﬁrst component, a’s preceded by a b by the second

component, and two a’s “covered” by a single b by the third component. The inclusion of b

within the scope of the

∗

operator allows any number additional b’s to occur anywhere in the

string.

Another regular expression for this language is ((a ∪ λ)b(a ∪ λ))

∗

.

10 CHAPTER 2. LANGUAGES

31. Every aa in the expression (b ∪ ab ∪ aab)

∗

is followed by at least one b, which precludes the

occurrence of aaa. However, every string in this expression ends with b while strings in the

language may end in a or aa as well as b. Thus the expression

(b ∪ ab ∪ aab)

∗

(λ ∪ a ∪ aa)

represents the set of all strings over ¦a, b¦ that do not contain the substring aaa.

32. A string can begin with any number of b’s. Once a’s are generated, it is necessary to ensure that

an occurrence of ab is either followed by another b or ends the string. The regular expression

b

∗

(a ∪ abb

+

)

∗

(λ ∪ ab) describes the desired language.

34. To obtain an expression for the strings over ¦a, b, c¦ with an odd number of occurrences of

the substring ab we ﬁrst construct an expression w deﬁning strings over ¦a, b, c¦ that do not

contain the substring ab,

w = b

∗

(a ∪ cb

∗

)

∗

.

Using w, the desired set can be written (wabwab)

∗

wabw.

37. The regular expression (b

∗

ab

∗

a)

∗

b

∗

∪ (a

∗

ba

∗

b)

∗

a

∗

ba

∗

deﬁnes strings over ¦a, b¦ with an

even number of a’s or an odd number of b’s. This expression is obtained by combining an

expression for each of the component subsets with the union operation.

38. Exercises 37 and 38 illustrate the signiﬁcant diﬀerence between union and intersection in

describing patterns with regular expressions. There is nothing intuitive about a regular ex-

pression for this language. The exercise is given, along with the hint, to indicate the need

for an algorithmic approach for producing regular expressions. A technique accomplish this

will follow from the ability to reduce ﬁnite state machines to regular expressions developed in

Chapter 6. Just for fun, a solution is

[aa ∪ ab(bb)

∗

ba] ∪ [(b ∪ a(bb)

∗

ba)(a(bb)

∗

a)

∗

(b ∪ ab(bb)

∗

a)]

∗

.

39. a) (ba)

+

(a

∗

b

∗

∪ a

∗

)= (ba)

+

(a

∗

)(b

∗

∪ λ) (identity 5)

= (ba)

∗

baa

∗

(b

∗

∪ λ) (identity 4)

= (ba)

∗

ba

+

(b

∗

∪ λ)

c) Except where noted, each step employs an identity from rule 12 in Table 2.1.

(a ∪ b)

∗

= (b ∪ a)

∗

(identity 5)

= b

∗

(b ∪ a)

∗

(identity 5)

= b

∗

(a

∗

b

∗

)

∗

= (b

∗

a

∗

)

∗

b

∗

(identity 11)

= (b ∪ a)

∗

b

∗

= (a ∪ b)

∗

b

∗

(identity 5)

Chapter 3

Context-Free Grammars

1. a) Derivation Rule

S ⇒ abSc S → abSc

⇒ ababScc S → abSc

⇒ ababAcc S → A

⇒ ababcAdcc A → cAd

⇒ ababccddcc A → cd

b) The derivation tree corresponding to the preceding derivation is

S

b S

a b S

A

c

A

d d

d

c

c a

c) L(G)= ¦(ab)

n

c

m

d

m

c

n

[ n ≥ 0, m > 0¦

4. a) S ⇒ AB

⇒ aAB

⇒ aaB

⇒ aaAB

⇒ aaaB

⇒ aaab

b) S ⇒ AB

11

12 CHAPTER 3. CONTEXT-FREE GRAMMARS

⇒ AAB

⇒ AAb

⇒ Aab

⇒ aAab

⇒ aaab

c) There are 20 derivations that generate DT. There is no trick to obtaining this answer,

you simply must count the possible derivations represented by the tree.

6. a) The S rules S → aaSB and S → λ generate an equal number of leading aa’s and trailing

B’s. Each B is transformed into one or more b’s. Since at least one application of the

rule S → aaSB is necessary to generate b’s, strings consisting solely of b’s are not in the

language. The language of the grammar is ¦(aa)

i

b

j

[ i > 0, j ≥ i¦ ∪ ¦λ¦.

c) Repeated applications of the recursive rule S → abSdc produce a string of the form

(ab)

i

S(dc)

i

, i ≥ 0. The recursion is terminated by an application of the rule S → A. The

A rules produce strings of the form (cd)

j

(ba)

j

, j ≥ 0. The language generated by the

rules is ¦(ab)

i

(cd)

j

(ba)

j

(dc)

i

[ i ≥ 0, j ≥ 0¦.

8. The language consisting of the set of strings ¦a

n

b

m

c

2n+m

[ m, n > 0¦ is generated by

S → aScc [ aAcc

A → bAc [ bc

For each leading a generated by the S rules, two c’s are produced at the end of the string. The

rule A rules generate an equal number of b’s and c’s.

12. A recursive deﬁnition of the set of strings over ¦a, b¦ that contain the same number of a’s and

b’s is given in Example 2.2.3. The recursive deﬁnition provides the insight for constructing the

rules of the context-free grammar

S → λ

S → SaSbS [ SbSaS [ SS

that generates the language. The basis of the recursive deﬁnition consists of the null string.

The ﬁrst S rule produces this string. The recursive step consists of inserting an a and b in

a previously generated string or by concatenating two previously generated strings. These

operations are captured by the second set of S rules.

13. An a can be speciﬁcally placed in the middle position of a string using the rules

A → aAa [ aAb [ bAa [ bAb [ a

The termination of the application of recursive A rules by the rule A → a inserts the symbol

a into the middle of the string. Using this strategy, the grammar

S → aAa [ aAb [ bBa [ bBb [ a [ b

A → aAa [ aAb [ bAa [ bAb [ a

B → aBa [ aBb [ bBa [ bBb [ b

generates all strings over ¦a, b¦ with the same symbol in the beginning and middle positions.

If the derivation begins with an S rule that begins with an a, the A rules ensure that an a

occurs in the middle of the string. Similarly, the S and B rules combine to produce strings

with a b in the ﬁrst and middle positions.

SOLUTIONS TO EXERCISES 13

16. The language (a ∪ b)

∗

aa(a ∪ b)

∗

bb(a ∪ b)

∗

is generated by

G

1

: S

1

→ aS

1

[ bS

1

[ aA

A → aB

B → aB [ bB [ bC

C → bD

D → aD [ bD [ λ

G

2

generates the strings (a ∪ b)

∗

bb(a ∪ b)

∗

aa(a ∪ b)

∗

G

2

: S

2

→ aS

2

[ bS

2

[ bE

E → bF

F → aF [ bF [ aG

G → aH

H → aH [ bH [ λ

A grammar G that generates

(a ∪ b)

∗

aa(a ∪ b)

∗

bb(a ∪ b)

∗

∪ (a ∪ b)

∗

bb(a ∪ b)

∗

aa(a ∪ b)

∗

can be obtained from G

1

and G

2

. The rules of G consist of the rules of G

1

and G

2

augmented

with S → S

1

[ S

2

where S is the start symbol of the composite grammar. The alternative in

these productions corresponds to the ∪ in the deﬁnition of the language.

While the grammar described above generates the desired language, it is not regular. The

rules S → S

1

[ S

2

do not have the form required for rules of a regular grammar. A regular

grammar can be obtained by explicitly replacing S

1

and S

2

in the S rules with the right-hand

sides of the S

1

and S

2

rules. The S rules of the new grammar are

S → aS

1

[ bS

1

[ aA

S → aS

2

[ bS

2

[ bE

The strategy used to modify the rules S → S

1

[ S

2

is an instance of a more general rule

modiﬁcation technique known as removing chain rules, which will be studied in detail in

Chapter 4.

20. The language ((a ∪ λ)b(a ∪ λ))

∗

is generated by the grammar

S → aA [ bB [ λ

A → bB

B → aS [ bB [ λ

This language consists of all strings over ¦a, b¦ in which every a is preceded or followed by a b.

An a generated by the rule S → aA is followed by a b. An a generated by B → aS is preceded

by a b.

24. The variables of the grammar indicate whether an even or odd number of ab’s has been

generated and the progress toward the next ab. The interpretation of the variables is

Variable Parity Progress toward ab

S even none

A even a

B odd none

C odd a

14 CHAPTER 3. CONTEXT-FREE GRAMMARS

The rules of the grammar are

S → aA [ bS

A → aA [ bB

B → aC [ bB [ λ

C → aC [ bS [ λ

A derivation may terminate with a λ-rule when a B or a C is present in the sentential form

since this indicates that an odd number of ab’s have been generated.

25. The objective is to construct a grammar that generates the set of strings over ¦a, b¦ containing

an even number of a’s or an odd number of b’s. In the grammar,

S → aO

a

[ bE

a

[ bO

b

[ aE

b

[ λ

E

a

→ aO

a

[ bE

a

[ λ

O

a

→ aE

a

[ bO

a

O

b

→ aO

b

[ bE

b

[ λ

E

b

→ aE

b

[ bO

b

the E

a

and O

a

rules generate strings with an even number of a’s. The derivation of a string

with a positive even number of a’s is initiated by the application of an S rule that generates

either E

a

or O

a

. The derivation then alternates between occurrences of E

a

and O

a

until it is

terminated with an application of the rule E

a

→ λ.

In a like manner, the O

b

and E

b

rules generate strings with an odd number of b’s. The

string aab can be generated by two derivations; one beginning with the application of the rule

S → aO

a

and the other beginning with S → aE

b

.

30. Let G be the grammar S → aSbS [ aS [ λ. We prove that every preﬁx of sentential form of

G has at least as many a’s as b’s. We will refer to this condition as the preﬁx property. The

proof is by induction of the length of derivations of G.

Basis: The strings aSbS, aS and λ are the only sentential forms produced by derivations of

length one. The preﬁx property is seen to hold for these strings by inspection.

Inductive hypothesis: Assume that every sentential form that can be obtained by a deriva-

tion of length n or less satisﬁes the preﬁx property.

Inductive step: Let w be a sentential form of G that can be derived using n + 1 rule

applications. The derivation of w can be written

S

n

⇒ uSv ⇒ w

where w is obtained by applying an S rule to uSv. By the inductive hypothesis uSv satisﬁes

the preﬁx property. A preﬁx of w consists of a preﬁx of uSv with the S replaced by λ, aS or

aSbS. Thus w also satisﬁes the preﬁx property.

32. a) The language of G is (a

+

b

+

)

+

.

b) The rules S → aS and S → Sb allow the generation of leading a’s or trailing b’s in any

order. Two leftmost derivations for the string aabb are

S ⇒ aS S ⇒ Sb

⇒ aSb ⇒ aSb

⇒ aabb ⇒ aabb

SOLUTIONS TO EXERCISES 15

c) The derivation trees corresponding to the derivations of (b) are

S

a

a

a

S b

b

b

S

S

S

S a

b

d) The unambiguous regular grammar

S → aS [ aA

A → bA [ bS [ b

is equivalent to G and generates strings unambiguously producing the terminals in a left-

to-right manner. The S rules generate the a’s and the A rules generate the b’s. The

process is repeated by the application of the rule A → bS or terminated by an application

of A → b.

33. a) The string consisting of ten a’s can be produced by two distinct derivations; one consisting

of ﬁve applications of the rule S → aaS followed by the λ-rule and the other consisting

of two applications of S → aaaaaS and the λ-rule.

To construct an unambiguous grammar that generates L(G), it is necessary to determine

precisely which strings are in L(G). The rules S → λ and S → aaS generate all strings

with an even number of a’s. Starting a derivation with a single application of S → aaaaaS

and completing it with applications of S → aaS and S → λ produces strings of length

5, 7, 9, . . . . The language of G consists of all strings of a’s except for a and aaa. This

language is generated by the unambiguous grammar

S → λ [ aa [ aaaA

A → aA [ a

d) L(G) is the set ¦λ¦∪¦a

i

b

j

[ i > 1, j > 0¦. The null string is generated directly by the rule

S → λ. The rule S → AaSbB generates one a and one b. Applications of S → AaSbB,

A → a, A → aA, and B → bB generate additional a’s and b’s. The rule A → a guarantees

that there are two or more a’s in every string nonnull in L(G).

To show that G is ambiguous we must ﬁnd a string w ∈ L(G) that can be generated by

two distinct leftmost derivations. The preﬁx of a’s can be generated either by applications

of the rule S → AaSbB or by the A rules. Consider the derivation of the string aaaabb

in which two a’s are generated by applications of the S rule.

Derivation Rule

S ⇒ AaSbB S → AaSbB

⇒ aaSbB A → a

⇒ aaAaSbBbB S → AaSbB

⇒ aaaaSbBbB A → a

⇒ aaaabBbB S → λ

⇒ aaaabbB B → λ

⇒ aaaabb B → λ

The string can also be derived using the rules A → aA and A → a to generate the a’s.

Derivation Rule

16 CHAPTER 3. CONTEXT-FREE GRAMMARS

S ⇒ AaSbB S → AaSbB

⇒ aAaSbB A → aA

⇒ aaAaSbB A → aA

⇒ aaaaSbB A → a

⇒ aaaabB S → λ

⇒ aaaabbB B → bB

⇒ aaaabb B → λ

The two distinct leftmost derivations of aaaabb demonstrate the ambiguity of G.

The regular grammar

S → aA [ λ

A → aA [ aB

B → bB [ b

generates strings in L(G) in a left-to-right manner. The rules S → aA and A → aB

guarantee the generation of at least two a’s in every nonnull string. The rule B → b,

whose application completes a derivation, ensures the presence of at least one b.

e) The variable A generates strings of the form (ab)

i

and B generates a

i

b

i

for all i ≥ 0.

The only strings in common are λ and ab. Each of these strings can be generated by two

distinct leftmost derivations.

To produce an unambiguous grammar, it is necessary to ensure that these strings are

generated by only one of A and B. Using the rules

S → A [ B

A → abA [ abab

B → aBb [ λ,

λ and ab are derivable only from the variable B.

34. a) The grammar G was produced in Exercise 33 d) to generate the language ¦λ¦ ∪ ¦a

i

b

j

[

i > 0, j > 1¦ unambiguously. A regular expression for this language is a

+

b

+

b ∪ λ.

b) To show that G is unambiguous it is necessary to show that there is a unique leftmost

derivation of every string in L(G). The sole derivation of λ is S ⇒ λ. Every other string

in L(G) has the form a

i

b

j

where i > 0 and j > 1. The derivation of such a string has the

form

Derivation Rule

S =⇒ aA S → aA

i−1

=⇒ a

i

A A → aA

=⇒ a

i

bB A → bB

j−2

=⇒ a

i

bb

j−2

B B → bB

=⇒ a

i

bb

j−2

b B → b

= a

i

b

j

At each step there is only one rule that can be applied to successfully derive a

i

b

j

. Initially

the S rule that produces an a must be employed, followed by exactly i − 1 applications

of the rule A → aA. Selecting the other A rule prior to this point would produce a string

with to few a’s. Using more than i −1 applications would produce too many a’s.

After the generation of a

i

, the rule A → bB begins the generation of the b’s. Any

deviation from the pattern in the derivation above would produce the wrong number

of b’s. Consequently, there is only one leftmost derivation of a

i

b

j

and the grammar is

unambiguous.

SOLUTIONS TO EXERCISES 17

39. a) The rules S → aABb, A → aA, and B → bB of G

1

are produced by the derivations

S ⇒ AABB ⇒ aABB ⇒ aABb, A ⇒ AA ⇒ aA, and B ⇒ BB ⇒ bB using the rules of

G

2

. Thus every rule of G

1

is either in G

2

or derivable in G

2

.

Now if w is derivable in G

1

, it is also derivable in G

2

by replacing the application of a rule

of G

1

by the sequence of rules of G

2

that produce the same transformation. If follows the

L(G

1

) ⊆ L(G

2

.

b) The language of G

1

is aa

+

b

+

b. Since the rules

A → AA [ a

generate a

+

and

B → BB [ b

generate b

+

, the language of G

2

is a

+

a

+

b

+

b

+

= aa

+

b

+

b.

40. A regular grammar is one with rules of the form A → aB, A → a, and A → λ, where A, B ∈ V

and a ∈ Σ. The rules of a right-linear grammar have the form A → λ, A → u, and A → uB,

where A, B ∈ V and u ∈ Σ

+

. Since every regular grammar is also right-linear, all regular

languages are generated by right-linear grammars.

We now must show that every language generated by a right-linear grammar is regular. Let G

be a right-linear grammar. A regular grammar G

**that generates L(G) is constructed from the
**

rules of G. Rules of the form A → aB, A → a and A → λ in G are also in G

. A right-linear

rule A → a

1

. . . a

n

with n > 1 is transformed into a sequence of rules

A → a

1

T

1

T

1

→ a

2

T

2

.

.

.

T

n−1

→ a

n

where T

i

are variables not occurring in G. The result of the application of the A → u of G is

obtained by the derivation

A ⇒ a

1

T

1

⇒ ⇒ a

1

. . . a

n−1

T

n−1

⇒ a

1

. . . a

n

in G

.

Similarly, rules of the form A → a

1

. . . a

n

B, with n > 1, can be transformed into a sequence

of rules as above with the ﬁnal rule having the form T

n−1

→ a

n

B. Clearly, the grammar G

**constructed in this manner generates L(G).
**

18 CHAPTER 3. CONTEXT-FREE GRAMMARS

Chapter 4

Normal Forms for Context-Free

Grammars

3. An equivalent essentially noncontracting grammar G

L

with a nonrecursive start symbol is

constructed following the steps given in the proof of Theorem 4.2.3. Because S is recursive, a

new start symbol S

and the rule S

→ S are added to G

L

.

The set of nullable variables is ¦S

**, S, A, B¦. All possible derivations of λ from the nullable
**

variables are eliminated by the addition of ﬁve S rules, one A rule, and one B rule. The

resulting grammar is

S

→ S [ λ

S → BSA [ BS [ SA [ BA [ B [ S [ A

B → Bba [ ba

A → aA [ a

8. Following the technique of Example 4.3.1, an equivalent grammar G

C

that does not contain

chain rules is constructed. For each variable, Algorithm 4.3.1 is used to obtain the set of

variables derivable using chain rules.

CHAIN(S) = ¦S, A, B, C¦

CHAIN(A) = ¦A, B, C¦

CHAIN(B) = ¦B, C, A¦

CHAIN(C) = ¦C, A, B¦

These sets are used to generate the rules of G

C

according to the technique of Theorem 4.3.3,

producing the grammar

S → aa [ bb [ cc

A → aa [ bb [ cc

B → aa [ bb [ cc

C → aa [ bb [ cc

In the grammar obtained by this transformation, it is clear that the A, B, and C rules do not

contribute to derivations of terminal strings.

19

20 CHAPTER 4. NORMAL FORMS

15. An equivalent grammar G

U

without useless symbols is constructed in two steps. The ﬁrst step

involves the construction of a grammar G

T

that contains only variables that derive terminal

strings. Algorithm 4.4.2 is used to construct the set TERM of variables which derive terminal

strings.

Iteration TERM PREV

0 ¦D, F, G¦ -

1 ¦D, F, G, A¦ ¦D, F, G¦

2 ¦D, F, G, A, S¦ ¦D, F, G, A¦

3 ¦D, F, G, A, S¦ ¦D, F, G, A, S¦

Using this set, G

T

is constructed.

V

T

=¦S, A, D, F, G¦

Σ

T

=¦a, b¦

P

T

: S → aA

A → aA [ aD

D → bD [ b

F → aF [ aG [ a

G → a [ b

The second step in the construction of G

U

involves the removal of all variables from G

T

that

are not reachable from S. Algorithm 4.4.4 is used to construct the set REACH of variables

reachable from S.

Iteration REACH PREV NEW

0 ¦S¦ ∅ -

1 ¦S, A¦ ¦S¦ ¦S¦

2 ¦S, A, D¦ ¦S, A¦ ¦A¦

3 ¦S, A, D¦ ¦S, A, D¦ ¦D¦

Removing all references to variables in the set V

T

−REACH produces the grammar

V

U

=¦S, A, D¦

Σ

U

=¦a, b¦

P

U

: S → aA

A → aA [ aD

D → bD [ b

19. To convert G to Chomsky normal form, we begin by transforming the rules of G to the form

S → λ, A → a, or A → w where w is a string consisting solely of variables.

S → XAY B [ ABC [ a

A → XA [ a

B → Y BZC [ b

C → XY Z

X → a

Y → b

Z → c

SOLUTIONS TO EXERCISES 21

The transformation is completed by breaking each rule whose right-hand side has length greater

than 2 into a sequence of rules of the form A → BC.

S → XT

1

[ AT

3

[ a

T

1

→ AT

2

T

2

→ Y B

T

3

→ BC

A → XA [ a

B → Y T

4

[ b

T

4

→ BT

5

T

5

→ ZC

C → XT

6

T

6

→ Y Z

X → a

Y → b

Z → c

24. a) The derivation of a string of length 0, S ⇒ λ, requires one rule application.

A string of length n > 0 requires 2n −1; n −1 rules of the form A → BC and n rules of

the form A → a.

b) The maximum depth derivation tree for a string of length 4 has the form

S

A

A A

A A

a

a

A

a a

Generalizing this pattern we see that the maximum depth of the derivation tree of a string

of length n > 0 is n. The depth of the derivation tree for the derivation of λ is 1.

c) The minimum depth derivation tree of a string of length 4 has the form

S

A

A A

a a a

A A

A

a

Intuitively, the minimum depth is obtained when the tree is the “bushiest” possible. The

minimum depth of a derivation tree for a string of length n > 0 is lg(n) + 1|.

28. To obtain an equivalent grammar with no direct left recursion, the rule modiﬁcation that

removes direct left recursion is applied to the A and B rules producing

S → A [ C

A → BX [ aX [ B [ a

X → aBX [ aCX [ aB [ aC

22 CHAPTER 4. NORMAL FORMS

B → CbY [ Cb

Y → bY [ b

C → cC [ c

The application of an A rule generates B or a and the X rule produces elements of the form

described by (aB ∪ aC)

+

using right recursion.

Similarly, the B and Y rules produce Cbb

∗

using the right recursive Y rule to generate b

+

.

32. The transformation of a grammar G from Chomsky normal form to Greibach normal form is

accomplished in two phases. In the ﬁrst phase, the variables of the grammar are numbered

and an intermediate grammar is constructed in which the ﬁrst symbol of the right-hand side

of every rule is either a terminal or a variable with a higher number than the number of the

variable on the left-hand side of the rule. This is done by removing direct left recursion and

by applying the rule replacement schema of Lemma 4.1.3. The variables S, A, B, and C are

numbered 1, 2, 3, and 4 respectively. The S rules are already in the proper form. Removing

direct left recursion from the A rules produces the grammar

S → AB [ BC

A → aR

1

[ a

B → AA [ CB [ b

C → a [ b

R

1

→ BR

1

[ B

Applying the rule transformation schema of Lemma 4.1.3, the rule

B → AA can be converted into the desired form by substituting for the ﬁrst A, resulting

in the grammar

S → AB [ BC

A → aR

1

[ a

B → aR

1

A [ aA [ CB [ b

C → a [ b

R

1

→ BR

1

[ B

The second phase involves transformation of the rules of the intermediate grammar to en-

sure that the ﬁrst symbol of the right-hand side of each rule is a terminal symbol. Working

backwards from the C rules and applying Lemma 4.1.3, we obtain

S → aR

1

B [ aB [ aR

1

AC [ aAC [ aBC [ bBC [ bC

A → aR

1

[ a

B → aR

1

A [ aA [ aB [ bB [ b

C → a [ b

R

1

→ BR

1

[ B

Finally, Lemma 4.1.3 is used to transform the R

1

rules created in the ﬁrst phase into the

proper form, producing the Greibach normal form grammar

S → aR

1

B [ aB [ aR

1

AC [ aAC [ aBC [ bBC [ bC

A → aR

1

[ a

B → aR

1

A [ aA [ aB [ bB [ b

C → a [ b

R

1

→ aR

1

AR

1

[ aAR

1

[ aBR

1

[ bBR

1

[ bR

1

→ aR

1

A [ aA [ aB [ bB [ b

SOLUTIONS TO EXERCISES 23

35. We begin by showing how to reduce the length of strings on the right-hand side of the rules

of a grammar in Greibach normal form. Let G = (V, Σ, S, P) be a grammar in Greibach

normal form in which the maximum length of the right-hand side of a rule is n, where n is any

natural number greater than 3. We will show how to transform G to a Greibach normal form

grammar G

**in which the right-hand side of each rule has length at most n −1.
**

If a rule already has the desired form, no change is necessary. We will outline the approach

to transform a rule of the form A → aB

2

B

n−1

B

n

into a set of rules of the desired form.

The procedure can be performed on each rule with right-hand side of length n to produce a

grammar in which the right-hand side of each rule has length at most n −1.

A new variable [B

n−1

B

n

] is added to grammar and the rule

A → aB

2

B

n−1

B

n

is replaced with the rule

A → aB

2

B

n−2

[B

n−1

B

n

]

whose right-hand side has length n − 1. It is now necessary to deﬁne the rules for the new

variable [B

n−1

B

n

]. Let the B

n−1

rules of G be

B

n−1

→ β

1

u

1

[ β

1

u

2

[ [ β

m

u

m

,

where β

i

∈ Σ, u

i

∈ V

∗

, and length(β

i

u

i

) ≤ n. There are three cases to consider in creating

the new rules.

Case 1: length(β

i

u

i

) ≤ n −2. Add the rule

[B

n−1

B

n

] → β

i

u

i

B

n

to G

.

Case 2: length(β

i

u

i

) = n−1. In this case, the B

n−1

rule can be written B

n−1

→ β

i

C

2

C

n−1

.

Create a new variable [C

n−1

B

n

] and add the rule

B

n−1

→ β

i

C

2

C

n−2

[C

n−1

B

n

]

to G

.

Case 3: length(β

i

u

i

) = n. In this case, the B

n−1

rule can be written B

n−1

→ β

i

C

2

C

n−1

C

n

.

Create two new variables [C

n−2

C

n−1

] and [C

n

B

n

] and add the rule

B

n−1

→ β

i

C

2

C

n−3

[C

n−2

C

n−1

][C

n

B

n

]

to G

.

Each of the rules generated in cases 1, 2, and 3 have right-hand sides of length n − 1. The

process must be repeated until rules have been created for each variable created by the rule

transformations.

The construction in case 3 requires the original rule to have at least three variables. This

length reduction process can be repeated until each rule has a right-hand side of length at

most 3. A detailed proof that this construction produces an equivalent grammar can be found

in Harrison

1

.

1

M. A. Harrison, Introduction to Formal Languages, Addison-Wesley, 1978

24 CHAPTER 4. NORMAL FORMS

Chapter 5

Finite Automata

1. a) The state diagram of M is

q

1

b

a

q

0

b

a

a

b

q

2

b) i) [q

0

, abaa] ii) [q

0

, bbbabb]

¬ [q

0

, baa] ¬ [q

1

, bbabb]

¬ [q

1

, aa] ¬ [q

1

, babb]

¬ [q

2

, a] ¬ [q

1

, abb]

¬ [q

2

, λ] ¬ [q

2

, bb]

¬ [q

0

, b]

¬ [q

1

, λ]

iii) [q

0

, bababa] iv) [q

0

, bbbaa]

¬ [q

1

, ababa] ¬ [q

1

, bbaa]

¬ [q

2

, baba] ¬ [q

1

, baa]

¬ [q

0

, aba] ¬ [q

1

, aa]

¬ [q

0

, ba] ¬ [q

2

, a]

¬ [q

1

, a] ¬ [q

2

, λ]

¬ [q

2

, λ]

c) The computations in (i), (iii), and (iv) terminate in the accepting state q

2

. Therefore,

the strings abaa, bababa, and bbbaa are in L(M).

d) Two regular expressions describing L(M) are a

∗

b

+

a

+

(ba

∗

b

+

a

+

)

∗

and (a

∗

b

+

a

+

b)

∗

a

∗

b

+

a

+

.

4. The proof is by induction on length of the input string. The deﬁnitions of

ˆ

δ and

ˆ

δ

are the

same for strings of length 0 and 1.

Assume that

ˆ

δ(q

i

, u) =

ˆ

δ

(q

i

, u) for all strings u of length n or less. Let w = bva be a string of

length of n + 1, with a, b ∈ Σ and length(v) = n −1.

25

26 CHAPTER 5. FINITE AUTOMATA

ˆ

δ(q

i

, w) =

ˆ

δ(q

i

, bva)

= δ(

ˆ

δ(q

i

, bv), a) (deﬁnition of

ˆ

δ)

= δ(

ˆ

δ

(q

i

, bv), a) (inductive hypothesis)

= δ(

ˆ

δ

(δ(q

i

, b), v), a) (deﬁnition of

ˆ

δ

)

Similarly,

ˆ

δ

(q

i

, w) =

ˆ

δ

(q

i

, bva)

=

ˆ

δ

(δ(q

i

, b), va) (deﬁnition of

ˆ

δ

)

=

ˆ

δ(δ(q

i

, b), va) (inductive hypothesis)

= δ(

ˆ

δ(δ(q

i

, b), v), a) (deﬁnition of

ˆ

δ)

= δ(

ˆ

δ

(δ(q

i

, b), v), a) (inductive hypothesis)

Thus

ˆ

δ(q

i

, w) =

ˆ

δ

(q

i

, w) for all strings w ∈ Σ

∗

as desired.

5. The DFA

c

c

b

a

a

c

q

2

b

a,b,c

a,b

q

1

q

0

q

3

that accepts a

∗

b

∗

c

∗

uses the loops in states q

0

, q

1

, and q

2

to read a’s, b’s, and c’s. Any

deviation from the prescribed order causes the computation to enter q

3

and reject the string.

8. A DFA that accepts the strings over ¦a, b¦ that do not contain the substring aaa is given by

the state diagram

a

b

b

a

a

q

2

q

1

q

0

b

a,b

q

3

The states are used to count the number of consecutive a’s that have been processed. When

three consecutive a’s are encountered, the DFA enters state q

3

, processes the remainder of the

input, and rejects the string.

SOLUTIONS TO EXERCISES 27

15. The DFA

q

0

q

4

q

1

a

a

q

2

a

a

a,b,c b,c b,c b,c b,c

q

3

accepts strings of even length over ¦a, b, c¦ that contain exactly one a. A string accepted by

this machine must have exactly one a and the total number of b’s and c’s must be odd. States

q

1

or q

3

are entered upon processing a single a. The state q

3

represents the combination of the

two conditions required for acceptance. Upon reading a second a, the computation enters the

nonaccepting state q

4

and rejects the string.

20. The states of the machine are used to count the number of symbols processed since an a has

been read or since the beginning of the string, whichever is smaller.

b

a

a

a

b

b b

q

2

q

3

q

1

q

0

a

a,b

q

4

A computation is in state q

i

if the previous i elements are not a’s. If an input string has a

substring of length four without an a, the computation enters q

4

and rejects the string. All

other strings accepted.

21. The DFA to accept this language diﬀers from the machine in Exercise 20 by requiring every

fourth symbol to be an a. The ﬁrst a may occur at position 1, 2, 3, or 4. After reading the

ﬁrst a, the computation enters state q

4

and checks for a pattern consisting of a combination of

three b’s or c’s followed by an a. If the string varies from this pattern, the computation enters

state q

8

and rejects the input.

b,c

b,c

b,c

b,c

b,c

a

a

a

a

a

a

a

a

b,c

q

4

q

5

q

7

q

6

q

1

q

2

b,c

b,c

q

3

q

0

q

8

a,b,c

28 CHAPTER 5. FINITE AUTOMATA

Strings of length 3 or less are accepted, since the condition is vacuously satisﬁed.

23. a) The transition table for the machine M is

δ a b

q

0

¦q

0

, q

1

¦ ∅

q

1

∅ ¦q

1

, q

2

¦

q

2

¦q

0

, q

1

¦ ∅

b) [q

0

, aaabb] [q

0

, aaabb]

¬ [q

1

, aabb] ¬ [q

0

, aabb]

¬ [q

1

, abb]

[q

0

, aaabb] [q

0

, aaabb]

¬ [q

0

, aabb] ¬ [q

0

, aabb]

¬ [q

0

, abb] ¬ [q

0

, abb]

¬ [q

1

, bb] ¬ [q

0

, bb]

¬ [q

1

, b]

¬ [q

1

, λ]

[q

0

, aaabb]

¬ [q

0

, aabb]

¬ [q

0

, abb]

¬ [q

1

, bb]

¬ [q

1

, b]

¬ [q

2

, λ]

c) Yes, the ﬁnal computation above reads the entire string and halts in the accepting state

q

2

.

d) A regular expressions for L(M) is a

+

b

+

(ab

+

∪ a

+

ab

+

)

∗

. Processing a

+

b

+

leaves the

computation in state q

2

. After arriving at q

2

there are two ways to leave and return,

taking the cycle q

0

, q

1

, q

2

or the cycle q

1

, q

2

. The ﬁrst path processes a string from

a

+

ab

+

and the second ab

+

. This expression can be reduced to (a

+

b

+

)

+

.

25. c) The NFA

c

a

b

a

q

3

q

0

q

2

q

1

a

accepts the language (abc)

∗

a

∗

. Strings of the form (abc)

∗

are accepted in q

0

. State q

3

accepts (abc)

∗

a

+

.

SOLUTIONS TO EXERCISES 29

29. The set of strings over ¦a, b¦ whose third to the last symbol is b is accepted by the NFA

q

1

q

2

a,b a,b

q

0

b

a,b

q

3

When a b is read in state q

0

, nondeterminism is used to choose whether to enter state q

1

or remain in state q

0

. If q

1

is entered upon processing the third to the last symbol, the

computation accepts the input. A computation that enters q

1

in any other manner terminates

unsuccessfully.

39. Algorithm 5.6.3 is used to construct a DFA that is equivalent to the NFA M in Exercise 17.

Since M does not contain λ-transitions, the input transition function used by the algorithm is

the transition function of M. The transition table of M is given in the solution to Exercise 17.

The nodes of the equivalent NFA are constructed in step 2 of Algorithm 5.6.3. The generation

of Q

**is traced in the table below. The start state of the determininstic machine is ¦q
**

0

¦, which

is the λ-closure of q

0

. The algorithm proceeds by choosing a state X ∈ Q

and symbol a ∈ Σ

for which there is no arc leaving X labeled a. The set Y consists of the states that may be

entered upon processing an a from a state in X.

X input Y Q

Q

:= ¦¦q

0

¦¦

¦q

0

¦ a ¦q

0

, q

1

¦ Q

:= Q

∪ ¦¦q

0

, q

1

¦¦

¦q

0

¦ b ∅ Q

:= Q

∪ ¦∅¦

¦q

0

, q

1

¦ a ¦q

0

, q

1

¦

¦q

0

, q

1

¦ b ¦q

1

, q

2

¦ Q

:= Q

∪ ¦¦q

1

, q

2

¦¦

∅ a ∅

∅ b ∅

¦q

1

, q

2

¦ a ¦q

0

, q

1

¦

¦q

1

, q

2

¦ b ¦q

1

, q

2

¦

The accepting state is ¦q

1

, q

2

¦. The state diagram of the deterministic machine is

{q

0

} {q

0

,

q

1

}

{q

1

,

q

2

}

b

a

a

b b

a

a, b

0/

43. To prove that q

m

is equivalent to q

n

we must show that

ˆ

δ(q

m

, w) ∈ F if, and only if,

ˆ

δ(q

n

, w) ∈

F for every string w ∈ Σ

∗

.

Let w be any string over Σ. Since q

i

and q

j

are equivalent,

ˆ

δ(q

i

, uw) ∈ F if, and only if,

ˆ

δ(q

j

, uw) ∈ F. The equivalence of q

m

and q

n

follows since

ˆ

δ(q

i

, uw) =

ˆ

δ(q

m

, w) and

ˆ

δ(q

n

, uw) =

ˆ

δ(q

n

, w).

30 CHAPTER 5. FINITE AUTOMATA

44. Let δ

([q

i

], a) = [δ(q

i

, a)] be the transition function deﬁned on classes of equivalent states of a

DFA and let q

i

and q

j

be two equivalent states. To show that δ

**is well deﬁned, we must show
**

that δ

([q

i

], a) = δ

([q

j

], a).

If δ

([q

i

], a)q

m

and δ

([q

j

], a) = q

n

, this reduces to showing that [q

m

] = [q

n

]. However, this

equality follows from the result in Exercise 43.

Chapter 6

Properties of Regular Languages

2. a) This problem illustrates one technique for determining the language of a ﬁnite automaton

with more than one accepting. The node deletion algorithm must be employed individu-

ally for each accepting state. Following this strategy, a regular expression is obtained by

deleting nodes from the graphs

a

a

G

1

q

1

q

0

b

q

1

q

2

G

2

b

b

a

b

a

q

2

b

b

q

0

Deleting q

1

and q

2

from G

1

produces

q

0

b

with the corresponding regular expression b

∗

. Removing q

2

from G

2

we obtain the ex-

pression graph

q

1

q

0

ab

+

b

a

accepting b

∗

a(ab

+

)

∗

. Consequently, the original NFA accepts strings of the form b

∗

∪

b

∗

a(ab

+

)

∗

.

5. a) Associating the variables S, A, and B with states q

0

, q

1

, and q

2

respectively, the regular

grammar

S → aA [ λ

A → aA [ aB [ bS

B → bA [ λ

31

32 CHAPTER 6. PROPERTIES OF REGULAR LANGUAGES

is obtained from the arcs and the accepting states of M.

b) A regular expression for L(M) can be obtained using the node deletion process. Deleting

the state q

1

produces the graph

q

2

q

0

a

+

b

ba

+

aa

*

a

ba

*

b

The set of strings accepted by q

2

is described by the regular expression (a

+

b)

∗

aa

∗

a(ba

+

∪

ba

∗

b(a

+

b)

∗

aa

∗

a)

∗

.

By deleting states q

2

then q

1

, we see that q

0

accepts (a(a ∪ ab)

∗

b)

∗

. The language of

M is the union of the strings accepted by q

0

and q

2

.

6. Let G be a regular grammar and M the NFA that accepts L(G) obtained by the construction

in Theorem 6.3.1. We will use induction on the length of the derivations of G to show that

there is a computation [q

0

, w] ¬

∗

[C, λ] in M whenever there is a derivation S

∗

⇒ wC in G.

The basis consists of derivations of the form S ⇒ aC using a rule S → aC. The rule produces

the transition δ(q

0

, a) = C in M and the corresponding derivation is [q

0

, a] ¬ [C, λ] as desired.

Now assume that [q

0

, w] ¬

∗

[C, λ] whenever there is a derivation S

n

⇒ wC.

Let S

∗

⇒ wC be a computation of length n + 1. Such a derivation can be written

S

n

⇒ uB

uaC,

where B → aC is a rule of G. By the inductive hypothesis, there is a computation [q

0

, u] ¬

∗

[B, λ]. Combining the transition δ(a, B) = C obtained from the rule B → aC with the

preceding computation produces

[q

0

, w] = [q

0

, ua]

¬

∗

[B, a]

¬ [C, λ]

as desired.

7. a) Let L be any regular language over ¦a, b, c¦ and let L

**be the language consisting of
**

all strings over ¦a, b, c¦ that end in aa. L

**is regular since it is deﬁned by the regular
**

expression (a ∪ b ∪ c)

∗

aa. The set

L ∩ L

= ¦w [ w ∈ L and w contains an a¦,

which consists of all strings in L that end with aa, is regular by Theorem 6.4.3.

11. a) Let G = (V, Σ, S, P) be a regular grammar that generates L. Without loss of generality,

we may assume that G does not contain useless symbols. The algorithm to remove useless

symbols, presented in Section 4.4, does not alter the form of rules of the grammar. Thus

the equivalent grammar obtained by this transformation is also regular.

Derivations in G have the form S

∗

⇒ uA

∗

⇒ uv where u, v ∈ Σ

∗

. A grammar G

that

generates P = ¦u [ uv ∈ L¦, the set of preﬁxes of L, can be constructed by augmenting

the rules of G with rules A → λ for every variable A ∈ V. The preﬁx u is produced by

the derivation S

∗

⇒ uA ⇒ u in G

.

SOLUTIONS TO EXERCISES 33

12. A DFA M

that accepts the language L

**can be constructed from a DFA M = (Q, Σ, δ, q
**

0

, F)

that accepts L. The states of M

**are ordered pairs of the form [q
**

i

, X], where q

i

∈ Q and X is a

subset of Q. Intuitively (but not precisely), the state q

i

represents the number of transitions

from the start state when processing a string and the set X consists of states that require the

same number of transitions to reach an accepting state.

The construction of the states Q

and the transition function δ

of M

**is iterative. The start
**

state of the machine is the ordered pair [q

0

, F]. For each state [q

i

, X] and symbol a such that

δ([q

i

, X], a) is undeﬁned, the transition is created by the rule

δ([q

i

, X], a) = [q

i

(a), Y],

where Y = ¦q

j

[ δ(q

j

, a) ∈ X¦. If it is not already in Q

**, the ordered pair [q
**

i

(a), Y] is added

to the states of M

**. The process is repeated until a transition is deﬁned for each state-symbol
**

pair.

A state [q

i

, X] is accepting if q

i

∈ X. Back to the intuition, this occurs when the number of

transitions from the start state is the same as the number of transitions needed to reach an

accepting state. That is, when half of a string in L has been processed.

13. b) To show that the set P of even length palindromes is not regular, it suﬃces to ﬁnd

sequences of strings u

i

and v

i

, i = 0, 1, 2, . . . , from ¦a, b¦

∗

that satisfy

i) u

i

v

i

∈ P, for i ≥ 0

ii) u

i

v

j

∈ P whenever i = j.

Let u

i

= a

i

b and v

i

= ba

i

. Then u

i

v

i

is the palindrome a

i

bba

i

. For i = j, the string

u

i

v

j

= a

i

bba

j

is not in P. We conclude, by Corollary 6.5.2, that P is not regular.

14. b) Assume that L = ¦a

n

b

m

[ n < m¦ is regular. Let k be the number speciﬁed by the

pumping lemma and z be the string a

k

b

k+1

. By the pumping lemma, z can be written

uvw where

i) v = λ

ii) length(uv) ≤ k

iii) uv

i

w ∈ L for all i ≥ 0.

By condition (ii), v consists solely of a’s. Pumping v produces the string uv

2

w that

contains at least as many a’s as b’s. Thus uv

2

w ∈ L and L is not regular.

e) The language L consists of strings of the form λ, a, ab, aba, abaa, abaab, and so forth.

Assume that L is regular and let z = abaab ba

k−1

ba

k

b, where k is the number speciﬁed

by the pumping lemma. We will show that there is no decomposition z = uvw in which

v satisﬁes the conditions of the pumping lemma.

The argument utilizes the number of b’s in the substring v. There are three cases to

consider.

Case 1: v has no b’s. In this case, the string uv

0

w has consecutive sequences of a’s in

which the second sequence has at most the same number of a’s as its predecessor. Thus

uv

0

w ∈ L.

Case 2: v has one b. In this case, v has the form a

s

ba

t

and is obtained from a substring

ba

j

ba

j+1

b in z. The string uv

2

w obtained by pumping v has a substring ba

j

ba

s+t

ba

j+1

b,

which does not follow pattern of increasing the number of a’s by one in each subsequent

substring of a’s. Thus uv

2

w ∈ L.

Case 3: v has two or more b’s. In this case, v contains a substring ba

t

b. Pumping v

produces two substrings of the form ba

t

b in uv

2

w. Since no string in L can have two

distinct substrings of this form, uv

2

w ∈ L.

34 CHAPTER 6. PROPERTIES OF REGULAR LANGUAGES

Thus there is no substring of z that satisﬁes the conditions of the pumping lemma and

we conclude the L is not regular.

17. b) Let L

1

be a nonregular language and L

2

be a ﬁnite language. Since every ﬁnite language

is regular, L

1

∩ L

2

is regular. Now assume that L

1

−L

2

is regular. By Theorem 6.4.1,

L

1

= (L

1

−L

2

) ∪ (L

1

∩ L

2

)

is regular. But this is a contradiction, and we conclude that L

1

−L

2

is not regular. This

result shows that a language cannot be “a little nonreguar”; removing any ﬁnite number

of elements from a nonregular set cannot make it regular.

18. a) Let L

1

=a

∗

b

∗

and L

2

= ¦a

i

b

i

[ i ≥ 0¦. Then L

1

∪ L

2

= L

1

, which is regular.

b) In a similar manner, let L

1

= ∅ and L

2

= ¦a

i

b

i

[ i ≥ 0¦. Then L

1

∪ L

2

= L

2

, which is

nonregular.

19. a) The regular languages over a set Σ are constructed using the operations union, concate-

nation, and Kleene star. We begin by showing that these set operations are preserved by

homomorphisms. That is, for sets X and Y and homomorphism h

i) h(XY) = h(X)h(Y)

ii) h(X ∪ Y) = h(X) ∪ h(Y)

iii) h(X

∗

) = h(X)

∗

.

The set equality h(XY) = h(X)h(Y) can be obtained by establishing the inclusions h(XY)

⊆ h(X)h(Y) and h(X)h(Y) ⊆ h(XY).

Let x be an element of h(XY). Then x = h(uv) for some u ∈ X and v ∈ Y. Since h is a

homomorphism, x = h(u)h(v) and x ∈ h(X)h(Y). Thus h(XY) ⊆ h(X)h(Y). To establish

the opposite inclusion, let x be an element in h(X)h(Y). Then x = h(u)h(v) for some u ∈

X and v ∈ Y. As before, x = h(uv) and h(X)h(Y) ⊆ h(XY). The other two set equalities

can be established by similar arguments.

Now let Σ

1

and Σ

2

be alphabets, X a language over Σ

1

, and h a homomorphism from Σ

∗

1

to Σ

∗

2

. We will use the recursive deﬁnition of regular sets to show that h(X) is regular

whenever X is. The proof is by induction on the number of applications of the recursive

step in Deﬁnition 2.3.1 needed to generate X.

Basis: The basis consists of regular sets ∅, ¦λ¦, and ¦a¦ for every a ∈ Σ

1

. The ho-

momorphic images of these sets are the sets ∅, ¦λ¦, and ¦h(a)¦, which are regular over

Σ

2

.

Inductive hypothesis: Now assume that the homomorphic image of every regular set

deﬁnable using n or fewer applications of the recursive step is regular.

Inductive step: Let X be a regular set over Σ

1

deﬁnable by n + 1 applications of the

recursive step in Deﬁnition 2.3.1. Then X can be written Y ∪ Z, YZ, or Y

∗

where Y and

Z are deﬁnable by n or fewer applications. By the inductive hypothesis, h(Y) and h(Z)

are regular. It follows that h(X), which is either h(Y) ∪h(Z), h(Y)h(Z), or h(Y)

∗

, is also

regular.

21. For every regular grammar G = (V, Σ, S, P) there is a corresponding a left-regular grammar

G

= (V, Σ, S, P

), deﬁned by

i) A → Ba ∈ P

if, and only if, A → aB ∈ P

ii) A → a ∈ P

if, and only if, A → a ∈ P

iii) A → λ ∈ P

if, and only if, A → λ ∈ P.

SOLUTIONS TO EXERCISES 35

The following Lemma establishes a relationship between regular and left-regular grammars.

Lemma. Let G and G

be corresponding regular and left-regular grammars. Then L(G

) =

L(G)

R

.

The lemma can be proven by showing that S

∗

⇒ u in G if, and only if, S

∗

⇒ u

R

in G

. The

proof is by induction on the length of the derivations.

a) Let L be a language generated by a left-regular grammar G

**. By the preceding lemma,
**

the corresponding regular grammar G generates L

R

. Since regularity is preserved by

the operation of reversal, (L

R

)

R

= L is regular. Thus every language generated by a

left-regular grammar is regular.

b) Now we must show that every regular language is generated by a left-regular grammar.

If L is a regular language, then so is L

R

. This implies that there is a regular grammar G

that generates L

R

. The corresponding left-regular grammar G

generates (L

R

)

R

= L.

29. The ≡

M

equivalence class of a state q

i

consists of all strings for which the computation of M

halts in q

i

. The equivalence classes of the DFA in Example 5.3.1 are

state equivalence class

q

0

(a ∪ ba)

∗

q

1

a

∗

b(a

+

b)

∗

q

2

a

∗

b(a

+

b)

∗

b(a ∪ b)

∗

31. Let M = (Q, Σ, δ, q

0

, F) be the minimal state DFA that accepts a language L as constructed

by Theorem 6.7.4 and let n be the number of states of M. We must show that any other DFA

M

**with n states that accepts L is isomorphic to M.
**

By Theorem 6.7.5, the ≡

M

equivalence classes of the states M are identical to the ≡

L

equiv-

alence classes of the language L.

By the argument in Theorem 6.7.5, each ≡

M

equivalence class is a subset of a ≡

L

equivalence

class. Since there are same number of ≡

M

and ≡

L

equivalence classes, the equivalence classes

deﬁned by L and M

must be identical.

The start state in each machine is the state associated with [λ]

≡

L

. Since the transition function

can be obtained from the ≡

L

equivalence classes as outlined in Theorem 6.7.4, the transition

functions of M and M

**are identical up to the names given to the states.
**

36 CHAPTER 6. PROPERTIES OF REGULAR LANGUAGES

Chapter 7

Pushdown Automata and

Context-Free Languages

1. a) The PDA M accepts the language ¦a

i

b

j

[ 0 ≤ j ≤ i¦. Processing an a pushes A onto the

stack. Strings of the form a

i

are accepted in state q

1

. The transitions in q

1

empty the

stack after the input has been read. A computation with input a

i

b

j

enters state q

2

upon

processing the ﬁrst b. To read the entire input string, the stack must contain at least j

A’s. The transition δ(q

2

, λ, A) = [q

2

, λ] will pop any A’s remaining on the stack.

The computation for an input string that has fewer a’s than b’s or in which an a occurs

after a b halts in state q

2

without processing the entire string. Thus strings with either

of these forms are rejected.

b) The state diagram of M is

q

2

q

1

λ λ/ λ

b A/ λ

λ A/ λ λ A/ λ

b A/ λ

a λ /A

q

0

d) To show that the strings aabb and aaab are in L(M), we trace a computation of M that

accepts these strings.

State String Stack

q

0

aabb λ

q

0

abb A

q

0

bb AA

q

2

b A

q

2

λ λ

State String Stack

q

0

aaab λ

q

0

aab A

q

0

ab AA

q

0

b AAA

q

2

λ AA

37

38 CHAPTER 7. PUSHDOWN AUTOMATA

q

2

λ A

q

2

λ λ

Both of these computations terminate in the accepting state q

2

with an empty stack.

3. d) The pushdown automaton deﬁned by the transitions

δ(q

0

, λ, λ) = ¦[q

1

, C]¦

δ(q

1

, a, A) = ¦[q

2

, A]¦

δ(q

1

, a, C) = ¦[q

2

, C]¦

δ(q

1

, b, B) = ¦[q

3

, B]¦

δ(q

1

, b, C) = ¦[q

3

, C]¦

δ(q

1

, a, B) = ¦[q

1

, λ]¦

δ(q

1

, b, A) = ¦[q

1

, λ]¦

δ(q

1

, λ, C) = ¦[q

5

, λ]¦

δ(q

2

, λ, λ) = ¦[q

1

, A]¦

δ(q

3

, λ, λ) = ¦[q

4

, B]¦

δ(q

4

, λ, λ) = ¦[q

1

, B]¦

accepts strings that have twice as many a’s as b’s. A computation begins by pushing a C

onto the stack, which serves as a bottom-marker throughout the computation. The stack

is used to record the relationship between the number of a’s and b’s scanned during the

computation. The stacktop will be a C when the number of a’s processed is exactly twice

the number of b’s processed. The stack will contain n A’s if the automaton has read n

more a’s than b’s. If n more b’s than a’s have been read, the stack will hold 2n B’s.

When an a is read with an A or C on the top of the stack, an A is pushed onto the stack.

This is accomplished by the transition to q

2

. If a B is on the top of the stack, the stack

is popped removing one b. If a b is read with a C or B on the stack, two B’s are pushed

onto the stack. Processing a b with an A on the stack pops the A.

The lone accepting state of the automaton is q

5

. If the input string has the twice as many

a’s as b’s, the transition to q

5

pops the C, terminates the computation, and accepts the

string.

h) The language L = ¦a

i

b

j

[ 0 ≤ i ≤ j ≤ 2 i¦ is generated by the context-free grammar

S → aSB [ λ

B → bb [ b

The B rule generates one or two b’s for each a. A pushdown automaton M that accepts L

uses the a’s to record an acceptable number of matching b’s on the stack. Upon processing

an a, the computation nondeterministically pushes one or two A’s onto the stack. The

transitions of M are

δ(q

0

, a, λ) = ¦[q

1

, A]¦

δ(q

0

, λ, λ) = ¦[q

3

, λ]¦

δ(q

0

, a, λ) = ¦[q

0

, A]¦

δ(q

0

, b, A) = ¦[q

2

, λ]¦

δ(q

1

, λ, λ) = ¦[q

0

, A]¦

δ(q

2

, b, A) = ¦[q

2

, λ]¦.

The states q

2

and q

3

are the accepting states of M. The null string is accepted in q

3

. For

a nonnull string a

i

b

j

∈ L, one of the computations will push exactly j A’s onto the stack.

The stack is emptied by processing the b’s in q

2

.

The state diagram of the PDA is

SOLUTIONS TO EXERCISES 39

q

2

q

3

λ λ/ λ

b A/ λ

b A/ λ

a λ /A

a λ /A

q

0

q

1

λ λ/ A

8. Let M = (Q, Σ, Γ, δ, q

0

, F) be a PDA that accepts L by ﬁnal state and empty stack. We

construct a PDA M

= (Q∪¦q

s

, q

f

¦, Σ, Γ∪¦C¦, δ

, q

s

, ¦q

f

¦) from M by adding a new stack

symbol C, new states q

s

and q

f

, and transitions for these states. The transition function δ

is

constructed by augmenting δ with the transitions

δ

(q

i

, λ, C) = ¦[q

f

, λ]¦ for all q

i

∈ F

δ

(q

s

, λ, λ) = ¦[q

0

, C]¦

Let w be a string accepted by M in an accepting state q

i

. A computation of M

with input

w begins by putting C on the stack and entering q

0

. The stack symbol C acts as a marker

designating the bottom of the stack. From q

0

, M

**continues with a computation identical to
**

that of M with input w. If the computation of M with w ends in an accepting state q

i

with an

empty stack, the computation of M

**is completed by the λ-transition from q
**

i

to q

f

that pops

the C from the stack. Since the computation of M

halts in q

f

, w ∈ L(M

).

We must also guarantee that the any string w accepted by M

**by ﬁnal state is accepted by M
**

by ﬁnal state and empty stack. The lone ﬁnal state of M

is q

f

, which can be entered only by

a λ-transition from a ﬁnal state q

i

of M with C as the stacktop. Thus the computation of M

**must have the form
**

[q

s

, w, λ]

¬ [q

0

, w, C]

¬

∗

[q

i

, λ, C]

¬ [q

f

, λ, λ]

Deleting the ﬁrst and last transition provides a computation of M that accepts w by ﬁnal state

and empty stack.

12. The state diagram for the extended PDA obtained from the grammar is

q

1

b A/ λ

c B/ λ

c B/ B

b A/ A

a λ /ABA

a λ /BB

q

0

16. Let M = (Q, Σ, Γ, δ, q

0

, F) be an extended PDA. We will outline the construction of a context-

free grammar that generates L(M). The steps follow precisely those given in Theorem 7.3.2,

except we begin with extended transitions rather than standard transitions.

The ﬁrst step is to construct an equivalent PDA M

with transition function δ

by augmenting

δ with the transitions:

[q

j

, B

1

B

n

A] ∈ δ

(q

i

, u, B

1

B

n

A) for every A ∈ Γ,

40 CHAPTER 7. PUSHDOWN AUTOMATA

whenever [q

j

, λ] ∈ δ(q

i

, u, B

1

B

n

) is a transition of M. The string of variables B

1

B

n

in

the preceding rules may be empty.

These rules transform a transition of M that does not remove an element from the stack into

one that initially pops the stack and later replaces the same symbol on the top of the stack.

Any string accepted by a computation that utilizes a new transition can also be obtained by

applying the original transition; hence, L(M) = L(M

).

A grammar G = (V, Σ, P, S) is constructed from the transitions of M

. The alphabet of G

is the input alphabet of M

**. The variables of G consist of a start symbol S and objects of
**

the form 'q

i

, A, q

j

` where the q’s are states of M

**and A ∈ Γ ∪ ¦λ¦. The variable 'q
**

i

, A, q

j

`

represents a computation that begins in state q

i

, ends in q

j

, and removes the symbol A from

the stack. The rules of G are constructed as follows:

1. S → 'q

0

, λ, q

j

` for each q

j

∈ F.

2. Each transition [q

j

, B

1

B

n

] ∈ δ

(q

i

, x, A), where A ∈ Γ∪¦λ¦, generates the set of rules

¦'q

i

, A, q

k

` → x'q

j

, B

1

, q

m

1

`'q

m

1

, B

2

, q

m

2

` 'q

m

n−1

, B

m

, q

k

` [ q

m

i

, q

k

∈ Q¦.

3. Each transition [q

j

, B

1

B

n

A] ∈ δ

(q

i

, x, A), where A ∈ Γ, generates the set of rules

¦'q

i

, A, q

k

` → x'q

j

, B

1

, q

m

1

`'q

m

1

, B

2

, q

m

2

` 'q

m

n−1

, B

m

, q

m

n

`'q

m

n

, A, q

k

` [ q

k

, q

m

i

∈ Q¦.

4. For each state q

k

∈ Q,

'q

k

, λ, q

k

` → λ.

A derivation begins with a rule of type 1 whose right-hand side represents a computation that

begins in state q

0

, ends in a ﬁnal state, and terminates with an empty stack, in other words, a

successful computation in M

**. Rules of types 2 and 3 trace the action of the machine. Rules
**

of type 4 are used to terminate derivations. The rule 'q

k

, λ, q

k

` → λ represents a computation

from a state q

k

to itself that does not alter the stack, that is, the null computation.

The proof that the rules generate L(M) follows the same strategy as Theorem 7.3.2. Lemmas

7.3.3 and 7.3.4 relate the derivations from a variable 'q

i

, A, q

k

` to computations of M.

17. a) Assume that language L consisting of strings over ¦a¦ whose lengths are a perfect square

is context-free. By the pumping lemma, there is a number k such that every string in L

with length k or more can be written uvwxy where

i) length(vwx) ≤ k

ii) v and x are not both null

iii) uv

i

wx

i

y ∈ L, for i ≥ 0.

The string a

k

2

must have a decompositon uvwxy that satisﬁes the preceding conditions.

Consider the length of the string z = uv

2

wx

2

y obtained by pumping uvwxy.

length(z) = length(uv

2

wx

2

y)

= length(uvwxy) +length(u) +length(v)

= k

2

+length(u) +length(v)

≤ k

2

+k

< (k + 1)

2

Since the length of z is greater than k

2

but less than (k + 1)

2

, we conclude that z ∈ L

and that L is not context-free.

SOLUTIONS TO EXERCISES 41

f) Assume that the language L consisting of preﬁxes of the string

abaabaaabaaaab . . . ba

n

ba

n+1

b . . .

is context-free and let k be the number speciﬁed by the pumping lemma. Consider the

string z = abaab ba

k

b, which is in the language and has length greater than k. Thus z

can be written uvwxy where

i) length(vwx) ≤ k

ii) v and x are not both null

iii) uv

i

wx

i

y ∈ L, for i ≥ 0.

To show that the assumption that L is context-free produces a contradiction, we examine

all possible decompositions of z that satisfy the conditions of the pumping lemma. By

(ii), one or both of v and x must be nonnull. In the following argument we assume that

v = λ.

Case 1: v has no b’s. In this case, v consists solely of a’s and lies between two consecutive

b’s. That is, v occurs in z in a position of the form

. . . ba

n

ba

i

va

j

ba

n+2

b . . .

where i +length(v) +j = n+1. Pumping v produces an incorrect number of a’s following

ba

n

b and, consequently, the resulting string is not in the language.

Case 2: v has two or more b’s. In this case, v contains a substring ba

n

b. Pumping v

produces a string with two substrings of the form ba

n

b. No string with this property is

in L.

Case 3: v has one b. Then v can be written a

i

ba

j

and occurs in z as

. . . ba

n−1

ba

n−i

va

n+1−j

b . . . .

Pumping v produces the substring

. . . ba

n−1

ba

n−i

a

i

ba

j

a

i

ba

j

a

n+1−j

b . . . = . . . ba

n−1

ba

n

ba

j+i

ba

n+1

b . . . ,

which cannot occur in a string in L.

Regardless of its makeup, pumping any nonnull substring v of z produces a string that is

not in the language L. A similar argument shows that pumping x produces a string not

in L whenever x is nonnull. Since one of v or x is nonnull, there is no decomposition of z

that satisﬁes the requirements of the pumping lemma and we conclude that the language

is not context-free.

18. a) The language L

1

= ¦a

i

b

2i

c

j

[ i, j ≥ 0¦ is generated by the context-free grammar

S → AC

A → aAbb [ λ

C → cC [ λ

b) Similarly, L

2

= ¦a

j

b

i

c

2i

[ i, j ≥ 0¦ is generated by

S → AB

A → aA [ λ

B → bBcc [ λ

42 CHAPTER 7. PUSHDOWN AUTOMATA

c) Assume L

1

∩ L

2

= ¦a

i

b

2i

c

4i

[ i ≥ 0¦ is context-free and let k be the number speciﬁed by

the pumping lemma. The string z = a

k

b

2·k

c

4·k

must admit a decomposition uvwxy that

satisﬁes the conditions of the pumping lemma. Because of the restriction on its length,

the substring vwx must have the form a

i

, b

i

, c

i

, a

i

b

j

, or b

i

c

j

. Pumping z produces the

string uv

2

wx

2

y. This operation increases the number of at least one, possibly two, but

not all three types of terminals in z. Thus uv

2

wx

2

y ∈ L, contradicting the assumption

that L is context-free.

20. Let z be any string of length 2 or more in L. Then z can be written in the form z = pabq or

z = pbaq, where p and q are strings from Σ

∗

. That is, z must contain the substring ab or the

substring ba. A decomposition of z that satisﬁes the pumping lemma for z = pabq is

u = p, v = ab, w = λ, x = λ, y = q

A similar decomposition can be produced for strings of the form pbaq.

22. Let L be a linear language. Then there is a linear grammar G = (V, Σ, S, P) that generates

L and, by Theorem 4.3.3, we may assume that G has no chain rules. Let r by the number of

variables of G and t be the maximum number of terminal symbols on the right-hand of any

rule. Any derivation that produces a string of length k = (r + 1)t or more must have at least

r + 1 rule applications. Since there are only r variables, variable that is transformed on the

r + 1st rule application must have occurred previously in the derivation.

Thus any derivation of a string z ∈ L of length greater than k can be written

S

∗

⇒ uAy

∗

⇒ uvAxy

∗

⇒ uvwxy,

where the second occurrence of A in the preceding derivation is the r + 1st rule application.

Repeating the subderivation A

∗

⇒ vAx i times prior to completing the derivation with the

subderivation A

∗

⇒ w produces a string uv

i

wx

i

y in L.

Since the sentential form uvAxy is generated by at most r rule applications, the string uvxy

must have length less than k as desired.

26. Let G = (V, Σ, S, P) be a context-free grammar. A grammar G

that generates L(G)

R

is built

from G. The variables, alphabet, and start symbol of G

**are the same as those of G. The rules
**

of G

**are obtained by reversing the right-hand of the rules of G; A → w
**

R

∈ P

if, and only if,

A → w ∈ P.

Induction on the length of derivations is used to show that a string u is a sentential form of G

if, and only if, u

R

is a sentential form of G

**. We consider sentential forms of G generated by
**

leftmost derivations and of G

by rightmost derivations.

Basis: The basis consists of sentential forms produced by derivations of length one. These are

generated by the derivation S ⇒ u in G and S ⇒ u

R

in G

where S → u is a rule in P.

Inductive hypothesis: Assume that u is a sentential form of G derivable by n or fewer

rule applications if, and only if, u

R

is a sentential form of G

**derivable by n or fewer rule
**

applications.

Inductive step: Let u be a sentential form of G derived by n + 1 rule applications. The

derivation has the form

S

n

⇒ xAy ⇒ xwy = u

SOLUTIONS TO EXERCISES 43

where A is the leftmost variable in xAy and A → w is a rule in P.

By the inductive hypothesis, y

R

Ax

R

is a sentential form of G

**derivable by n rule applications.
**

Using the rule A → w

R

in P

, u

R

is derivable by the rightmost derivation

S

n

⇒ y

R

Ax

R

⇒ y

R

w

R

x

R

= u

R

.

In a like manner, we can prove that the reversal of every sentential form derivable in G

is

derivable in G. It follows that L(G

) = L(G)

R

.

27. Let G = (V, Σ, S, P) be a context-free grammar that generates L and let G

= (V ∪¦X¦,

Σ − ¦a¦, S, P

**∪ ¦X → λ¦) be the grammar obtained from G by replacing all occurrences of
**

the terminal a in the right-hand side of the rules of G with a new variable X. The set of rules

of G

is completed by adding the rule X → λ. The grammar G

generates er

a

(L).

The proof follows from a straightforward examination of the derivations of G and G

. For

simplicity, let u : y/x represent a string u ∈ Σ

∗

in which all occurrences of the symbol y in u

are replaced by x.

Let u be a string in L. Then there is a derivation S

∗

⇒ u in G. Performing the same derivation

with the transformed rules of G

yields S

∗

⇒ u : a/X. Completing the derivation with the

rule X → λ produces u : a/λ. Thus, for every u ∈ L, the erasure of u is in L(G

) and

er

a

(L) ⊆ L(G

).

Conversely, let u be any string in L(G

**). Reordering the rules in the derivation of u so that
**

applications of the rule X → λ come last, u is generated by a derivation of the form S

∗

⇒ v

∗

⇒ u.

The subderivation S

∗

⇒ v uses only modiﬁed rules of G. Thus v : X/a is in G and u is in er

a

(L).

It follows that L(G

) ⊆ er

a

(L).

The two preceding inclusions show that L(G

) = er

a

(L) and consequently that er

a

(L) is

context-free.

28. a) Let L be a context-free language over Σ, G = (V, Σ, S, P) a context-free grammar that

generates L, and h a homomorphism from Σ

∗

to Σ

∗

. We will build a context-free grammar

G

that generates h(L) = ¦h(w) [ w ∈ L¦.

Let Σ = ¦a

1

, . . . , a

n

¦ and h(a

i

) = x

i

. The rules of G

**are obtained from those of G by
**

substitution. If A → u is a rule of G, then

A → u : a

i

/u

i

, for all i

is a rule of G

**. (See the solution to Exercise 27 for an explanation of the notation.)
**

To show that h(L) is context-free, it suﬃces to show that it is generated by the grammar

G

**. For any string w ∈ L, mimicking its derivation in G using the corresponding rules of
**

G

produces h(w). Moreover the derivations in G

**can only produce strings of this form
**

and L(G

) = h(L) as desired.

b) First we note that a mapping h : Σ → Σ on the elements of the alphabet can be extended

to a homomorphism on Σ

∗

as follows: a string u = a

i

1

a

i

2

a

i

k

maps to the string

h(a

i

1

)h(a

i

2

) h(a

i

k

).

Using the preceding observation, the mapping on Σ deﬁned by h(a) = λ, h(b) = b, for

all b = a, is a homomorphism from Σ

∗

to Σ

∗

. The image of this mapping, which is

context-free by part a), is er

a

(L).

c) The mapping from ¦a

i

b

i

c

i

[ i ≥ 0¦ that erases the c’s maps a noncontext-free language

to a context-free language.

44 CHAPTER 7. PUSHDOWN AUTOMATA

30. b) Let Σ = ¦a, b, c¦ and let h: Σ

∗

→ Σ

∗

be the homomorphism obtained by using concate-

nation to extend the mapping h(a) = a, h(b) = bb, and h(c) = ccc from elements of Σ

to strings over Σ

∗

. The inverse image of ¦a

i

b

2i

c

3i

[ i ≥ 0¦ under the homomorphism

h is ¦a

i

b

i

c

i

[ i ≥ 0¦. In Example 7.4.1, the pumping lemma was used to show that

the latter language is not context-free. Since the class of context-free languages is closed

under inverse homomorphic images (Exercise 29), it follows that ¦a

i

b

2i

c

3i

[ i ≥ 0¦ is not

context-free.

Chapter 8

Turing Machines

1. a) q

0

BaabcaB b) q

0

BbcbcB

¬ Bq

1

aabcaB ¬ Bq

1

bcbcB

¬ Baq

1

abcaB ¬ Bcq

1

cbcB

¬ Baaq

1

bcaB ¬ Bccq

1

bcB

¬ Baacq

1

caB ¬ Bcccq

1

cB

¬ Baaccq

1

aB ¬ Bccccq

1

B

¬ Baaccaq

1

B ¬ Bcccq

2

cB

¬ Baaccq

2

aB ¬ Bccq

2

cbB

¬ Baacq

2

ccB ¬ Bcq

2

cbbB

¬ Baaq

2

cbcB ¬ Bq

2

cbbbB

¬ Baq

2

abbcB ¬ q

2

BbbbbB

¬ Bq

2

acbbcB

¬ q

2

BccbbcB

c) The state diagram of M is

q

1

q

2

q

0

a/a R

b/c R

c/c R

a/c L

c/b L

B/B L B/B R

d) The result of a computation is to replace the a’s in the input string with c’s and the c’s

with b’s.

3. a) Starting with the rightmost symbol in the input and working in a right-to-left manner,

the machine diagrammed below moves each symbol one position to the right.

45

46 CHAPTER 8. TURING MACHINES

q

1

q

f

q

0

a/a R

b/b R

a/B R

b/B R B/b L

B/a L

B/B L B/B R

B/B L

q

2

q

4

q

3

On reading the ﬁrst blank following the input, the head moves to the left. If the input

is the null string, the computation halts in state q

f

with the tape head in position zero

as desired. Otherwise, an a is moved to the right by transitions to states q

2

and q

3

.

Similarly, states q

4

and q

3

shift a b. This process is repeated until the entire string has

been shifted.

c) A Turing machine to insert blanks between the symbols in the input string can iteratively

use the machine from Exercise 3 a) to accomplish the task. The strategy employed by

the machine is

i. Mark tape position 0 with a #.

ii. If the input string is not empty, change the ﬁrst symbol to X or Y to record if it was

an a or b, respectively.

iii. Move to the end of the string and use strategy from the machine in part a) to move

the unmarked input on square to the right.

iv. If there are no unmarked symbols on the tape, change all of the X’s to a’s and Y ’s

to b’s, and halt in position 0.

v. Otherwise, repeat the process on the unmarked string.

A Turing machine that accomplishes these tasks is

q

1

q

2

q

9

q

10

q

f

q

3

q

0

a/a R

b/b R

b/Y R

a/X R

b/b R

a/a R

Y/Y R

X/X R

b/b L

a/a L

Y/b L

X/a L

B/# R

#/B R

B/B L

B/B R

B/B L

B/B L

B/B L

q

4

a/B R

b/B R B/b L

B/a L

B/B L

q

5

q

6

q

7

q

8

SOLUTIONS TO EXERCISES 47

Processing the null string uses the arc from q

1

to q

f

. Shifting a string one square to the

right is accomplished by states q

3

to q

7

. The arc from q

4

to q

8

when the shift has been

completed. If another shift is needed, the entire process is repeated by entering q

1

.

5. c) A computation of

q

1

q

2

q

5

q

3

q

0

b/b R

X/X R

X/X L

b/b L

X/X L

a/X L B/B R

B/B R

B/B L

B/B R

a/a R

X/X R

B/B R

q

4

a/a L

X/X L

b/X L

q

6

begins by ﬁnding the ﬁrst a on the tape and replacing it with an X (state q

1

). The tape

head is then returned to position zero and a search is initiated for a corresponding b. If a b

is encountered in state q

3

, an X is written and the tape head is repositioned to repeat the

cycle q

1

, q

2

, q

3

, q

4

. If no matching b is found, the computation halts in state q

3

rejecting

the input. After all the a’s have been processed, the entire string is read in q

1

and q

5

is

entered upon reading the trailing blank. The computation halts in the accepting state q

6

if no b’s remain on the tape.

7. We refer to the method of acceptance deﬁned in the problem as acceptance by entering. We

show that the languages accepted by entering are precisely those accepted by ﬁnal state, that

is, the recursively enumerable languages.

Let M = (Q, Σ, Γ, δ, q

0

, F) be a Turing machine that accepts by entering. The machine M

= (Q, Σ, Γ, δ

, q

0

, F) with transition function δ

deﬁned by

i) δ

(q

i

, x) = δ(q

j

, x) for all q

i

∈ F

ii) δ

(q

i

, x) is undeﬁned for all q

i

∈ F

accepts L(M) be ﬁnal state. Computations of M

are identical to those of M until M

enters

an accepting state. When this occurs, M

**halts accepting the input. A computation of M that
**

enters an accepting state halts in that state in M

.

Now let M = (Q, Σ, Γ, δ, q

0

, F) be a Turing machine that accepts by ﬁnal state. A computation

of M may enter and leave accepting states prior to termination; the intermediate states have

no bearing on the acceptance of the input. We construct a machine M

that accepts L(M) by

entering. M

**is deﬁned by the quintuple (Q ∪ ¦q
**

f

¦, Σ, Γ, δ

, q

0

, ¦q

f

¦). The transitions of M

are deﬁned by

i) δ

(q

i

, x) = δ(q

i

, x) whenever δ(q

i

, x) is deﬁned

ii) δ

(q

i

, x) = [q

f

, x, R] if q

i

∈ F and δ(q

i

, x) is undeﬁned.

A computation of M that accepts an input string halts in an accepting state q

i

. The corre-

sponding computation of M

reaches q

i

and then enters q

f

, the sole accepting state of M

. Thus

entering q

f

in M

is equivalent to halting in an accepting state of M and L(M) = L(M

).

48 CHAPTER 8. TURING MACHINES

8. Clearly, every recursively enumerable language is accepted by a Turing machine in which

stationary transitions are permitted. A standard Turing machine can be considered to be such

a machine whose transition function does not include any stationary transitions.

Let M be a Turing machine (Q, Σ, Γ, δ, q

0

, F) with stationary transitions. We construct a

standard Turing machine M

that accepts L(M). The transition function δ

of M

is constructed

from that of M. A transition of M that designates a movement of the tape head generates an

identical transition of M

.

i) δ

(q

i

, x) = δ(q

i

, x) whenever δ(q

i

, x) = [q

j

, y, d] where d ∈ ¦L, R¦

A pair of standard transitions are required to perform the same action as a stationary tran-

sition δ(x, q

i

) = [q

j

, y, S]. The ﬁrst transition prints a y, moves right and enters a new state.

Regardless of the symbol being scanned, the subsequent transition moves left to the original

position and enters q

j

.

ii) δ

(q

i

, x) = [q

t

, y, R] for every transition δ(q

i

, x) = [q

j

, y, S], where a new state q

t

∈ Q is is

added for every stationary transition

iii) δ

(q

t

, z) = [q

j

, z, L] for every z ∈ Γ and every state q

t

added in (ii)

10. a) Let M = (Q, Σ, Γ, δ, q

0

, F) be a standard Turing machine. We will build a context-

sensitive Turing machine M

that accepts L(M). The components of M

**are the same as
**

those of M except for the transition function δ

**. For every transition δ(q
**

i

, x) = [q

j

, y, d]

of M, M

**has the transitions
**

δ

(q

i

, xz) = [q

j

, yz, d] for all z ∈ Γ.

Intuitively, a computation of M

**ignores the second input symbol in the transition.
**

b) Assume that the context-sensitive transition function has transitions

δ

(q

i

, xz

i

) = [q

j

i

, y

i

z

i

, d]

for symbols z

1

, z

2

, . . . , z

n

from the tape alphabet. The computation of the standard

machine should read the x in state q

i

, determine the symbol on the right, and rewrite the

original position accordingly. This can be accomplished using the transitions

δ

(q

i

, x) = [q

i

, x

, R]

δ

(q

i

, z

1

) = [q

i

1

, z

1

, L]

δ

(q

i

1

, x) = [q

j

1

, y

1

, d]

.

.

.

δ

(q

i

, z

n

) = [q

i

n

, z

n

, L]

δ

(q

i

n

, x

) = [q

j

n

, y

n

, d]

δ

(q

i

, w) = [q

i

, w, L] for w = z

where x

**is a symbol not in tape alphabet of the context-sensitive machine. If the context-
**

sensitive transition is not applicable, the sequence of standard transitions will check the

symbol to the right and halt in state q

i

.

c) By part a) we see that every recursively enumerable language is accepted by a context-

sensitive Turing machine. Part b) provides the opposite inclusion; given a context-

sensitive machine we can build a standard machine that accepts the same language.

Thus the family of languages accepted by these types of machines are identical.

SOLUTIONS TO EXERCISES 49

14. b) The two-tape Turing machine with tape alphabet ¦a, b, B¦ and accepting state is q

4

deﬁned by the transitions

δ(q

0

, B, B) = [q

1

; B, R; B, R]

δ(q

1

, x, B) = [q

1

; x, R; x, R] for every x ∈ ¦a, b¦

δ(q

1

, B, B) = [q

2

; B, L; B, S]

δ(q

2

, x, B) = [q

2

; x, L; B, S] for every x ∈ ¦a, b¦

δ(q

2

, B, B) = [q

3

; B, R; B, L]

δ(q

3

, x, x) = [q

3

; x, R; x, L] for every x ∈ ¦a, b¦

δ(q

3

, B, B) = [q

4

; B, S; B, S]

accepts the palindromes over ¦a, b¦. The input is copied onto tape 1 in state q

1

. State

q

2

returns the head reading tape 1 to the initial position. With tape head 1 moving

left-to-right and head 2 moving right-to-left, the strings on the two tapes are compared.

If both heads simultaneously read a blank, the computation terminates in q

4

.

The maximal number of transitions of a computation with an input string of length n

occurs when the string is accepted. Tape head 1 reads right-to-left, left-to-right, and then

right-to-left through the input. Each pass requires n+1 transitions. The computation of

a string that is not accepted halts when the ﬁrst mismatch of symbols on tape 1 and tape

2 is discovered. Thus, the maximal number of transitions for an input string of length n

is 3(n + 1).

19. A deterministic machine M was constructed in Exercise 5 (c) that accepts strings over ¦a, b¦

with the same number of a’s and b’s. Using M, a composite machine is constructed to determine

whether an input string contains a substring of length three or more with the same number of

a’s and b’s. Nondeterminism is used to ‘choose’ a substring to be examined. States p

0

to p

7

nondeterministically select a substring of length three or more from the input.

p

1

p

2

p

3

p

0

a/B R

b/B R

b/b R

a/a R

b/b R

a/a R

b/b R

a/a R

b/B R

a/B R

b/b R

a/a R

b/b L

a/a L

b/b L

a/a L

b/B R

a/B R

B/B R

B/B S

B/B R

To M:

p

4

B/B L

B/B L

p

5

p

6

p

7

q

1

The transition to state q

1

of M begins the computation that checks whether the chosen sub-

string has the same number of a’s and b’s. The accepting states of the composite machine are

the accepting states of M.

This problem illustrates the two important features in the design of Turing machines. The

ﬁrst is the ability to use existing machines as components in more complex computations. The

machine constructed in Exercise 5 (c) provides the evaluation of a single substring needed in

this computation. The ﬂexibility and computational power that can be achieved by combining

submachines will be thoroughly examined in Chapter 9.

The second feature is the ability of a nondeterministic design to remove the need for the

consideration of all possible substrings. The computation of a deterministic machine to solve

50 CHAPTER 8. TURING MACHINES

this problem must select a substring of the input and decide if it satisﬁes the conditions. If

not, another substring is is generated and examined. This process must be repeated until an

acceptable substring is found or all substrings have been generated and evaluated.

21. The two-tape nondeterministic machine whose state diagram is given below accepts ¦uu [ u ∈

¦a, b¦

∗

¦. In the diagram, x and y represent arbitrary elements from ¦a, b¦.

q

1

[x/x R, B/x R]

[x/x S, B/B L]

[x/x S, y/y L]

[B/B R, B/B R]

[B/B R, B/B R]

q

2

q

0

[x/x S, B/B R]

[x/x R, y/y R]

q

3

q

4

The computation begins by copying an initial segment of the input on tape 1 to tape 2. A

nondeterministic transition to state q

2

represents a guess that the current position of the head

on tape 1 is the beginning of the second half of the string. The head on tape 2 is repositioned

at the leftmost square in state q

2

. The remainder of the string on tape 1 is then compared with

the initial segment that was copied onto tape 2. If a blank is read simultaneously on these

tapes, the string is accepted. Finding any mismatch between the tapes in this comparison

causes the computation to halt without accepting the input.

The maximal number of transitions occurs when all but the ﬁnal symbol of the input string is

copied onto tape 2. In this case, the head on tape 2 moves to tape position n, back to position

0, and begins the comparison. This can be accomplished in 2(length(w) + 1) transitions.

23. Let M be a nondeterministic Turing machine that halts for all inputs. The technique in-

troduced in the construction of an equivalent deterministic machine M

given in Section 9.7

demonstrates that L(M) is recursively enumerable. M

**systematically simulates all computa-
**

tions of M, beginning with computations of length 0, length 1, length 2, etc. The simulation of

a computation of M is guided by a sequence of numbers that are generated on tape 3. When

one of the simulated computations accepts the input string, M

**halts and accepts. If the input
**

is not in L(M), M

**indeﬁnitely continues the cycle of generating a sequence of numbers on tape
**

3 and simulating the computation of M associated with that sequence.

Unfortunately, the preceding approach cannot be used to show that L is recursive since the

computation of M

**will never terminate for a string that is not in the language. To show that
**

L(M) is recursive, we must construct a deterministic machine that halts for all inputs. This can

be accomplished by adding a fourth tape to M

**that records the length of the longest simulated
**

computation that has not halted prematurely. A computation terminates prematurely if the

simulation terminates before executing all of the transitions indicated by the sequence on tape

3. Upon the termination of a simulated computation that did not halt prematurely, the length

of the sequence on tape 3 is recorded on tape 4.

The machine M

**checks the length of non-prematurely terminating computations of M prior to
**

simulating a subsequent computation of M. After a numeric sequence of length n is generated

tape 3, tape 4 is checked to see if there has been a previous simulated computation using n−1

transitions. If not, M

halts and rejects. Otherwise, M

**simulates the next computation.
**

If a string u is in L(M), the computation of M

**will simulate an accepting computation of M and
**

accept u. If u in not in L(M), then all computations of M will terminate. Let k be the length

of the longest computation of M with input u. When the simulations reach computations with

k+2 transitions, M

**will halt and reject u.
**

SOLUTIONS TO EXERCISES 51

26. Let M = (Q, Σ, Γ, δ, q

0

, F) be a Turing machine that demonstrates that L is a recursive

language. Since every computation of M halts, the machine (Q, Σ, Γ, δ, q

0

, Q − F) accepts

the complement of L. Thus L is not only recursively enumerable, but also recursive.

Conversely, we must show that L is recursive when both L and its complement are recursively

enumerable. By Theorem 8.3.2, there are machines M

1

= (Q

1

, Σ, Γ

1

, δ

1

, q

0

) and M

2

= (Q

2

,

Σ, Γ

2

, δ

2

, p

0

) that accept L and L by halting. These machines provide suﬃcient information

to decide whether a string u is in L. If u ∈ L, then the computation of M

1

halts. On the other

hand, M

2

halts when u ∈ L.

We construct a two-tape machine M that simulates the computations of both M

1

and M

2

.

The input alphabet of M is Σ, the tape alphabet is Γ

1

∪ Γ

2

and the start state is r

0

. A

computation begins by copying the input onto tape 2 and repositioning the tape heads at the

initial position. This is accomplished by the transitions deﬁned in (i) through (v) below. M

then ‘runs’ M

1

on tape 1 and M

2

on tape 2. The states of M that simulate the computation

of the composite machines are elements of the Cartesian product Q

1

Q

2

¦1, 2¦. That is, a

state is an ordered triple consisting of a state of M

1

, a state of M

2

and an element of ¦1, 2¦.

i) δ(r

0

, B, B) = [r

1

; B, R; B, R]

ii) δ(r

1

, x, B) = [r

1

; x, R; x, R] for every x ∈ Σ

iii) δ(r

1

, B, B) = [r

2

; B, L; B, L]

iv) δ(r

2

, x, x) = [r

2

; x, L; x, L] for every x ∈ Σ

v) δ(r

2

, B, B) = [[q

0

, p

0

, 1]; B, S; B, S]

vi) δ([q

i

, p

j

, 1], x, y) = [[q

n

, p

j

, 2]; s, d

1

; y, S] whenever δ

1

(q

i

, x) = [q

n

, s, d

1

]

vii) δ([q

i

, p

j

, 2], x, y) = [[q

i

, p

m

, 1]; x, S; t, d

2

] whenever δ

2

(p

j

, x) = [p

n

, t, d

2

]

When a 1 occurs in the state, M processes a transition of M

1

on tape 1. The status of the

computation of M

2

is unaﬀected by such a transition. Similarly, a transition of the form

δ([q

i

, p

j

, 2], x, y) indicates that a transition of M

2

is to be executed on tape 2.

For every input string, either the computation of M

1

or M

2

terminates. A string is accepted if

the computation halts due to the lack of a transition for a conﬁguration on tape 1. Thus the

set of ﬁnal states of M consists of triples of the form [q

i

, p

j

, 1].

32. The machine in Figure 8.1 generates all nonnull strings over an alphabet ¦1, 2, . . . , n¦. The

strategy of this machine can be employed to build a two-tape machine that enumerates all

strings over ¦0, 1¦. Intuitively, the strings are generated on tape 2 and then output on tape 1.

q

1

[B/B S, 1/1 L]

[B/B S, 0/0 L]

[B/B S, B/0 L]

[B/B S, 0/1 L]

[B/B S, 0/0 R]

[B/B S, B/B R]

[B/0 R, 0/0 R]

[B/1 R, 1/1 R]

[B/B S, B/B L]

[B/B S, 1/0 L]

[B/B R, B/B S]

[B/# R, B/B R]

q

4

q

3

q

2

q

0

52 CHAPTER 8. TURING MACHINES

The cycle of producing and writing a string on tape 1 begins in q

1

. A # is written on tape

1 followed by the string in the input position on tape 2. For the remainder of the cycle, the

tape head on tape 1 remains stationary while the next string is generated on tape 2.

33. We will outline the strategy employed by a Turing machine that enumerates all ordered pairs

of natural numbers. The number n will be represented by the string 1

n+1

and we will denote

this representation by n.

The enumerating machine has three tapes: the enumeration tape, the diagonal tape, and

the work tape. The computation consists of an initialization phase followed by a loop that

enumerates the ordered pairs.

Initialization:

1. 1 is written on the diagonal tape.

2. #1B1# is written on the enumerating tape.

The initialization writes the ordered pair [0, 0] on enumerating tape. Entering the loop that

comprises the main portion of the computation, the diagonal tape holds the value of a diagonal

element [n, n] in the grid NN. All the elements in the square bounded by [0, 0] and [n, n]

have already been written on the enumerating tape.

Loop:

1. The number on the diagonal tape is incremented, producing n + 1.

2. The diagonal pair [n + 1, n + 1], n + 1Bn + 1#, is written on the enumerating tape.

3. n is written on the work tape.

4. While the work tape is not blank

- the string on the work is copied tape to the enumeration tape,

- B is written on the enumeration tape,

- the string on the diagonal tape is copied to the enumeration tape,

- # is written on the enumeration tape,

- a 1 is erased from the work tape.

5. n is written on the work tape.

6. While the work tape is not blank

- the string on the diagonal is copied tape to the enumeration tape,

- B is written on the enumeration tape,

- the string on the work tape is copied to the enumeration tape,

- # is written on the enumeration tape,

- a 1 is erased from the work tape.

The diagonal element [n + 1, n + 1] is written on the enumerating tape in step 2. The loop

beginning at step 4 writes the pairs [n, n +1], [n −1, n +1], . . . , [0, n +1] on the enumerating

tape. In a like manner, step 6 writes the pairs [n + 1, n], [n + 1, n − 1], . . . , [n + 1, 0]. Each

subsequent iteration through the loop increases the size of the square in the NN that has

been enumerated.

34. Deﬁnition 8.8.1, the deﬁnition of the enumeration of a language by a Turing machine, does

not require the machine to halt when the entire language has been written on tape 1. After

completely listing the strings in a ﬁnite language, an enumerating machine may continue

indeﬁnitely. For a string not in the language that is lexicographically greater than any string

in the language, the halting condition given in Case 2 of Theorem 8.8.7 is never triggered.

Chapter 9

Turing Computable Functions

2. Let M a machine that computes the partial characteristic function of a language L. A machine

M

that accepts L can be built by extending M. When a computation of M halts, M

**- checks to see if 1 is on the tape,
**

- if so, M

**enters an accepting state and halts, otherwise
**

- M

**enters a rejecting state and halts.
**

5. d) The Turing machine whose state diagram is given below computes the relation

even(n) =

1 if n is even

0 otherwise.

The input string represents an even number if a blank is read in q

2

and an odd number

if a blank is read in q

3

.

q

1

q

2

q

3

q

0

1/X R B/B L

1/B L

1/1 R

1/1 R

X/1 L

1/1 L B/1 L

1/B L

B/B R

B/B L

X/1 R

q

7

q

4

q

5

q

6

q

f

After determining the parity, the input is erased and the appropriate answer is written

on the tape.

6. b) The ﬁnd left macro FL

q

f

q

0

1/1 L

B/B L

1/1 L

moves right-to-left reading blanks until it encounters a 1. The computation then traverses

the sequence of 1’s and halts in state q

f

.

53

54 CHAPTER 9. TURING COMPUTABLE FUNCTIONS

7. a) A machine M that computes the function f(n) = 2n + 3 can be obtained by combining

the copy, add, and constant macros.

*

*

* *

* * *

CPY

1

CPY

1

ML

1

MR

1

MR

1

*

ML

1

A

A

C

3

(1)

*

*

The machine C

(1)

3

computes the function c

(1)

3

, which was shown to be Turing computable

in Example 9.4.2. Tracing the computation of M with input n illustrates the interaction

among the machines that make up M.

Machine Conﬁguration

BnB

CPY

1

BnBnB

MR

1

BnBnB

CPY

1

BnBnBnB

MR

1

BnBnBnB

C

(1)

3

BnBnB3B

ML

1

BnBnB3B

A BnBn + 3B

ML

1

BnBn + 3B

A B2n + 3B

8. a) A machine can be constructed to compute the characteristic function of the greater than

relation

gt(n, m) =

1 if n > m

0 otherwise.

The relations lt and gt satisfy the equality gt(n, m) = lt(m, n). The machine to compute

gt uses this relationship and the machine LT from Exercise 5 f):

SOLUTIONS TO EXERCISES 55

CPY

1,1

LT

T

MR

1

ML

1

E

1 *

* * *

*

* *

The action of the composite machine GT is exhibited by tracing a computation. The

underscore indicates the position of the tape head in the conﬁguration.

Machine Conﬁguration

BnBmB

CPY

1,1

BnBmBnB

MR

1

BnBmBnB

LT BnBlt(m, n)B

ML

1

BnBlt(m, n)B

E

1

B. . . Blt(m, n)B

T Blt(m, n)B

10. a) The function

f = add ◦ (mult ◦ (id, id), add ◦ (id, id))

is a one-variable function; the evaluation begins with the input provided to the occurrences

of the identity function in f. Evaluating f with argument n yields

f(n) = add(mult(id(n), id(n)), add(id(n), id(n)))

= add(mult(n, n), add(n, n))

= add(n n, n +n)

= n

2

+ 2 n

b) The function p

(2)

1

◦(s◦p

(2)

1

, e◦p

(2)

2

) is the two-variable empty function. Regardless of the

value of the arguments, e ◦ p

(2)

2

produces an undeﬁned value in the second component of

the ordered pair. This, in turn, causes the entire function to be undeﬁned.

12. a) Let g be the function g(n) = 2n and let h be

h(n) =

n/2 if n is even

↑ otherwise.

Not only is h ◦ g total, it is the identity function.

Modifying h by deﬁning h(n) to be 0 for all odd n produces a pair of functions g and h

that provide a solution to Exercise 11 a).

b) Let g be g(n) = 2n + 1 and h be the function deﬁned in part a). For any n, g(n) is an

odd number and, consequently, h ◦ g is undeﬁned.

13. Let f be a Turing computable function and F a machine that computes f. We construct a

machine M that returns the ﬁrst natural number n for which f(n) = 0. A computation consists

56 CHAPTER 9. TURING COMPUTABLE FUNCTIONS

of a cycle that produces BnBf(n)B, for n = 0, 1, . . . , until a value n for which f(n) = 0 is

encountered.

*

*

* *

* *

*

CPY

1

F

ML

1

MR

1

BRN

n = 0

n > 0

*

S E

1

*

The computation begins with the counter 0 on the tape. A working copy of 0 is made and F

is run on the copy. The BRN macro is used to determine whether the computed value of f

is 0. If not, the computed value is erased, the counter is incremented, and the subsequent f

value is computed. The cycle of generating a natural number and computing the f value halts

when a natural number n is found for which f(n) = 0.

If f never assumes the value 0, the computation continues indeﬁnitely. If f is not total, the

computation will return n where n is the ﬁrst value for which f(n) = 0 only if f is deﬁned for

all m < n. Otherwise, M loops upon encountering the ﬁrst value for which f(m)↑.

15. Let R be a Turing computable unary relation and M a machine that computes the characteristic

function of R. A machine that accepts R can be built using M and the branch macro.

*

* M BRN

n = 0

n > 0

*

*

The computation of M halts with 0 on the tape if, and only if, the input string is not an

element of R. Consequently, the composite machine accepts R.

17. Let L ⊆ ¦1¦

+

be a recursive language and let M = (Q, Σ, Γ, δ, q

0

, F) be a machine that

accepts L. The language L deﬁnes a unary relation R on the natural numbers by

R(n) =

1, 1

n+1

∈ L;

0, otherwise.

To show that R is computable, we build a machine M

**that writes the correct answer on the
**

tape at the end of a computation of M. The process is not quite as simple as it sounds, since M

must erase the tape prior to writing the answer. That means, M

**must record the furthest right
**

on the tape reached during a computation of M. To do this, we augment the tape alphabet to

SOLUTIONS TO EXERCISES 57

include symbols [L, x] and [R, x], for each x ∈ Γ, that act as left and right endmarkers of the

portion of the tape read during a computation of M.

A computation of M

**with input u consists of the following steps:
**

(a) Initially M

**writes [L, B] and [R, B] in the squares on either of the input, producing
**

[L, B]u[R, B].

(b) The computation of M on the string BuB is simulated by M

on [L, B]u[R, B].

(c) If the simulation of M indicates that M would halt in an accepting state, M

erases the

tape and writes 11 in the input position.

(d) If the simulation of M indicates that M would halt in a nonaccepting state, M

erases the

tape and writes 1 in the input position.

The start state of M

is q

0

and the transition function is denoted δ

**. The transitions to insert
**

the endmarkers are

δ

(q

0

, B) = [q

1

, [L, B], R]

δ(q

1

, x) = [q

1

, x, R] for all x ∈ Σ

δ(q

1

, B) = [q

2

, [R, B], L]

δ(q

2

, x) = [q

2

, x, L] for all x ∈ Σ

δ(q

2

, x) = δ(q

0

, x)

Once the endmarkers are in place, the computation of M is simulated by the transitions

δ

(q

i

, x) = δ(q

i

, x) for every transition δ(q

i

, x)

δ

(q

i

, [L, x]) = [q

j

, [L, y], R] for every transition δ(q

i

, x) = [q

j

, y, R]

δ

(q

i

, [R, x]) = [q

j

, [R, y], L] for every transition δ(q

i

, x) = [q

j

, y, L]

δ

(q

i

, [R, x]) = [q

i,x,1

, y, R] for every transition δ(q

i

, x) = [q

j

, y, R]

δ

(q

i,x,1

, B) = [q

i,x,2

, [R, B], R]

δ

(q

i,x,2

, B) = [q

j

, B, L]

When a move to the right is indicated with the tape head reading the right endmarker, the

appropriate symbol is written and the tape head moves right. At this point it writes the new

endmarker [R, B] on the tape and enters q

j

to continue the simiulation of M.

When M halts in an accepting state, M

**ﬁnds the right endmarker, erases the tape, and writes
**

58 CHAPTER 9. TURING COMPUTABLE FUNCTIONS

the answer on the tape. For every q

i

∈ F with δ(q

i

, x) undeﬁned,

δ

(q

i

, x) = [q

3

, x, R]

δ

(q

i

, [R, x]) = [q

4

, B, L] for x ∈ Γ

δ

(q

i

, [L, x]) = [q

3

, [L, x], R] for x ∈ Γ

δ

(q

3

, x) = [q

3

, x, R] for x ∈ Σ

δ

(q

3

, [R, x]) = [q

4

, B, L] for x ∈ Γ

δ

(q

4

, x) = [q

4

, B, L] for x ∈ Γ

δ

(q

4

, [L, x]) = [q

5

, B, R] for x ∈ Γ

δ

(q

5

, B) = [q

6

, 1, R]

δ

(q

6

, B) = [q

7

, 1, L]

δ

(q

7

, 1) = [q

f

, 1, L]

where q

f

is the terminal state of M

.

A similar set of transitions write a negative answer when M halts in a nonaccepting state.

19. A comparison of cardinalities will be used to show that there are uncomputable functions that

satisfy f(i) = i for all even natural numbers. By Theorem 9.5.1 we know that the set of

computable number-theoretic functions is countable.

Diagonalization can be used to show that there are uncountably many functions that are the

identity on the set of even natural numbers. Assume that this set of functions is countably

inﬁnite. Countability implies that there is an exhaustive listing f

0

, f

1

, . . . , f

i

, . . . of these

functions. Consider the function

f(n) =

n if n is even

f

(n−1)/2

(n) + 1 otherwise

Clearly f is the identity on all even numbers. However,

f(1) = f

0

(1)

f(3) = f

1

(3)

f(5) = f

2

(5)

.

.

.

f(2n + 1) = f

n

(2n + 1)

.

.

.

Since f = f

n

for all n, f is not in the listing f

0

, f

1

, . . . , f

i

, . . . . This contradicts the assumption

that the set is countably inﬁnite.

Since the set Turing computable functions is countable and the set of number-theoretic func-

tions that are the identity on the even natural numbers is uncountable, there functions of the

latter form that are uncomputable.

Chapter 10

The Chomsky Hierarchy

1. a) The unrestricted grammar

S → X [ Y [ aPAbQb

X → aaX [ λ

Y → bbY [ λ

P → aPA [ λ

Q → bQb [ E

Ab → bA

AE → a

Aa → aa

generates the language ¦a

i

b

j

a

i

b

j

[ i, j ≥ 0¦. Rules S → X and S → Y initiate derivations

that generate λ, a

2i

, and b

2i

. The derivation of a string a

i

b

j

a

i

b

j

, with i, j > 0, begins

with an application of S → aPAbQb. Repeated applications of the P and Q rules produce

a

i

A

i

b

j

Eb

j

. The rules Ab → bA, AE → a, and Aa → aa are then used to obtain the desired

conﬁguration of the terminals. The derivation of aabaab demonstrates the repositioning

of the variable A and its transformation to the terminal a.

S ⇒ aPAbQb

⇒ aaPAAbQb

⇒ aaAAbQb

⇒ aaAAbEb

⇒ aaAbAEb

⇒ aabAAEb

⇒ aabAab

⇒ aabaab

3. Let G = (V, Σ, P, S) be an unrestricted grammar. We construct a grammar G

= (V

, Σ,

P

, S) that generates L(G) in which each rule has the form u → v where u ∈ (V

)

+

and v ∈

(V

∪ Σ)

∗

. The set of variables of V

**consists of the variables of V and a new variable T
**

a

for

every a ∈ Σ. For a string u ∈ (V ∪ Σ)

∗

, let T(u) denote the string obtained by replacing each

terminal a in u with the corresponding variable T

a

. The rules of G

are

i) T(u) → T(v) for every rule u → v ∈ P

ii) T

a

→ a for every a ∈ Σ.

59

60 CHAPTER 10. THE CHOMSKY HIERARCHY

The grammar G contains a derivation S

∗

⇒ w if, and only if, S

∗

⇒ T(w) is a derivation of G

.

Using the rules deﬁned in (ii) to transform each variable T

a

to the corresponding terminal, we

see that L(G) = L(G

).

4. c) Let L

1

and L

2

be two recursively enumerable languages. We will construct an unrestricted

grammar G that generates L

1

L

2

from grammars G

1

= (V

1

, Σ

1

, P

1

, S

1

) and G

2

= (V

2

,

Σ

2

, P

2

, S

2

) that generate L

1

and L

2

respectively.

Let us ﬁrst recall the proof of Theorem 7.5.1, which established that the context-free

languages are closed under concatenation. The grammar

G = (V

1

∪ V

2

, Σ

2

∪ Σ

2

, P

1

∪ P

2

∪ ¦S → S

1

S

2

¦, S)

was constructed from G

1

and G

2

where S ∈ V

1

∪ V

2

and V

1

and V

2

are assumed to be

disjoint. Unfortunately, we must be careful in combining unrestricted grammars in this

manner since the left-hand side of a rule may contain more than one symbol. For example,

consider derivations S

1

∗

⇒ x in G

1

and S

2

∗

⇒ y in G

2

. It follows that S

∗

⇒ xy. Now either

P

1

or P

2

may contain a rule u → v where xy = x

uy

**. In this case, the transformation
**

obtained by the application of the rule u → v may produce a string that is not in the

concatenation of L

1

and L

2

.

The grammars G

1

and G

2

must be constructed to insure that no rule can overlap the

strings derived from S

1

and S

2

. Using Exercise 3, we can assume that the left-hand side

of every rule in G

1

and G

2

consists solely of variables. As before, we also assume that V

1

and V

2

are disjoint. When G

1

and G

2

satisfy these conditions, the grammar G deﬁned

above generates L

1

L

2

.

6. a) S ⇒ SBA

⇒ aBA

⇒ aAB

⇒ aaBB

⇒ aabB

⇒ aabb

b) The language of G is ¦a

i+1

b

2i

[ i ≥ 0¦.

c) L(G) is generated by the rules S → aSbb [ a.

8. a) The language L satisﬁes the pumping lemma. Let z be any string in L, then z = a

i

b

j

c

k

with 0 < i ≤ j ≤ k. Selecting u = a

i

b

j−1

, v = b, w = λ, x = c, and y = c

k−1

is

a decomposition in which v and x are pumpable. Even though the pumping lemma is

satiaﬁed, L is nevertheless not context-free.

b) The context-sensitive grammar

S → aAbc

A → aAbC [ B

B → bBC [ BC [ bC [ C

Cc → cc

generates the language ¦a

i

b

j

c

k

[ 0 < i ≤ j ≤ k¦. The S rule puts a c on the end of the

string. The recursive A rules generate a matching number of a’s, b’s, and C’s. The B

rules produce a matching C for each b and, possibly, additional C’s. The C’s are moved

to the end of the string by the Cb → bC rule and then turned to c’s by Cc → cc.

10. Let u → v be a monotonic rule with length(v) > 2. We deﬁne a set P of monotonic rules that

produce the same transformation as u → v. The rules of P are constructed so that a string

SOLUTIONS TO EXERCISES 61

on the right-hand side of a rule has length less than length(v). Without loss of generality we

assume that u, v ∈ V

∗

. If not, every occurrence of a terminal a in u and v is replaced by a

new variable T

a

and the rule T

a

→ a is added to P as in Exercise 3. The construction of P is

divided into two cases.

i) If length(u) = 1, then u → v can be written A → BCDv

where A, B, C, D ∈ V and v

∈

V

∗

. Using two new variables X and Y , the monotonic rules

A → XY

X → BC

Y → Dv

**deﬁne the same transformation as u → v.
**

ii) If length(u) > 1, then u → v can be written AEu

→ BCDv

. The rules

AE → XY

X → B

Y u

→ CDv

**generate the transformation u → v.
**

In either case, the right-hand side of each rule produced using these transformations has length

at most length(v) − 1. This procedure can be repeated until the length of the right-hand of

each rule is at most two.

11. The string transformation obtained by applying the rule AB → CD can be obtained by the

sequential application of the three rules

AB → XB

XB → XD

XD → CD

Each of these rules has the context-sensitive form uAv → uwv with w = λ.

13. The answer depends on the depth of the tree T. If the depth is 0, the language consists only

of the null string, which is regular.

If the tree has depth 1, the language is (LU ∪RU)

∗

. This follows since the most steps down

the tree that can be taken from the root is one, either right or left, followed by a move up the

tree. This process can be repeated any number of times to produce a string in L.

In general, a string is accepted if it satisﬁes the following two conditions:

a) the number of steps that move down tree equals the number of steps that move up, snd

b) the steps do not specify a move down from a leaf or up from the root.

For a tree of depth n, a DFA with 2n + 2 states can be constructed to accept the language.

State q

0

is the accepting state and represents a string with the same number of U’s as L’s and

R’s. States q

i

, i = 1, . . . , n, are entered when a string has i more U’s than L’s and R’s. States

q

i

, i = n + 1, . . . , 2n, are entered when a string has i more L’s and R’s than U’s. If the string

represents a path the would leave the tree, the DFA enters an error state q

e

and rejects the

string.

If the full binary tree has inﬁnite depth, the strings λ, LU, LLUU, . . . , L

i

U

i

, . . . , are in the

language. This sequence can be used with Corollary 6.5.2 to establish that the language is not

regular.

62 CHAPTER 10. THE CHOMSKY HIERARCHY

To show that the language is context-free, a pushdown automaton can be constructed to accept

it. Whenever an L or R is read, a A is pushed onto the stack. When a U is read, if there

is an A on the stack it is popped. Otherwise, the computation rejects the string. A string is

accepted if the computation processes the entire string and halts with an empty stack.

15. Let L be a recursively enumerable language accepted by the standard Turing machine M =

(Q, Σ, Γ, δ, q

0

, F) and let c be a terminal symbol not in Σ. The language

L

= ¦ wc

i

[ M accepts w by a computation in which the tape

head does not read tape position length(w) +i + 1¦

is accepted by a linear-bounded machine M

constructed from M. A computation of M

with

initial tape conﬁguration 'u` begins by checking if the u has the form wc

j+1

for some string

w ∈ Σ

∗

and j ≥ 0. If so, then the ﬁnal c is erased and the input shifted one position to the right

producing 'Bwc

j

`. The tape head is then moved to position 1 to read the blank preceding

the input string. For the remainder of the computation, the transitions of M

are identical to

those of M except M

treats the c as a blank. A transition of δ

[q

i

, c] of M

**performs the same
**

actions as δ[q

i

, B] of M.

Let k be the furthest tape position scanned during the computation of M that accepts a string

w. Choosing i so that length(w) + i + 1 > k, the computation of M

**will remain within the
**

segment of tape enclosed within the endmarkers. Consequently M

accepts wc

i

.

Moreover, the only way that M

accepts a string wc

i

is to mimic a computation of M that

accepts w that remains within the tape the ﬁrst length(w) +i tape positions.

Chapter 11

Decision Problems and the

Church-Turing Thesis

4. A three-tape Turing machine can be designed to solve the “2

n

” problem. In this solution we

represent the natural number n by 1

n

. The numbers 2

0

, 2

1

, 2

2

, . . . are sequentially produced

on tape 2 and compared with the input string on tape 1. Tape 3 is used in the generation of

the sequence on tape 2. The computation of the machine with input u consists of the following

actions.

1. A 1 is written on tapes 2 and 3.

2. Reading left to right, the length of the strings on tapes 1 and 2 is compared.

3. If blanks are simultaneously read on tapes 1 and 2, the computation halts accepting the

input.

4. If a blank is read on tape 1 and a 1 on tape 2, the computation halts rejecting the input.

5. If a blank is read on tape 2 and a 1 on tape 1, then

- the string on tape 3 is concatenated to the string on to tape 2,

- tape 3 is erased and the string from tape 2 is copied to tape 3,

- all tape heads are repositioned at the initial position, and

- the computation continues with step 2.

At the beginning of step 2, tapes 2 and 3 both contain the representation of 2

i

. When the

length of the input string u is compared with the string on tape 2, there are three possible

outcomes: length(u) = 2

i

and the input is accepted in step 3, length(u) < 2

i

and the input is

rejected in step 4, or length(u) > 2

i

. In the latter case, the string 2

i+1

is generated in step 5

and the comparison is repeated.

7. a) A regular grammar G = (V, Σ, P, S) has rules of the form A → aB, A → a, or A → λ

where A, B ∈ V and a ∈ Σ. To construct the encoding of the rules of the grammar over

the set ¦0, 1¦, we assign a number to each of the variables and terminals. If [V [ = n then

we number the variables from 1 to n with S assigned 1. The terminals are numbered from

n + 1 to n +m where m = [Σ[. The symbol assigned number n is encoded by the string

1

n

. The encoding of a variable A and a terminal a will be denoted en(A) and en(a). The

encoding of a string w over (V ∪ Σ)

∗

, denoted en(w), consists of the encoding of each of

the symbols in w separated by 0’s.

63

64 CHAPTER 11. DECISION PROBLEMS

Rules of the form A → λ, A → a, and A → aB are encoded by the strings en(A),

en(A)0en(a), and en(A)0en(a)0en(B), respectively. Rules are separated by 00. The

representation of a regular grammar G, denoted R(G), is the concatenation of the encod-

ing of the rules.

Consider the regular grammar G

S → aS [ aA

A → bA [ λ

that generates the language a

+

b

∗

. The grammar G is represented by the string

en(S)0en(a)0en(S)00en(S)0en(a)0en(A)00en(A)0en(b)0en(A)00en(A).

b) A nondeterministic two-tape Turing machine M is designed to solve the question of deriv-

ability in a regular grammar. A computation of M that decides whether a string w is in

L(G) begins with input consisting of the encoding of the rules of the grammar followed

by 000 and the encoding of w.

The actions of a machine M that solves the derivability problem are described below. M

uses the representation of the rules of G to simulate a derivation of G on tape 2.

1. The computation of M begins by determining whether the input has the form

R(G)000en(w). If not, M halts rejecting the input.

2. The encoding of S is written in position one of tape 2.

3. Let uA be the string that is encoded on tape 2. A rule A → v is chosen nondeter-

ministically from the encodings on tape 1. The encoding of the variable A is then

replaced with the encoding of v on tape 2.

4. If v is a terminal symbol or the null string, the string on tape 2 is compared with the

encoding of w on tape 1. If they are identical, M halts and accepts the input.

5. If v = aB for some a ∈ Σ and B ∈ V, then the lengths of ua and w are compared.

The input is rejected if length(ua) > length(w). If not, steps 3–5 are repeated.

Step 5 insures that a computation of M halts after the simulation of at most length(w) +

1 rule applications. Consequently, all computations of M terminate and M solves the

derivability problem.

7. c) A strategy for a machine to reduce membership in the language ¦x

i

y

i+1

[ i ≥ 0¦ to

¦a

i

b

i

[ i ≥ 0¦ is described below. The appropriate action is determined by the form of

input string.

i) If there is an x that occurs after a y, the input is erased and a is written in the input

position.

ii) If the input is λ, an a is written in the input position.

iii) If the input string has the form x

+

, the x’s are changed to a’s.

iv) If the string has the form x

∗

y

+

, a y is erased from the end of the input and the

remaining x’s and y’s are changed to a’s and b’s.

11. The universal machine U is modiﬁed to construct a machine U

1

that accepts strings over

¦0, 1¦ of the form R(M)w where the computation of M with input w prints a 1. U

1

accepts

by ﬁnal state and has a single accepting state.

A computation of U

1

initially determines whether the input has the form R(M)w. If so, the

computation continues with the simulation of the transitions of M in the manner of the uni-

versal machine. Before the simulation of a transition of M by U

1

, step 4 (b) of the description

of the universal machine in Theorem 11.4.1, the symbol y to be printed is examined. If y is 1,

then the U

1

enters the accepting state and halts. Otherwise, the simulation of the computation

of M continues.

SOLUTIONS TO EXERCISES 65

14. At ﬁrst glance, this problem might seem to require the computation of the machine M to

be tested on every possible input string—but this is not case. The ﬁrst 10 transitions of a

computation can read at most the ﬁrst 10 symbols in the input string. Thus all that is necessary

is to check the computation of M on all strings of length 10 or less. If a computation on this

ﬁnite set of strings requires more than 10 transitions, the answer is yes. If no computation

with any string from this set requires more than 10 transitions, the answer is no.

A modiﬁcation of the universal machine can be used to perform this check. Two tapes are

added to the universal; one used in the generation of the strings from Σ

∗

of length 10 or less

and the other to count the number of transitions in the simulation of a computation of M.

Tape 4 will hold the input and tape 5 will serve as a counter. The input to this machine is the

representation of M.

The computation consists of the following cycle:

1. The null string is written on tape 4.

2. The string 1

n

is written on tape 5.

3. The string on tape 4 is copied to tape 3 and the encoding of state q

o

is written on tape 2.

4. The computation of M with the input string from tape 4 is simulated on tapes 2 and 3.

With each simulated transition, a 1 is erased from tape 5.

5. If the simulation of M speciﬁes a transition and tape 5 is scanning a blank, the compu-

tation halts and accepts.

6. If the simulation of M halts and a 1 is read on tape 5 then

- tapes 2, 3 and 5 are erased,

- the string 1

n

is written on tape 5, and

- the next string is generated on tape 4.

7. If the string on tape 4 has length greater than 10, the computation halts and rejects the

input. Otherwise, the computation continues with step 3.

66 CHAPTER 11. DECISION PROBLEMS

Chapter 12

Undecidability

3. Let M be a Turing machine that accepts a nonrecursive language L. Without loss of generality,

we may assume that the computation of M continues indeﬁnitely whenever the input is not in

L.

Assume that there is a machine H that solves the Halting Problem for M. The halting machine

H accepts a string w if M halts with input w. That is, if w ∈ L. In addition, the computation

of H halts in a nonaccepting state for all strings in L. Consequently, L(H) = L. Since H halts

for all input strings, it follows that L is recursive. This is a contradiction since our original

assumption was that L is nonrecursive. Thus we conclude that there is no halting machine H.

6. We will show that the Halting Problem is reducible to the question of whether a Turing machine

enters a designated state. The reduction has the form

Reduction Input Condition

Halting Problem M, w M halts with w

to ↓ ↓

State Problem M

, w, q

f

M

enters state q

f

when run with w

The string w is the same for both problems. The machine M

**is constructed from M by adding
**

an additional state q

f

. The idea behind the reduction that M

enters q

f

whenever M halts.

Let M = (Q, Σ, Γ, δ, q

0

, F) be a Turing machine. The machine

M

= (Q ∪ ¦q

f

¦, Σ, Γ, δ

, q

0

, ¦q

f

¦) with transition function

i) δ

(q

i

, x) = δ(q

i

, x) whenever δ(q

i

, x) is deﬁned

ii) δ

(q

i

, x) = [q

f

, x, R] whenever δ(q

i

, x) is undeﬁned

enters q

f

for precisely the strings for which M halts. The computations of M

are identical to

those of M until M halts. When this occurs, M

enters q

f

. Thus M halting is equivalent to M

entering q

f

.

Assume that there is a machine S that solves the state problem. The input to S is a rep-

resentation of a Turing machine M, input string w, and state q

i

. S accepts the input if the

computation of M with input w enters state q

i

. Adding a preprocessor N to S, we construct a

machine H that solves the halting problem.

The input to N is R(M)w, the representation of a Turing machine M followed by an input

string. By adding the encoding of transitions described above to R(M), N constructs the

representation of the machine M

. N then adds q

f

to the end of the string producing R(M

)wq

f

.

67

68 CHAPTER 12. UNDECIDABILITY

The machine H combines N and S. A computation of H

1. runs N on input R(M)w producing R(M

)wq

f

2. runs S on R(M

)wq

f

.

H accepts input R(M)w if, and only if, M

enters state q

f

when run with input w. That

is, whenever M halts with input w. Step 1 is a reduction of the Halting Problem to the

State Problem. We conclude that the machine S cannot exist and that the State Problem is

undecidable.

12. a) The language L consisting solely of the string w is recursive, since every ﬁnite language

is recursive. The complement of L is also recursive and clearly does not contain w.

Consequently the property of containing the string w is a nontrivial property of recursively

enumerable languages. Thus, by Rice’s Theorem, the question of w being in L(M) is

undecidable.

14. a) Rice’s Theorem can be used to show that L

=∅

is not recursive. The languages ¦a¦

∗

and

∅ are both recursive, with the former language satisfying the property ‘is not the empty

set’ the latter not satisfying the property. Thus the property ‘is not the empty set’ is

nontrivial and membership in L

=∅

is undecidable.

b) To accept a string R(M) we need to determine if M will accept at least one input string.

Intuitively, the strategy might appear to use a universal machine to simulate computations

of M until one string in Σ∗ is found that is accepted by M or all strings have been checked

and found not to be accepted by M. Unfortunately, if the simulation encounters one

string for which the computation of M does not halt, this strategy will not be able to

check subsequent strings in Σ

∗

. This is the same diﬃculty encountered in showing that

a recursively enumerable language can be enumerated (Theorem 8.8.6). The solution to

our diﬃculty is the same as used in that proof.

A modiﬁcation of the universal machine can be used to show that L

=∅

is recursively

enumerable. We add three tapes and use two enumerating machines that have previously

been described. Let u

0

, u

1

, u

2

, . . . be a lexicographic ordering of the strings in Σ

∗

.

Ordered pairs [i, j] are generated on tape 4 using the enumerating the machine introduced

in Exercise 8.33. When a pair [i, j] is written the tape, the string u

i

is generated on tape

5 and i

j

is written on tape 6.

At this point, the computation of M with input u

i

is simulated on tapes 2 and 3 for

j transitions. The string on tape 6 is used as a counter to halt the simulation when j

transitions have been executed. If u

i

is accepted by M in j transitions, L(M) is accepted.

Otherwise, the work tapes are reinitialized, the next ordered pair is generated, and the

simulation continues.

If M accepts some string u

n

, it does so in some ﬁnite number m of transitions. When

[n, m] is generated, u

n

will be checked and L(M) accepted. If M does not accept any

string, the loop of generating and testing will continue indeﬁnitely.

16. a) A solution to the Post correspondence system ¦[a, aa], [bb, b], [a, bb]¦ is obtained by playing

the dominoes

a a bb bb

aa bb b b

17. a) We will show that no sequence of dominoes can be constructed that solves this Post

correspondence problem. A solution to the Post correspondence system

¦[ab, a], [ba, bab], [b, aa], [ba, ab]¦

SOLUTIONS TO EXERCISES 69

must begin with [ab, a] or [ba, bab] since these are the only pairs that have the same

leftmost symbol. A domino that extends the sequence beginning with [ab, a] must have a

b as the ﬁrst symbol in the bottom string. The only such ordered pair is [ba, bab]. Playing

this produces

ab ba

a bab

in which the top and bottom strings diﬀer in the third symbol.

There are three ways to extend a sequence that begins with [ba, bab].

i)

ba ba

bab bab

ii)

ba b

bab aa

iii)

ba ab

bab ba

Playing [ba, bab] produces strings that disagree at the fourth position. We now turn our

attention to trying to extend sequence (ii). The next domino must have an a in the ﬁrst

position in the top string. Playing the only domino of this form we obtain the sequence

ba b ab

bab aa a

in which the strings disagree in the ﬁfth position.

The sole remaining candidate is sequence (iii). Note that the rightmost end of these

strings has the same form as the sequence consisting solely of the domino [ba, bab]. That

is, the length of the bottom string is one greater than that of the top string with a b being

the rightmost symbol on the bottom. The preceding argument can be repeated to show

that

ba ba ba

bab ab ab

is the sole extension of (iii) that satisﬁes the conditions of a solution to this Post corre-

spondence system.

The only way in which this sequence can be extended is to continually play [ba, ab].

Utilizing this strategy, the length of the top string is always one less than that of the

bottom. It follows that there is no sequence of dominoes that form a solution to the Post

correspondence system.

18. We can determine whether a Post correspondence system for an alphabet Σ = ¦a¦ has a

solution by examining the relationship of number of symbols in the top of each domino with

the number in the bottom. We will consider four cases.

Case 1: the number of a’s in the top is greater than the number of a’s in the bottom of each

domino. In this case, the Post correspondence system has no solution since every way to play

the dominoes produces a longer top string than bottom string.

Case 2: the number of a’s in the top is less than the number of a’s in the bottom of each

domino. This case also is analogous to case 1 and there is no solution.

70 CHAPTER 12. UNDECIDABILITY

Case 3: there is a domino with the same number of a’s in the top and bottom. This case has

a solution consisting of that single domino.

Case 4: none of the previous cases hold. This means that the Post correspondence system

contains two dominoes [a

i

, a

j

] and [a

m

, a

n

] such that i > j and m < n. Playing n − m

dominoes of the form [a

i

, a

j

] followed by i − j dominoes of the form [a

m

, a

n

]produces a top

string with

(n −m)i + (i −j)m = ni −mi +mi −jm = ni −mj

a’s and a bottom string with

(n −m)j + (i −j)m = nj −mj +in −jn = in −mj

a’s. Thus any Post correspondence system satisfying this condition has a solution.

Examining the preceding four cases, we can determine whether any Post correspondence system

over a one symbol alphabet has a solution. Thus this special case of the Post Correspondence

Problem is decidable.

21. Let G

U

be a grammar whose rules have the form

S → u

i

Si [ u

i

i, i = 1, . . . , n

generated from a Post correspondence system. We will show that L(G

U

) is context-free.

The language L(G

U

) consists of strings in which each u

i

is matched to the integer i in reverse

order. We can build a context-free grammar G

that generates L(G

U

). The derivations of G

**produce all strings in which the matching does not occur. To describe the construction we will
**

consider the substrings u

i

and integers i to be units. There are two classes of strings that do

not satisfy the matching criteria;

a) strings with an odd number of units, and

b) strings with m units in which there is some position k such that u

i

is at position k and i

is not at position m−k.

The variables O and E generate the strings in L(G

U

) with an odd number of units and units

with a mismatch, respectively.

S → O [ E

O → u

i

jO [ ju

i

O [ ijO [ u

i

u

j

O [ u

i

[ j where i, j = 1, . . . , n

E → u

i

Ei [ A where i = 1, . . . , n

A → u

j

Bk [ iBj [ u

i

Bu

j

[ iBu

j

where i, j, k = 1, . . . , n and j = k

B → u

i

B [ iB [ λ where i = 1, . . . , n

The variable E produces strings with matching units. The application of an A rule inserts

a mismatch into the string. This is followed by a derivation from B that generates any

combination of units.

22. First we note that a Post correspondence problem C has inﬁnitely many solutions whenever

it is solvable. If w is the string spelled by a solution, then ww, www, . . . are also spelled by

solutions. These solutions are obtained by repeating the sequence of dominoes that spell w.

Let C be a Post correspondence system and let G

U

and G

V

be the upper and lower grammars

deﬁned by C. By Theorem 12.7.1, C has a solution if, and only if, L(G

U

) ∩ L(G

V

) = ∅.

Combining this with the previous observation, C having a solution is equivalent to L(G

U

)

and L(G

V

) having inﬁnitely many elements in common. It follows that there is no algorithm

that can determine whether the intersection of the languages generated by two context-free

grammars is an inﬁnite set.

SOLUTIONS TO EXERCISES 71

24. We will use a proof by contradiction to show that the question of determining whether the

languages of two arbitrary context-free grammars G

1

and G

2

satisfy L(G

1

) ⊆ L(G

2

). Assume

such an algorithm exists and let G

2

be an arbitrary context-free grammar with alphabet Σ.

We know that there is a context-free grammar G

1

that generates Σ

∗

. In this case, an algorithm

that determines whether L(G

1

) ⊆ L(G

2

) also determines whether L(G

2

) = Σ

∗

.

However, by Theorem 12.7.3, we know that there is no algorithm that can determine whether

the language of an arbitrary context-free grammar G with input alphabet Σ is Σ

∗

. Thus our

assumption must be false and there is no algorithm that determines whether L(G

1

) ⊆ L(G

2

).

72 CHAPTER 12. UNDECIDABILITY

Chapter 13

µ-Recursive Functions

3. b) The proper subtraction function sub(x, y) was deﬁned in Table 13.2.1 by

sub(x, 0) = x

sub(x, y + 1) = pred(sub(x, y))

where pred is the predecessor function. The functions g = p

(1)

1

and h = pred◦p

(3)

3

formally

deﬁne sub. Using g and h in the deﬁnition of primitive recursion produces

sub(x, 0) = g(x) = x

sub(x, y + 1) = h(x, y, sub(x, y)) = pred(sub(x, y))

as desired.

5. a)

f(3, 0) = g(3)

= 3

f(3, 1) = h(3, 0, f(3, 0))

= h(3, 0, 3)

= 3 + 3

= 6

f(3, 2) = h(3, 1, f(3, 1))

= h(3, 0, 6)

= 9

b) A closed form for f(3, n) is 3(n + 1) and f(m, n) = m(n + 1).

8. d) The values of the predicate

even(y) =

1 if y is even

0 otherwise

alternate between 0 and 1. The value of even(y+1) can be obtained directly from even(y)

using proper subtraction.

73

74 CHAPTER 13. µ-RECURSIVE FUNCTIONS

even(0) = 1

even(y + 1) = 1

˙

−even(y)

The right-hand side of the deﬁnition by primitive recursion given above uses only constant

functions, proper subtraction, and the previous value of even. Consequently, even is also

primitive recursive.

e) To deﬁne half(y) using primitive recursion, we must determine the relationship between

half(y) and half(y + 1).

half(y + 1) =

half(y) if y + 1 is odd

half(y) + 1 if y + 1 is even

Translating this into the language of primitive recursion, we obtain the primitive recursive

deﬁnition

half(0) = 0

half(y + 1) = half(y) +even(y + 1).

f) If y +1 is not a perfect square, the sqrt(y +1) = sqrt(y). When y +1 is a perfect square,

sqrt(y) is incremented.

sqrt(0) = 0

sqrt(y + 1) = sqrt(y)

+eq(mult(sqrt(y + 1), sqrt(y + 1)), y + 1).

9. c) The function btw, between, can be obtained from the conjunction of two instances of lt:

btw(x, y, z) = lt(y, x) lt(x, z).

d) The integer square root function deﬁned in Exercise 7 f) can be used to show that the

relation prsq is primitive recursive. A natural number x is a perfect square if it is the

product of the square root of x with itself. The equality predicate is used in the deﬁnition

of prsq to check this condition,

prsq(x) = eq(x, sqrt(x) sqrt(x)).

11. a) The two-variable predicate

f(x, y) =

1 if g(i) < g(x) for all i ≤ y

0 otherwise

is true only when the inequality g(i) < g(x) is satisﬁed by every natural number i between

0 and y. The bounded product

f(x, y) =

y

¸

i=0

lt(g(i), g(x))

computes f using the primitive recursive function g. Since the family of primitive recursive

functions is closed under bounded products, f is primitive recursive.

e) The bounded sum can be used to count the number of times g(i) = x in the range

0 ≤ i ≤ y. The predicate eq(g(i), x) is 1 only if g(i) = x. The summation

n(x, y) =

y

¸

i=0

eq(g(i), x)

records the number times this occurs in the designated range.

SOLUTIONS TO EXERCISES 75

f) The function n(x, y), deﬁned above, provides the counter needed for the construction of

a function that ﬁnds the third occurrence of x in the sequence g(0), g(1), . . . , g(y). The

ﬁrst i that satisﬁes the predicate eq(n(x, i), 3) is the third integer whose value g(i) is

x. Bounded minimalization is the primitive recursive tool for ﬁnding the ﬁrst value that

satisﬁes a predicate:

thrd(x, y) = ge(n(x, y), 3)

y

µz [eq(n(x, z), 3)].

The factor ge(n(x, y), 3) in the deﬁnition of thrd provides the default value whenever

there are fewer than three integers in the range 0 to y for which g assumes the value x.

12. a) First we note that, by deﬁnition, no number is a divisor of 0. Thus, the greatest common

divisor of 0 and any number is 0. Finding the greatest common divisor of x and y can be

thought of as a search procedure. The objective is to ﬁnd the greatest number less than

or equal to x that divides both x and y. The predicate divides(x, x

˙

−z) divides(y, x

˙

−z)

is 1 whenever x

˙

−z is a divisor of both x and y. The function

g(x, y) =

x

µz [divides(x, x

˙

−z) divides(y, x

˙

−z)]

sequentially examines the predicates

divides(x, x) divides(y, x)

divides(x, x

˙

−1) divides(y, x

˙

−1)

divides(x, x

˙

−2) divides(y, x

˙

−2)

.

.

.

The greatest common divisor can be obtained directly from g.

gcd(x, y) = sg(x) sg(y) ((x

˙

−g(x, y))

The sg predicates handle the special case when one or both of the arguments are 0.

13. The function f is obtained using primitive recursion and the primitive recursive functions g

and min as follows:

f(0) = g(0)

f(y + 1) = min(f(y), g(y + 1))

16. a) The prime decomposition of 18000 is 2

4

3

2

5

3

. This is a G¨odel number since it consists

of an initial sequence of the primes. The encoded sequence is 3, 1, 2.

17. a) To construct a primitive recursive one-variable predicate gdn whose value indicates whether

the input is the encoding of a G¨odel number, we begin by deﬁning an auxiliary function

g(x) =

x

µz [cosg(divides(x, pn(z)))]

that returns the number of the ﬁrst prime that does not occur in the prime decomposition

of x. If there are no primes greater than pn(g(x)) that divide x, then x is a G¨odel number

Deciding whether there is prime greater than pn(g(x)) that occurs in the prime decom-

position of x seems like it may require an unbounded search. However, we can determine

if there are primes greater than pn(g(x)) that divide x by constructing the product of the

powers of the primes less than pn(g(x)).

gdn(x) = eq(x,

g(x)−1

¸

i=0

pn(i)

dec(i,x)+1

)

76 CHAPTER 13. µ-RECURSIVE FUNCTIONS

If the product equals x, then all the primes that divide x occur before pn(g(x)). In this

case x is the encoding of a G¨odel number. The equality predicate returns zero when there

are primes greater than pn(g(x)) that divide x.

18. If z is the encoding of an ordered pair [x, y], the primitive recursive function gn

1

(dec(1, z), dec(0, z))

produces the encoding of [y, x].

20. Functions f

1

and f

2

deﬁned by simultaneous recursion are intuitively computable, the deﬁnition

provides a description for calculating the values f

1

(x, y) and f

2

(x, y). However, the deﬁnition

does not follow the form of a deﬁnition by primitive recursion since the computation of f

1

(x, y+

1) requires both f

1

(x, y) and f

2

(x, y).

The encoding and decoding functions can be used to deﬁne a two-variable primitive recursive

function f that encodes both f

1

and f

2

. The initial value of f is obtained directly from those

of f

1

and f

2

:

f(x, 0) = gn

1

(f

1

(x, 0), f

2

(x, 0)) = gn

1

(g

1

(x), g

2

(x)).

The functions h

1

and h

2

and f(x, y) are used to compute the values of the ordered pair

[f

1

(x, y + 1), f

2

(x, y + 1)]. The value of f(x, y + 1) is obtained by encoding this ordered pair.

f(x, y + 1) = gn

1

(h

1

(x, y, dec(0, f(x, y)), dec(1, f(x, y))),

h

2

(x, y, dec(0, f(x, y)), dec(1, f(x, y))))

Since each of the functions used in the construction of f is primitive recursive, f is primitive

recursive.

The functions f

1

and f

2

are obtained directly from f by

f

1

(x, y) = dec(0, f(x, y))

and

f

2

(x, y) = dec(1, f(x, y)).

23. b) The value of the function root(c

2

, c

1

, c

0

) is the ﬁrst nonnegative integral root of the

quadratic equation c

2

x

2

+c

1

x+c

0

. If there is no such root, root(c

2

, c

1

, c

0

) is undeﬁned.

The function

µz[eq(c

2

z

2

+c

1

z +c

0

, 0)]

obtained using the unbounded µ-operator computes root.

24. Since the new µ-operator extends the original operator to partial predicates, every function

generated using the standard operator is also deﬁnable using the extended µ-operator. Thus

every µ-recursive and, consequently, Turing computable function can be obtained in this man-

ner.

Using an argument similar to that in Theorem 13.6.4, we can show that a function f(x

1

, . . . , x

n

)

deﬁnable using the extended µ-operator is Turing computable. This reduces to showing that

a function obtained from a Turing computable partial predicate p(x

1

, . . . , x

n

, z) using the

extended µ-operator is Turing computable. The proof uses the strategy employed in Theorem

13.6.4 for the standard µ-operator. If the evaluation of p(x

1

, . . . , x

n

, j) is undeﬁned in step 3,

then the computation never halts indicating that f(x

1

, . . . , x

n

) is undeﬁned. Thus the proper

value of µz[p(x

1

, . . . , x

n

, z)] is produced by the Turing machine.

The preceding arguments have shown that family of functions obtained utilizing both the

µ-operator and the extended µ-operator are precisely the Turing computable functions.

SOLUTIONS TO EXERCISES 77

28. a) The value of tr

M

(x, y − 1) is the conﬁguration of the computation of M with input x

prior to the yth transition. The value of the new symbol function ns(tr

M

(x, y −1)) is the

encoding of the symbol to be written on the yth transition. The function prt is deﬁned

by

prt(x, y) = eq(ns(tr

M

(x, y −1)), 1),

which checks if the symbol to be printed is a 1.

b) Unbounded minimalization µz[prt(x, z)] returns the ﬁrst value z for which prt(x, z) is true.

Composing this with the sign function produces the desired result when the computation

of M prints a 1. The function

fprt(x) = sg ◦ µz[prt(x, z)]

is undeﬁned if M never prints a 1.

c) If the function fprt were primitive recursive, then we would always obtain an answer

indicating the ﬁrst occurrence of the printing of a 1 or a default answer (probably 0) if 1

is never printed.

This total function could be used to produce an algorithm for determining whether the

computation of an arbitrary Turing machine M will print a 1 when run with input x.

The answer to this question could be obtained as follows:

1. Build tr

M

.

2. Build prt.

3. Build fprt.

4. Obtain the value fprt(x).

By Exercise 12.7 we know that the problem of determining whether the computation

of an arbitrary Turing machine will print a 1 is undecidable. Consequently, there is no

algorithm capable of making this determination and fprt must not be primitive recursive.

78 CHAPTER 13. µ-RECURSIVE FUNCTIONS

Chapter 14

Time Complexity

1. c) The function f(n) = n

2

+ 5n + 2 + 10/n is obtained by multiplying the polynomials

in the numerator and dividing each term by n

2

. Intuitively, n

2

is the most signiﬁcant

contributor to the growth of f. Formally, the rate of growth of f can be obtained directly

from Deﬁnition 14.2.1. Clearly, n

2

is O(f). Conversely,

n

2

+ 5n + 2 + 10/n ≤ n

2

+ 5n

2

+ 2n

2

+ 10n

2

= 18n

2

for all n > 0. Thus f = O(n

2

). Combining the two relationships we see that f and n

2

have the same rates of growth.

3. e) To show that the factorial function grows faster than an exponential function, we prove

that n! > 2

n

for all n ≥ 4.

n! =

n

¸

i=0

i

= 2 3 4

n

¸

i=5

i

> 2

4

n

¸

i=5

i

> 2

4

n

¸

i=5

2

> 2

n

Thus, setting c to 1 and n

0

to 4 we see that 2

n

≤ c n! for all n ≥ n

0

. Thus, 2

n

= O(n!).

f) The inequality n! > 2

n

for n ≥ 4, established in Exercise 3 (e), is used to prove that n! =

O(2

n

). Assume that n! = O(2

n

). Then there are constants c and n

0

such that

n! ≤ c 2

n

,

whenever n ≥ n

0

. Let m be a number greater than the maximum of n

0

, 2 c, and 4. Then

m! = m (m−1)!

> 2 c (m−1)!

> 2 c 2

m−1

= c 2

m

79

80 CHAPTER 14. TIME COMPLEXITY

with m > n

0

.

Consequently, there are no constants that satisfy the requirements of Deﬁnition 14.2.1

and n! = O(2

n

).

4. It is not the case that 3

n

= O(2

n

). If so, then there are constants c and n

0

such that 3

n

≤ c 2

n

for all n ≥ n

0

. It follows that (3/2)

n

≤ c for all n greater than n

0

. Taking the logarithm base

3/2 of each side of this inequality, we see that the assumption that 3

n

= O(2

n

) implies that

n ≤ log

3/2

c for all n > n

0

. This is a contradiction since log

3/2

c is a constant and we can

select a natural number n larger than this constant. Since the assumption that 3

n

= O(2

n

)

produced a contradiction, we conclude that 3

n

= O(2

n

).

7. b) We will show, directly from the deﬁnition of big Θ, that fg ∈ Θ(n

r+t

).

Since f ∈ Θ(n

r

), there are positive constants c

1

, c

2

, and n

0

such that

0 ≤ c

1

n

r

≤ f(n) ≤ c

2

n

r

for all n ≥ n

0

. Similarly, g ∈ Θ(n

t

) implies there are positive constants d

1

, d

2

, and m

0

such that

0 ≤ d

1

n

t

≤ g(n) ≤ d

2

n

t

for all n ≥ m

0

.

First we show that fg ∈ O(n

r+t

). The preceding inequalities show that

f(n)g(n) ≤ (c

2

n

r

) (d

2

n

t

)

= (c

2

d

2

)n

r+t

for all n ≥ max¦n

0

, m

0

¦ as desired.

The same approach establishes the lower bound:

f(n)g(n) ≥ (c

1

n

r

) (d

1

n

t

)

= (c

1

d

1

)n

r+t

for all n ≥ max¦n

0

, m

0

¦.

8. c) The length of the input of a Turing machine that computes a function of more than one

variable is considered to be the length of the arguments plus the blanks that separate

them. The Turing machine in Example 9.1.2 computes the binary function of string

concatenation. The input has the form BuBvB with length length(u) +length(v) + 1.

A computation moves through u and then shifts each element of v one position to the

left. For an input of length n, the worst case number of transitions occurs when u = λ

and length(v) = n − 1. The values of the trace function tr

M

are given for the ﬁrst four

values of n.

n tr

M

(n)

1 4

2 8

3 12

4 16

A computation with input BBvB begins by reading the two blanks that precede v.

Translating each symbol of v requires three transitions. The computation is completed

by returning the tape head to the leftmost tape position. This requires length(v) + 2

transitions. Thus tr

M

(n) = 2 + 3(n −1) +n + 1 = 4n.

SOLUTIONS TO EXERCISES 81

11. The computation of a standard machine that accepts the language L = ¦a

i

b

i

[ 0 ≤ i ≤

50¦ ∪ ¦u [ length(u) > 100¦ begins using 100 states to check the length of the input. If the

machine has not encountered a blank after 100 transitions, the computation halts and accepts

the input string.

If the machine reads a blank prior to reading 100 input symbols, the computation

1. moves the tape head to position 0, and

2. runs a machine from that accepts ¦a

i

b

i

[ i ≥ 0¦.

The latter step can be accomplished in (n+2)(n+1)/2 transitions for an input string of length

n.

The maximum number of transitions in the entire computation for an input of length n is

tc

M

(n) =

2n + 2 + (n + 2)(n + 1)/2, if n ≤ 100

101, otherwise.

The asymptotic complexity of M is constant, tc

M

(n) = Θ(1), since the number of transitions

is constant as n approaches inﬁnity.

13. A one-tape deterministic Turing machine can be designed to accept the language ¦a

i

b

i

[ i ≥ 0¦

in time O(nlg(n)) by marking half of the a’s and half of the b’s on each pass through the input.

The computation begins reading the input, marking every other a with an X, and recording

whether the number of a’s was even or odd. Upon scanning a b, it is marked as well as every

other subsequent b. As with the a’s, the states are used to record parity of the number of

b’s read. If an a is encountered after the ﬁrst b, the computation halts and rejects the input.

Upon completing the scan of the input, the following halting conditions are checked:

(a) If no a’s and b’s were found, the string is accepted.

(b) If there were a’s but no b’s, the string is rejected.

(c) If there were b’s but no a’s, the string is rejected.

(d) If the parity of the number of a’s and b’s was diﬀerent, the string is rejected.

If none of the halting conditions were triggered, tape head returns to tape position 0 to make

another pass of the input. On this pass, the X’s are skipped while marking every other a

and b. On the completion of the pass, the termination conditions are checked. The process is

repeated until one of the terminating conditions is satisﬁed.

The maximum number of transitions for a string of length n occurs with the input a

n/2

b

n/2

if

n is even and a

(n+1)/2

b

(n−1)/2

if n is odd. Each iteration of the preceding cycle requires 2n+2

transitions and the maximum number of iterations is lg(n)|. Thus the maximum number of

transitions is (2n + 2)lg(n)|.

The number of transitions could be reduced by one-half if we marked symbols going both

left-to-right and right-to-left. However, this would not alter the big oh evaluation of the time

complexity.

82 CHAPTER 14. TIME COMPLEXITY

Chapter 15

P, NP, and Cook’s Theorem

1. a) The computations for the string aa are

q

0

BaaB q

0

BaaB q

0

BaaB q

0

BaaB

¬ Bq

1

aaB ¬ Bq

1

aaB ¬ Bq

1

aaB ¬ Bq

1

aaB

¬ Baq

1

aB ¬ Baq

1

aB ¬ Baq

1

aB ¬ q

2

BaaB

¬ Baaq

1

B ¬ Bq

2

aaB ¬ Bq

2

aaB

¬ q

2

BaaB ¬ Baq

3

aB

¬ Baaq

3

B

b) The worst-case performance with input a

n

occurs when the computation reads the ﬁrst

n −1 a’s in q

1

and transitions to q

2

, stays in q

2

and moves left until it reads the ﬁrst a,

then moves right in state q

3

until the computation halts.

c) The time complexity is

tc

M

(n) =

1, if n = 0;

3n −1, otherwise.

5. a) We will use a proof by contradiction to show that the language

L = ¦R(M)w [ M accepts w using at most 2

length(w)

transitions¦

is not accepted by any deterministic polynomial-time Turing machine. The contradiction

is obtained using diagonalization and self-reference.

Assume that L is in {. Then L is also in {. Since L is in {, there is some Turing machine

M

that accepts it in polynomial time. The self-reference occurs in asking if R(M

)R(M

)

is in L; that is, does M

accept R(M

)?

Consider the ﬁrst possibility, that R(M

)R(M

) is in L. This means that M

**does not accept
**

R(M

) in 2

length(R(M

))

steps. Thus either the computation takes more than 2

R(M

)

or

M

does not accept R(M

). The ﬁrst case indicates that the computations of M

are not

polynomially bounded and the second case indicates that R(M

)R(M

) is not in L. Both

of these possibilities yield a contradiction.

The second possibility is that R(M

)R(M

) is not in L. This means that M

accepts R(M

)

in at most 2

length(R(M

))

transitions. Consequently R(M

)R(M

) is in L, a contradiction.

Both possibilities, that R(M

)R(M

) is in L and R(M

)R(M

) is not in L, produce a

contradiction. Thus our assumption could not be true and the language L is not in {.

83

84 CHAPTER 15. {, ^{, AND COOK’S THEOREM

9. Intuitively, the polynomial time bound p(n) can be used to terminate all computations of M

that use more than p(n) transitions. To accomplish this, a machine M

**is constructed that has
**

one more tape than M, which we will call the counter tape. The computation of M

**1. Writes p(n) 1’s on the counter tape and repositions the tape head at the beginning of
**

the string of 1’s.

2. While the counter tape head is reading a 1, M

**- Moves the counter tape head one position to the right.
**

- If a transition of M is deﬁned, then M

**simulates the transition on original tapes.
**

Otherwise M

**halts and returns the same result as M.
**

3. M

**halts and does not accept when a B is read on the counter tape.
**

For every string u ∈ L(M), there is at least computation that requires p(n) or fewer transitions

and accepts u. The simulation of this computation in M

will also accept u. Clearly, L(M

) is

contained in L(M) since it only accepts strings that are accepted by M. Thus M

accepts L(M)

and halts for all input strings.

The computation of M

**for an input string u of length n consists of the writing p(n) 1’s on the
**

counter tape and simulating the computation of M. The number of transitions in the simulation

is bounded by p(n) and writing p(n) 1’s can be accomplished in time polynomial in n.

12. In the ﬁrst printing of the book, the state diagram is incorrect. The arcs on the bottom should

go from right-to-left and there should be a transition B/B L on the arc from q

6

to q

f

. The

answers given are for the state diagram with these modiﬁcations.

a)

q

0

BabbaB ¬ #abq

5

bBB

¬ #q

1

abbaB ¬ #aq

5

bdBB

¬ #aq

2

bbaB ¬ #q

5

addBB

¬ #abq

3

baB ¬ q

5

#cddBB

¬ #abbq

4

aB ¬ Bq

6

cddBB

¬ #abbBq

4

B ¬ q

f

BcddBB

¬ #abbq

5

BB

b) Any string will cause the maximum number of transitions. The computation reads the

entire string from left-to-right, then again from right-to-left, and ends with two transitions

to erase the endmarker.

c) tc

M

(n) = 2n + 4

d) The function computed by the machine is not a reduction of L to Q. The string ab is

mapped to cd; the former is not in L while the latter is in Q.

15. Any combination of three of the clauses x, y, z, and x ∨ y ∨ z is satisﬁable. The four

clauses, however, are unsatisﬁable. The satisfaction of the clause x ∨ y ∨ z requires the

truth assignment for one of the Boolean variables to be 0. This precludes the clause that

consists of that variable alone from being satisﬁed.

17. The assumption the { = ^{ is critical in the arguments that follow. Of course, we do not

know if this assumption is true.

SOLUTIONS TO EXERCISES 85

a) Let L be a language in ^{ that is neither empty nor is its complement empty. That

means that are strings u and v such that u ∈ L and v ∈ L.

To prove that L is NP-complete, we must show that every language Q in ^{ is reducible

to L in polynomial time. Since ^{ = {, we know that there is a Turing machine M that

accepts Q in polynomial time. A polynomial time reduction of Q to L can be obtained

as follows: For a string w

1. Run M to determine if w ∈ Q.

2. If w ∈ Q, then erase the tape and write u in the input position. Else erase the tape

and write v in the input position.

This is clearly a reduction of Q to L. Moreover, it can be accomplished in polynomial

time since the computation of Q is polynomial.

b) In part (a) we have shown that every language in ^{ other than ∅ and its complement

is NP-complete. These languages clearly are in { = ^{, but are not NP-complete. To

be able to reduce a nonempty language Q to the ∅, we must map strings in Q to a string

in ∅ and strings not in Q to a string not in ∅. Unfortunately, we cannot do the former

since there are no strings in ∅.

86 CHAPTER 15. {, ^{, AND COOK’S THEOREM

Chapter 16

NP-Complete Problems

1. A formula is in 2-conjunctive normal form if it is the conjunction of clauses each of which has

two literals. The 2-Satisﬁability Problem is to determine whether an arbitrary 2-conjunctive

normal form is satisﬁable.

We will describe a polynomial-time algorithm that solves the 2-Satisﬁability Problem. The

algorithm constructs a truth assignment if the formula is satisﬁable. If not, the attempt to

construct the assignment fails.

Before outlining the strategy, we ﬁrst note that the assignment of a truth value to a variable

may be used to reduce the length of a 2-conjunctive normal form formula that needs to be

considered for determining satisﬁability. Consider the formula

(x

1

∨ x

2

) ∧ (x

2

∨ x

3

) ∧ (x

1

∨ x

3

) ∧ (x

1

∨ x

4

).

If t(x

1

) = 1, then any clause that contains x

1

can be deleted since it is satisﬁed by t. This

produces

(x

2

∨ x

3

) ∧ (x

1

∨ x

3

).

Moreover, the occurrence of the literal x

1

can be deleted from clauses since its value will not

satisfy the clause. Applying this reduction yields

(x

2

∨ x

3

) ∧ (x

3

).

Since x

3

occurs as a singleton, it is necessary to assign t(x

3

) = 1 to satisfy the formula. Doing

the reduction produces

(x

2

).

The only truth assignment that satisﬁes this clause is t(x

2

) = 0.

In this example, we set t(x

1

) = 1 and reduced based on this truth assignment. When all clauses

are removed following the preceding rules, we have succeeded in constructing a satisfying truth

assignment. In the preceding reduction, no value has been assigned to the variable x

4

. This

indicates that the value assigned to x

4

is irrelevant; either choice will yield an assignment that

satisﬁes the formula.

Before describing the algorithm in detail, let us consider two more examples; one which fails

to ﬁnd a satisfying truth assignment and one which requires several steps to complete. We

attempt to ﬁnd a satisfying truth assignment for

(x

1

∨ x

2

) ∧ (x

1

∨ x

2

) ∧ (x

1

∨ x

3

) ∧ (x

2

∨ x

3

).

87

88 CHAPTER 16. NP-COMPLETE PROBLEMS

Setting t(x

1

) = 1 yields

(x

2

) ∧ (x

3

) ∧ (x

2

∨ x

3

).

Reducing by setting t(x

2

) = 0 produces the contradictory clauses

(x

3

) ∧ (x

3

).

The reduction fails because there is no assignment that can satisfy both of these clauses. At

this point we conclude that there is no satisfying truth assignment that can be obtained by

setting t(x

1

) to 1. The next step would be to begin the process by setting t(x

1

) = 0 and trying

to ﬁnd a satisfying truth assignment with this assignment to x

1

.

Our ﬁnal example illustrates the need to select the truth value for more than one variable to

produce a satisfying assignment. The result of assigning t(x

1

) = 1 in

(x

1

∨ x

2

) ∧ (x

1

∨ x

3

) ∧ (x

3

∨ x

2

) ∧ (x

4

∨ x

5

)

produces

(x

3

) ∧ (x

3

∨ x

2

) ∧ (x

4

∨ x

5

),

requiring t(x

3

) = 1 in order to satisfy the formula. The eﬀect of this assignment yields

(x

2

) ∧ (x

4

∨ x

5

),

which requires t(x

2

) = 1 in a satisfying truth assignment. The resulting formula

(x

4

∨ x

5

),

matches neither of the termination conditions; the formula is not empty and it does not contain

contradictory clauses. We continue the process by selecting a truth assignment for one of the

variables in (x

4

∨ x

5

) and applying the reduction procedure.

Each reduction process is initiated with the selection of a truth assignment for a variable x

i

.

If the reduction does not produce contradictory clauses, this truth assignment, along with of

those obtained during the reduction, are incorporated into the ﬁnal truth assignment.

To describe the algorithm we let u = w

1

∧ ∧ w

m

be a 2-conjunctive normal form formula

and V = ¦x

1

, . . . , x

n

¦ be the set of variables of u. The objective is to build a satisfying truth

assignment t on V. For a formula v, we use the reduction procedure to build a truth assignment

T on the set of variables in v, denoted var(v). If the reduction does not fail, we then assign

the truth values of T to t.

The algorithm proceeds as follows:

1. Let v = u.

2. Set T(x) ↑ for all x ∈ var(v), and pick a variable x

i

in var(v).

3. Set T(x

i

) = 1 and reduce v.

4. If the reduction did not fail and produced the formula v

, then

- Set t(x) = T(x) for all x ∈ var(v) for which T(x) is deﬁned.

- If v

**is empty, continue with step 8,
**

- otherwise set v = v

**and continue with step 2.
**

5. The reduction for T(x

i

) = 1 has failed. Set T(x

i

) = 0 and reduce v.

6. If the reduction did not fail and produced the formula v

, then

- Set t(x) = T(x) for all x ∈ var(v) for which T(x) is deﬁned.

SOLUTIONS TO EXERCISES 89

- If v

**is empty, continue with step 8,
**

- otherwise set v = v

**and continue with step 2.
**

7. If both reductions failed, the formula is unsatisﬁable.

8. For each x

j

with t(x

j

) ↑, select any truth value for t(x

j

). The truth assignment t satisﬁes

u.

The truth assignment t clearly satisﬁes u, since the reduction removes a clause only when it

has been satisﬁed. Moreover, the algorithm will produce a truth assignment whenever u is

satisﬁable. Assume that u is satisﬁed by some truth assignment t

**. The selection of truth values
**

T(x

i

) = t

(x

i

) in steps 3, 5, and 7 will produce the truth assignment t = t

. Consequently, we

are assured of the construction of some truth assignment whenever u is satisﬁable.

Once a variable is assigned a truth value in step 4 or 6, that value does not change. At most

2n reductions are possible and each reduction can alter at most m clauses. Thus the algorithm

completes in time polynomial to the number of variables and clauses in the formula.

6. A guess-and-check strategy that yields a nondeterministic polynomial time solution to the

minimum set cover problem is straightforward. The guess nondeterministically selects a sub-

collection (

**of ( of size k. The check veriﬁes that every element in S is in the union of the
**

sets in (

.

We show that the Minimum Set Cover Problem is NP-hard by reducing the 3-Satisﬁability

Problem to it. First we will outline the construction of a set S, a collection ( of subsets of

S, and a number k from a 3-conjunctive normal form formula u. We will then show that S is

covered by k sets in ( if, and only if, u is satisﬁable.

To accomplish the reduction, we must reformulate truth assignments and clauses in terms of

sets. Let

u = w

1

∧ ∧ w

m

= (u

1,1

∨ u

1,2

∨ u

1,3

) ∧ ∧ (u

m,1

∨ u

m,2

∨ u

m,3

)

be a 3-conjunctive normal form formula with m clauses. The variables of u are ¦x

1

, . . . , x

n

¦

and the symbol u

j,k

represents the kth literal in clause w

j

.

We will construct the elements of S and the sets of ( in three steps. The sets are built so that

the smallest cover (

**of S consists of 2mn sets from (. We begin with the sets that represent
**

truth assignments. For each variable x

i

, construct the sets

¦x

i,1

, a

i,1

, b

i,1

¦

¦x

i,1

, a

i,2

, b

i,1

¦

¦x

i,2

, a

i,2

, b

i,2

¦

¦x

i,2

, a

i,3

, b

i,2

¦

¦x

i,3

, a

i,3

, b

i,3

¦

¦x

i,3

, a

i,4

, b

i,3

¦

.

.

.

¦x

i,m

, a

i,m

, b

i,m

¦

¦x

i,m

, a

i,1

, b

i,m

¦

There are 2mn sets of this form, 2m for each variable x

i

. The elements x

i,j

, x

i,j

, i = 1, . . . , n

and j = 1, . . . , m, and a

i,1

, . . . , a

i,m

and b

i,1

, . . . , b

i,m

, i = 1, . . . , n are added to the set S.

90 CHAPTER 16. NP-COMPLETE PROBLEMS

The preceding sets are the only sets of ( that contain the elements a

i,1

, . . . , a

i,m

and b

i,1

, . . . , b

i,m

.

For a ﬁxed variable i, we can cover a

i,1

, . . . , a

i,m

and b

i,1

, . . . , b

i,m

with m of these sets. This

is possible only if we choose all the sets that contain x

i,k

or all of the sets that contain x

i,k

.

This choice provides our intuition for a truth assignment. If the cover contains all the sets with

x

i,k

, we associate this with a truth assignment that assigns 1 to x

i,k

. In a like manner, if the

cover contains all the sets with x

i,k

, we associate this with a truth assignment that assigns 0

to x

i,k

.

Now we add elements and sets used to represent the clauses of u. For each clause w

j

,

- If the literal x

i

occurs in w

j

, then construct set ¦x

i,j

, s

1,j

, s

2,j

¦.

- If the literal x

i

occurs in w

j

, then construct set ¦x

i,j

, s

1,j

, s

2,j

¦.

These are the only sets of ( that contain s

1,j

and s

2,j

. Thus at least one of the three sets

generated from w

j

is required for any cover of S.

To this point, the cover that we are building requires mn+m sets: mn from the truth setting

sets and m from the clause sets. Each of the a

i,j

’s, b

i,j

’s, s

1,j

’s, and s

2,j

are covered by such a

selection. If the sets from the truth setting component for a variable x

i

all contain x

i,j

, then

the x

i,j

’s are not covered. Similarly, if the sets from the truth setting component all contain

x

i,j

, then the x

i,j

’s are not covered. We must add more sets so that these elements will be

covered. We will refer to these sets as the completion sets, since they complete the coverage

of the x

i,j

’s or x

i,j

’s

For each variable x

i

and clause w

j

, we add the sets

¦x

i,j

, c

1,k

, c

2,k

¦

¦x

i,j

, c

1,k

, c

2,k

¦

for k = 1, . . . , m(n −1). The elements c

1,k

and c

2,k

appear only in these sets. Thus m(n −1)

completion sets are necessary to cover the c’s

This completes the construction of S and ( from u. From the form of the sets, we see that a

cover of S must contain at least 2mn sets from (. We now must show that u is satisﬁable if,

and only if, there are 2mn sets in ( that cover S.

If u is satisﬁed by a truth assignment t, we can build a cover of S consisting of 2mn sets as

follows. For each variable x

i

,

- If t(x

i

) = 1, then add the mn sets of the form ¦x

i,1

, a

i,k

, b

i,k

¦ to (

.

- Otherwise add the sets ¦x

i,1

, a

i,k

, b

i,k

¦ to (

.

Each clause w

j

is satisﬁed by at least one literal. For one of the satisfying literals we will add a

clause set to (

. If t(x

i

) = 1 and x

i

is in w

j

, then add the set ¦x

i,j

, s

1,j

, s

2,j

¦ to (

. If t(x

i

) = 0

and x

i

is in w

j

, then add ¦x

i,j

, s

1,j

, s

2,j

¦ to (

**. In either case, this set covers s
**

1,j

and s

2,j

and adds one set to (

.

For a clause w

j

that was satisﬁed by a positive literal x

i

in the preceding step, the elements

x

t,j

, where t = i, are not covered by our sets. These are covered by the addition of the m−1

completion sets

¦x

t,j

, c

1,k

, c

2,k

¦,

where t = i and a diﬀerent k is selected for each set. These m−1 sets simultaneously complete

the coverage of the x

t,j

’s and the c

1,k

’s and c

2,k

’s.

SOLUTIONS TO EXERCISES 91

Similarly, for a clause w

j

that was satisﬁed by a negative literal x

i

, the elements x

t,j

, where

t = i, are not covered by the truth setting or clause sets. These are covered by the addition of

the m−1 sets of the form

¦x

t,j

, c

1,k

, c

2,k

¦,

where t = i.

Thus we have a total of mn truth setting sets, m clause sets, and m(n−1) completion sets for

a cover of S by 2mn sets.

Conversely, if S is covered by 2mn sets from (

**. The cover must consist of mn truth setting
**

sets, m linking sets, and m(n − 1) of the completion sets. We can obtain a truth assignment

from the truth setting sets in (

. If the sets ¦x

i,1

, a

i,k

, b

i,k

¦ are in the cover, then t(x

i

) = 0;

otherwise, t(x

i

) = 1.

For each clause w

j

, one set of the form ¦x

i,j

, s

1,j

, s

2,j

¦ or ¦x

i,j

, s

1,j

, s

2,j

¦ is in the cover. In

the former case, the x

i

satisﬁes w

j

. In the latter case, the x

i

is satisﬁes w

j

.

8. We will show that the Longest Path Problem is NP-hard by reducing the Hamiltonian Circuit

Problem to it. The transformation converts a graph G = (N, A) into a graph G

= (N

, A

)

in two steps.

We construct G

**from G by adding three nodes and the arcs incident to these nodes. Let N
**

= ¦v

1

, . . . , v

n

¦ be the set of nodes of G. First we make a copy v

1

of v

1

. For each arc [v

i

, v

1

] in

A, v

i

= v

1

, add an arc [v

i

, v

1

] as illustrated in the diagram below.

v

4

v

3

v

3

v

1

v

1

y x

v

2

v

4

v

2

v

3

v

1

v

1

' v

1

'

v

4

v

2

The construction of G

**is completed by adding nodes x and y and arcs [x, v
**

1

] and [v

1

, y]. The

reduction is complete by selecting the number k = n + 2 as the input in the Longest Path

Problem.

To show that the preceding construction is a reduction, we must show that G has a tour if,

and only if, G

**has an acyclic path of length n + 2.
**

Assume that G has a tour. The tour can be written as a path v

1

, v

i

2

, . . . , v

i

n

, v

1

of length n.

Replacing the v

1

at the end of the path with v

1

and adding the arcs [x, v

1

] and [v

1

, y] creates

an acyclic path of length n + 2 in G

.

Now assume that G

has an acyclic path of length n + 2. Since G

**has n + 3 nodes, this path
**

must visit every node. To reach nodes x and y, the path must begin with the arc [x, v

1

] and

end with [v

1

, y]. Deleting these produces an acyclic path with endpoints v

1

and v

1

. Equating

v

1

and v

1

produces a tour v

1

, v

i

2

, . . . , v

i

n

, v

1

in G.

92 CHAPTER 16. NP-COMPLETE PROBLEMS

9. a) The Multiprocessor Scheduling Problem can be converted into a decision problem by

adding a deadline for the computations as follows:

Input: set of tasks A,

length of task function l : A → N

number of processors k, and

deadline m

Output: yes, if there is a partition A

1

, . . . , A

k

such

that l(A

i

) ≤ m for all i,

no, otherwise.

With the deadline, the problem is to determine if there an assignment of tasks to proces-

sors that ensures that all tasks will be completed by time m.

b) Following the strategy employed in the proof of the NP-completeness of the Bin Packing

Problem, we will reduce the Partition Problem to the Multiprocessor Scheduling Problem.

The reduction transforms an instance of the Partition Problem into an instance of the

Multiprocessor Scheduling Problem. The transformation is accomplished by the mapping

Partition Multiprocessor

Scheduling

Set A A

Function v l = v

Number of sets k = 2

Bound m = v(A)/2

Now we must show that an instance (A, v) of the Partition Problem has a solution if, and

only if, the corresponding instance (A, l, k, m) of the Multiprocessor Scheduling Problem

has a solution.

Assume that there is a subset A

⊆ A such that v(A

**) = v(A)/2. Then the partition
**

A

1

= A

, A

2

= A − A

**is a solution to the corresponding Multiprocessor Scheduling
**

Problem.

Conversely, assume that the Multiprocessor Scheduling Problem has a solution A

1

and

A

2

. Since A = A

1

∪A

2

, l(A) = l(A

1

) +l(A

2

). However, satisfaction of the deadline bound

ensures us that l(A

1

) ≤ l(A)/2 and l(A

2

) ≤ l(A)/2. It follows that l(A

1

) = l(A

2

) = l(A)/2

and A

1

is a solution to the Partition Problem.

10. To show that the Integer Linear Programming Problem is NP-hard, we reduce the question

of the satisﬁability of a 3-conjunctive normal form formula to that of satisfying a system of

integral inequalities. Let

u = w

1

∧ w

2

∧ ∧ w

m

be a 3-conjunctive normal form formula with clauses w

i

= u

i,1

∨ u

i,2

∨ u

i,3

and let V =

¦x

1

, x

2

, . . . , x

n

¦ be the set of Boolean variables occurring in w.

We will consider the literals as variables that may assume integer values. For each pair of

literals x

i

and x

i

in V, the four equations

1. x

i

≥ 0

2. x

i

≥ 0

3. x

i

+x

i

≥ 1

4. −x

i

−x

i

≥ −1

are satisﬁed only if one of x

i

and x

i

is one and the other is 0. A set of inequalities 1–4) is

constructed for each variable x

i

in V. Satisfying the 4n inequalities deﬁnes a truth assignment

for V. For each clause w

i

in u, we deﬁne the inequality

SOLUTIONS TO EXERCISES 93

5. u

i,1

+u

i,2

+u

i,3

≥ 1,

where u

i,j

is the literal in the jth position of clause i. This inequality is satisﬁed only if one

of the literals in the clause is satisﬁed.

An integer linear programming system Ax ≥ b can be constructed from the 4 n inequalities

whose satisfaction deﬁnes a truth assignment for the variables of V and the m inequalities the

represent the clauses. The matrix A has a column for each literal x

i

and its negation x

i

. A

row of A consists of the coeﬃcients of the left-hand side of one of the 4 n + m inequalities.

The column vector b is obtained from the right-hand side of the inequalities. The system Ax

≥ b is solvable if, and only if, the solution vector x deﬁnes a truth assignment that satisﬁes u.

Clearly, a representation of A and b can be constructed in polynomial time from the clause u.

Thus the Integer Linear Programming Problem is NP-hard.

13. Consider an instance of the Bin Packing Problem with items A = ¦a

1

, . . . , a

n

¦, size function

s, and bin size m with the optimal number of bins denoted b

∗

. We will show that the ﬁrst-ﬁt

algorithm generates a packing which requires at most 2b

∗

bins.

Let

T =

n

¸

i=1

s(a

i

),

be the total size of the items in the set A. T can be used to obtain lower bound on the number

of bins in the optimal solution:

b

∗

≥ T/m|.

First we note that at the completion of the ﬁrst-ﬁt algorithm, there is at most one bin that is

less than half full. If a packing produces two bins numbered r and s that are both less than

half full, then that packing could not have been produced by the ﬁrst-ﬁt algorithm. Assume

that packing bin r occurred before packing bin s. When encountering the items in bin s, the

ﬁrst-ﬁt algorithm would have placed these items into bin r. Thus whatever strategy was used

to assign the items, it was not the ﬁrst-ﬁt algorithm.

Now assume that 2T/m| + 1 or more bins are used in the ﬁrst-ﬁt packing. By the argument

in the preceding paragraph, at least 2T/m| are more than half full. Thus the total size of the

contents is greater than

m/22T/m| ≥ T,

which is a contradiction since T is the sum of the size of all the a

i

’s.

Thus we conclude that at most 2T/m| = 2T/m| = 2b

∗

are required in the solution produced

by the ﬁrst-ﬁt algorithm.

15. We will show that the modiﬁcation to the greedy algorithm produces a 2-approximation algo-

rithm for the Knapsack Problem. Let S = ¦a

1

, . . . , a

n

¦ be the set of objects, b the size bound,

and s : S → N and v : S → N be the size and value functions, respectively. Also let c

∗

be the

value of the optimal solution and c

g

be the value produced by the greedy algorithm.

We assume that the items have been sorted by relative value,

v(a

1

)/s(a

1

) ≥ v(a

2

)/s(a

2

) ≥ ≥ v(a

n

)/s(a

n

),

as required by the greedy algorithm and that every item in the list has size at most b. Any

item with size greater than b can be removed from consideration since it cannot appear in a

solution.

If all the items ﬁt into the knapsack, then the optimal solution consists of all the items in S

and this solution is discovered by the greedy algorithm.

94 CHAPTER 16. NP-COMPLETE PROBLEMS

Now we consider the case where

n

¸

i=1

s(a

i

) > b.

Let a

k

be the ﬁrst item not able to be put into the knapsack by the greedy algorithm. Then

k−1

¸

i=1

v(a

i

) ≤ c

g

≤ c

∗

.

The elements a

1

, a

2

, . . ., a

k

provide an optimal solution for a Knapsack Problem with the same

set S but having size bound b

=

¸

k

i=1

s(a

i

). The total value for this problem is

¸

k

i=1

v(a

i

) =

c

. Now c

∗

≤ c

**since any set of items that can be packed into a knapsack with size bound b
**

can also be packed into the larger knapsack with size bound b

.

Let a

∗

denote the item that has the largest value in S. Since a

∗

has the maximum value of

any item in S, it follows that v(a

∗

) ≥ v(a

k

). Combining the inequalities, v(a

∗

) ≥ v(a

k

) and

¸

k−1

i=1

v(a

i

) ≤ c

g

we get

max¦v(a

∗

), c

g

¦

≥ max

¸

v(a

k

),

k−1

¸

i=1

v(a

i

)

¸

≥

v(a

k

) +

k−1

¸

i=1

v(a

i

)

/2

≥

k

¸

i=1

v(a

i

)

/2

≥ c

/2

≥ c

∗

/2

as desired. The third step follows from the inequality

max¦x, y¦ ≥ (x +y)/2

that compares the maximum and the average of two numbers.

Chapter 17

Additional Complexity Classes

2. A three-tape Turing machine can be designed to accept the language ¦a

i

b

i

[ i ≥ 0¦ in loga-

rithmic space. The input is on tape 1 and tapes 2 and 3 serve as counters. Using the binary

representation, the number of a’s that have been read is recorded on tape 2 and the num-

ber of b’s on tape three. A computation consists of two steps: accumulating the counts and

comparing the results. The machine accepting ¦a

i

b

i

[ i ≥ 0¦

1. Reads the input on tape 1 and

- If an a is read after a b, the computation halts and rejects the input.

- On reading an a, the counter on tape 2 is incremented.

- On reading a b, the counter on tape 3 is incremented.

2. After processing the input, the strings on tapes 2 and 3 are compared.

- If the strings are identical, the input is accepted.

- Otherwise, it is rejected.

The worst case performance for a string of length n occurs with the input string a

n

. In this

case, the counter on tape 2 uses lg(n)| + 1 tape squares.

3. The tape bound limits the number of conﬁgurations that can occur in an accepting computa-

tion. By Corollary 17.3.3, we know that the number of transitions is bounded by c

s(n)

, where

c is determined by the number of states and tape symbols of M.

The strategy of employing a counter tape to terminate computations in which the number of

transitions exceeds the bound can be used to construct a Turing machine M

that accepts L

and halts for all inputs. This strategy was described in the solution to Exercise 9 in Chapter

15. The machine M

**is no longer subject to the space bound, but it does demonstrate that L
**

is recursive.

7. We sketch the design of a one-tape deterministic machine M for which every computation with

input of length n > 1 reads exactly 2

n

tape squares. The input alphabet for M is ¦a¦.

For an input string a

n

, the computation begins with tape conﬁguration Ba

n

B. The computa-

tion begins by writing a # in tape position 0 and reading the input. If the input string has

length 0, 1, or 2. The computation terminates after reading the entire input string. For input

of length 2, the computation reads the initial blank, the input aa, and the following blank

using 4 tape squares as required.

For computations with input a

n

, n > 2, we construct a loop that iteratively doubles the length

of a string produced on previous iteration. The objective is to construct a string of length 2

n

95

96 CHAPTER 17. ADDITIONAL COMPLEXITY CLASSES

on the tape beginning at position 0. Since this string must overwrite the input string, we will

use the additional tape symbols to diﬀerentiate the input from the generated string.

The string beginning at position 0 that has length 4, 8, 16, and so forth will consist of #, b’s,

c’s and X’s and be referred to as the generated string. Each time we double the generated

string, we will mark one input symbol as processed. The c’s represent unprocessed input that

are already in the generated string and the a’s represent unprocessed whose tape positions

have not already been incorporated into the string. After all the a’s have been changed to b’s

or c’s, the generated string is expanded by adding X’s.

The computation will halt when all the input symbols have been processed. The meanings

associated with the tape symbols are

# left tape boundary marker

a, a

**unprocessed input symbol
**

c, c

**unprocessed input symbol in the generated string
**

b, b

**processed input symbol
**

X, X

**elements added to the generated string beyond the n −1st
**

tape position

Y elements used in the copying

The symbols with primes are used in the extension of the generated string.

As previously noted, the computation begins by writing a # in tape position 0 and reading

the input. For input of length greater than 2, the ﬁnal a is erased and the head returns to

tape position 0. The ﬁrst a in the input is then changed to b and the second a is changed to a

c. If the next symbol is blank, an X is written. Otherwise, the next a is also changed to a c.

This produces the tape conﬁguration #bcX for an input string of length 3 and #bcca

n−4

for

an input of length 4 or more.

The # and b indicate that two symbols have been processed and the length of the generated

string is 4.

The loop that generates the string proceeds as follows:

(a) The tape head moves to the right to ﬁnd an a or c. If none is found, the computation

halts.

(b) The tape head returns to position 0.

(c) One symbol at a time, each symbol b, c, X in the generated string is replaced by a b

, c

,

X

**and a symbol is added to the end of the generated string.
**

- If an a is encountered, it is changed to an a

.

- If a blank is encountered, it is changed you Y .

(d) After writing the ﬁnal Y or a

**, the tape head returns to position 0. On this pass it changes
**

Y and X

to X, a

and c

to c, and b

to b.

(e) The ﬁrst occurrence of an a or c is changed to a b.

(f) The tape head returns to position 0 and the computation continues with step a).

For example, for input aaaaa, the tape conﬁgurations at the beginning of each loop are

#bccaBBBBBBBBBBBB

16

#bbccXXXBBBBBBBBB

16

#bbbcXXXXXXXXXXXB

16

#bbbbXXXXXXXXXXXX

16

SOLUTIONS TO EXERCISES 97

12. Doubling the amount of available tape does not change the set of languages that can be

accepted. We show that a language accepted by a Turing machine M whose computations

require at most 2s(n) tape squares can also be accepted by Turing machine M

with tape

bound s(n). By Theorem 17.2.2, we can restrict our attention to Turing machines with one

work tape.

Let M = (Q, Σ, Γ, δ, q

0

, F) be a Turing machine whose computation have a 2s(n) tape bound.

The input and tape alphabets Σ

and Γ

of M

are

Σ

= Σ, and

Γ

= ¦B¦ ∪ (Γ Γ ¦1, 2¦).

Since M

is designed to accept L(M), the input alphabets of M and M

**are identical. The
**

tape alphabet of M

**has triples [x, y, i] with x, y ∈ Γ and i ∈ ¦1, 2¦. The reduction in amount
**

of tape required by the computations of M

**is achieved by the storing more information in a
**

single tape symbol; a tape symbol of M

**contains two symbols from M. The integer value in
**

the ordered triple indicates which of the two symbols from Γ would be scanned by M.

When M

reads a B on the work tape, M

**interprets the blank as the triple [B, B, 1] and
**

the transition converts it into a triple. The transition function δ

of M

**is obtained from the
**

transition function δ of M as follows:

Transition of M Transitions of M

δ(q

i

, a, x) = [q

j

; b, d; z, R] δ

(q

i

, a, [x, y, 1]) = [q

j

; b, d; [z, y, 2], S]

δ

(q

i

, a, [y, x, 2]) = [q

j

; b, d; [y, z, 2], R]

δ(q

i

, a, x) = [q

j

; b, d; z, L] δ

(q

i

, a, [x, y, 1]) = [q

j

; a, d; [z, y, 1], L]

δ

(q

i

, a, [y, x, 2]) = [q

j

; b, d; [y, z, 1], S]

δ(q

i

, a, B) = [q

j

; b, d; z, R] δ

(q

i

, a, B) = [q

j

; b, d; [z, B, 2], S]

δ(q

i

, a, B) = [q

j

; b, d; z, L] δ

(q

i

, a, B) = [q

j

; b, d; [z, B, 1], L]

where x, y, z are elements of Γ, d ∈ ¦L, R¦, and S indicates that the tape head remains

stationary in the transition.

The computation of M

**with an input string u simulates that of M with u but reads at most
**

s(n) tape squares on the work tape.

15. The NP-hardness of a {-space complete language follows directly from the deﬁnition of {-

Space and the inclusions shown in Figure 17.4. Let Q be a {-Space complete language. This

means that Q is in {-Space and that every language in {-Space is reducible to Q in polynomial

time. Since ^{ is a subset of {-space, it follows that every language in ^{ is reducible to Q

in polynomial time and, consequently, Q is NP-hard.

98 CHAPTER 17. ADDITIONAL COMPLEXITY CLASSES

Chapter 18

Parsing: An Introduction

2. The subgraph of the graph of the grammar G consisting of all leftmost derivations of length

three or less is

Level

AB S

aS

1 0

aB aBB

aba

B

ba

BB BBB

baB

abAB

ababAB

ababB

aaS

aaaS

aaAB

aaB

aAB

aabAB

aabB

abB

abBB

abba

2

3

5. The search tree created by Algorithm 18.2.1 while parsing ((b)) is given below. The nodes on

each level are listed in the order of their generation.

99

100 CHAPTER 18. PARSING: AN INTRODUCTION

S —A

A + T

(A + T)

(A) + T

T + T + T

A + T + T + T

b + T

A + T + T

T + T

T

b

(b)

(A)

(T)

((A))

((T)) ((b))

((A + T))

(T + T)

(T) + T

(A + T) + T

(A) + T + T

T + T + T + T

A + T + T + T + T

(A + T + T)

b + T + T

(b + T)

((A) + T)

((A)) + T

(T + T + T)

(A + T + T + T)

(b) + T

(T + T) + T

(A + T + T) + T

(T) + T + T

(A + T) + T + T

b + T + T + T

(A) + T + T + T

T + T + T + T + T

A + T + T + T + T + T

The derivation of ((b)) is obtained by following the arcs from the root S to the goal node ((b)).

Derivation Rule

S ⇒ A S → A

⇒ T A → T

⇒ (A) T → (A)

⇒ (T) A → T

⇒ ((A)) T → (A)

⇒ ((T)) A → T

⇒ ((b)) T → b

11. Algorithm 18.4.1 will not terminate when run with input string aa and grammar

S → A [ a [ aa

A → S

The rule S → a establishes Sa as the result of the ﬁrst reduction for the string aa. The parse

proceeds by alternating reductions with the rule A → S and the rule S → A.

Chapter 19

LL(k) Grammars

2. a) The lookahead sets are given for the rules of the grammar. The lookahead set for a

variable A is the union of the lookahead sets of the A rules.

Rule Lookahead set

S → ABab ¦abab, acab, aab, cbab, ccab, cab¦

S → bAcc ¦bacc, bccc¦

A → a ¦abab, acab, aab, acc¦

A → c ¦cbab, ccab, cab, ccc¦

B → b ¦bab¦

B → c ¦cab¦

B → λ ¦ab¦

3. b) The FIRST

1

and FOLLOW

1

sets for the variables of the grammar are

Variable FIRST

1

FOLLOW

1

S a, # λ

A a a, #

B a #

To determine whether the grammar is LL(1), the techniques outlined in Theorem 19.2.5

are used to construct the lookahead sets from the FIRST

1

and FOLLOW

1

sets.

Rule Lookahead set

S → AB# ¦a, #¦

A → aAb ¦a¦

A → B ¦a, #¦

B → aBc ¦a¦

B → λ ¦#¦

Since the lookahead sets for the alternative A rules both contain the symbol a, the gram-

mar is not strong LL(1).

6. a) Algorithm 19.4.1 is used to construct the FIRST

2

sets for each of the variables of the

grammar

S → ABC###

A → aA [ a

B → bB [ λ

C → cC [ a [ b [ c.

101

102 CHAPTER 19. LL(K) GRAMMARS

The rules of the grammar produce the following assignment statements for step 3.2 of the

algorithm.

F(S) := F(S) ∪ trunc

2

(F

(A)F

(B)F

(C)¦cc¦)

F(A) := F(A) ∪ trunc

2

(¦a¦F

(A)) ∪ ¦a¦)

F(B) := F(B) ∪ trunc

2

(¦b¦F

(B))

F(C) := F(C) ∪ trunc

2

(¦c¦F

(C)) ∪ ¦a, b, c¦

The algorithm is traced by exhibiting the sets produced after each iteration.

Step F(S) F(A) F(B) F(C)

0 ∅ ∅ ¦λ¦ ∅

1 ∅ ¦a¦ ¦λ, b¦ ¦a, b, c¦

2 ¦aa, ab, ac¦ ¦a, aa¦ ¦λ, b, bb¦ ¦a, b, c, ca, cb, cc¦

3 ¦aa, ab, ac¦ ¦a, aa¦ ¦λ, b, bb¦ ¦a, b, c, ca, cb, cc¦

The rules that contain a variable on the right-hand side generate the assignment state-

ments used in steps 3.2.2 and 3.2.3 of Algorithm 18.5.1. The rules S → ABC##,

B → bB, and C → cC produce

FL(C) := FL(C) ∪ trunc

2

(¦##¦FL

(S))

= FL(C) ∪ ¦##¦

FL(B) := FL(B) ∪ trunc

2

(FIRST

2

(C)¦##¦FL

(S))

= FL(B) ∪ ¦a#, b#, c#, ca, cb, cc¦

FL(A) := FL(A) ∪ trunc

2

(FIRST

2

(B)FIRST

2

(C)¦##¦FL

(S))

= FL(A) ∪ ¦a#, b#, c#, ba, bb, bc¦

FL(B) := FL(B) ∪ FL

(B)

FL(C) := FL(C) ∪ FL

(C)

The last two assignment statements may be omitted from consideration since they do not

contribute to the generation of FL(B) and FL(C).

Step FL(S) FL(A) FL(B) FL(C)

0 ¦λ¦ ∅ ∅ ∅

1 ¦a#, b#, c#, ba, bb, bc¦ ¦a#, b#, c#, ca, cb, cc¦ ¦##¦ ∅

2 ¦a#, b#, c#, ba, bb, bc¦ ¦a#, b#, c#, ca, cb, cc¦ ¦##¦ ∅

The length two lookahead sets are used to construct the FIRST

2

and FOLLOW

2

sets.

Rule Lookahead set

S → ABC## ¦aa, ab, ac¦

A → aA ¦aa¦

A → a ¦aa, ab, ac¦

B → bB ¦ba, bb, bc¦

B → λ ¦a#, b#, c#, ca, cb, cc¦

C → cC ¦ca, cb, cc¦

C → a ¦a#¦

C → b ¦b#¦

C → c ¦c#¦

SOLUTIONS TO EXERCISES 103

The lookahead sets of the A rules show that the grammar is not strong LL(2).

7. To establish part (3) of Lemma 19.2.2, we must show that the identity

FIRST

k

(au) = ¦av [ v ∈ FIRST

k−1

(u)¦

holds for all k ≥ 1. For convenience we let FIRST

0

(u) = ¦λ¦ for all strings u. Let v ∈

FIRST

k−1

(u) for some k ≥ 1. This implies that there is a derivation u

∗

⇒ vx where vx ∈ Σ

∗

,

length(v) = k − 1 or length(v) < k − 1 and x = λ. Consequently, au

∗

⇒ avx and av is in

FIRST

k

(au).

The preceding argument shows that FIRST

k

(au) ⊆ ¦a¦FIRST

k−1

(u). We must now establish

the opposite inclusion. Let av ∈ FIRST

k−1

(au). Then au

∗

⇒ avx where length(v) = k −1 or

length(v) < k − 1 and xλ. The derivation u

∗

⇒ vx, obtained by deleting the leading a from

the preceding derivation, shows that v ∈ FIRST

k

(u).

8. We will use a proof by contradiction to show every LL(k) grammar is unambiguous. Let G

be an LL(k) grammar and assume that G is ambiguous. Then there is some string z that has

two distinct leftmost derivations. Let S

∗

⇒ uAv, u ∈ Σ

∗

, be the initial subderivation that is

identical in both derivations of z. The two derivations can be written

1) S

∗

⇒ uAv

∗

⇒ ux

1

v

∗

⇒ uw = z

2) S

∗

⇒ uAv

∗

⇒ ux

2

v

∗

⇒ uw = z.

The derivations continue by applying rules A → x

1

and A → x

2

to the variable A.

These derivations show that the string trunc

k

(w) is in both LA

k

(uAv, A → x

1

) and LA

k

(uAv, A →

x

2

). This is a contradiction; the sets LA

k

(uAv, A → x

1

) and LA

k

(uAv, A → x

2

) are disjoint

since G is an LL(k) grammar. Thus our assumption, that G is ambiguous, must be false.

The exercise asked us to show that every strong LL(k) grammar is unambiguous. This follows

immediately since the strong LL(k) grammars are a subfamily of the LL(k) grammars.

9. a) The lookahead sets for the rules of G

1

are

Rule Lookahead set

S → aSb ¦a

j

a

i

c

i

b

j

[ j > 0, i ≥ 0¦

S → A ¦a

i

c

i

[ i ≥ 0¦

A → aAc ¦a

i

c

i

[ i > 0¦

A → λ ¦λ¦

For any k > 0, the string a

k

is a lookahead string for both of the S rules. Thus G

1

is not

strong LL(k).

A pushdown automaton that accepts L(G

1

) is deﬁned by the following transitions.

δ(q

0

, a, λ) = [q

1

, A]

δ(q

1

, a, λ) = [q

1

, A]

δ(q

1

, b, A) = [q

2

, λ]

δ(q

1

, c, A) = [q

3

, λ]

δ(q

2

, b, A) = [q

2

, λ]

δ(q

3

, b, A) = [q

2

, λ]

δ(q

3

, c, A) = [q

3

, λ]

States q

0

and q

1

read an a and push A onto the stack. State q

3

reads the c’s and q

2

the

b’s with each transition popping the stack. The accepting states are q

0

and q

2

.

104 CHAPTER 19. LL(K) GRAMMARS

11. a) The process of transforming the grammar into an equivalent strong LL(1) grammar begins

by removing the directly left recursive A rules, producing

S → A#

A → aB [ ZaB

B → bBc [ λ

Z → bZ [ cZ [ b [ c

The resulting grammar is not strong LL(1) since there are two Z rules that begin with b

and two that begin with c. Left factoring these rules we obtain

S → A#

A → aB [ ZaB

B → bBc [ λ

Z → bX [ cY

X → Z [ λ

Y → Z [ λ

Examining the length one lookahead sets, we see that this grammar is strong LL(1).

Rule Lookahead set

S → A# ¦a, b, c¦

A → ab ¦a¦

A → Zab ¦b, c¦

B → bBc ¦b¦

B → λ ¦#¦

Z → bX ¦b¦

Z → cY ¦c¦

X → Z ¦b, c¦

X → λ ¦a¦

Y → Z ¦b, c¦

Y → λ ¦a¦

13. a) The lookahead sets for the grammar G

S → aAcaa [ bAbcc

A → a [ ab [ λ

are

Rule Lookahead set

S → aAcaa ¦aacaa, aabcaa, acaa¦

S → bAbcc ¦babcc, babbcc, bbcc¦

A → a ¦acaa, abcc¦

A → ab ¦abcaa, abbcc¦

A → λ ¦caa, bcc¦

One symbol lookahead is suﬃcient to discriminate between the S rules. Four symbols are

required to choose the appropriate A rule. Thus G is strong LL(4).

To show that G is LL(3), the lookahead sets are constructed for the sentential forms of

the grammar.

Rule Sentential form Lookahead set

S → aAcaa S ¦aaccaa, aabcaa, acaa¦

SOLUTIONS TO EXERCISES 105

S → bAbcc S ¦babcc, babbcc, bbcc¦

A → a aAcaa ¦acaa¦

A → ab aAcaa ¦abcaa¦

A → λ aAcaa ¦caa¦

A → a bAbcc ¦abcc¦

A → ab bAbcc ¦abbcc¦

A → λ bAbcc ¦bcc¦

14. For every i > 0, the string a

i

is a preﬁx of both a

i

and a

i

b

i

.

A nondeterministic pushdown automaton that accepts ¦a

i

[ i ≥ 0¦ ∪ ¦a

i

b

i

[ i ≥ 0¦ was

given in Example 7.1.2. A deterministic approach to recognizing strings of this form uses an

endmarker # on the strings. The computation reads leading a’s and pushes an A on the stack

in state q

0

until either the endmarker or a b is read. If the endmarker is read, the stack is

popped and the string is accepted. If a b is read, a loop is entered that compares the number

of b’s with the number of A’s stored on the stack. The pushdown automaton deﬁned by the

transitions

δ(q

0

, a, λ) = ¦[q

0

, A]¦

δ(q

0

, #, λ) = ¦[q

1

, λ]¦

δ(q

0

, b, A) = ¦[q

2

, λ]¦

δ(q

1

, λ, A) = ¦[q

1

, λ]¦

δ(q

2

, b, A) = ¦[q

2

, λ¦

δ(q

2

, #, λ) = ¦[q

2

, λ¦

with accepting states q

1

and q

2

accepts the language.

15. It follows immediately from the deﬁnitions of LL(1) and strong LL(1) grammars that a strong

LL(1) grammar G is also LL(1). To conclude that these families of grammars are identical, we

must show that every LL(1) grammar is also strong LL(1).

Let G = (V, Σ, S, P) be an LL(1) grammar and assume that it is not strong LL(1). Since G

is not strong LL(1), there are derivations

S

∗

⇒ u

1

Az

1

⇒ u

1

xz

1

∗

⇒ u

1

v

1

z

1

∗

⇒ u

1

v

1

w

1

S

∗

⇒ u

2

Az

2

⇒ u

2

yz

2

∗

⇒ u

2

v

2

z

2

∗

⇒ u

2

v

2

w

2

where

a) u

i

, v

i

, and w

i

are in Σ

∗

, z

i

∈ (Σ ∪ V)

∗

,

b) u

1

v

1

w

1

= u

2

v

2

w

2

,

c) A → x and A → y are distinct rules of G, and

d) FIRST

1

(v

1

w

1

) = FIRST

1

(v

2

w

2

).

That is, the FIRST sets do not distinguish between the rule A → x and A → y.

We will use the preceding derivations to show that the original grammar G is not LL(1). But

this is a contradiction, so our assumption that G is not strong LL(1) must be false. There are

two cases to consider: when v

1

= v

2

= λ and when at least one of v

1

or v

2

is not λ.

106 CHAPTER 19. LL(K) GRAMMARS

Case 1: v

1

= v

2

= λ. In this case, both of the rules A → x and A → y initiate derivations of

the form A

∗

⇒ λ. Using these subderivations, we have two distinct derivations

S

∗

⇒ u

1

Az

1

⇒ u

1

xz

1

∗

⇒ u

1

z

1

∗

⇒ u

1

w

1

S

∗

⇒ u

1

Az

1

⇒ u

1

yz

1

∗

⇒ u

1

z

1

∗

⇒ u

1

w

1

of the string u

1

w

1

. Since the lookahead sets LA

1

(u

1

Az

1

, A → x) and LA

1

(u

1

Az

1

, A → y) are

not disjoint, G is not LL(1).

Case 2: At least one of v

1

or v

2

is not λ. Without loss of generality, assume that v

1

= λ. From

d) above we note that

FIRST

1

(v

1

w

1

) = FIRST

1

(v

1

) = FIRST

1

(v

2

w

2

).

We can build derivations

S

∗

⇒ u

2

Az

2

⇒ u

2

xz

2

∗

⇒ u

2

v

1

z

2

∗

⇒ u

2

v

1

w

2

S

∗

⇒ u

2

Az

2

⇒ u

2

yz

2

∗

⇒ u

2

v

2

z

2

∗

⇒ u

2

v

2

w

2

,

which demonstrate that LA

1

(u

2

Az

2

, A → x) and LA

1

(u

2

Az

2

, A → y) have the same FIRST

1

set and consequently G is not LL(1).

In both cases, we have shown that a grammar that is strong LL(1) is also LL(1). Combining

this with inclusion of the familty of LL(1) grammars in the family of strong LL(1) grammars

yields the equality of these families of grammars.

Chapter 20

LR(k) Grammars

1. a) The LR(0) contexts for the rules of G

S → AB

A → aA [ b

B → bB [ a

are obtained from the rightmost derivations

S ⇒ AB

i

⇒ Ab

i

B ⇒ Ab

i

a

j

⇒ a

j

Ab

i

B ⇒ a

j

bb

i

a

Rule LR(0) contexts

S → AB ¦AB¦

A → aA ¦a

i

A [ i > 0¦

A → b ¦a

i

b [ i ≥ 0¦

B → bB ¦Ab

i

B [ i > 0¦

B → a ¦Ab

i

a [ i ≥ 0¦

The nondeterministic LR(0) machine of G is constructed directly from the speciﬁcations

of Deﬁnition 20.3.2.

107

108 CHAPTER 20. LR(K) GRAMMARS

q

0 S→ .AB

λ

A→ .aA

λ

λ

λ

A→ a.A

λ

A→ .b A→ b.

b

B

B

A

a

A

a

b

S→ A.B S→ AB.

B→ bB.

B→ a. B→ b.B

B→ .a B→ .bB

λ

λ

λ

λ

A→ aA.

The deterministic LR(0) machine of G is

q

0

S→ .AB

A→ .aA

A→ .aA

A→ a.A

A→ .b

A→ .b

A→ b.

b

b

a

a

a

A

A

b b

S→ A.B

B

B

S→ AB.

B→ bB.

B→ a.

B→ b.B

B→ .a

B→ .a

B→ .bB

B→ .bB

A→ aA.

a

Since every state with a complete item contains only that item, the grammar G is LR(0).

SOLUTIONS TO EXERCISES 109

3. The grammar AE is not LR(0) since A is an LR(0) context of the rule S → A and a viable

preﬁx of the rule A → A+T.

7. a) The nondeterministic LR(1) machine is constructed for the grammar

S → Ac

A → BA [ λ

B → aB [ b

The λ-transitions from the item [S → .Ac, ¦λ¦] generate LR(1) items with lookahead set

¦c¦ since the symbol c must follow all reductions that produce A. Similarly the lookahead

set ¦a, b, c¦ is generated by λ-transitions from [A → .BA, ¦c¦] since the string that follows

B, A in this case, generates a, b and λ.

[S→ .Ac, {λ}]

[A→ ., {c}]

[S→ A.c, {λ}] [S→ Ac., {λ}]

[B→ .aB, {a,b,c}] [A→ .BA, {c}]

[A→ B.A, {c}]

[A→ BA., {c}]

A

a

b

c

A

λ

λ

λ λ

λ

λ

λ

λ

B

B

q

0

[B→ .b, {a,b,c}]

[B→ .b, {a,b,c}]

[B→ a.B, {a,b,c}]

[B→ aB., {a,b,c}]

The corresponding deterministic LR(1) is

110 CHAPTER 20. LR(K) GRAMMARS

[S→ .Ac, {λ}]

[A→ ., {c}]

[A→ ., {c}]

[S→ A.c, {λ}]

[A→ .BA, {c}]

[S→ Ac., {λ}]

[B→ .aB, {a,b,c}]

[B→ .aB, {a,b,c}]

[B→ .aB, {a,b,c}]

[A→ .BA, {c}]

[A→ B.A, {c}]

[A→ BA., {c}]

A

a

a

b

b

c

A

B

B

B

q

0

[B→ .b, {a,b,c}]

[B→ .b, {a,b,c}]

[B→ .b, {a,b,c}]

[B→ .b, {a,b,c}]

[B→ a.B, {a,b,c}]

[B→ aB., {a,b,c}]

a

Since the deterministic machine satisﬁes the conditions of Deﬁnition 20.5.3, the grammar

is LR(1).

Preface

This solution manual was written to accompany the third edition of Languages and Machines: An Introduction to the Theory of Computer Science. It contains complete solutions to approximately 200 exercises from the text, including the “starred” exercises. Acquiring a thorough background in and mastery of the foundations of computer science is not a spectator sport, active participation is required. This participation takes several forms; putting forth the eﬀort to understand the theorems and their proofs, following the techniques used to solve the problems in the examples, and solving problems on your own. It is the ability to do the latter that exhibits proﬁciency with the material. These solutions have been provided as a tool to assist students in developing their problem solving skills. The exercises in this manual were selected to complement the presentation in the text. The solutions illustrate general techniques for solving the particular type of problem or extend the theoretical development of a topic. Understanding the strategies employed in these solutions should provide valuable insights that can be used in solving many of the other exercises in the text. Thomas Sudkamp Dayton, Ohio

Copyright 2005: Thomas Sudkamp All rights reserved. No part of this is to be reproduced, stored in a retrieval system, or transmitted in any form without the prior written permission of the author or the Addison-Wesley Publishing Co. i

ii

N P.Contents 1 Mathematical Preliminaries 2 Languages 3 Context-Free Grammars 4 Normal Forms for Context-Free Grammars 5 Finite Automata 6 Properties of Regular Languages 7 Pushdown Automata and Context-Free Languages 8 Turing Machines 9 Turing Computable Functions 10 The Chomsky Hierarchy 11 Decision Problems and the Church-Turing Thesis 12 Undecidability 13 µ-Recursive Functions 14 Time Complexity 15 P. and Cook’s Theorem 16 NP-Complete Problems 17 Additional Complexity Classes 18 Parsing: An Introduction 19 LL(k) Grammars 20 LR(k) Grammars 1 7 11 19 25 31 37 45 53 59 63 67 73 79 83 87 95 99 101 107 iii .

iv .

6.3. 13. This implies that a ∈ X and a ∈ Y. To prove that ≡p is an equivalence relation. Consequently. and onto but not the identity. symmetric. 1 . It is not a total function from N to N. A completely analogous argument can be used to establish the equality of the sets (X ∩ Y) and X ∪ Y. one-to-one. ii) Now assume that a ∈ (X ∪ Y). since it is not deﬁned for odd natural numbers. a ∈ X and a ∈ Y. which explicitly considers the case when p = 2.1. d) The function f (n) = n/2 ↑ if n is even otherwise maps the even natural numbers onto the entire set of natural numbers. i) Reﬂexivity: For every natural number n. n mod p = n mod p. This is shown using the same argument given in Example 1. we must show that it is reﬂexive. it is not onto since the range is the set of even numbers. in terms of union. b) The function f (n) = 0 n−1 if n = 0 otherwise is total and onto. It is not one-to-one since f (0) = f (1) = 0. It follows that a ∈ (X ∪ Y). i) Let a ∈ X ∩ Y. a) The function f (n) = 2n is total and one-to-one. a ∈ (X ∪ Y). c) The function 1 0 f (n) = n if n = 0 if n = 1 otherwise is total. By the deﬁnition of intersection. a ∈ X ∩ Y. Part (i) shows that X ∩ Y is a subset of (X ∪ Y). To prove that X ∩ Y = (X ∪ Y) requires establishing both of the inclusions X ∩ Y ⊆ (X ∪ Y) and (X ∪ Y) ⊆ X ∩ Y. Then a ∈ X ∪ Y. while (ii) establishes the inclusion (X ∪ Y) ⊆ X ∩ Y. and transitive. However.Chapter 1 Mathematical Preliminaries 5. Thus a ∈ X and a ∈ Y or.

by commutativity. then m mod p = n mod p. contradicting the assumption that f0 . ii) Symmetry: If [m. Clearly f (i) = fi (i) for any i. The equivalence classes of ≡p are the sets consisting of natural numbers that are equal mod p: [0]≡p = {0.49999 . the number 1 is represented by both .x0 x1 x2 . . . This representation deﬁnes a one-to-one correspondence between the rational numbers and the set N × (N − {0}). which follows from the commutativity of addition. . and . Since fi (i) > 0. [p − 1]≡p = {p − 1. i) Reﬂexivity: To demonstrate reﬂexivity.} [1]≡p = {1. fn . The latter set is known to be countable by Theorem 1. 15.} . p. then m + k = n + j. 1] can be expressed by an inﬁnite decimal . 3p. . n] by ≡ is m + n = n + m. The requirement for [m. we must show that every ordered pair [m. . n] ≡ [j. Subtracting j + k from each side yields m + t = n + s. . m]. n] is related to itself. showing that [m. we obtain m + k + j + t = n + j + k + s. Deﬁne a function f as follows: f (0) = f0 (0) + 1 f (i) = fi (i) + f (i − 1) for i > 0.xn . Diagonalization is used to prove that there are an uncountable number of monotone increasing functions. fn .4. k] ≡ [s. . . . .50000 . then n mod p = k mod p. we ﬁrst observe that every real number in (0. . p + 1. To obtain a 2 . exhaustively enumerates the monotone increasing functions. iii) Transitivity: If n mod p = m mod p and m mod p = k mod p. . 22. Now we will show that there is at least one (in fact there are inﬁnitely many) monotone increasing function that is not in the listing. . t] as desired. . . n]. MATHEMATICAL PRELIMINARIES ii) Symmetry: If n mod p = m mod p.} [2]≡p = {2. With such a representation. . f2 .}. . iii) Transitivity: [m. . . .. 3p + 1. a] is uncountable. n] ≡ [s.4. Then these functions can be listed in a sequence f0 . 3p − 1. t] imply m + k = n + j and j + t = k + s. . . j + n = k + m and [j. p + 2. . . To show that the set of real numbers in the interval [0. 2p + 1. k] and [j. . . 18. Assume that the set of monotone increasing functions is countable. 4p − 1. . f1 . . it follows that f (i) > f (i − 1) for all i and f is monotone increasing. The set of non-negative rational numbers is deﬁned by {n/m | n ∈ N. m ∈ N − {0}} A rational number n/m can be represented by the ordered pair [n. . k]. . . . . f1 . Adding the second equality to the ﬁrst.2 CHAPTER 1. . . . k] ≡ [m. . 2p − 1. n] ≡ [j. n] to be related to [m. 3p + 2. Thus the assumption that the monotone increasing functions comprise a countable set leads to a contradiction and we conclude that the set is uncountable. 2p. 2p + 2. Again. 25. .

. . .. xi . 28. . Before proving the Schr¨der-Bernstein Theorem. Let the decimal expansion of r n be denoted . . card(A) ≤ card(B) means there is a one-to-one function f from A into B. card(A) ≤ card(B). . and we conclude that the set is uncountable. . By deﬁnition. r0 = x00 r1 = x10 r2 = x20 . The function h demonstrates that A and B have the same cardinality. is deﬁned using the diagonal elements in the array formed by the xii ’s as follows: xi = 2 1 if xii = 1 otherwise. This implies that there is a sequence r0 . . . .. . . that contains all of the real numbers in the interval (0. The diagram f B A . . the ith row of which consists of the expansion of ri . Therefore the assumption that the enumeration contains all real numbers in (0. .xn0 xn1 xn2 .. . A real number r = x0 x1 . r n . 1] fails. and B ⊆ A. . . The enumeration given above is used to construct an inﬁnite two-dimensional array. x01 x11 x21 . . we consider only decimal expansions that do not end with an inﬁnite sequence of zeros. Clearly r = ri for any i since the ith position of r. r 2 . we consider the special case where card(B) ≤ o card(A). . We will use f to construct a one-to-one function h : A → B from A onto B. With the unique decimal representation. is not identical to the ith position of ri . 1]. x02 x12 x22 . Assume the set of real numbers in (0. two numbers are distinct if they diﬀer at any position in the decimal expansion.SOLUTIONS TO EXERCISES 3 unique representation.. . 1] is countable. . . .. The relationship card(B) ≤ card(A) follows immediately from the inclusion B ⊆ A since the identity function id : B → A is a one-to-one function from B into A. r 1 . ..

Since x ∈ C. Because h is a one-to-one function from A to B. Although you will be familiar with functional composition from previous mathematical studies. . The function f is deﬁned for all elements in A (which includes all elements of B) and the values of f . must all be in B. each element of B is ‘hit’ by the mapping h. . A−B Now deﬁne the function h : A → B as follows: h(z) = f (z). . and . then s = t and x = y and we are done. y ∈ C. . If m = n. h(x) = x and x is covered by the mapping. Then x = h(x) = h(y) = y. we conclude that card(A) = card(B). x = f m (s) and y = f n (t) for some s. . f (x). There are four cases to consider.4 CHAPTER 1. Case 4: x. f i (x).4 of the text if you need a reminder. If x ∈ B and x ∈ C. Then h(x) = f (f m (s)) = h(y) = f (f n (t)). Let Im(Y) = {x ∈ X | x = g(y) for some y in Y} be the image of Y in X under g. except for x itself which is in A − B. t ∈ A − B. To show that h is a one-to-one. otherwise. if z ∈ C. A similar argument shows x = y when m < n. Same argument as case 2. For each x ∈ B but not in C. Case 2: x ∈ C and y ∈ C. Let f i denote the composition of f with itself i times. Since x and y are both in C. . Now . we must prove that h(x) = h(y) implies that x = y. Case 3: x ∈ C and y ∈ C. there are one-to-one functions f : X → Y and g : Y → X. and f 0 denote the identity function. y ∈ C. indicated by the heads of the arrows. Applying the function f n to both sides we get f m (s) = f n (t). By the assumption. The proof uses the fact that the composition of one-to-one functions is oneto-one. Every element in ch(x) is in B. For each x ∈ A − B. . But h(x) = h(y) = y. Case 1: x. we deﬁne the set ch(x) = {x. . To prove the Schr¨der-Bernstein Theorem in its generality. x = y. a description and formal deﬁnition are given in Section 9. or equivalently. then x = f (t) for some t ∈ C because each element in C is either in A − B or obtained by the result of applying f to an element in C. which is a contradiction. f (f (x))).Im(Y) is a subset of X. z. . Thus h(x) cannot equal h(y) in this case. This implies that y ∈ C.}. Then f m−n (s) = t.the composition f ◦ g is a one-to-one mapping from X into Im(Y). Consequently. Assume that m > n. We now show that h maps A onto B. h(x) = f (x) is in C. we must show that card(X) ≤ o card(Y) and card(Y) ≤ card(X) implies card(X) = card(Y) for arbitrary sets X and Y. Let C= x∈ ch(x). MATHEMATICAL PRELIMINARIES illustrates the mapping f .Im(Y) has the same cardinality as Y (g is one-to-one and onto).

Let L be the set of the points in N × N on the line deﬁned by n = 3 · m. for all values k = 1. Basis: if n = 0 then m · n = 0 Recursive step: m · s(n) = m + (m · n) Closure: m · n = k only if this equality can be obtained from m · 0 = 0 using ﬁnitely many applications of the recursive step. The ﬁrst rule in the recursive step generates all sets containing a single natural number. . n] ∈ L only if it can be obtained from [0. We prove. Closure: A set X is in F only if it can be obtained from the basis elements by a ﬁnite number of applications of the recursive step. that n 2i = 2n+1 − 1 i=0 Basis: The basis is n = 0. n. {0} ∈ F Recursive step: If {n} ∈ F. . The set F of ﬁnite subsets of the natural numbers can be deﬁned recursively as follows: Basis: ∅. 0] ∈ L. card(X) = card(Im(Y)). It follows that card(X) = card(Y) by Exercise 27. If X. 2. 33. n] ∈ L. Closure: [m. . Recursive step: If [m. then X ∪ Y ∈ F. the summation is decomposed into the sum of the ﬁrst n powers of 2 and 2n+1 . 39. then [s(m). The second rule combines previously generated sets to obtain sets of larger cardinality. 37. The product of two natural numbers can be deﬁned recursively using addition and the successor operator s. then {s(n)} ∈ F. Y ∈ F. L can be deﬁned recursively by Basis: [0. by induction on n. 0 2i = 2 0 = 1 = 2 1 − 1 i=0 Inductive hypothesis: Assume. . 0] using ﬁnitely many applications of the recursive step. 31. s(s(s(n)))] ∈ L. that k 2i = 2k+1 − 1 i=0 Inductive step: We need to show that n+1 2i = 2(n+1)+1 − 1 i=0 To utilize the inductive hypothesis. . We explicitly show that the equality holds for this case.SOLUTIONS TO EXERCISES 5 By the preceding result.

trees consisting solely of the root. e) The frontier is the set {x14 . x6 . 48. and [x10 . [x5 . x10 ]. Inductive hypothesis: Assume that every complete binary tree T of depth k. which is certainly not the case in family trees. . d) The subtree generated by x2 is comprised of the arcs [x2 . of x15 and x11 is x1 . Recall that by our deﬁnition is node is an ancestor of itself. x14 ]. Recursive step: If y ∈ R and [y. . 46. For any such tree T. x7 ]. x11 ]. satisﬁes nodes(T) = 2k+1 − 1. [x7 . Basis: x ∈ R. x2 . x12 . Basis: The basis consists of trees of depth zero. Thus nodes(T) = = = = = nodes(T ) + 2 · leaves(T ) nodes(T ) + 2 · [(nodes(T ) + 1)/2] (Exercise 47) 2 · nodes(T ) + 1 2 · (2n+1 − 1) + 1 (inductive hypothesis) (n+1)+1 2 −1 . x15 . x11 . x7 . c) The minimal common ancestor of x14 and x11 is x2 . [x2 . Inductive step: Let T be a complete binary tree of depth n + 1. MATHEMATICAL PRELIMINARIES 2i = i=0 i=0 2i + 2n+1 (inductive hypothesis) = 2n+1 − 1 + 2n+1 = 2 · 2n+1 − 1 = 2(n+1)+1 − 1 43. x8 . Let nodes(T) and leaves(T) denote the number of nodes and leaves in a tree T. [x2 . T is obtained by adding two children to each leaf of a complete binary tree T of depth n. We need to show that nodes(T) = 2(n+1)+1 − 1. Since T is complete binary. k = 0. . that is. Induction on the depth of the tree is used to prove that a complete binary tree T of depth n has 2n+1 − 1 nodes.6 n+1 n CHAPTER 1. then z ∈ R. Closure: y ∈ R only if y can be obtained from x by ﬁnitely many applications of the recursive step. x6 ]. x1 }. The set R of nodes reachable from a given node x in a directed graph is deﬁned recursively using the adjacency relation A. where n ≥ 0. . it is also strictly binary and leaves(T ) = (nodes(T ) + 1)/2 by Exercise 47. a) The depth of the tree is 4. z] ∈ A. n. b) The set of ancestors of x11 is {x11 . nodes(T) = 1 = 21 − 1. x16 }. x5 ]. x3 .

Inductive step: Let w be a string of length n + 1. Clearly. Closure: A string u is in L only if it can be obtained from λ using a ﬁnite number of applications of the recursive step. Basis: The basis consists of the null string. The set L can be deﬁned recursively as follows: Basis: λ ∈ L Recursive step: If u ∈ L and u can be written xyzw where x.1. z. 7 . and iii) xbyazaw ∈ X. Then w = ua and (wR )R = = = = = = = ((ua)R )R (aR uR )R (auR )R (uR )R aR uaR ua w (Theorem 2. Basis: The basis consists of the null string. Inductive hypothesis: Assume that all strings with twice as many a’s as b’s of length k.6) (inductive hypothesis) 8. Inductive hypothesis: Assume that (w R )R = w for all strings w ∈ Σ∗ of length n or less. that w = (w R )R for every string w ∈ Σ∗ . by induction on the length of the string. We prove. A proof by induction on the length of strings demonstrates that every string with twice as many a’s as b’s is generated by the recursive deﬁnition. which satisﬁes the relation between the number of a’s and b’s. w ∈ Σ∗ then i) xayazbw ∈ L. Note that every string in L has length divisible by three. Let L denote the set of strings over Σ = {a.1.6) (Theorem 2. every string generated by the preceding deﬁnition has twice as many a’s as b’s. y. In this case. are generated by the recursive deﬁnition. b} that contain twice as many a’s as b’s. (λR )R = (λ)R = λ as desired. 0 ≤ k ≤ n.Chapter 2 Languages 3. ii) xaybzaw ∈ X.

Since u contains at least three elements. or xbyazaw for some x. Then w can be written ua where length(u) = n. Inductive hypothesis: Assume that every string w ∈ W with length n or less is in P. 12. v ∈ P. LANGUAGES Inductive step: Let u be a string of length n + 3 with twice as many a’s as b’s. then w = λ and λ ∈ P by the basis of the recursive deﬁnition. It follows that xyzw has twice as many a’s as b’s and. xaybzaw. Thus. Inductive step: Let w ∈ W be a string of length n + 1. we conclude that ava = av R a and v = v R . By the inductive hypothesis. from the recursive step in the deﬁnition of P. where w is generated by n applications of the recursive step. w = wR = (ua)R = auR Since w begins and ends with with same symbol. for every a ∈ Σ. uR = = = = = and u ∈ W. z ∈ Σ∗ . Basis: If length(u) = 0. by the inductive hypothesis. Again. is generated by the recursive deﬁnition. Let P denote the set of palindromes deﬁned by the recursive deﬁnition and let W = {w ∈ Σ ∗ | w = wR }. Inductive step: Let u be a string generated by n + 1 applications of the recursive step. w. one additional application of the recursive step produces u from xyzw. w = w R for every such string. It follows. using reversals we get wR = = = (ava)R a R v R aR av R a (awa)R a R w R aR awR a awa u (Theorem 2. Inductive hypothesis: Assume that every string generated by n or fewer applications of the recursive step is in W. The proof is by induction on the number of applications of the recursive step in the deﬁnition of palindrome required to generate the string. it may be written w = ava. strings of length one in W are also in P. x can be written xayazbw. We begin by proving that P ⊆ W.6) (inductive hypothesis) Since w = w R . Then u = awa for some string w and symbol a ∈ Σ. Similarly. y.1.8 CHAPTER 2. The proof is by induction on the length of the strings in W. . that w = ava is also in P. We now show that W ⊆ P. Basis: The basis consists of strings of P that are generated with no applications of the recursive step. n ≥ 1. Establishing that P = W requires demonstrating that each of the sets is a subset of the other. Taking the reversal. Thus. Clearly. This set consists of λ and a.

Have we missed some strings with the desired property? Consider aba. The null string. 21. a string in L1 ∩ L3 must consist only of a’s and have length divisible by 12. The regular expression (b∗ ab∗ ab∗ a)b∗ deﬁnes the same language. The +’s in the regular expression a+ b∗ c∗ ∪ a∗ b+ c∗ ∪ a∗ b∗ c+ ensure the presence of at least one element in each string in the language. The language L1 ∩ L3 contains all strings that are in both L1 and L3 . Any number of a’s and c’s. Clearly every string in this language has substrings ab and ba. and aaaba. L1 ∩ L3 = (a12 )∗ . b} of length four. Since L 3 requires length divisible by four. with length zero. The leading a and trailing cc are explicitly placed in the expression a(a ∪ c)∗ b(a ∪ c)∗ b(a ∪ c)∗ cc. b∗ is concatenated to the end of the preceding expression. a’s preceded by a b by the second component. In the regular expression (ab ∪ ba ∪ aba ∪ b)∗ . each string must contain at least one a or one b or one c. Since the null string is not allowed in the language.SOLUTIONS TO EXERCISES 9 13. A regular expression for the set of strings containing substrings ab and ba can be obtained by adding (a ∪ b)∗ aba(a ∪ b)∗ ∪ (a ∪ b)∗ bab(a ∪ b)∗ to the expression above. the null string is the only string that does have at least three a’s. Another regular expression for this language is ((a ∪ λ)b(a ∪ λ))∗ . a’s followed by a b are produced by the ﬁrst component. 24. contains all strings with length divisible by four. represented by the expression (a ∪ c)∗ . Applying the Kleene star to the preceding expression produces strings in which the number of a’s is a multiple of three. 28. is in L3 . obtained by applying the Kleene star operation to L2 . Every string described by the preceding expression has length four or more. The language L2 consists of all strings over {a. At ﬁrst glance it may seem that the desired language is given by the regular expression (a ∪ b)∗ ab(a ∪ b)∗ ba(a ∪ b)∗ ∪ (a ∪ b)∗ ab(a ∪ b)∗ ba(a ∪ b)∗ . it need not contain one of every symbol. b} that contain the substrings aa and bb is represented by (a ∪ b)∗ aa(a ∪ b)∗ bb(a ∪ b)∗ ∪ (a ∪ b)∗ bb(a ∪ b)∗ aa(a ∪ b)∗ . The two expressions joined by the ∪ indicate that the aa may precede or follow the bb. 23. By being in L1 . L3 . each string must consist solely of a’s and have length divisible by three. may surround the two b’s. 15. The language consisting of strings over {a. Strings consisting solely of b’s are obtained from (b∗ ab∗ ab∗ ab∗ )∗ b∗ by concatenating λ from (b∗ ab∗ ab∗ ab∗ )∗ with b’s from b∗ . The set of strings over {a. . To obtain strings with no a’s. 26. That is. bab. The inclusion of b within the scope of the ∗ operator allows any number additional b’s to occur anywhere in the string. b} containing exactly three a’s is deﬁned by the regular expression b∗ ab∗ ab∗ ab∗ . However. (b∗ ab∗ ab∗ ab∗ )∗ does not contain strings consisting of b’s alone. However. and two a’s “covered” by a single b by the third component.

Thus the expression (b ∪ ab ∪ aab)∗ (λ ∪ a ∪ aa) represents the set of all strings over {a.10 CHAPTER 2. This expression is obtained by combining an expression for each of the component subsets with the union operation. A technique accomplish this will follow from the ability to reduce ﬁnite state machines to regular expressions developed in Chapter 6. Once a’s are generated. However. c} with an odd number of occurrences of the substring ab we ﬁrst construct an expression w deﬁning strings over {a. Using w. The regular expression b∗ (a ∪ abb+ )∗ (λ ∪ ab) describes the desired language. each step employs an identity from rule 12 in Table 2. 38.1. the desired set can be written (wabwab)∗ wabw. b} with an even number of a’s or an odd number of b’s. which precludes the occurrence of aaa. 37. b. c} that do not contain the substring ab. to indicate the need for an algorithmic approach for producing regular expressions. The regular expression (b∗ ab∗ a)∗ b∗ ∪ (a∗ ba∗ b)∗ a∗ ba∗ deﬁnes strings over {a. Exercises 37 and 38 illustrate the signiﬁcant diﬀerence between union and intersection in describing patterns with regular expressions. Every aa in the expression (b ∪ ab ∪ aab)∗ is followed by at least one b. To obtain an expression for the strings over {a. There is nothing intuitive about a regular expression for this language. A string can begin with any number of b’s. along with the hint. 32. . Just for fun. it is necessary to ensure that an occurrence of ab is either followed by another b or ends the string. 39. 34. b. w = b∗ (a ∪ cb∗ )∗ . a) (ba)+ (a∗ b∗ ∪ a∗ )= (ba)+ (a∗ )(b∗ ∪ λ) (identity 5) = (ba)∗ baa∗ (b∗ ∪ λ) (identity 4) = (ba)∗ ba+ (b∗ ∪ λ) (a ∪ b)∗ = = = = = = (b ∪ a)∗ b∗ (b ∪ a)∗ b∗ (a∗ b∗ )∗ (b∗ a∗ )∗ b∗ (b ∪ a)∗ b∗ (a ∪ b)∗ b∗ (identity 5) (identity 5) (identity 11) (identity 5) c) Except where noted. LANGUAGES 31. every string in this expression ends with b while strings in the language may end in a or aa as well as b. a solution is [aa ∪ ab(bb)∗ ba] ∪ [(b ∪ a(bb)∗ ba)(a(bb)∗ a)∗ (b ∪ ab(bb)∗ a)] ∗ . b} that do not contain the substring aaa. The exercise is given.

a) S ⇒ AB ⇒ aAB ⇒ aaB ⇒ aaAB ⇒ aaaB ⇒ aaab S ⇒ AB 11 d b) . a) Derivation S ⇒ abSc ⇒ ababScc ⇒ ababAcc ⇒ ababcAdcc ⇒ ababccddcc Rule S → abSc S → abSc S→A A → cAd A → cd b) The derivation tree corresponding to the preceding derivation is S a b S c a b S c A c A d d c) L(G)= {(ab)n cm dm cn | n ≥ 0.Chapter 3 Context-Free Grammars 1. m > 0} 4.

13. strings consisting solely of b’s are not in the language. the S and B rules combine to produce strings with a b in the ﬁrst and middle positions.3. The rule A rules generate an equal number of b’s and c’s. two c’s are produced at the end of the string. The basis of the recursive deﬁnition consists of the null string. j ≥ 0}. b} that contain the same number of a’s and b’s is given in Example 2. Each B is transformed into one or more b’s. The language of the grammar is {(aa)i bj | i > 0. i ≥ 0. An a can be speciﬁcally placed in the middle position of a string using the rules A → aAa | aAb | bAa | bAb | a The termination of the application of recursive A rules by the rule A → a inserts the symbol a into the middle of the string. the grammar S → aAa | aAb | bBa | bBb | a | b A → aAa | aAb | bAa | bAb | a B → aBa | aBb | bBa | bBb | b generates all strings over {a.2. 8. 12. . The recursion is terminated by an application of the rule S → A. Using this strategy. you simply must count the possible derivations represented by the tree. If the derivation begins with an S rule that begins with an a. Since at least one application of the rule S → aaSB is necessary to generate b’s. b} with the same symbol in the beginning and middle positions. a) The S rules S → aaSB and S → λ generate an equal number of leading aa’s and trailing B’s. The language generated by the rules is {(ab)i (cd)j (ba)j (dc)i | i ≥ 0. The ﬁrst S rule produces this string. c) Repeated applications of the recursive rule S → abSdc produce a string of the form (ab)i S(dc)i . CONTEXT-FREE GRAMMARS c) There are 20 derivations that generate DT. j ≥ 0. A recursive deﬁnition of the set of strings over {a. n > 0} is generated by S → aScc | aAcc A → bAc | bc For each leading a generated by the S rules. The recursive deﬁnition provides the insight for constructing the rules of the context-free grammar S→λ S → SaSbS | SbSaS | SS that generates the language. The recursive step consists of inserting an a and b in a previously generated string or by concatenating two previously generated strings. These operations are captured by the second set of S rules. The A rules produce strings of the form (cd)j (ba)j .12 ⇒ AAB ⇒ AAb ⇒ Aab ⇒ aAab ⇒ aaab CHAPTER 3. 6. There is no trick to obtaining this answer. j ≥ i} ∪ {λ}. Similarly. The language consisting of the set of strings {an bm c2n+m | m. the A rules ensure that an a occurs in the middle of the string.

b} in which every a is preceded or followed by a b. An a generated by B → aS is preceded by a b. The rules S → S1 | S2 do not have the form required for rules of a regular grammar. 20.SOLUTIONS TO EXERCISES 16. The language ((a ∪ λ)b(a ∪ λ))∗ is generated by the grammar S → aA | bB | λ A → bB B → aS | bB | λ This language consists of all strings over {a. The S rules of the new grammar are S → aS1 | bS1 | aA S → aS2 | bS2 | bE The strategy used to modify the rules S → S1 | S2 is an instance of a more general rule modiﬁcation technique known as removing chain rules. it is not regular. The alternative in these productions corresponds to the ∪ in the deﬁnition of the language. The language (a ∪ b)∗ aa(a ∪ b)∗ bb(a ∪ b)∗ is generated by G1 : S1 → aS1 | bS1 | aA A → aB B → aB | bB | bC C → bD D → aD | bD | λ G2 generates the strings (a ∪ b)∗ bb(a ∪ b)∗ aa(a ∪ b)∗ G2 : S2 → aS2 | bS2 | bE E → bF F → aF | bF | aG G → aH H → aH | bH | λ A grammar G that generates (a ∪ b)∗ aa(a ∪ b)∗ bb(a ∪ b)∗ ∪ (a ∪ b)∗ bb(a ∪ b)∗ aa(a ∪ b)∗ 13 can be obtained from G1 and G2 . While the grammar described above generates the desired language. The rules of G consist of the rules of G1 and G2 augmented with S → S1 | S2 where S is the start symbol of the composite grammar. A regular grammar can be obtained by explicitly replacing S1 and S2 in the S rules with the right-hand sides of the S1 and S2 rules. 24. The interpretation of the variables is Variable S A B C Parity even even odd odd Progress toward ab none a none a . which will be studied in detail in Chapter 4. The variables of the grammar indicate whether an even or odd number of ab’s has been generated and the progress toward the next ab. An a generated by the rule S → aA is followed by a b.

The proof is by induction of the length of derivations of G. S → aOa | bEa | bOb | aEb | λ Ea → aOa | bEa | λ Oa → aEa | bOa Ob → aOb | bEb | λ Eb → aEb | bOb the Ea and Oa rules generate strings with an even number of a’s. Let G be the grammar S → aSbS | aS | λ. a) The language of G is (a+ b+ )+ . Inductive step: Let w be a sentential form of G that can be derived using n + 1 rule applications. The string aab can be generated by two derivations. The derivation of a string with a positive even number of a’s is initiated by the application of an S rule that generates either Ea or Oa . We will refer to this condition as the preﬁx property. aS and λ are the only sentential forms produced by derivations of length one. b) The rules S → aS and S → Sb allow the generation of leading a’s or trailing b’s in any order. We prove that every preﬁx of sentential form of G has at least as many a’s as b’s. Thus w also satisﬁes the preﬁx property. A preﬁx of w consists of a preﬁx of uSv with the S replaced by λ. The preﬁx property is seen to hold for these strings by inspection. The derivation of w can be written S ⇒ uSv ⇒ w where w is obtained by applying an S rule to uSv. In the grammar. CONTEXT-FREE GRAMMARS | bS | bB | bB | λ | bS | λ A derivation may terminate with a λ-rule when a B or a C is present in the sentential form since this indicates that an odd number of ab’s have been generated. one beginning with the application of the rule S → aOa and the other beginning with S → aEb .14 The rules of the grammar are S → aA A → aA B → aC C → aC CHAPTER 3. In a like manner. 32. Inductive hypothesis: Assume that every sentential form that can be obtained by a derivation of length n or less satisﬁes the preﬁx property. aS or aSbS. b} containing an even number of a’s or an odd number of b’s. 30. Basis: The strings aSbS. Two leftmost derivations for the string aabb are S ⇒ aS ⇒ aSb ⇒ aabb S ⇒ Sb ⇒ aSb ⇒ aabb n . The objective is to construct a grammar that generates the set of strings over {a. The derivation then alternates between occurrences of Ea and Oa until it is terminated with an application of the rule Ea → λ. the Ob and Eb rules generate strings with an odd number of b’s. 25. By the inductive hypothesis uSv satisﬁes the preﬁx property.

A → a. and B → bB generate additional a’s and b’s. To show that G is ambiguous we must ﬁnd a string w ∈ L(G) that can be generated by two distinct leftmost derivations. The S rules generate the a’s and the A rules generate the b’s. The null string is generated directly by the rule S → λ. . it is necessary to determine precisely which strings are in L(G). To construct an unambiguous grammar that generates L(G). The rules S → λ and S → aaS generate all strings with an even number of a’s. 33. Consider the derivation of the string aaaabb in which two a’s are generated by applications of the S rule. Starting a derivation with a single application of S → aaaaaS and completing it with applications of S → aaS and S → λ produces strings of length 5. The rule S → AaSbB generates one a and one b.SOLUTIONS TO EXERCISES c) The derivation trees corresponding to the derivations of (b) are S a S a b S b a a S S b S b 15 d) The unambiguous regular grammar S → aS | aA A → bA | bS | b is equivalent to G and generates strings unambiguously producing the terminals in a leftto-right manner. 7. A → aA. The preﬁx of a’s can be generated either by applications of the rule S → AaSbB or by the A rules. . . The rule A → a guarantees that there are two or more a’s in every string nonnull in L(G). The process is repeated by the application of the rule A → bS or terminated by an application of A → b. Derivation Rule . one consisting of ﬁve applications of the rule S → aaS followed by the λ-rule and the other consisting of two applications of S → aaaaaS and the λ-rule. j > 0}. The language of G consists of all strings of a’s except for a and aaa. Derivation S ⇒ AaSbB ⇒ aaSbB ⇒ aaAaSbBbB ⇒ aaaaSbBbB ⇒ aaaabBbB ⇒ aaaabbB ⇒ aaaabb Rule S → AaSbB A→a S → AaSbB A→a S→λ B→λ B→λ The string can also be derived using the rules A → aA and A → a to generate the a’s. a) The string consisting of ten a’s can be produced by two distinct derivations. 9. This language is generated by the unambiguous grammar S → λ | aa | aaaA A → aA | a d) L(G) is the set {λ} ∪ {ai bj | i > 1. . Applications of S → AaSbB.

Using more than i − 1 applications would produce too many a’s. the rule A → bB begins the generation of the b’s.16 CHAPTER 3. it is necessary to ensure that these strings are generated by only one of A and B. e) The variable A generates strings of the form (ab)i and B generates ai bi for all i ≥ 0. Every other string in L(G) has the form ai bj where i > 0 and j > 1. Selecting the other A rule prior to this point would produce a string with to few a’s. there is only one leftmost derivation of ai bj and the grammar is unambiguous. The rule B → b. The regular grammar S → aA | λ A → aA | aB B → bB | b generates strings in L(G) in a left-to-right manner. Initially the S rule that produces an a must be employed. j > 1} unambiguously. b) To show that G is unambiguous it is necessary to show that there is a unique leftmost derivation of every string in L(G). After the generation of ai . Each of these strings can be generated by two distinct leftmost derivations. CONTEXT-FREE GRAMMARS S ⇒ AaSbB ⇒ aAaSbB ⇒ aaAaSbB ⇒ aaaaSbB ⇒ aaaabB ⇒ aaaabbB ⇒ aaaabb S → AaSbB A → aA A → aA A→a S→λ B → bB B→λ The two distinct leftmost derivations of aaaabb demonstrate the ambiguity of G. Using the rules S→A|B A → abA | abab B → aBb | λ. ensures the presence of at least one b. The only strings in common are λ and ab. A regular expression for this language is a+ b+ b ∪ λ. The derivation of such a string has the form Derivation S =⇒ aA i−1 =⇒ ai A =⇒ ai bB =⇒ ai bbj−2 B =⇒ ai bbj−2 b = a i bj j−2 Rule S → aA A → aA A → bB B → bB B→b At each step there is only one rule that can be applied to successfully derive a i bj . whose application completes a derivation. . 34. λ and ab are derivable only from the variable B. a) The grammar G was produced in Exercise 33 d) to generate the language {λ} ∪ {a i bj | i > 0. The rules S → aA and A → aB guarantee the generation of at least two a’s in every nonnull string. The sole derivation of λ is S ⇒ λ. Any deviation from the pattern in the derivation above would produce the wrong number of b’s. To produce an unambiguous grammar. Consequently. followed by exactly i − 1 applications of the rule A → aA.

SOLUTIONS TO EXERCISES 39.

17

a) The rules S → aABb, A → aA, and B → bB of G1 are produced by the derivations S ⇒ AABB ⇒ aABB ⇒ aABb, A ⇒ AA ⇒ aA, and B ⇒ BB ⇒ bB using the rules of G2 . Thus every rule of G1 is either in G2 or derivable in G2 . Now if w is derivable in G1 , it is also derivable in G2 by replacing the application of a rule of G1 by the sequence of rules of G2 that produce the same transformation. If follows the L(G1 ) ⊆ L(G2 . b) The language of G1 is aa+ b+ b. Since the rules A → AA | a generate a+ and B → BB | b generate b+ , the language of G2 is a+ a+ b+ b+ = aa+ b+ b.

40. A regular grammar is one with rules of the form A → aB, A → a, and A → λ, where A, B ∈ V and a ∈ Σ. The rules of a right-linear grammar have the form A → λ, A → u, and A → uB, where A, B ∈ V and u ∈ Σ+ . Since every regular grammar is also right-linear, all regular languages are generated by right-linear grammars. We now must show that every language generated by a right-linear grammar is regular. Let G be a right-linear grammar. A regular grammar G that generates L(G) is constructed from the rules of G. Rules of the form A → aB, A → a and A → λ in G are also in G . A right-linear rule A → a1 . . . an with n > 1 is transformed into a sequence of rules A → a 1 T1 T1 → a2 T2 . . . Tn−1 → an where Ti are variables not occurring in G. The result of the application of the A → u of G is obtained by the derivation A ⇒ a1 T1 ⇒ · · · ⇒ a1 . . . an−1 Tn−1 ⇒ a1 . . . an in G . Similarly, rules of the form A → a1 . . . an B, with n > 1, can be transformed into a sequence of rules as above with the ﬁnal rule having the form Tn−1 → an B. Clearly, the grammar G constructed in this manner generates L(G).

18

CHAPTER 3. CONTEXT-FREE GRAMMARS

Chapter 4

**Normal Forms for Context-Free Grammars
**

3. An equivalent essentially noncontracting grammar GL with a nonrecursive start symbol is constructed following the steps given in the proof of Theorem 4.2.3. Because S is recursive, a new start symbol S and the rule S → S are added to GL . The set of nullable variables is {S , S, A, B}. All possible derivations of λ from the nullable variables are eliminated by the addition of ﬁve S rules, one A rule, and one B rule. The resulting grammar is S →S | λ S → BSA | BS | SA | BA | B | S | A B → Bba | ba A → aA | a 8. Following the technique of Example 4.3.1, an equivalent grammar GC that does not contain chain rules is constructed. For each variable, Algorithm 4.3.1 is used to obtain the set of variables derivable using chain rules. CHAIN(S) = {S, A, B, C} CHAIN(A) = {A, B, C} CHAIN(B) = {B, C, A} CHAIN(C) = {C, A, B} These sets are used to generate the rules of GC according to the technique of Theorem 4.3.3, producing the grammar S → aa A → aa B → aa C → aa | | | | bb bb bb bb | | | | cc cc cc cc

In the grammar obtained by this transformation, it is clear that the A, B, and C rules do not contribute to derivations of terminal strings. 19

D} ΣU = {a. G} ΣT = {a. Iteration 0 1 2 3 Using this set. VT = {S. F. A. S} Removing all references to variables in the set VT − REACH produces the grammar VU = {S. NORMAL FORMS 15.4. S → XAY B | ABC | a A → XA | a B → Y BZC | b C → XY Z X→a Y →b Z→c . D} NEW {S} {A} {D} TERM {D. or A → w where w is a string consisting solely of variables. Iteration 0 1 2 3 REACH {S} {S.4. G} {D. D} {S. The ﬁrst step involves the construction of a grammar GT that contains only variables that derive terminal strings. A} {S. G. To convert G to Chomsky normal form. S} PREV {D. A} {D. D. A. G. S} {D. A} {S. b} PU : S → aA A → aA | aD D → bD | b 19. A.20 CHAPTER 4. G. A. G} {D. F. A. Algorithm 4. F. G. GT is constructed. we begin by transforming the rules of G to the form S → λ. F. b} PT : S → aA A → aA | aD D → bD | b F → aF | aG | a G→a | b The second step in the construction of GU involves the removal of all variables from GT that are not reachable from S. Algorithm 4. D} PREV ∅ {S} {S. F. G. F. A. F. F. A.2 is used to construct the set TERM of variables which derive terminal strings. A} {D. An equivalent grammar GU without useless symbols is constructed in two steps. A → a.4 is used to construct the set REACH of variables reachable from S. A.

The depth of the derivation tree for the derivation of λ is 1. S ⇒ λ. S → XT1 | AT3 | a T1 → AT2 T2 → Y B T3 → BC A → XA | a B → Y T4 | b T4 → BT5 T5 → ZC C → XT6 T6 → Y Z X→a Y →b Z→c 24. requires one rule application. a) The derivation of a string of length 0. the minimum depth is obtained when the tree is the “bushiest” possible. n − 1 rules of the form A → BC and n rules of the form A → a. b) The maximum depth derivation tree for a string of length 4 has the form S A A A a A a A a A a Generalizing this pattern we see that the maximum depth of the derivation tree of a string of length n > 0 is n. 28. A string of length n > 0 requires 2n − 1.SOLUTIONS TO EXERCISES 21 The transformation is completed by breaking each rule whose right-hand side has length greater than 2 into a sequence of rules of the form A → BC. The minimum depth of a derivation tree for a string of length n > 0 is lg(n) + 1 . c) The minimum depth derivation tree of a string of length 4 has the form S A A a A a A a A A a Intuitively. To obtain an equivalent grammar with no direct left recursion. the rule modiﬁcation that removes direct left recursion is applied to the A and B rules producing S→A | C A → BX | aX | B | a X → aBX | aCX | aB | aC .

The variables S. the variables of the grammar are numbered and an intermediate grammar is constructed in which the ﬁrst symbol of the right-hand side of every rule is either a terminal or a variable with a higher number than the number of the variable on the left-hand side of the rule. we obtain S → aR1 B A → aR1 | B → aR1 A C→a | b R1 → BR1 | aB | aR1 AC | aAC | aBC | bBC | bC a | aA | aB | bB | b | B Finally.1. the rule B → AA can be converted into the desired form by substituting for the ﬁrst A. 2. In the ﬁrst phase. The transformation of a grammar G from Chomsky normal form to Greibach normal form is accomplished in two phases. resulting in the grammar S → AB | BC A → aR1 | a B → aR1 A | aA | CB | b C→a | b R1 → BR1 | B The second phase involves transformation of the rules of the intermediate grammar to ensure that the ﬁrst symbol of the right-hand side of each rule is a terminal symbol. 32. B.3 is used to transform the R1 rules created in the ﬁrst phase into the proper form.1. producing the Greibach normal form grammar S → aR1 B | aB | aR1 AC | aAC | aBC | bBC | bC A → aR1 | a B → aR1 A | aA | aB | bB | b C→a | b R1 → aR1 AR1 | aAR1 | aBR1 | bBR1 | bR1 → aR1 A | aA | aB | bB | b . Removing direct left recursion from the A rules produces the grammar S → AB | BC A → aR1 | a B → AA | CB | b C→a | b R1 → BR1 | B Applying the rule transformation schema of Lemma 4.22 B → CbY | Cb Y → bY | b C → cC | c CHAPTER 4. the B and Y rules produce Cbb∗ using the right recursive Y rule to generate b+ . NORMAL FORMS The application of an A rule generates B or a and the X rule produces elements of the form described by (aB ∪ aC)+ using right recursion. Similarly.3. A. The S rules are already in the proper form.3. Lemma 4. Working backwards from the C rules and applying Lemma 4.3. 3. and 4 respectively. and C are numbered 1.1. This is done by removing direct left recursion and by applying the rule replacement schema of Lemma 4.1.

and 3 have right-hand sides of length n − 1. A detailed proof that this construction produces an equivalent grammar can be found in Harrison1 . where n is any natural number greater than 3. Case 1: length(βi ui ) ≤ n − 2. It is now necessary to deﬁne the rules for the new variable [Bn−1 Bn ]. Σ. 1978 . the Bn−1 rule can be written Bn−1 → βi C2 · · · Cn−1 Cn . In this case. Create two new variables [Cn−2 Cn−1 ] and [Cn Bn ] and add the rule Bn−1 → βi C2 · · · Cn−3 [Cn−2 Cn−1 ][Cn Bn ] to G . 2. In this case. Create a new variable [Cn−1 Bn ] and add the rule Bn−1 → βi C2 · · · Cn−2 [Cn−1 Bn ] to G . We will show how to transform G to a Greibach normal form grammar G in which the right-hand side of each rule has length at most n − 1. where βi ∈ Σ. Each of the rules generated in cases 1. Let G = (V. The process must be repeated until rules have been created for each variable created by the rule transformations. P) be a grammar in Greibach normal form in which the maximum length of the right-hand side of a rule is n. Let the Bn−1 rules of G be Bn−1 → β1 u1 | β1 u2 | · · · | βm um . A new variable [Bn−1 Bn ] is added to grammar and the rule A → aB2 · · · Bn−1 Bn is replaced with the rule A → aB2 · · · Bn−2 [Bn−1 Bn ] whose right-hand side has length n − 1. ui ∈ V∗ . We will outline the approach to transform a rule of the form A → aB2 · · · Bn−1 Bn into a set of rules of the desired form. This length reduction process can be repeated until each rule has a right-hand side of length at most 3. The procedure can be performed on each rule with right-hand side of length n to produce a grammar in which the right-hand side of each rule has length at most n − 1. There are three cases to consider in creating the new rules.SOLUTIONS TO EXERCISES 23 35. no change is necessary. A. S. Introduction to Formal Languages. Case 2: length(βi ui ) = n−1. Case 3: length(βi ui ) = n. We begin by showing how to reduce the length of strings on the right-hand side of the rules of a grammar in Greibach normal form. If a rule already has the desired form. and length(βi ui ) ≤ n. The construction in case 3 requires the original rule to have at least three variables. Harrison. Addison-Wesley. Add the rule [Bn−1 Bn ] → βi ui Bn to G . the Bn−1 rule can be written Bn−1 → βi C2 · · · Cn−1 . 1 M.

NORMAL FORMS .24 CHAPTER 4.

bababa. bababa] [q1 . abb] [q2 . 25 . with a. aba] [q0 . ˆ ˆ Assume that δ(qi . babb] [q1 .Chapter 5 Finite Automata 1. aa] [q2 . The deﬁnitions of δ and δ are the same for strings of length 0 and 1. bbbaa] [q1 . a) The state diagram of M is a q0 b b a q1 b a q2 b) i) [q0 . (iii). The proof is by induction on length of the input string. u) = δ (qi . b ∈ Σ and length(v) = n − 1. u) for all strings u of length n or less. λ] [q0 . λ] iii) [q0 . λ] iv) c) The computations in (i). and (iv) terminate in the accepting state q 2 . bbabb] [q1 . baa] [q1 . λ] ii) [q0 . abaa] [q0 . d) Two regular expressions describing L(M) are a∗ b+ a+ (ba∗ b+ a+ )∗ and (a∗ b+ a+ b)∗ a∗ b+ a+ . a] [q2 . bbaa] [q1 . bbbabb] [q1 . b] [q1 . ˆ ˆ 4. baa] [q1 . and bbbaa are in L(M). ababa] [q2 . the strings abaa. Therefore. ba] [q1 . Let w = bva be a string of length of n + 1. a] [q2 . baba] [q0 . aa] [q2 . bb] [q0 . a] [q2 .

bva) ˆ = δ(δ(qi . b). a) ˆ (deﬁnition of δ) (inductive hypothesis) ˆ (deﬁnition of δ ) ˆ (deﬁnition of δ ) (inductive hypothesis) ˆ (deﬁnition of δ) (inductive hypothesis) ˆ ˆ Thus δ(qi . b). and c’s. b). w) for all strings w ∈ Σ∗ as desired. w) = δ (qi . b’s. b). va) ˆ = δ(δ(qi . . the DFA enters state q3 . b} that do not contain the substring aaa is given by the state diagram b a. b). ˆ ˆ δ (qi . a) ˆ = δ(δ (qi . a) Similarly. a) ˆ = δ(δ (δ(qi .b c q1 c q2 c that accepts a∗ b∗ c∗ uses the loops in states q0 . bv). v). bv). FINITE AUTOMATA ˆ ˆ δ(qi . w) = δ (qi . Any deviation from the prescribed order causes the computation to enter q3 and reject the string. When three consecutive a’s are encountered. 8.b q0 a b q1 a q2 a q3 b The states are used to count the number of consecutive a’s that have been processed. w) = δ(qi . and q2 to read a’s. bva) ˆ = δ (δ(qi . and rejects the string.b.c q3 a a q0 b b a. va) ˆ = δ(δ(δ(qi . processes the remainder of the input. 5.26 CHAPTER 5. v). a) ˆ = δ(δ (δ(qi . q1 . A DFA that accepts the strings over {a. The DFA a. v).

c q2 b. 3. the computation enters the nonaccepting state q4 and rejects the string.c q3 a a q4 a a b. 20. The states of the machine are used to count the number of symbols processed since an a has been read or since the beginning of the string.c b.b q0 b a q1 a a b q2 b q3 b q4 A computation is in state qi if the previous i elements are not a’s.c q2 a a q1 b.c q1 b. the computation enters q4 and rejects the string.b.c b. The state q3 represents the combination of the two conditions required for acceptance. or 4. The ﬁrst a may occur at position 1.c q5 a a q6 b. The DFA q0 b.c q8 a. If the string varies from this pattern. b.c b.c b. whichever is smaller.c 27 accepts strings of even length over {a. the computation enters state q8 and rejects the input. A string accepted by this machine must have exactly one a and the total number of b’s and c’s must be odd. Upon reading a second a.c q7 a a .c a q3 a q4 a. All other strings accepted. 21. States q1 or q3 are entered upon processing a single a. The DFA to accept this language diﬀers from the machine in Exercise 20 by requiring every fourth symbol to be an a. q0 b.c b. 2. If an input string has a substring of length four without an a. the computation enters state q4 and checks for a pattern consisting of a combination of three b’s or c’s followed by an a.b. c} that contain exactly one a.SOLUTIONS TO EXERCISES 15. a a. After reading the ﬁrst a.

aabb] [q0 . abb] [q1 . b] [q2 . bb] [q1 . After arriving at q2 there are two ways to leave and return. d) A regular expressions for L(M) is a+ b+ (ab+ ∪ a+ ab+ )∗ . aabb] [q1 . taking the cycle q0 . State q3 accepts (abc)∗ a+ . 23. q2 } ∅ [q0 . Processing a+ b+ leaves the computation in state q2 . . q1 } b ∅ {q1 . aaabb] [q0 . abb] [q0 . This expression can be reduced to (a+ b+ )+ . aabb] a {q0 . q1 . bb] [q0 . The ﬁrst path processes a string from a+ ab+ and the second ab+ . q2 or the cycle q1 . aabb] [q0 . b] [q1 . q2 . aabb] [q0 . aaabb] [q0 . bb] [q1 . aaabb] [q1 . FINITE AUTOMATA Strings of length 3 or less are accepted. λ] c) Yes. Strings of the form (abc)∗ are accepted in q0 . a) The transition table for the machine M is δ q0 q1 q2 b) [q0 . 25. aaabb] [q0 . since the condition is vacuously satisﬁed.28 CHAPTER 5. abb] [q0 . c) The NFA a q3 a q0 a q1 q2 b c accepts the language (abc)∗ a∗ . λ] [q0 . q1 } ∅ {q0 . abb] [q1 . aaabb] [q0 . the ﬁnal computation above reads the entire string and halts in the accepting state q2 .

the computation accepts the input. q1 } {q1 . b a {q0. If q1 is entered upon processing the third to the last symbol.6. To prove that qm is equivalent to qn we must show that δ(qm . q1 } {q1 . q2 } input a b a b a b a b Y {q0 . The set Y consists of the states that may be entered upon processing an a from a state in X. q1 } ∅ {q0 . uw) = δ(qm . Since M does not contain λ-transitions. Algorithm 5. The set of strings over {a. X {q0 } {q0 } {q0 . A computation that enters q1 in any other manner terminates unsuccessfully. ˆ Let w be any string over Σ.3. δ(q .SOLUTIONS TO EXERCISES 29. The state diagram of the deterministic machine is a {q0} b / 0 a. uw) ∈ F. uw) ∈ F if. 39.b q2 a. The algorithm proceeds by choosing a state X ∈ Q and symbol a ∈ Σ for which there is no arc leaving X labeled a.3 is used to construct a DFA that is equivalent to the NFA M in Exercise 17. The start state of the determininstic machine is {q 0 }. nondeterminism is used to choose whether to enter state q1 or remain in state q0 . w) and δ(qn . The nodes of the equivalent NFA are constructed in step 2 of Algorithm 5.6. The generation of Q is traced in the table below. q2 } {q1 . q2 } ∅ ∅ {q0 . δ(qi . The transition table of M is given in the solution to Exercise 17. the input transition function used by the algorithm is the transition function of M. w). and only if. q1 } {q0 . q1 } ∅ ∅ {q1 . δ(qn .b q0 b q1 a. ˆ ˆ ˆ ˆ δ(qj . The equivalence of qm and qn follows since δ(qi .b q3 29 When a b is read in state q0 . q1 }} Q := Q ∪ {∅} Q := Q ∪ {{q1 . q1} a b {q1. w) ∈ F if. q2} b ˆ ˆ 43. q2 } Q Q := {{q0 }} Q := Q ∪ {{q0 . and only if. w) ∈ ∗ F for every string w ∈ Σ . q2 }. Since qi and qj are equivalent. b} whose third to the last symbol is b is accepted by the NFA a. which is the λ-closure of q0 . q2 }} The accepting state is {q1 . uw) = ˆ n .

FINITE AUTOMATA 44. a)] be the transition function deﬁned on classes of equivalent states of a DFA and let qi and qj be two equivalent states. a) = δ ([qj ]. a). To show that δ is well deﬁned. . If δ ([qi ].30 CHAPTER 5. a) = qn . Let δ ([qi ]. However. we must show that δ ([qi ]. a) = [δ(qi . this reduces to showing that [qm ] = [qn ]. this equality follows from the result in Exercise 43. a)qm and δ ([qj ].

Chapter 6

**Properties of Regular Languages
**

2. a) This problem illustrates one technique for determining the language of a ﬁnite automaton with more than one accepting. The node deletion algorithm must be employed individually for each accepting state. Following this strategy, a regular expression is obtained by deleting nodes from the graphs

b b

G1

q0

a

q1

a b

q2

b

b

G2

q0

a

q1

a b

q2

**Deleting q1 and q2 from G1 produces
**

b q0

**with the corresponding regular expression b∗ . Removing q2 from G2 we obtain the expression graph
**

b q0 a ab+

q1

accepting b∗ a(ab+ )∗ . Consequently, the original NFA accepts strings of the form b∗ ∪ b∗ a(ab+ )∗ . 5. a) Associating the variables S, A, and B with states q0 , q1 , and q2 respectively, the regular grammar S → aA | λ A → aA | aB | bS B → bA | λ 31

32

CHAPTER 6. PROPERTIES OF REGULAR LANGUAGES is obtained from the arcs and the accepting states of M. b) A regular expression for L(M) can be obtained using the node deletion process. Deleting the state q1 produces the graph

a+b q0 aa*a ba*b ba+ q2

The set of strings accepted by q2 is described by the regular expression (a+ b)∗ aa∗ a(ba+ ∪ ba∗ b(a+ b)∗ aa∗ a)∗ . By deleting states q2 then q1 , we see that q0 accepts (a(a ∪ ab)∗ b)∗ . The language of M is the union of the strings accepted by q0 and q2 . 6. Let G be a regular grammar and M the NFA that accepts L(G) obtained by the construction in Theorem 6.3.1. We will use induction on the length of the derivations of G to show that ∗ there is a computation [q0 , w] ∗ [C, λ] in M whenever there is a derivation S ⇒ wC in G. The basis consists of derivations of the form S ⇒ aC using a rule S → aC. The rule produces the transition δ(q0 , a) = C in M and the corresponding derivation is [q0 , a] [C, λ] as desired. Now assume that [q0 , w]

∗ ∗

[C, λ] whenever there is a derivation S ⇒ wC.

n

**Let S ⇒ wC be a computation of length n + 1. Such a derivation can be written S ⇒ uB uaC,
**

∗ n

where B → aC is a rule of G. By the inductive hypothesis, there is a computation [q 0 , u] [B, λ]. Combining the transition δ(a, B) = C obtained from the rule B → aC with the preceding computation produces [q0 , w] = [q0 , ua] ∗ [B, a] [C, λ] as desired.

7.

a) Let L be any regular language over {a, b, c} and let L be the language consisting of all strings over {a, b, c} that end in aa. L is regular since it is deﬁned by the regular expression (a ∪ b ∪ c)∗ aa. The set L ∩ L = {w | w ∈ L and w contains an a}, which consists of all strings in L that end with aa, is regular by Theorem 6.4.3.

11.

a) Let G = (V, Σ, S, P) be a regular grammar that generates L. Without loss of generality, we may assume that G does not contain useless symbols. The algorithm to remove useless symbols, presented in Section 4.4, does not alter the form of rules of the grammar. Thus the equivalent grammar obtained by this transformation is also regular. ∗ ∗ Derivations in G have the form S ⇒ uA ⇒ uv where u, v ∈ Σ∗ . A grammar G that generates P = {u | uv ∈ L}, the set of preﬁxes of L, can be constructed by augmenting the rules of G with rules A → λ for every variable A ∈ V. The preﬁx u is produced by ∗ the derivation S ⇒ uA ⇒ u in G .

SOLUTIONS TO EXERCISES

33

12. A DFA M that accepts the language L can be constructed from a DFA M = (Q, Σ, δ, q0 , F) that accepts L. The states of M are ordered pairs of the form [qi , X], where qi ∈ Q and X is a subset of Q. Intuitively (but not precisely), the state qi represents the number of transitions from the start state when processing a string and the set X consists of states that require the same number of transitions to reach an accepting state. The construction of the states Q and the transition function δ of M is iterative. The start state of the machine is the ordered pair [q0 , F]. For each state [qi , X] and symbol a such that δ([qi , X], a) is undeﬁned, the transition is created by the rule δ([qi , X], a) = [qi (a), Y], where Y = {qj | δ(qj , a) ∈ X}. If it is not already in Q , the ordered pair [qi (a), Y] is added to the states of M . The process is repeated until a transition is deﬁned for each state-symbol pair. A state [qi , X] is accepting if qi ∈ X. Back to the intuition, this occurs when the number of transitions from the start state is the same as the number of transitions needed to reach an accepting state. That is, when half of a string in L has been processed. 13. b) To show that the set P of even length palindromes is not regular, it suﬃces to ﬁnd sequences of strings ui and vi , i = 0, 1, 2, . . . , from {a, b}∗ that satisfy i) ui vi ∈ P, for i ≥ 0 ii) ui vj ∈ P whenever i = j. Let ui = ai b and vi = bai . Then ui vi is the palindrome ai bbai . For i = j, the string ui vj = ai bbaj is not in P. We conclude, by Corollary 6.5.2, that P is not regular. 14. b) Assume that L = {an bm | n < m} is regular. Let k be the number speciﬁed by the pumping lemma and z be the string ak bk+1 . By the pumping lemma, z can be written uvw where i) v = λ ii) length(uv) ≤ k iii) uv i w ∈ L for all i ≥ 0. By condition (ii), v consists solely of a’s. Pumping v produces the string uv 2 w that contains at least as many a’s as b’s. Thus uv 2 w ∈ L and L is not regular. e) The language L consists of strings of the form λ, a, ab, aba, abaa, abaab, and so forth. Assume that L is regular and let z = abaab · · · bak−1 bak b, where k is the number speciﬁed by the pumping lemma. We will show that there is no decomposition z = uvw in which v satisﬁes the conditions of the pumping lemma. The argument utilizes the number of b’s in the substring v. There are three cases to consider. Case 1: v has no b’s. In this case, the string uv 0 w has consecutive sequences of a’s in which the second sequence has at most the same number of a’s as its predecessor. Thus uv 0 w ∈ L. Case 2: v has one b. In this case, v has the form as bat and is obtained from a substring baj baj+1 b in z. The string uv 2 w obtained by pumping v has a substring baj bas+t baj+1 b, which does not follow pattern of increasing the number of a’s by one in each subsequent substring of a’s. Thus uv 2 w ∈ L. Case 3: v has two or more b’s. In this case, v contains a substring bat b. Pumping v produces two substrings of the form bat b in uv 2 w. Since no string in L can have two distinct substrings of this form, uv 2 w ∈ L.

x = h(uv) and h(X)h(Y) ⊆ h(XY). a) The regular languages over a set Σ are constructed using the operations union.3. and {a} for every a ∈ Σ1 . P) there is a corresponding a left-regular grammar G = (V. removing any ﬁnite number of elements from a nonregular set cannot make it regular. By Theorem 6. The other two set equalities can be established by similar arguments. Σ. For every regular grammar G = (V. Now assume that L1 − L2 is regular. for sets X and Y and homomorphism h i) h(XY) = h(X)h(Y) ii) h(X ∪ Y) = h(X) ∪ h(Y) iii) h(X∗ ) = h(X)∗ . PROPERTIES OF REGULAR LANGUAGES Thus there is no substring of z that satisﬁes the conditions of the pumping lemma and we conclude the L is not regular. S. Then x = h(u)h(v) for some u ∈ X and v ∈ Y. which is nonregular. Since every ﬁnite language is regular. The proof is by induction on the number of applications of the recursive step in Deﬁnition 2. But this is a contradiction. YZ. Σ. b) Let L1 be a nonregular language and L2 be a ﬁnite language. A → aB ∈ P ii) A → a ∈ P if. Since h is a homomorphism. or h(Y) ∗ . Then L1 ∪ L2 = L1 . L1 = (L1 − L2 ) ∪ (L1 ∩ L2 ) is regular. S. Inductive step: Let X be a regular set over Σ1 deﬁnable by n + 1 applications of the recursive step in Deﬁnition 2. Thus h(XY) ⊆ h(X)h(Y). A → a ∈ P iii) A → λ ∈ P if. P ). We begin by showing that these set operations are preserved by homomorphisms. The homomorphic images of these sets are the sets ∅. 19. b) In a similar manner.34 CHAPTER 6. {λ}. which is either h(Y) ∪ h(Z).1. Let x be an element of h(XY). 17. 18. X a language over Σ1 . and only if. To establish the opposite inclusion. L1 ∩ L2 is regular. x = h(u)h(v) and x ∈ h(X)h(Y). h(Y) and h(Z) are regular. Now let Σ1 and Σ2 be alphabets.3. Then x = h(uv) for some u ∈ X and v ∈ Y. Then X can be written Y ∪ Z. That is. or Y∗ where Y and Z are deﬁnable by n or fewer applications. deﬁned by i) A → Ba ∈ P if.1. is also regular.4.1 needed to generate X. Basis: The basis consists of regular sets ∅. . which are regular over Σ2 . and only if. which is regular. h(Y)h(Z). It follows that h(X). and only if. and Kleene star. Then L1 ∪ L2 = L2 . We will use the recursive deﬁnition of regular sets to show that h(X) is regular 2 whenever X is. A → λ ∈ P. By the inductive hypothesis. and we conclude that L1 − L2 is not regular. and h a homomorphism from Σ∗ 1 to Σ∗ . let L1 = ∅ and L2 = {ai bi | i ≥ 0}. 21. Inductive hypothesis: Now assume that the homomorphic image of every regular set deﬁnable using n or fewer applications of the recursive step is regular. and {h(a)}. a) Let L1 =a∗ b∗ and L2 = {ai bi | i ≥ 0}. {λ}. concatenation. As before. let x be an element in h(X)h(Y). The set equality h(XY) = h(X)h(Y) can be obtained by establishing the inclusions h(XY) ⊆ h(X)h(Y) and h(X)h(Y) ⊆ h(XY). This result shows that a language cannot be “a little nonreguar”.

7. We must show that any other DFA M with n states that accepts L is isomorphic to M. The corresponding left-regular grammar G generates (LR )R = L. (LR )R = L is regular. the corresponding regular grammar G generates LR . Let M = (Q. The ≡M equivalence class of a state qi consists of all strings for which the computation of M halts in qi . 29. Then L(G ) = L(G)R . The proof is by induction on the length of the derivations.5. each ≡M equivalence class is a subset of a ≡L equivalence class. the equivalence classes deﬁned by L and M must be identical. Since there are same number of ≡M and ≡L equivalence classes. Since regularity is preserved by the operation of reversal.3.7. the transition functions of M and M are identical up to the names given to the states. By Theorem 6. If L is a regular language.4 and let n be the number of states of M. q0 . Thus every language generated by a left-regular grammar is regular. Σ. The equivalence classes of the DFA in Example 5. S ⇒ uR in G .SOLUTIONS TO EXERCISES 35 The following Lemma establishes a relationship between regular and left-regular grammars. a) Let L be a language generated by a left-regular grammar G . F) be the minimal state DFA that accepts a language L as constructed by Theorem 6.1 are state q0 q1 q2 equivalence class (a ∪ ba)∗ a∗ b(a+ b)∗ a∗ b(a+ b)∗ b(a ∪ b)∗ ∗ ∗ 31. This implies that there is a regular grammar G that generates LR . the ≡M equivalence classes of the states M are identical to the ≡L equivalence classes of the language L. δ. By the argument in Theorem 6.5. then so is LR . b) Now we must show that every regular language is generated by a left-regular grammar. By the preceding lemma. Lemma. Since the transition function L can be obtained from the ≡L equivalence classes as outlined in Theorem 6.7. The start state in each machine is the state associated with [λ]≡ . Let G and G be corresponding regular and left-regular grammars. and only if.4.7. . The lemma can be proven by showing that S ⇒ u in G if.

36 CHAPTER 6. PROPERTIES OF REGULAR LANGUAGES .

A computation with input ai bj enters state q2 upon processing the ﬁrst b. b) The state diagram of M is a λ /A q0 λ λ/λ λ A/λ q1 b A/λ λ A/λ q2 b A/λ d) To show that the strings aabb and aaab are in L(M). the stack must contain at least j A’s. Strings of the form ai are accepted in state q1 . A) = [q2 . To read the entire input string.Chapter 7 Pushdown Automata and Context-Free Languages 1. The transitions in q1 empty the stack after the input has been read. Thus strings with either of these forms are rejected. The computation for an input string that has fewer a’s than b’s or in which an a occurs after a b halts in state q2 without processing the entire string. λ] will pop any A’s remaining on the stack. Processing an a pushes A onto the stack. a) The PDA M accepts the language {ai bj | 0 ≤ j ≤ i}. we trace a computation of M that accepts these strings. The transition δ(q2 . λ. State q0 q0 q0 q2 q2 State q0 q0 q0 q0 q2 String aabb abb bb b λ String aaab aab ab b λ 37 Stack λ A AA A λ Stack λ A AA AAA AA .

If the input string has the twice as many a’s as b’s. A) = {[q1 . an A is pushed onto the stack. λ]} δ(q1 . B) = {[q1 . The stack is emptied by processing the b’s in q2 . d) The pushdown automaton deﬁned by the transitions δ(q0 . 3. A) = {[q2 . The stack will contain n A’s if the automaton has read n more a’s than b’s. This is accomplished by the transition to q2 . λ) = {[q0 . terminates the computation. the transition to q5 pops the C. If n more b’s than a’s have been read. The states q2 and q3 are the accepting states of M. λ) = {[q1 . C) = {[q5 . The stack is used to record the relationship between the number of a’s and b’s scanned during the computation. two B’s are pushed onto the stack. and accepts the string. λ]} δ(q0 . C]} δ(q1 . C) = {[q2 . C]} δ(q1 . λ) = {[q0 . B]} δ(q4 . the stack will hold 2n B’s. If a B is on the top of the stack. A) = {[q2 . PUSHDOWN AUTOMATA A λ Both of these computations terminate in the accepting state q2 with an empty stack. b. λ) = {[q1 . b. a. one of the computations will push exactly j A’s onto the stack. a. Upon processing an a. B]} δ(q1 . The transitions of M are δ(q0 . The null string is accepted in q3 . λ]} δ(q1 . The lone accepting state of the automaton is q5 . λ. λ. the computation nondeterministically pushes one or two A’s onto the stack. A pushdown automaton M that accepts L uses the a’s to record an acceptable number of matching b’s on the stack. λ) = {[q1 . B]} accepts strings that have twice as many a’s as b’s. C) = {[q3 . λ. A computation begins by pushing a C onto the stack. A]} δ(q2 . λ. a. λ) = {[q4 . When an a is read with an A or C on the top of the stack. A]} δ(q1 . A]} δ(q0 . a. The stacktop will be a C when the number of a’s processed is exactly twice the number of b’s processed.38 q2 q2 λ λ CHAPTER 7. λ) = {[q1 . a. b. which serves as a bottom-marker throughout the computation. b. Processing a b with an A on the stack pops the A. A]} δ(q0 . A]} δ(q3 . B) = {[q3 . The state diagram of the PDA is . h) The language L = {ai bj | 0 ≤ i ≤ j ≤ 2 · i} is generated by the context-free grammar S → aSB | λ B → bb | b The B rule generates one or two b’s for each a. For a nonnull string ai bj ∈ L. A) = {[q2 . λ) = {[q3 . the stack is popped removing one b. λ]} δ(q1 . C]} δ(q1 . λ. λ]}. If a b is read with a C or B on the stack. λ. b. λ]} δ(q2 . λ.

Σ. If the computation of M with w ends in an accepting state q i with an empty stack. C) = {[qf . The lone ﬁnal state of M is qf . C] ∗ for all qi ∈ F [qi . and transitions for these states. The transition function δ is constructed by augmenting δ with the transitions δ (qi . w ∈ L(M ). qf }. Γ. λ. λ. A computation of M with input w begins by putting C on the stack and entering q0 . δ. {qf }) from M by adding a new stack symbol C.2. δ.SOLUTIONS TO EXERCISES a λ /A q0 a λ /A λ λ/A q1 b A/λ q2 39 λ λ/λ q3 b A/λ 8. λ] [q0 . Thus the computation of M must have the form [qs . Σ. M continues with a computation identical to that of M with input w. u. From q0 . The ﬁrst step is to construct an equivalent PDA M with transition function δ by augmenting δ with the transitions: [qj . Σ. Let M = (Q. F) be an extended PDA. C]} Let w be a string accepted by M in an accepting state qi . . which can be entered only by a λ-transition from a ﬁnal state qi of M with C as the stacktop. We must also guarantee that the any string w accepted by M by ﬁnal state is accepted by M by ﬁnal state and empty stack. C] [qf . w. λ]} δ (qs . λ) = {[q0 . We construct a PDA M = (Q ∪ {qs . 12. The state diagram for the extended PDA obtained from the grammar is a λ /BB a λ /ABA b A/A c B/B b A/λ c B/λ q0 q1 16. new states qs and qf . qs . B1 · · · Bn A] ∈ δ (qi . δ . the computation of M is completed by the λ-transition from qi to qf that pops the C from the stack. The stack symbol C acts as a marker designating the bottom of the stack. except we begin with extended transitions rather than standard transitions. q0 . The steps follow precisely those given in Theorem 7. We will outline the construction of a contextfree grammar that generates L(M). w.3. B1 · · · Bn A) for every A ∈ Γ. Let M = (Q. Γ. Since the computation of M halts in qf . q0 . λ. Γ ∪ {C}. λ. F) be a PDA that accepts L by ﬁnal state and empty stack. λ] Deleting the ﬁrst and last transition provides a computation of M that accepts w by ﬁnal state and empty stack.

Consider the length of the string z = uv 2 wx2 y obtained by pumping uvwxy. S → q0 . B2 . for i ≥ 0. the null computation. qm1 qm1 . P. The alphabet of G is the input alphabet of M .3. Rules of type 4 are used to terminate derivations. PUSHDOWN AUTOMATA whenever [qj . in other words. qk ∈ Q}. ends in a ﬁnal state. qmn qmn . qm2 · · · qmn−1 . hence. A grammar G = (V. where A ∈ Γ ∪ {λ}. The string of variables B1 · · · Bn in the preceding rules may be empty. Σ.4 relate the derivations from a variable qi . L(M) = L(M ). λ. Any string accepted by a computation that utilizes a new transition can also be obtained by applying the original transition. there is a number k such that every string in L with length k or more can be written uvwxy where i) length(vwx) ≤ k ii) v and x are not both null iii) uv i wxi y ∈ L. The variables of G consist of a start symbol S and objects of the form qi . Each transition [qj . that is. B1 · · · Bn A] ∈ δ (qi . qk → x qj . and terminates with an empty stack. . qk → x qj . S) is constructed from the transitions of M . λ. qmi ∈ Q}. By the pumping lemma. qj represents a computation that begins in state qi .40 CHAPTER 7. and removes the symbol A from the stack. Bm . generates the set of rules { qi . The variable qi . B1 . The rule qk . A). qk to computations of M. 4. ends in qj . For each state qk ∈ Q. x. A. we conclude that z ∈ L and that L is not context-free. λ] ∈ δ(qi .2. The rules of G are constructed as follows: 1. 17. qj for each qj ∈ F. A derivation begins with a rule of type 1 whose right-hand side represents a computation that begins in state q0 . A.3. Each transition [qj . These rules transform a transition of M that does not remove an element from the stack into one that initially pops the stack and later replaces the same symbol on the top of the stack. λ. x. Lemmas 7. u. generates the set of rules { qi . A. qk | qk . qk → λ represents a computation from a state qk to itself that does not alter the stack. qm2 · · · qmn−1 . A. qk . length(z) = length(uv 2 wx2 y) 2 = length(uvwxy) + length(u) + length(v) = k 2 + length(u) + length(v) ≤ < k2 + k (k + 1)2 Since the length of z is greater than k 2 but less than (k + 1)2 . B2 . qm1 qm1 . a) Assume that language L consisting of strings over {a} whose lengths are a perfect square is context-free. a successful computation in M . qj where the q’s are states of M and A ∈ Γ ∪ {λ}.3. 2. The proof that the rules generate L(M) follows the same strategy as Theorem 7.3 and 7. where A ∈ Γ. Bm . B1 · · · Bn ) is a transition of M. Rules of types 2 and 3 trace the action of the machine. 3. A. B1 . qk | qmi . A). A. The string ak must have a decompositon uvwxy that satisﬁes the preceding conditions. B1 · · · Bn ] ∈ δ (qi . qk → λ.

Pumping v produces the substring .SOLUTIONS TO EXERCISES f) Assume that the language L consisting of preﬁxes of the string abaabaaabaaaab . To show that the assumption that L is context-free produces a contradiction. for i ≥ 0. . Consider the string z = abaab · · · bak b. ban−1 ban−i van+1−j b . which is in the language and has length greater than k. In this case. consequently. . Case 2: v has two or more b’s. Pumping v produces a string with two substrings of the form ban b. A similar argument shows that pumping x produces a string not in L whenever x is nonnull. . . pumping any nonnull substring v of z produces a string that is not in the language L. where i + length(v) + j = n + 1. a) The language L1 = {ai b2i cj | i. . ban−1 ban−i ai baj ai baj an+1−j b . . ban−1 ban baj+i ban+1 b . v occurs in z in a position of the form . . That is. j ≥ 0} is generated by the context-free grammar S → AC A → aAbb | λ C → cC | λ b) Similarly. . v consists solely of a’s and lies between two consecutive b’s. 41 is context-free and let k be the number speciﬁed by the pumping lemma. . Regardless of its makeup. . . Since one of v or x is nonnull. we examine all possible decompositions of z that satisfy the conditions of the pumping lemma. one or both of v and x must be nonnull. Case 1: v has no b’s. ban bai vaj ban+2 b . j ≥ 0} is generated by S → AB A → aA | λ B → bBcc | λ . there is no decomposition of z that satisﬁes the requirements of the pumping lemma and we conclude that the language is not context-free. 18. which cannot occur in a string in L. In this case. L2 = {aj bi c2i | i. . . Thus z can be written uvwxy where i) length(vwx) ≤ k ii) v and x are not both null iii) uv i wxi y ∈ L. Then v can be written ai baj and occurs in z as . By (ii). . . . . the resulting string is not in the language. . In the following argument we assume that v = λ. No string with this property is in L. . . . . = . v contains a substring ban b. Pumping v produces an incorrect number of a’s following ban b and. Case 3: v has one b. ban ban+1 b .

Any derivation that produces a string of length k = (r + 1)t or more must have at least r + 1 rule applications. and only if. ai bj . A → w ∈ P. S. Basis: The basis consists of sentential forms produced by derivations of length one. Let L be a linear language. A decomposition of z that satisﬁes the pumping lemma for z = pabq is u = p. or bi cj . possibly two. PUSHDOWN AUTOMATA c) Assume L1 ∩ L2 = {ai b2i c4i | i ≥ 0} is context-free and let k be the number speciﬁed by the pumping lemma. 26. the substring vwx must have the form ai . Σ.3. P) be a context-free grammar. P) that generates L and. variable that is transformed on the r + 1st rule application must have occurred previously in the derivation. The rules of G are obtained by reversing the right-hand of the rules of G. w = λ. A grammar G that generates L(G)R is built from G. bi . S. uR is a sentential form of G derivable by n or fewer rule applications. v = ab. Since there are only r variables. We consider sentential forms of G generated by leftmost derivations and of G by rightmost derivations. Then z can be written in the form z = pabq or z = pbaq. Thus any derivation of a string z ∈ L of length greater than k can be written S ⇒ uAy ⇒ uvAxy ⇒ uvwxy. x = λ. The variables. The string z = ak b2·k c4·k must admit a decomposition uvwxy that satisﬁes the conditions of the pumping lemma. A → w R ∈ P if. ci . Let G = (V. Inductive hypothesis: Assume that u is a sentential form of G derivable by n or fewer rule applications if. Let r by the number of variables of G and t be the maximum number of terminal symbols on the right-hand of any rule. Thus uv 2 wx2 y ∈ L. This operation increases the number of at least one. Induction on the length of derivations is used to show that a string u is a sentential form of G if. z must contain the substring ab or the substring ba. alphabet. uR is a sentential form of G . These are generated by the derivation S ⇒ u in G and S ⇒ uR in G where S → u is a rule in P. and start symbol of G are the same as those of G. y = q A similar decomposition can be produced for strings of the form pbaq. and only if.42 CHAPTER 7. by Theorem 4. we may assume that G has no chain rules. 22. The derivation has the form n S ⇒ xAy ⇒ xwy = u ∗ ∗ ∗ . ∗ Repeating the subderivation A ⇒ vAx i times prior to completing the derivation with the ∗ subderivation A ⇒ w produces a string uv i wxi y in L. contradicting the assumption that L is context-free. where the second occurrence of A in the preceding derivation is the r + 1st rule application. and only if.3. Let z be any string of length 2 or more in L. That is. Σ. Because of the restriction on its length. the string uvxy must have length less than k as desired. Inductive step: Let u be a sentential form of G derived by n + 1 rule applications. Then there is a linear grammar G = (V. 20. Pumping z produces the string uv 2 wx2 y. where p and q are strings from Σ∗ . but not all three types of terminals in z. Since the sentential form uvAxy is generated by at most r rule applications.

For simplicity. G = (V. y R AxR is a sentential form of G derivable by n rule applications. 28. a) Let L be a context-free language over Σ. Using the rule A → w R in P . In a like manner. the erasure of u is in L(G ) and era (L) ⊆ L(G ). Let G = (V.SOLUTIONS TO EXERCISES where A is the leftmost variable in xAy and A → w is a rule in P. The proof follows from a straightforward examination of the derivations of G and G . c) The mapping from {ai bi ci | i ≥ 0} that erases the c’s maps a noncontext-free language to a context-free language. Σ. . We will build a context-free grammar G that generates h(L) = {h(w) | w ∈ L}. then A → u : ai /ui . 27. The rules of G are obtained from those of G by substitution. . ∗ The subderivation S ⇒ v uses only modiﬁed rules of G. It follows that L(G ) = L(G)R . Reordering the rules in the derivation of u so that ∗ ∗ applications of the rule X → λ come last. for all i is a rule of G . P ∪ {X → λ}) be the grammar obtained from G by replacing all occurrences of the terminal a in the right-hand side of the rules of G with a new variable X. S. Let Σ = {a1 . Conversely. for every u ∈ L. is era (L). mimicking its derivation in G using the corresponding rules of G produces h(w). uR is derivable by the rightmost derivation S ⇒ y R AxR ⇒ y R wR xR = uR . For any string w ∈ L. The image of this mapping. S. 43 By the inductive hypothesis. is a homomorphism from Σ∗ to Σ∗ . we can prove that the reversal of every sentential form derivable in G is derivable in G. and h a homomorphism from Σ∗ to Σ∗ . Then there is a derivation S ⇒ u in G. h(b) = b. Thus. for all b = a. b) First we note that a mapping h : Σ → Σ on the elements of the alphabet can be extended to a homomorphism on Σ∗ as follows: a string u = ai1 ai2 · · · aik maps to the string h(ai1 )h(ai2 ) · · · h(aik ). (See the solution to Exercise 27 for an explanation of the notation. Completing the derivation with the rule X → λ produces u : a/λ. If A → u is a rule of G. . let u be any string in L(G ). Performing the same derivation ∗ with the transformed rules of G yields S ⇒ u : a/X. . P) be a context-free grammar that generates L and let G = (V ∪{X}. The two preceding inclusions show that L(G ) = era (L) and consequently that era (L) is context-free. it suﬃces to show that it is generated by the grammar G . let u : y/x represent a string u ∈ Σ∗ in which all occurrences of the symbol y in u are replaced by x. The grammar G generates era (L). Moreover the derivations in G can only produce strings of this form and L(G ) = h(L) as desired. Using the preceding observation. Σ − {a}. Σ. The set of rules of G is completed by adding the rule X → λ. an } and h(ai ) = xi . Thus v : X/a is in G and u is in era (L). ∗ n . S. P) a context-free grammar that generates L. Let u be a string in L. u is generated by a derivation of the form S ⇒ v ⇒ u. the mapping on Σ deﬁned by h(a) = λ. which is context-free by part a).) To show that h(L) is context-free. It follows that L(G ) ⊆ era (L).

PUSHDOWN AUTOMATA b) Let Σ = {a. Since the class of context-free languages is closed under inverse homomorphic images (Exercise 29). h(b) = bb. b. and h(c) = ccc from elements of Σ to strings over Σ∗ .44 30. CHAPTER 7. it follows that {ai b2i c3i | i ≥ 0} is not context-free. the pumping lemma was used to show that the latter language is not context-free. .1.4. c} and let h : Σ∗ → Σ∗ be the homomorphism obtained by using concatenation to extend the mapping h(a) = a. In Example 7. The inverse image of {ai b2i c3i | i ≥ 0} under the homomorphism h is {ai bi ci | i ≥ 0}.

3.Chapter 8 Turing Machines 1. a) Starting with the rightmost symbol in the input and working in a right-to-left manner. a) q0 BaabcaB Bq1 aabcaB Baq1 abcaB Baaq1 bcaB Baacq1 caB Baaccq1 aB Baaccaq1 B Baaccq2 aB Baacq2 ccB Baaq2 cbcB Baq2 abbcB Bq2 acbbcB q2 BccbbcB b) q0 BbcbcB Bq1 bcbcB Bcq1 cbcB Bccq1 bcB Bcccq1 cB Bccccq1 B Bcccq2 cB Bccq2 cbB Bcq2 cbbB Bq2 cbbbB q2 BbbbbB c) The state diagram of M is a/a R b/c R c/c R q0 B/B R q1 B/B L a/c L c/b L q2 d) The result of a computation is to replace the a’s in the input string with c’s and the c’s with b’s. the machine diagrammed below moves each symbol one position to the right. 45 .

change all of the X’s to a’s and Y ’s to b’s. Similarly. If the input is the null string. repeat the process on the unmarked string. iv. If there are no unmarked symbols on the tape. v. Mark tape position 0 with a #. the computation halts in state qf with the tape head in position zero as desired. The strategy employed by the machine is i. Move to the end of the string and use strategy from the machine in part a) to move the unmarked input on square to the right. change the ﬁrst symbol to X or Y to record if it was an a or b. A Turing machine that accomplishes these tasks is X/X R Y/Y R B/B R q8 a/a R b/b R q0 B/# R q1 a/X R b/Y R q2 a/a R b/b R a/B R B/B L q4 b/B R q5 B/a L q7 q3 B/B L B/B L q9 X/a L Y/b L B/B L q6 B/b L B/B L #/B R q10 a/a L b/b L qf . TURING MACHINES q2 a/B R qf b/B R B/a L q3 q4 B/b L B/B L On reading the ﬁrst blank following the input. ii. respectively. If the input string is not empty. Otherwise. c) A Turing machine to insert blanks between the symbols in the input string can iteratively use the machine from Exercise 3 a) to accomplish the task. an a is moved to the right by transitions to states q 2 and q3 . the head moves to the left. This process is repeated until the entire string has been shifted. iii.46 a/a R b/b R q0 B/B R q1 B/B L CHAPTER 8. states q4 and q3 shift a b. Otherwise. and halt in position 0.

Shifting a string one square to the right is accomplished by states q3 to q7 . The arc from q4 to q8 when the shift has been completed. c) A computation of b/b R X/X R q0 B/B R B/B L q5 B/B R q6 X/X L q1 a/X L b/b L X/X L q2 B/B R a/a R X/X R q3 b/X L a/a L X/X L q4 B/B R begins by ﬁnding the ﬁrst a on the tape and replacing it with an X (state q1 ). x) is undeﬁned for all qi ∈ F accepts L(M) be ﬁnal state. After all the a’s have been processed. q0 .SOLUTIONS TO EXERCISES 47 Processing the null string uses the arc from q1 to qf . x) whenever δ(qi . Thus entering qf in M is equivalent to halting in an accepting state of M and L(M) = L(M ). F) with transition function δ deﬁned by i) δ (qi . the recursively enumerable languages. The computation halts in the accepting state q 6 if no b’s remain on the tape. We construct a machine M that accepts L(M) by entering. Computations of M are identical to those of M until M enters an accepting state. Σ. the entire process is repeated by entering q 1 . δ. F) be a Turing machine that accepts by entering. x) for all qi ∈ F ii) δ (qi . M halts accepting the input. δ . q0 . x) = δ(qj . . M is deﬁned by the quintuple (Q ∪ {qf }. We refer to the method of acceptance deﬁned in the problem as acceptance by entering. x. q0 . q0 . If a b is encountered in state q3 . A computation of M may enter and leave accepting states prior to termination. δ . Γ. A computation of M that accepts an input string halts in an accepting state q i . q3 . the intermediate states have no bearing on the acceptance of the input. the entire string is read in q 1 and q5 is entered upon reading the trailing blank. δ. q4 . x) = δ(qi . Σ. When this occurs. the computation halts in state q3 rejecting the input. x) is deﬁned ii) δ (qi . A computation of M that enters an accepting state halts in that state in M . an X is written and the tape head is repositioned to repeat the cycle q1 . Γ. {qf }). We show that the languages accepted by entering are precisely those accepted by ﬁnal state. Let M = (Q. q2 . The machine M = (Q. Σ. Γ. F) be a Turing machine that accepts by ﬁnal state. R] if qi ∈ F and δ(qi . that is. The transitions of M are deﬁned by i) δ (qi . Γ. The corresponding computation of M reaches qi and then enters qf . x) = [qf . If no matching b is found. Σ. If another shift is needed. Now let M = (Q. 5. 7. the sole accepting state of M . x) is undeﬁned. The tape head is then returned to position zero and a search is initiated for a corresponding b.

. y. δ. This can be accomplished using the transitions δ (qi . Let M be a Turing machine (Q. L] for w = z where x is a symbol not in tape alphabet of the context-sensitive machine. Regardless of the symbol being scanned. L] δ (qi1 . x) = δ(qi . z. A standard Turing machine can be considered to be such a machine whose transition function does not include any stationary transitions. y. R] for every transition δ(qi . i) δ (qi . the subsequent transition moves left to the original position and enters qj . d] δ (qi . yz. The ﬁrst transition prints a y. Thus the family of languages accepted by these types of machines are identical. x . If the contextsensitive transition is not applicable. xzi ) = [qji . z2 . . We will build a contextsensitive Turing machine M that accepts L(M). d] for all z ∈ Γ. Σ. a) Let M = (Q. Σ. x) whenever δ(qi . R} A pair of standard transitions are required to perform the same action as a stationary transition δ(x. Clearly. and rewrite the original position accordingly. b) Assume that the context-sensitive transition function has transitions δ (qi . w) = [qi .48 CHAPTER 8. c) By part a) we see that every recursively enumerable language is accepted by a contextsensitive Turing machine. yi zi . d] of M. zn from the tape alphabet. q0 . . d] where d ∈ {L. Γ. Γ. A transition of M that designates a movement of the tape head generates an identical transition of M . x) = [qt . x) = [qj . Part b) provides the opposite inclusion. x ) = [qjn . d] for symbols z1 . . . The transition function δ of M is constructed from that of M. w. d] . x) = [qj1 . F) with stationary transitions. where a new state qt ∈ Q is is added for every stationary transition iii) δ (qt . We construct a standard Turing machine M that accepts L(M). zn . q0 . . For every transition δ(qi . moves right and enters a new state. qi ) = [qj . given a contextsensitive machine we can build a standard machine that accepts the same language. TURING MACHINES 8. Intuitively. x) = [qi . The components of M are the same as those of M except for the transition function δ . z1 ) = [qi1 . y. zn ) = [qin . L] for every z ∈ Γ and every state qt added in (ii) 10. the sequence of standard transitions will check the symbol to the right and halt in state qi . δ (qi . F) be a standard Turing machine. yn . y. δ. S]. x) = [qj . ii) δ (qi . y1 . R] δ (qi . y. . z1 . every recursively enumerable language is accepted by a Turing machine in which stationary transitions are permitted. S]. x) = [qj . z) = [qj . The computation of the standard machine should read the x in state qi . a computation of M ignores the second input symbol in the transition. L] δ (qin . M has the transitions δ (qi . determine the symbol on the right. xz) = [qj .

49 b) The two-tape Turing machine with tape alphabet {a. S] for every x ∈ {a. S. b}. R] = [q1 . B. States p 0 to p7 nondeterministically select a substring of length three or more from the input. B) = [q1 . B) δ(q3 . R. B. R. R. Each pass requires n + 1 transitions. left-to-right. S] = [q2 . B. B) δ(q2 . L. x. The ﬁrst is the ability to use existing machines as components in more complex computations. The second feature is the ability of a nondeterministic design to remove the need for the consideration of all possible substrings. L] = [q4 .SOLUTIONS TO EXERCISES 14. R] = [q2 . B. The ﬂexibility and computational power that can be achieved by combining submachines will be thoroughly examined in Chapter 9. the strings on the two tapes are compared. and then right-to-left through the input. x) δ(q3 . the maximal number of transitions for an input string of length n is 3(n + 1). B. S] = [q3 . The computation of a deterministic machine to solve . B. The accepting states of the composite machine are the accepting states of M. B. b} accepts the palindromes over {a. B) δ(q1 . L] = [q3 . B. B. 19. B} and accepting state is q 4 deﬁned by the transitions δ(q0 . The input is copied onto tape 1 in state q 1 . This problem illustrates the two important features in the design of Turing machines. The machine constructed in Exercise 5 (c) provides the evaluation of a single substring needed in this computation. x. the computation terminates in q 4 . Using M. State q2 returns the head reading tape 1 to the initial position. a/B R b/B R p0 B/B R p1 b/b R a/a R p2 b/b R a/a R p3 b/b R a/a R b/b R a/a R p4 b/B R a/B R B/B S p5 b/B R a/B R b/b L a/a L To M: B/B R p7 b/b L a/a L p6 B/B L B/B L q1 The transition to state q1 of M begins the computation that checks whether the chosen substring has the same number of a’s and b’s. If both heads simultaneously read a blank. b} for every x ∈ {a. Nondeterminism is used to ‘choose’ a substring to be examined. x. B) δ(q1 . B. B. B. The computation of a string that is not accepted halts when the ﬁrst mismatch of symbols on tape 1 and tape 2 is discovered. The maximal number of transitions of a computation with an input string of length n occurs when the string is accepted. Tape head 1 reads right-to-left. With tape head 1 moving left-to-right and head 2 moving right-to-left. x. B. b. Thus. a composite machine is constructed to determine whether an input string contains a substring of length three or more with the same number of a’s and b’s. x. R. b} with the same number of a’s and b’s. L. b} for every x ∈ {a. x. B) δ(q2 . x. A deterministic machine M was constructed in Exercise 5 (c) that accepts strings over {a. x.

If u in not in L(M). The maximal number of transitions occurs when all but the ﬁnal symbol of the input string is copied onto tape 2. the head on tape 2 moves to tape position n. M simulates the next computation. etc. back to position 0. If the input is not in L(M). The machine M checks the length of non-prematurely terminating computations of M prior to simulating a subsequent computation of M. B/B L] [x/x S. another substring is is generated and examined. The simulation of a computation of M is guided by a sequence of numbers that are generated on tape 3. B/B R] [x/x R. This can be accomplished by adding a fourth tape to M that records the length of the longest simulated computation that has not halted prematurely. Let M be a nondeterministic Turing machine that halts for all inputs. After a numeric sequence of length n is generated tape 3. M halts and rejects. In this case. the preceding approach cannot be used to show that L is recursive since the computation of M will never terminate for a string that is not in the language. and begins the comparison. Unfortunately. The head on tape 2 is repositioned at the leftmost square in state q2 . Otherwise. y/y R] q3 [B/B R. b}∗ }. we must construct a deterministic machine that halts for all inputs. In the diagram. If a blank is read simultaneously on these tapes. The two-tape nondeterministic machine whose state diagram is given below accepts {uu | u ∈ {a. b}. Finding any mismatch between the tapes in this comparison causes the computation to halt without accepting the input. M indeﬁnitely continues the cycle of generating a sequence of numbers on tape 3 and simulating the computation of M associated with that sequence.50 CHAPTER 8. M will halt and reject u. B/x R] q0 [B/B R. B/B R] q4 The computation begins by copying an initial segment of the input on tape 1 to tape 2. the computation of M will simulate an accepting computation of M and accept u. This can be accomplished in 2(length(w) + 1) transitions. Upon the termination of a simulated computation that did not halt prematurely. beginning with computations of length 0. M systematically simulates all computations of M. the string is accepted. To show that L(M) is recursive. A nondeterministic transition to state q2 represents a guess that the current position of the head on tape 1 is the beginning of the second half of the string. The remainder of the string on tape 1 is then compared with the initial segment that was copied onto tape 2. [x/x R. If a string u is in L(M). . length 2. Let k be the length of the longest computation of M with input u. When one of the simulated computations accepts the input string.7 demonstrates that L(M) is recursively enumerable. If not. 21. B/B R] q1 [x/x S. TURING MACHINES this problem must select a substring of the input and decide if it satisﬁes the conditions. y/y L] q2 [x/x S. The technique introduced in the construction of an equivalent deterministic machine M given in Section 9. then all computations of M will terminate. If not. 23. tape 4 is checked to see if there has been a previous simulated computation using n − 1 transitions. length 1. This process must be repeated until an acceptable substring is found or all substrings have been generated and evaluated. x and y represent arbitrary elements from {a. When the simulations reach computations with k+2 transitions. the length of the sequence on tape 3 is recorded on tape 4. M halts and accepts. A computation terminates prematurely if the simulation terminates before executing all of the transitions indicated by the sequence on tape 3.

B/0 L] [B/B S. 1]. q0 . 1]. pj . That is. x. A computation begins by copying the input onto tape 2 and repositioning the tape heads at the initial position. B. B) = [r1 . B/B R] q2 [B/0 R. B. y. S. 2]. q0 . y) indicates that a transition of M2 is to be executed on tape 2. Since every computation of M halts. t. there are machines M1 = (Q1 . Conversely. pj . 1/1 L] q0 [B/B R. x. Γ2 . L] iv) δ(r2 . either the computation of M1 or M2 terminates. B) = [r2 .1 generates all nonnull strings over an alphabet {1. B/B R] q3 [B/B S. p0 ) that accept L and L by halting. x) = [pn . S] vi) δ([qi . Thus the set of ﬁnal states of M consists of triples of the form [qi . 0/0 R] [B/1 R. L. S. i) δ(r0 . These machines provide suﬃcient information to decide whether a string u is in L. R. 0/1 L] [B/B S. x. δ2 . a state is an ordered triple consisting of a state of M1 . 2]. Similarly. δ1 . 2]. 0/0 R] q4 [B/B S. Σ. M processes a transition of M1 on tape 1. y) = [[qn . x. R. B) = [[q0 .3.SOLUTIONS TO EXERCISES 51 26. 32. The strategy of this machine can be employed to build a two-tape machine that enumerates all strings over {0. d2 ] whenever δ2 (pj . M2 halts when u ∈ L. [B/B S. R] for every x ∈ Σ iii) δ(r1 . Σ. Thus L is not only recursively enumerable. x. Σ. then the computation of M1 halts. pj . q0 ) and M2 = (Q2 . 1/1 R] q1 [B/B S. x) = [qn . B. a transition of the form δ([qi . Γ. Q − F) accepts the complement of L. x. M then ‘runs’ M1 on tape 1 and M2 on tape 2. y) = [[qi . L. Intuitively. . t. the machine (Q. a state of M2 and an element of {1. Γ. d1 ] vii) δ([qi . Σ. On the other hand. 2}. R] ii) δ(r1 . pj . x. B. d2 ] When a 1 occurs in the state. Γ1 . but also recursive. S] whenever δ1 (qi . 1}.2. 0/0 L] [B/B S. . B. 1/0 L] [B/B S. A string is accepted if the computation halts due to the lack of a transition for a conﬁguration on tape 1. δ. δ. we must show that L is recursive when both L and its complement are recursively enumerable. 2. The states of M that simulate the computation of the composite machines are elements of the Cartesian product Q1 × Q2 × {1. B. We construct a two-tape machine M that simulates the computations of both M 1 and M2 . x. 1]. The status of the computation of M2 is unaﬀected by such a transition. . 1]. pj . s. B/B L] . the strings are generated on tape 2 and then output on tape 1. If u ∈ L. L] for every x ∈ Σ v) δ(r2 . x. pm . By Theorem 8. The machine in Figure 8. . B/B S] [B/# R. B) = [r1 . B. p0 . x. x) = [r2 . The input alphabet of M is Σ. the tape alphabet is Γ1 ∪ Γ2 and the start state is r0 . s. d1 . For every input string. 2}. Let M = (Q. B. This is accomplished by the transitions deﬁned in (i) through (v) below. n}. F) be a Turing machine that demonstrates that L is a recursive language. B.

All the elements in the square bounded by [0. does not require the machine to halt when the entire language has been written on tape 1. and the work tape. For a string not in the language that is lexicographically greater than any string in the language. The number on the diagonal tape is incremented.a 1 is erased from the work tape. Loop: 1. n] have already been written on the enumerating tape. 33. A # is written on tape 1 followed by the string in the input position on tape 2. [0.1.7 is never triggered.the string on the diagonal is copied tape to the enumeration tape.8. The diagonal pair [n + 1. the diagonal tape. The enumerating machine has three tapes: the enumeration tape. n + 1]. [n + 1. . .# is written on the enumeration tape. .52 CHAPTER 8.# is written on the enumeration tape. . 2. . We will outline the strategy employed by a Turing machine that enumerates all ordered pairs of natural numbers. an enumerating machine may continue indeﬁnitely. 5. Initialization: 1. n + 1]. Each subsequent iteration through the loop increases the size of the square in the N×N that has been enumerated. . n + 1Bn + 1#. is written on the enumerating tape. Entering the loop that comprises the main portion of the computation. .B is written on the enumeration tape.B is written on the enumeration tape.8. 1 is written on the diagonal tape. 0] and [n. While the work tape is not blank . n − 1]. The initialization writes the ordered pair [0. 3. The loop beginning at step 4 writes the pairs [n. #1B1 # is written on the enumerating tape. . The diagonal element [n + 1. .a 1 is erased from the work tape. step 6 writes the pairs [n + 1. the halting condition given in Case 2 of Theorem 8.the string on the diagonal tape is copied to the enumeration tape. [n + 1. 0]. the diagonal tape holds the value of a diagonal element [n. . the deﬁnition of the enumeration of a language by a Turing machine. . 6. While the work tape is not blank . After completely listing the strings in a ﬁnite language. 4. . the tape head on tape 1 remains stationary while the next string is generated on tape 2. . n + 1] is written on the enumerating tape in step 2. . n is written on the work tape. n + 1] on the enumerating tape. n + 1]. In a like manner. 34.the string on the work tape is copied to the enumeration tape. . [n − 1. n]. . n] in the grid N×N. The number n will be represented by the string 1 n+1 and we will denote this representation by n. . Deﬁnition 8. producing n + 1. 2. n is written on the work tape.the string on the work is copied tape to the enumeration tape. 0] on enumerating tape. TURING MACHINES The cycle of producing and writing a string on tape 1 begins in q1 . For the remainder of the cycle. The computation consists of an initialization phase followed by a loop that enumerates the ordered pairs.

d) The Turing machine whose state diagram is given below computes the relation even(n) = 1 0 if n is even otherwise. 5.checks to see if 1 is on the tape.Chapter 9 Turing Computable Functions 2. When a computation of M halts. The input string represents an even number if a blank is read in q2 and an odd number if a blank is read in q3 . . A machine M that accepts L can be built by extending M. M enters an accepting state and halts. b) The ﬁnd left macro FL B/B L q0 1/1 L 1/1 L qf moves right-to-left reading blanks until it encounters a 1. the input is erased and the appropriate answer is written on the tape. 53 . otherwise . M .M enters a rejecting state and halts.if so. 1/B L q0 B/B R q1 1/X R B/B L 1/B L X/1 R q5 B/1 L q6 1/1 L qf q4 X/1 L q2 1/1 R 1/1 R q3 B/B L q7 After determining the parity. The computation then traverses the sequence of 1’s and halts in state qf . 6. Let M a machine that computes the partial characteristic function of a language L.

n). TURING COMPUTABLE FUNCTIONS a) A machine M that computes the function f (n) = 2n + 3 can be obtained by combining the copy. The machine to compute gt uses this relationship and the machine LT from Exercise 5 f): . Machine CPY1 MR1 CPY1 MR1 (1) C3 ML1 A ML1 A Conﬁguration BnB BnBnB BnBnB BnBnBnB BnBnBnB BnBnB3B BnBnB3B BnBn + 3B BnBn + 3B B2n + 3B (1) 8. a) A machine can be constructed to compute the characteristic function of the greater than relation 1 if n > m gt(n.54 7. Tracing the computation of M with input n illustrates the interaction among the machines that make up M. m) = lt(m. add. * CPY1 * MR1 * CPY1 * MR1 * ML1 * A * ML1 * C (1) 3 * A * (1) The machine C 3 computes the function c 3 . CHAPTER 9. which was shown to be Turing computable in Example 9. m) = 0 otherwise.2. and constant macros.4. The relations lt and gt satisfy the equality gt(n.

Modifying h by deﬁning h(n) to be 0 for all odd n produces a pair of functions g and h that provide a solution to Exercise 11 a). . Blt(m. a) Let g be the function g(n) = 2n and let h be h(n) = n/2 ↑ if n is even otherwise. 13.1 MR1 LT ML1 E1 T 10. n). add(id(n). . h ◦ g is undeﬁned. causes the entire function to be undeﬁned.SOLUTIONS TO EXERCISES 55 * CPY1. consequently. n)B B . id(n))) add(mult(n. id). id)) is a one-variable function. add(n. add ◦ (id. n)B add(mult(id(n). A computation consists . a) The function f = add ◦ (mult ◦ (id. n)) add(n · n. it is the identity function. e ◦ produces an undeﬁned value in the second component of the ordered pair. n)B Blt(m. g(n) is an odd number and. n + n) n2 + 2 · n e◦p (2) p2 (2) 2 ) is the two-variable empty function. We construct a machine M that returns the ﬁrst natural number n for which f (n) = 0. Regardless of the value of the arguments. Conﬁguration BnBmB BnBmBnB BnBmBnB BnBlt(m. id(n)). Evaluating f with argument n yields f (n) = = = = b) The function p (2) (2) 1 ◦ (s ◦ p 1 . The underscore indicates the position of the tape head in the conﬁguration. Let f be a Turing computable function and F a machine that computes f . n)B BnBlt(m. 12. Machine CPY1. This. Not only is h ◦ g total. the evaluation begins with the input provided to the occurrences of the identity function in f .1 * MR1 * LT * * T * E1 * ML1 The action of the composite machine GT is exhibited by tracing a computation. in turn. For any n. b) Let g be g(n) = 2n + 1 and h be the function deﬁned in part a).

56 CHAPTER 9. . Γ. 15. F) be a machine that accepts L. A machine that accepts R can be built using M and the branch macro. If f never assumes the value 0. That means. for n = 0. . the composite machine accepts R. and the subsequent f value is computed. To show that R is computable. the computed value is erased. 1. The language L deﬁnes a unary relation R on the natural numbers by R(n) = 1. To do this. 1n+1 ∈ L. the counter is incremented. M must record the furthest right on the tape reached during a computation of M. we augment the tape alphabet to . δ. since M must erase the tape prior to writing the answer. the computation will return n where n is the ﬁrst value for which f (n) = 0 only if f is deﬁned for all m < n. If f is not total. otherwise. and only if. Let R be a Turing computable unary relation and M a machine that computes the characteristic function of R. The cycle of generating a natural number and computing the f value halts when a natural number n is found for which f (n) = 0. Σ. Otherwise. 0. 17. the computation continues indeﬁnitely. q0 . * n=0 CPY1 * MR1 * F * BRN * n>0 * * S * ML1 * E1 The computation begins with the counter 0 on the tape. . we build a machine M that writes the correct answer on the tape at the end of a computation of M. A working copy of 0 is made and F is run on the copy. the input string is not an element of R. . M loops upon encountering the ﬁrst value for which f (m) ↑. n=0 * M * BRN n>0 * * The computation of M halts with 0 on the tape if. The process is not quite as simple as it sounds. The BRN macro is used to determine whether the computed value of f is 0. Consequently. If not. until a value n for which f (n) = 0 is encountered. Let L ⊆ {1}+ be a recursive language and let M = (Q. TURING COMPUTABLE FUNCTIONS of a cycle that produces BnBf (n)B.

2 . R] δ (qi . M ﬁnds the right endmarker. At this point it writes the new endmarker [R. x] and [R. R] When a move to the right is indicated with the tape head reading the right endmarker. B) = [qi. L] for every transition δ(qi . y. R] δ (qi. x. x) = [q2 . B]u[R. B]. (d) If the simulation of M indicates that M would halt in a nonaccepting state. [R. y. y. A computation of M with input u consists of the following steps: (a) Initially M writes [L. B. [R. the appropriate symbol is written and the tape head moves right. L] δ (qi . x) δ (qi . M erases the tape and writes 1 in the input position. x]) = [qj . R] δ (qi. y]. B) = [q2 .1 . x) for all x ∈ Σ for all x ∈ Σ Once the endmarkers are in place. (b) The computation of M on the string BuB is simulated by M on [L. B]. B]. the computation of M is simulated by the transitions δ (qi . R] for every transition δ(qi . [R. B] on the tape and enters qj to continue the simiulation of M. that act as left and right endmarkers of the portion of the tape read during a computation of M. x) = δ(q0 . x]) = [qi. x) = [q1 . L] δ(q2 .x. The start state of M is q0 and the transition function is denoted δ . x) = [qj . x]. R] δ(q1 . R] δ(q1 . L] for every transition δ(qi . and writes . B] and [R. L] δ(q2 . The transitions to insert the endmarkers are δ (q0 . x) = [qj . for each x ∈ Γ. x]) = [qj . (c) If the simulation of M indicates that M would halt in an accepting state. [L. x) = δ(qi . x) = [qj .1 .x.SOLUTIONS TO EXERCISES 57 include symbols [L. [R. [L. y. B]u[R. B) = [q1 . erases the tape.x. When M halts in an accepting state. B) = [qj . y]. x. producing [L. M erases the tape and writes 11 in the input position.x. [R. B]. x) for every transition δ(qi . B] in the squares on either of the input.2 . [L. B].

R] δ (q6 . 1. x) = [q3 . . x) undeﬁned. . B. . . . fi . there functions of the latter form that are uncomputable. δ (qi . . Assume that this set of functions is countably inﬁnite. . L] δ (q7 . A comparison of cardinalities will be used to show that there are uncomputable functions that satisfy f (i) = i for all even natural numbers. 1. . B. . R] δ (q5 . R] δ (qi . . B) = [q7 . x]. . f1 . R] δ (q3 . R] δ (q3 . This contradicts the assumption that the set is countably inﬁnite. A similar set of transitions write a negative answer when M halts in a nonaccepting state. B. [R. x]) = [q5 . Consider the function f (n) = n f(n−1)/2 (n) + 1 if n is even otherwise Clearly f is the identity on all even numbers.58 CHAPTER 9. . However. Since f = fn for all n. . x]) = [q4 . . of these functions. [L. x) = [q4 . Since the set Turing computable functions is countable and the set of number-theoretic functions that are the identity on the even natural numbers is uncountable. TURING COMPUTABLE FUNCTIONS the answer on the tape. x]) = [q4 . x. B. 1) = [qf . L] for x ∈ Γ for x ∈ Γ for x ∈ Σ for x ∈ Γ for x ∈ Γ for x ∈ Γ where qf is the terminal state of M . fi . f1 . [L.5. L] δ (q4 . f (2n + 1) = fn (2n + 1) . L] δ (qi . [R. 1. Countability implies that there is an exhaustive listing f0 . . x) = [q3 . . . x.1 we know that the set of computable number-theoretic functions is countable. f is not in the listing f0 . For every qi ∈ F with δ(qi . [L. By Theorem 9. x]) = [q3 . 19. . Diagonalization can be used to show that there are uncountably many functions that are the identity on the set of even natural numbers. . . f (1) = f0 (1) f (3) = f1 (3) f (5) = f2 (5) . L] δ (q4 . B) = [q6 .

The rules Ab → bA. Rules S → X and S → Y initiate derivations that generate λ.Chapter 10 The Chomsky Hierarchy 1. a) The unrestricted grammar S → X | Y | aP AbQb X → aaX | λ Y → bbY | λ P → aP A | λ Q → bQb | E Ab → bA AE → a Aa → aa generates the language {ai bj ai bj | i. Let G = (V. j ≥ 0}. The derivation of aabaab demonstrates the repositioning of the variable A and its transformation to the terminal a. 59 . The rules of G are i) T (u) → T (v) for every rule u → v ∈ P ii) Ta → a for every a ∈ Σ. For a string u ∈ (V ∪ Σ)∗ . with i. and Aa → aa are then used to obtain the desired conﬁguration of the terminals. AE → a. Repeated applications of the P and Q rules produce ai Ai bj Ebj . The set of variables of V consists of the variables of V and a new variable Ta for every a ∈ Σ. P . S ⇒ aP AbQb ⇒ aaP AAbQb ⇒ aaAAbQb ⇒ aaAAbEb ⇒ aaAbAEb ⇒ aabAAEb ⇒ aabAab ⇒ aabaab 3. Σ. We construct a grammar G = (V . S) that generates L(G) in which each rule has the form u → v where u ∈ (V )+ and v ∈ (V ∪ Σ)∗ . Σ. P. and b2i . j > 0. a2i . let T (u) denote the string obtained by replacing each terminal a in u with the corresponding variable Ta . S) be an unrestricted grammar. The derivation of a string ai bj ai bj . begins with an application of S → aP AbQb.

we see that L(G) = L(G ). We will construct an unrestricted grammar G that generates L1 L2 from grammars G1 = (V1 . The B rules produce a matching C for each b and. P2 . ∗ ∗ ∗ consider derivations S1 ⇒ x in G1 and S2 ⇒ y in G2 . and y = ck−1 is a decomposition in which v and x are pumpable. v = b. Using the rules deﬁned in (ii) to transform each variable Ta to the corresponding terminal. and only if. we must be careful in combining unrestricted grammars in this manner since the left-hand side of a rule may contain more than one symbol. possibly. When G1 and G2 satisfy these conditions. and C’s. As before. P1 . Let z be any string in L. a) S ⇒ SBA ⇒ aBA ⇒ aAB ⇒ aaBB ⇒ aabB ⇒ aabb ∗ b) The language of G is {ai+1 b2i | i ≥ 0}. the transformation obtained by the application of the rule u → v may produce a string that is not in the concatenation of L1 and L2 . S ⇒ T (w) is a derivation of G . The S rule puts a c on the end of the string. 8. which established that the context-free languages are closed under concatenation. P1 ∪ P2 ∪ {S → S1 S2 }. Σ1 .1. a) The language L satisﬁes the pumping lemma. L is nevertheless not context-free. The grammars G1 and G2 must be constructed to insure that no rule can overlap the strings derived from S1 and S2 . S1 ) and G2 = (V2 . c) L(G) is generated by the rules S → aSbb | a. b’s. THE CHOMSKY HIERARCHY The grammar G contains a derivation S ⇒ w if. 10. the grammar G deﬁned above generates L1 L2 . we also assume that V1 and V2 are disjoint. w = λ. additional C’s.5. S) was constructed from G1 and G2 where S ∈ V1 ∪ V2 and V1 and V2 are assumed to be disjoint. Let u → v be a monotonic rule with length(v) > 2.60 ∗ CHAPTER 10. 4. Even though the pumping lemma is satiaﬁed. Using Exercise 3. We deﬁne a set P of monotonic rules that produce the same transformation as u → v. For example. 6. The recursive A rules generate a matching number of a’s. The grammar G = (V1 ∪ V2 . In this case. b) The context-sensitive grammar S → aAbc A → aAbC | B B → bBC | BC | bC | C Cc → cc generates the language {ai bj ck | 0 < i ≤ j ≤ k}. The C’s are moved to the end of the string by the Cb → bC rule and then turned to c’s by Cc → cc. Unfortunately. c) Let L1 and L2 be two recursively enumerable languages. Let us ﬁrst recall the proof of Theorem 7. x = c. Selecting u = ai bj−1 . Now either P1 or P2 may contain a rule u → v where xy = x uy . Σ2 ∪ Σ2 . S2 ) that generate L1 and L2 respectively. then z = a i bj ck with 0 < i ≤ j ≤ k. we can assume that the left-hand side of every rule in G1 and G2 consists solely of variables. Σ2 . It follows that S ⇒ xy. The rules of P are constructed so that a string .

. . . either right or left. If the depth is 0. B. . .SOLUTIONS TO EXERCISES 61 on the right-hand side of a rule has length less than length(v). i) If length(u) = 1. This process can be repeated any number of times to produce a string in L. If the full binary tree has inﬁnite depth. This procedure can be repeated until the length of the right-hand of each rule is at most two. 11. If not. 2n. followed by a move up the tree. a string is accepted if it satisﬁes the following two conditions: a) the number of steps that move down tree equals the number of steps that move up. . are entered when a string has i more U ’s than L’s and R’s. are in the language. States qi . . In general. This follows since the most steps down the tree that can be taken from the root is one. the right-hand side of each rule produced using these transformations has length at most length(v) − 1. every occurrence of a terminal a in u and v is replaced by a new variable Ta and the rule Ta → a is added to P as in Exercise 3. ii) If length(u) > 1.5. a DFA with 2n + 2 states can be constructed to accept the language. The rules AE → XY X→B Y u → CDv generate the transformation u → v. the language consists only of the null string. Using two new variables X and Y . . n. 13. then u → v can be written A → BCDv where A. States qi . the monotonic rules A → XY X → BC Y → Dv deﬁne the same transformation as u → v. then u → v can be written AEu → BCDv . i = 1. . The answer depends on the depth of the tree T. The string transformation obtained by applying the rule AB → CD can be obtained by the sequential application of the three rules AB → XB XB → XD XD → CD Each of these rules has the context-sensitive form uAv → uwv with w = λ. . . C. The construction of P is divided into two cases. In either case. . If the tree has depth 1. State q0 is the accepting state and represents a string with the same number of U ’s as L’s and R’s. . are entered when a string has i more L’s and R’s than U ’s.2 to establish that the language is not regular. D ∈ V and v ∈ V∗ . the strings λ. LU . which is regular. the language is (LU ∪ RU )∗ . Li U i . i = n + 1. the DFA enters an error state qe and rejects the string. . For a tree of depth n. If the string represents a path the would leave the tree. . LLU U . Without loss of generality we assume that u. This sequence can be used with Corollary 6. . . snd b) the steps do not specify a move down from a leaf or up from the root. v ∈ V∗ .

The tape head is then moved to position 1 to read the blank preceding the input string. q0 . c] of M performs the same actions as δ[qi . 15. Otherwise.62 CHAPTER 10. then the ﬁnal c is erased and the input shifted one position to the right producing Bwcj . δ. The language L = { wci | M accepts w by a computation in which the tape head does not read tape position length(w) + i + 1} is accepted by a linear-bounded machine M constructed from M. A string is accepted if the computation processes the entire string and halts with an empty stack. the computation of M will remain within the segment of tape enclosed within the endmarkers. the transitions of M are identical to those of M except M treats the c as a blank. If so. Consequently M accepts wci . THE CHOMSKY HIERARCHY To show that the language is context-free. A computation of M with initial tape conﬁguration u begins by checking if the u has the form wc j+1 for some string w ∈ Σ∗ and j ≥ 0. Moreover. the computation rejects the string. Choosing i so that length(w) + i + 1 > k. A transition of δ [qi . Let k be the furthest tape position scanned during the computation of M that accepts a string w. When a U is read. the only way that M accepts a string wci is to mimic a computation of M that accepts w that remains within the tape the ﬁrst length(w) + i tape positions. F) and let c be a terminal symbol not in Σ. Σ. For the remainder of the computation. Whenever an L or R is read. B] of M. Let L be a recursively enumerable language accepted by the standard Turing machine M = (Q. a A is pushed onto the stack. if there is an A on the stack it is popped. Γ. a pushdown automaton can be constructed to accept it. .

7. we assign a number to each of the variables and terminals. In the latter case. In this solution we represent the natural number n by 1 n . or A → λ where A. there are three possible outcomes: length(u) = 2i and the input is accepted in step 3.all tape heads are repositioned at the initial position.Chapter 11 Decision Problems and the Church-Turing Thesis 4. A three-tape Turing machine can be designed to solve the “2n ” problem. length(u) < 2i and the input is rejected in step 4. S) has rules of the form A → aB. If |V | = n then we number the variables from 1 to n with S assigned 1. a) A regular grammar G = (V. 3. then . 1. 4. the computation halts rejecting the input. . and . If a blank is read on tape 2 and a 1 on tape 1. . To construct the encoding of the rules of the grammar over the set {0 . .tape 3 is erased and the string from tape 2 is copied to tape 3. The encoding of a string w over (V ∪ Σ)∗ . the computation halts accepting the input. If a blank is read on tape 1 and a 1 on tape 2. . Reading left to right. . The numbers 20 . 2. When the length of the input string u is compared with the string on tape 2.the string on tape 3 is concatenated to the string on to tape 2. the length of the strings on tapes 1 and 2 is compared. 22 . 5. tapes 2 and 3 both contain the representation of 2 i . 63 . P.the computation continues with step 2. Tape 3 is used in the generation of the sequence on tape 2. A → a. 1 }. A 1 is written on tapes 2 and 3. The encoding of a variable A and a terminal a will be denoted en(A) and en(a). If blanks are simultaneously read on tapes 1 and 2. The symbol assigned number n is encoded by the string 1 n . 21 . or length(u) > 2i . the string 2i+1 is generated in step 5 and the comparison is repeated. B ∈ V and a ∈ Σ. The computation of the machine with input u consists of the following actions. The terminals are numbered from n + 1 to n + m where m = |Σ|. denoted en(w). consists of the encoding of each of the symbols in w separated by 0 ’s. are sequentially produced on tape 2 and compared with the input string on tape 1. At the beginning of step 2. Σ.

iv) If the string has the form x∗ y + . If so. If they are identical. steps 3–5 are repeated. The actions of a machine M that solves the derivability problem are described below. respectively.4. The appropriate action is determined by the form of input string. A computation of U1 initially determines whether the input has the form R(M)w. 7. U 1 accepts by ﬁnal state and has a single accepting state.1. then the U1 enters the accepting state and halts. 1 } of the form R(M)w where the computation of M with input w prints a 1. M halts and accepts the input. . and en(A)0 en(a)0 en(B). the input is erased and a is written in the input position. The universal machine U is modiﬁed to construct a machine U1 that accepts strings over {0 . 2. M uses the representation of the rules of G to simulate a derivation of G on tape 2. Rules are separated by 00 . the computation continues with the simulation of the transitions of M in the manner of the universal machine. Consider the regular grammar G S → aS | aA A → bA | λ that generates the language a+ b∗ . A rule A → v is chosen nondeterministically from the encodings on tape 1. Before the simulation of a transition of M by U1 . a y is erased from the end of the input and the remaining x’s and y’s are changed to a’s and b’s. The encoding of the variable A is then replaced with the encoding of v on tape 2. the x’s are changed to a’s. If not. 11. an a is written in the input position. step 4 (b) of the description of the universal machine in Theorem 11. If y is 1.64 CHAPTER 11. If not. If v is a terminal symbol or the null string. DECISION PROBLEMS Rules of the form A → λ. Consequently. i) If there is an x that occurs after a y. Let uA be the string that is encoded on tape 2. en(A)0 en(a). 1. is the concatenation of the encoding of the rules. The encoding of S is written in position one of tape 2. all computations of M terminate and M solves the derivability problem. The grammar G is represented by the string en(S)0 en(a)0 en(S)00 en(S)0 en(a)0 en(A)00 en(A)0 en(b)0 en(A)00 en(A). Step 5 insures that a computation of M halts after the simulation of at most length(w) + 1 rule applications. 4. b) A nondeterministic two-tape Turing machine M is designed to solve the question of derivability in a regular grammar. and A → aB are encoded by the strings en(A). ii) If the input is λ. then the lengths of ua and w are compared. the symbol y to be printed is examined. denoted R(G). The computation of M begins by determining whether the input has the form R(G)000 en(w). A → a. The input is rejected if length(ua) > length(w). 5. A computation of M that decides whether a string w is in L(G) begins with input consisting of the encoding of the rules of the grammar followed by 000 and the encoding of w. M halts rejecting the input. 3. Otherwise. the string on tape 2 is compared with the encoding of w on tape 1. c) A strategy for a machine to reduce membership in the language {xi y i+1 | i ≥ 0} to {ai bi | i ≥ 0} is described below. If v = aB for some a ∈ Σ and B ∈ V. iii) If the input string has the form x+ . the simulation of the computation of M continues. The representation of a regular grammar G.

Two tapes are added to the universal. The computation consists of the following cycle: 1. 6.the next string is generated on tape 4. With each simulated transition. The ﬁrst 10 transitions of a computation can read at most the ﬁrst 10 symbols in the input string. this problem might seem to require the computation of the machine M to be tested on every possible input string—but this is not case. 4. If the simulation of M speciﬁes a transition and tape 5 is scanning a blank.SOLUTIONS TO EXERCISES 65 14. 2. the computation halts and accepts. the computation continues with step 3. The input to this machine is the representation of M. the answer is yes.tapes 2. The string on tape 4 is copied to tape 3 and the encoding of state qo is written on tape 2. If no computation with any string from this set requires more than 10 transitions. The string 1 n is written on tape 5. 3 and 5 are erased. 7. Tape 4 will hold the input and tape 5 will serve as a counter. 5. the answer is no. one used in the generation of the strings from Σ∗ of length 10 or less and the other to count the number of transitions in the simulation of a computation of M. a 1 is erased from tape 5. If the simulation of M halts and a 1 is read on tape 5 then . At ﬁrst glance. 3. the computation halts and rejects the input. . A modiﬁcation of the universal machine can be used to perform this check. If the string on tape 4 has length greater than 10. The computation of M with the input string from tape 4 is simulated on tapes 2 and 3. Thus all that is necessary is to check the computation of M on all strings of length 10 or less. Otherwise. The null string is written on tape 4. If a computation on this ﬁnite set of strings requires more than 10 transitions. .the string 1 n is written on tape 5. and .

66 CHAPTER 11. DECISION PROBLEMS .

it follows that L is recursive. Σ. if w ∈ L. The input to N is R(M)w. the representation of a Turing machine M followed by an input string. Σ. Γ. The input to S is a representation of a Turing machine M. By adding the encoding of transitions described above to R(M).Chapter 12 Undecidability 3. w ↓ M . That is. we construct a machine H that solves the halting problem. L(H) = L. This is a contradiction since our original assumption was that L is nonrecursive. Thus we conclude that there is no halting machine H. x) whenever δ(qi . The machine M is constructed from M by adding an additional state qf . F) be a Turing M = (Q ∪ {qf }. Since H halts for all input strings. 67 machine. qf Condition M halts with w ↓ M enters state qf when run with w The string w is the same for both problems. S accepts the input if the computation of M with input w enters state qi . input string w. and state qi . x) is deﬁned ii) δ (qi . In addition. Consequently. x. Let M = (Q. The idea behind the reduction that M enters qf whenever M halts. Γ. The halting machine H accepts a string w if M halts with input w. we may assume that the computation of M continues indeﬁnitely whenever the input is not in L. w. M enters qf . 6. N then adds qf to the end of the string producing R(M )wqf . Thus M halting is equivalent to M entering qf . δ. x) = δ(qi . Without loss of generality. x) is undeﬁned enters qf for precisely the strings for which M halts. We will show that the Halting Problem is reducible to the question of whether a Turing machine enters a designated state. {qf }) with transition function i) δ (qi . q0 . R] whenever δ(qi . the computation of H halts in a nonaccepting state for all strings in L. Assume that there is a machine H that solves the Halting Problem for M. x) = [qf . δ . N constructs the representation of the machine M . The reduction has the form Reduction Halting Problem to State Problem Input M. The computations of M are identical to those of M until M halts. When this occurs. Adding a preprocessor N to S. q0 . Let M be a Turing machine that accepts a nonrecursive language L. The machine . Assume that there is a machine S that solves the state problem.

the loop of generating and testing will continue indeﬁnitely. [a. [b. UNDECIDABILITY H accepts input R(M)w if. If M accepts some string un . aa]. . Thus the property ‘is not the empty set’ is nontrivial and membership in L=∅ is undecidable. We conclude that the machine S cannot exist and that the State Problem is undecidable.68 The machine H combines N and S. u1 . This is the same diﬃculty encountered in showing that a recursively enumerable language can be enumerated (Theorem 8. M enters state qf when run with input w. The languages {a}∗ and ∅ are both recursive. A solution to the Post correspondence system {[ab. a) Rice’s Theorem can be used to show that L=∅ is not recursive. 12. aa].6). u2 . 16. CHAPTER 12. L(M) is accepted. if the simulation encounters one string for which the computation of M does not halt. this strategy will not be able to check subsequent strings in Σ∗ . the next ordered pair is generated. [ba. b]. m] is generated. since every ﬁnite language is recursive.8. bab]. [ba. If M does not accept any string. At this point. the question of w being in L(M) is undecidable. . Ordered pairs [i. The string on tape 6 is used as a counter to halt the simulation when j transitions have been executed. j] is written the tape. Unfortunately. a bb bb b bb b 14. the work tapes are reinitialized. When a pair [i. The solution to our diﬃculty is the same as used in that proof.33. A computation of H 1. Intuitively. a) The language L consisting solely of the string w is recursive. Consequently the property of containing the string w is a nontrivial property of recursively enumerable languages. The complement of L is also recursive and clearly does not contain w. When [n. the computation of M with input ui is simulated on tapes 2 and 3 for j transitions. runs N on input R(M)w producing R(M )wqf 2. a]. That is. and the simulation continues. If ui is accepted by M in j transitions. . a) We will show that no sequence of dominoes can be constructed that solves this Post correspondence problem. the string ui is generated on tape 5 and i j is written on tape 6. j] are generated on tape 4 using the enumerating the machine introduced in Exercise 8. We add three tapes and use two enumerating machines that have previously been described. by Rice’s Theorem. it does so in some ﬁnite number m of transitions. b) To accept a string R(M) we need to determine if M will accept at least one input string. A modiﬁcation of the universal machine can be used to show that L=∅ is recursively enumerable. un will be checked and L(M) accepted. Step 1 is a reduction of the Halting Problem to the State Problem. be a lexicographic ordering of the strings in Σ∗ . [bb. ab]} . a) A solution to the Post correspondence system {[a. Otherwise. Let u0 . bb]} is obtained by playing the dominoes a aa 17. with the former language satisfying the property ‘is not the empty set’ the latter not satisfying the property. the strategy might appear to use a universal machine to simulate computations of M until one string in Σ∗ is found that is accepted by M or all strings have been checked and found not to be accepted by M. runs S on R(M )wqf . Thus. and only if. whenever M halts with input w.

The preceding argument can be repeated to show that ba bab ba ab ba ab is the sole extension of (iii) that satisﬁes the conditions of a solution to this Post correspondence system. The sole remaining candidate is sequence (iii). i) ii) iii) ba bab ba bab ba bab ba bab b aa ab ba Playing [ba. bab]. Playing the only domino of this form we obtain the sequence ba bab b aa ab a in which the strings disagree in the ﬁfth position. The only way in which this sequence can be extended is to continually play [ba. bab]. bab]. The only such ordered pair is [ba. 18.SOLUTIONS TO EXERCISES 69 must begin with [ab. the Post correspondence system has no solution since every way to play the dominoes produces a longer top string than bottom string. Case 1: the number of a’s in the top is greater than the number of a’s in the bottom of each domino. the length of the bottom string is one greater than that of the top string with a b being the rightmost symbol on the bottom. That is. ab]. Note that the rightmost end of these strings has the same form as the sequence consisting solely of the domino [ba. There are three ways to extend a sequence that begins with [ba. Utilizing this strategy. The next domino must have an a in the ﬁrst position in the top string. bab] since these are the only pairs that have the same leftmost symbol. We now turn our attention to trying to extend sequence (ii). a] or [ba. A domino that extends the sequence beginning with [ab. . Playing this produces ab a ba bab in which the top and bottom strings diﬀer in the third symbol. Case 2: the number of a’s in the top is less than the number of a’s in the bottom of each domino. the length of the top string is always one less than that of the bottom. We will consider four cases. In this case. bab] produces strings that disagree at the fourth position. a] must have a b as the ﬁrst symbol in the bottom string. This case also is analogous to case 1 and there is no solution. We can determine whether a Post correspondence system for an alphabet Σ = {a} has a solution by examining the relationship of number of symbols in the top of each domino with the number in the bottom. It follows that there is no sequence of dominoes that form a solution to the Post correspondence system.

.70 CHAPTER 12. . S→O | E O → ui jO | jui O | ijO | ui uj O | ui | j E → ui Ei | A A → uj Bk | iBj | ui Buj | iBuj B → ui B | iB | λ where i. 22. UNDECIDABILITY Case 3: there is a domino with the same number of a’s in the top and bottom. . Playing n − m dominoes of the form [ai . We can build a context-free grammar G that generates L(GU ). n where i = 1. The language L(GU ) consists of strings in which each ui is matched to the integer i in reverse order. Case 4: none of the previous cases hold. aj ] followed by i − j dominoes of the form [am . . j = 1. respectively.1. . . Examining the preceding four cases. an ] such that i > j and m < n. then ww. . . we can determine whether any Post correspondence system over a one symbol alphabet has a solution. Combining this with the previous observation. Thus any Post correspondence system satisfying this condition has a solution. C having a solution is equivalent to L(G U ) and L(GV ) having inﬁnitely many elements in common. and b) strings with m units in which there is some position k such that ui is at position k and i is not at position m − k. These solutions are obtained by repeating the sequence of dominoes that spell w. n and j = k where i = 1. . . and only if. . . . . To describe the construction we will consider the substrings ui and integers i to be units. n The variable E produces strings with matching units. an ]produces a top string with (n − m)i + (i − j)m = ni − mi + mi − jm = ni − mj a’s and a bottom string with (n − m)j + (i − j)m = nj − mj + in − jn = in − mj a’s. . There are two classes of strings that do not satisfy the matching criteria. www. By Theorem 12. . Thus this special case of the Post Correspondence Problem is decidable. First we note that a Post correspondence problem C has inﬁnitely many solutions whenever it is solvable. i = 1. n generated from a Post correspondence system. We will show that L(GU ) is context-free. It follows that there is no algorithm that can determine whether the intersection of the languages generated by two context-free grammars is an inﬁnite set. 21. The application of an A rule inserts a mismatch into the string. This is followed by a derivation from B that generates any combination of units. . a) strings with an odd number of units. . . If w is the string spelled by a solution. . Let GU be a grammar whose rules have the form S → ui Si | ui i. . This case has a solution consisting of that single domino. j. k = 1. aj ] and [am . C has a solution if. The variables O and E generate the strings in L(GU ) with an odd number of units and units with a mismatch. Let C be a Post correspondence system and let GU and GV be the upper and lower grammars deﬁned by C.7. This means that the Post correspondence system contains two dominoes [ai . . L(GU ) ∩ L(GV ) = ∅. n where i. The derivations of G produce all strings in which the matching does not occur. . . are also spelled by solutions.

Assume such an algorithm exists and let G2 be an arbitrary context-free grammar with alphabet Σ. an algorithm that determines whether L(G1 ) ⊆ L(G2 ) also determines whether L(G2 ) = Σ∗ .SOLUTIONS TO EXERCISES 71 24. . by Theorem 12. We know that there is a context-free grammar G1 that generates Σ∗ . Thus our assumption must be false and there is no algorithm that determines whether L(G 1 ) ⊆ L(G2 ). We will use a proof by contradiction to show that the question of determining whether the languages of two arbitrary context-free grammars G1 and G2 satisfy L(G1 ) ⊆ L(G2 ).7. However.3. In this case. we know that there is no algorithm that can determine whether the language of an arbitrary context-free grammar G with input alphabet Σ is Σ∗ .

UNDECIDABILITY .72 CHAPTER 12.

0) = x sub(x. y. 0. 0) = g(x) = x sub(x. 1) = = = = f (3. f (3. sub(x. 1)) = h(3. n) = m(n + 1). d) The values of the predicate even(y) = 1 0 if y is even otherwise alternate between 0 and 1.1 by sub(x. 0) = = f (3. 0.Chapter 13 µ-Recursive Functions 3. y)) = pred(sub(x. 2) g(3) 3 h(3. n) is 3(n + 1) and f (m. y)) as desired. 5. 8. f (3. y)) where pred is the predecessor function.2. Using g and h in the deﬁnition of primitive recursion produces sub(x. b) The proper subtraction function sub(x. a) f (3. 3) 3+3 6 (1) (3) 3 formally = h(3. 1. 0)) h(3. 6) = 9 b) A closed form for f (3. 0. y + 1) = h(x. The functions g = p 1 and h = pred◦p deﬁne sub. y + 1) = pred(sub(x. y) was deﬁned in Table 13. The value of even(y +1) can be obtained directly from even(y) using proper subtraction. 73 .

g(x)) computes f using the primitive recursive function g. prsq(x) = eq(x. and the previous value of even. f) If y + 1 is not a perfect square. c) The function btw. . sqrt(y) is incremented. The predicate eq(g(i). between. half (y + 1) = half (y) half (y) + 1 if y + 1 is odd if y + 1 is even Translating this into the language of primitive recursion. The summation y n(x. y.74 CHAPTER 13. we obtain the primitive recursive deﬁnition half (0) = 0 half (y + 1) = half (y) + even(y + 1). we must determine the relationship between half (y) and half (y + 1). y) = i=0 lt(g(i). y + 1). 11. the sqrt(y + 1) = sqrt(y). proper subtraction. f is primitive recursive. e) To deﬁne half (y) using primitive recursion. z) = lt(y. A natural number x is a perfect square if it is the product of the square root of x with itself. 9. y) = i=0 eq(g(i). The bounded product y f (x. can be obtained from the conjunction of two instances of lt: btw(x. d) The integer square root function deﬁned in Exercise 7 f) can be used to show that the relation prsq is primitive recursive. sqrt(x) · sqrt(x)). Since the family of primitive recursive functions is closed under bounded products. z). sqrt(0) = 0 sqrt(y + 1) = sqrt(y) + eq(mult(sqrt(y + 1). y) = 1 if g(i) < g(x) for all i ≤ y 0 otherwise is true only when the inequality g(i) < g(x) is satisﬁed by every natural number i between 0 and y. even is also primitive recursive. µ-RECURSIVE FUNCTIONS even(0) = 1 ˙ even(y + 1) = 1 − even(y) The right-hand side of the deﬁnition by primitive recursion given above uses only constant functions. a) The two-variable predicate f (x. x) records the number times this occurs in the designated range. sqrt(y + 1)). When y + 1 is a perfect square. e) The bounded sum can be used to count the number of times g(i) = x in the range 0 ≤ i ≤ y. x) · lt(x. Consequently. The equality predicate is used in the deﬁnition of prsq to check this condition. x) is 1 only if g(i) = x.

3) is the third integer whose value g(i) is x. 3)]. The function f is obtained using primitive recursion and the primitive recursive functions g and min as follows: f (0) = g(0) f (y + 1) = min(f (y). deﬁned above. . ˙ gcd(x. x) ˙ ˙ divides(x. . y). y). The objective is to ﬁnd the greatest number less than ˙ ˙ or equal to x that divides both x and y. 2. y) = ge(n(x. g(x)−1 x x y gdn(x) = eq(x. . x − z)] sequentially examines the predicates divides(x. g(1). x − z) · divides(y. The encoded sequence is 3. . x − 2) . then x is a G¨del number o Deciding whether there is prime greater than pn(g(x)) that occurs in the prime decomposition of x seems like it may require an unbounded search. a) The prime decomposition of 18000 is 24 · 32 · 53 . by deﬁnition. x) · divides(y. 3)· µz [eq(n(x. x − 1) · divides(y. y).SOLUTIONS TO EXERCISES 75 f) The function n(x. This is a G¨del number since it consists o of an initial sequence of the primes. . The ﬁrst i that satisﬁes the predicate eq(n(x. y)) The sg predicates handle the special case when one or both of the arguments are 0. Finding the greatest common divisor of x and y can be thought of as a search procedure. x − z)·divides(y. x − z) ˙ is 1 whenever x − z is a divisor of both x and y. a) First we note that. the greatest common divisor of 0 and any number is 0. 13. provides the counter needed for the construction of a function that ﬁnds the third occurrence of x in the sequence g(0). Thus. However. 1. g(y + 1)) 16. g(y). pn(z)))] that returns the number of the ﬁrst prime that does not occur in the prime decomposition of x. no number is a divisor of 0. 3) in the deﬁnition of thrd provides the default value whenever there are fewer than three integers in the range 0 to y for which g assumes the value x. x − 1) ˙ ˙ divides(x.x)+1 ) . 17. 12. y) =µz [divides(x. i). i=0 pn(i)dec(i. The predicate divides(x. we begin by deﬁning an auxiliary function o g(x) =µz [cosg(divides(x. a) To construct a primitive recursive one-variable predicate gdn whose value indicates whether the input is the encoding of a G¨del number. we can determine if there are primes greater than pn(g(x)) that divide x by constructing the product of the powers of the primes less than pn(g(x)). x − 2) · divides(y. The factor ge(n(x. z). . Bounded minimalization is the primitive recursive tool for ﬁnding the ﬁrst value that satisﬁes a predicate: thrd(x. The greatest common divisor can be obtained directly from g. The function ˙ ˙ g(x. If there are no primes greater than pn(g(x)) that divide x. y) = sg(x) · sg(y) · ((x − g(x.

dec(1. µ-RECURSIVE FUNCTIONS If the product equals x. root(c2 . y). every function generated using the standard operator is also deﬁnable using the extended µ-operator. y)))) Since each of the functions used in the construction of f is primitive recursive. b) The value of the function root(c2 .76 CHAPTER 13. 20. c1 . the deﬁnition provides a description for calculating the values f1 (x. Thus the proper value of µz[p(x1 .4.6. y) = dec(0. 0)) = gn1 (g1 (x). . . . In this case x is the encoding of a G¨del number. y + 1) = gn1 (h1 (x. y)). The function µz[eq(c2 · z 2 + c1 · z + c0 .4 for the standard µ-operator. Using an argument similar to that in Theorem 13. The preceding arguments have shown that family of functions obtained utilizing both the µ-operator and the extended µ-operator are precisely the Turing computable functions. dec(0. c0 ) is undeﬁned. If z is the encoding of an ordered pair [x. f2 (x. f (x. y) and f2 (x. . the primitive recursive function gn 1 (dec(1. we can show that a function f (x 1 . y+ 1) requires both f1 (x.6. xn . consequently. f (x. y. f (x. . This reduces to showing that a function obtained from a Turing computable partial predicate p(x1 . y)) and f2 (x. . The functions h1 and h2 and f (x. y))). 0). The functions f1 and f2 are obtained directly from f by f1 (x. then the computation never halts indicating that f (x1 . However. The proof uses the strategy employed in Theorem 13. y)). f (x. dec(0. f (x. Turing computable function can be obtained in this manner. dec(0. Since the new µ-operator extends the original operator to partial predicates. y]. . . f (x. 23. . then all the primes that divide x occur before pn(g(x)). f (x. j) is undeﬁned in step 3. If the evaluation of p(x1 . xn ) is undeﬁned. . h2 (x. c0 ) is the ﬁrst nonnegative integral root of the quadratic equation c2 · x2 + c1 · x + c0 . The value of f (x. . Thus every µ-recursive and. y) and f2 (x. The encoding and decoding functions can be used to deﬁne a two-variable primitive recursive function f that encodes both f1 and f2 . y + 1) is obtained by encoding this ordered pair. xn . z). z) using the extended µ-operator is Turing computable. xn ) deﬁnable using the extended µ-operator is Turing computable. . y) are used to compute the values of the ordered pair [f1 (x. 24. y + 1)]. z)) produces the encoding of [y. xn . . dec(1. The initial value of f is obtained directly from those of f1 and f2 : f (x. If there is no such root. . 0) = gn1 (f1 (x. 0)] obtained using the unbounded µ-operator computes root. . y. y) = dec(1. y). f is primitive recursive. . the deﬁnition does not follow the form of a deﬁnition by primitive recursion since the computation of f 1 (x. g2 (x)). . z)] is produced by the Turing machine. . f2 (x. The equality predicate returns zero when there o are primes greater than pn(g(x)) that divide x. . x]. c1 . . Functions f1 and f2 deﬁned by simultaneous recursion are intuitively computable. y)). . y + 1). 18.

Build prt. 3. The value of the new symbol function ns(tr M (x. . which checks if the symbol to be printed is a 1. y − 1)). Composing this with the sign function produces the desired result when the computation of M prints a 1. there is no algorithm capable of making this determination and f prt must not be primitive recursive. c) If the function f prt were primitive recursive. The function prt is deﬁned by prt(x. then we would always obtain an answer indicating the ﬁrst occurrence of the printing of a 1 or a default answer (probably 0) if 1 is never printed. Build f prt. z)] returns the ﬁrst value z for which prt(x. By Exercise 12.SOLUTIONS TO EXERCISES 28. The answer to this question could be obtained as follows: 1. y − 1)) is the encoding of the symbol to be written on the yth transition. 4. The function f prt(x) = sg ◦ µz[prt(x. 77 a) The value of trM (x. 2. Obtain the value f prt(x). Consequently. 1). z)] is undeﬁned if M never prints a 1.7 we know that the problem of determining whether the computation of an arbitrary Turing machine will print a 1 is undecidable. b) Unbounded minimalization µz[prt(x. y − 1) is the conﬁguration of the computation of M with input x prior to the yth transition. Build trM . This total function could be used to produce an algorithm for determining whether the computation of an arbitrary Turing machine M will print a 1 when run with input x. y) = eq(ns(trM (x. z) is true.

µ-RECURSIVE FUNCTIONS .78 CHAPTER 13.

n n! = i=0 i n = > > > 2·3·4· i=5 n i 24 · i=5 n i 2 i=5 24 · 2n Thus. and 4. we prove that n! > 2n for all n ≥ 4. Combining the two relationships we see that f and n2 have the same rates of growth. whenever n ≥ n0 . Formally. 3. the rate of growth of f can be obtained directly from Deﬁnition 14. Then there are constants c and n0 such that n! ≤ c · 2n .2. Then m! = m · (m − 1)! > 2 · c · (m − 1)! > = 79 2 · c · 2m−1 c · 2m . Intuitively. Thus f = O(n2 ). setting c to 1 and n0 to 4 we see that 2n ≤ c · n! for all n ≥ n0 . Thus. n2 is O(f ). Clearly. e) To show that the factorial function grows faster than an exponential function. 2 · c. established in Exercise 3 (e). Assume that n! = O(2n ). Conversely. 2n = O(n!). n2 is the most signiﬁcant contributor to the growth of f . Let m be a number greater than the maximum of n0 . f) The inequality n! > 2n for n ≥ 4.Chapter 14 Time Complexity 1. c) The function f (n) = n2 + 5n + 2 + 10/n is obtained by multiplying the polynomials in the numerator and dividing each term by n2 . is used to prove that n! = O(2n ). n2 + 5n + 2 + 10/n ≤ = n2 + 5n2 + 2n2 + 10n2 18n2 for all n > 0.1.

and n0 such that 0 ≤ c1 nr ≤ f (n) ≤ c2 nr for all n ≥ n0 . b) We will show. Consequently. The input has the form BuBvB with length length(u) + length(v) + 1.1 and n! = O(2n ). that f g ∈ Θ(nr+t ). g ∈ Θ(nt ) implies there are positive constants d1 . It is not the case that 3n = O(2n ). For an input of length n. The computation is completed by returning the tape head to the leftmost tape position. c2 . It follows that (3/2)n ≤ c for all n greater than n0 . Translating each symbol of v requires three transitions. we conclude that 3n = O(2n ). m0 }. Since the assumption that 3 n = O(2n ) produced a contradiction. If so. Thus trM (n) = 2 + 3(n − 1) + n + 1 = 4n.1. 8. . The same approach establishes the lower bound: f (n)g(n) ≥ (c1 nr ) · (d1 nt ) = (c1 d1 )nr+t for all n ≥ max{n0 . there are positive constants c1 . Similarly. 7. the worst case number of transitions occurs when u = λ and length(v) = n − 1. directly from the deﬁnition of big Θ. then there are constants c and n0 such that 3n ≤ c · 2n for all n ≥ n0 . First we show that f g ∈ O(nr+t ).2 computes the binary function of string concatenation. there are no constants that satisfy the requirements of Deﬁnition 14. This is a contradiction since log3/2 c is a constant and we can select a natural number n larger than this constant. n 1 2 3 4 trM (n) 4 8 12 16 A computation with input BBvB begins by reading the two blanks that precede v. d2 . 4.80 CHAPTER 14. TIME COMPLEXITY with m > n0 . A computation moves through u and then shifts each element of v one position to the left. The Turing machine in Example 9. and m0 such that 0 ≤ d1 nt ≤ g(n) ≤ d2 nt for all n ≥ m0 . The values of the trace function trM are given for the ﬁrst four values of n. Taking the logarithm base 3/2 of each side of this inequality. This requires length(v) + 2 transitions. c) The length of the input of a Turing machine that computes a function of more than one variable is considered to be the length of the arguments plus the blanks that separate them. we see that the assumption that 3 n = O(2n ) implies that n ≤ log3/2 c for all n > n0 . m0 } as desired.2. Since f ∈ Θ(nr ). The preceding inequalities show that f (n)g(n) ≤ (c2 nr ) · (d2 nt ) = (c2 d2 )nr+t for all n ≥ max{n0 .

and recording whether the number of a’s was even or odd.SOLUTIONS TO EXERCISES 81 11. the following halting conditions are checked: (a) If no a’s and b’s were found. (c) If there were b’s but no a’s. the string is rejected. Thus the maximum number of transitions is (2n + 2) lg(n) . The maximum number of transitions for a string of length n occurs with the input a n/2 bn/2 if n is even and a(n+1)/2 b(n−1)/2 if n is odd. the computation 1. Upon scanning a b. the X’s are skipped while marking every other a and b. However. . marking every other a with an X. (b) If there were a’s but no b’s. The maximum number of transitions in the entire computation for an input of length n is tcM (n) = 2n + 2 + (n + 2)(n + 1)/2. If an a is encountered after the ﬁrst b. If the machine reads a blank prior to reading 100 input symbols. the string is rejected. moves the tape head to position 0. the states are used to record parity of the number of b’s read. 13. The asymptotic complexity of M is constant. On the completion of the pass. and 2. If the machine has not encountered a blank after 100 transitions. As with the a’s. A one-tape deterministic Turing machine can be designed to accept the language {a i bi | i ≥ 0} in time O(n lg(n)) by marking half of the a’s and half of the b’s on each pass through the input. this would not alter the big oh evaluation of the time complexity. the computation halts and rejects the input. Each iteration of the preceding cycle requires 2n + 2 transitions and the maximum number of iterations is lg(n) . Upon completing the scan of the input. On this pass. The number of transitions could be reduced by one-half if we marked symbols going both left-to-right and right-to-left. tape head returns to tape position 0 to make another pass of the input. (d) If the parity of the number of a’s and b’s was diﬀerent. it is marked as well as every other subsequent b. The process is repeated until one of the terminating conditions is satisﬁed. The latter step can be accomplished in (n + 2)(n + 1)/2 transitions for an input string of length n. runs a machine from that accepts {ai bi | i ≥ 0}. since the number of transitions is constant as n approaches inﬁnity. the string is rejected. if n ≤ 100 otherwise. The computation begins reading the input. tcM (n) = Θ(1). the computation halts and accepts the input string. the string is accepted. 101. the termination conditions are checked. The computation of a standard machine that accepts the language L = {a i bi | 0 ≤ i ≤ 50} ∪ {u | length(u) > 100} begins using 100 states to check the length of the input. If none of the halting conditions were triggered.

TIME COMPLEXITY .82 CHAPTER 14.

does M accept R(M )? Consider the ﬁrst possibility. Thus either the computation takes more than 2R(M ) or M does not accept R(M ). if n = 0. that R(M )R(M ) is in L and R(M )R(M ) is not in L. This means that M does not accept R(M ) in 2length(R(M )) steps. Then L is also in P. there is some Turing machine M that accepts it in polynomial time. Both possibilities. 3n − 1.Chapter 15 P. The ﬁrst case indicates that the computations of M are not polynomially bounded and the second case indicates that R(M )R(M ) is not in L. produce a contradiction. then moves right in state q3 until the computation halts. Both of these possibilities yield a contradiction. The contradiction is obtained using diagonalization and self-reference. otherwise. 1. 83 . The second possibility is that R(M )R(M ) is not in L. and Cook’s Theorem 1. that is. N P. Consequently R(M )R(M ) is in L. that R(M )R(M ) is in L. The self-reference occurs in asking if R(M )R(M ) is in L. Since L is in P. stays in q2 and moves left until it reads the ﬁrst a. Thus our assumption could not be true and the language L is not in P. This means that M accepts R(M ) in at most 2length(R(M )) transitions. a contradiction. a) The computations for the string aa are q0 BaaB Bq1 aaB Baq1 aB Baaq1 B q0 BaaB Bq1 aaB Baq1 aB Bq2 aaB q2 BaaB q0 BaaB Bq1 aaB Baq1 aB Bq2 aaB Baq3 aB Baaq3 B q0 BaaB Bq1 aaB q2 BaaB b) The worst-case performance with input an occurs when the computation reads the ﬁrst n − 1 a’s in q1 and transitions to q2 . c) The time complexity is tcM (n) = 5. a) We will use a proof by contradiction to show that the language L = {R(M)w | M accepts w using at most 2length(w) transitions} is not accepted by any deterministic polynomial-time Turing machine. Assume that L is in P.

then M simulates the transition on original tapes. then again from right-to-left. . The number of transitions in the simulation is bounded by p(n) and writing p(n) 1 ’s can be accomplished in time polynomial in n. Intuitively. 17. however. The assumption the P = N P is critical in the arguments that follow. M . The string ab is mapped to cd. While the counter tape head is reading a 1. 15. the polynomial time bound p(n) can be used to terminate all computations of M that use more than p(n) transitions. the state diagram is incorrect. and ends with two transitions to erase the endmarker. the former is not in L while the latter is in Q. y. Clearly. For every string u ∈ L(M). The computation of M for an input string u of length n consists of the writing p(n) 1 ’s on the counter tape and simulating the computation of M. Otherwise M halts and returns the same result as M.Moves the counter tape head one position to the right. Of course.If a transition of M is deﬁned. z. M halts and does not accept when a B is read on the counter tape.84 CHAPTER 15. To accomplish this. In the ﬁrst printing of the book. 2. Writes p(n) 1 ’s on the counter tape and repositions the tape head at the beginning of the string of 1 ’s. The simulation of this computation in M will also accept u. we do not know if this assumption is true. The arcs on the bottom should go from right-to-left and there should be a transition B/B L on the arc from q 6 to qf . which we will call the counter tape. This precludes the clause that consists of that variable alone from being satisﬁed. 12. The computation reads the entire string from left-to-right. L(M ) is contained in L(M) since it only accepts strings that are accepted by M. The four clauses. The satisfaction of the clause ¬x ∨ ¬y ∨ ¬z requires the truth assignment for one of the Boolean variables to be 0. Thus M accepts L(M) and halts for all input strings. N P. a) q0 BabbaB #q1 abbaB #aq2 bbaB #abq3 baB #abbq4 aB #abbBq4 B #abbq5 BB #abq5 bBB #aq5 bdBB #q5 addBB q5 #cddBB Bq6 cddBB qf BcddBB b) Any string will cause the maximum number of transitions. and ¬x ∨ ¬y ∨ ¬z is satisﬁable. The answers given are for the state diagram with these modiﬁcations. P. The computation of M 1. a machine M is constructed that has one more tape than M. c) tcM (n) = 2n + 4 d) The function computed by the machine is not a reduction of L to Q. AND COOK’S THEOREM 9. . 3. Any combination of three of the clauses x. there is at least computation that requires p(n) or fewer transitions and accepts u. are unsatisﬁable.

SOLUTIONS TO EXERCISES 85 a) Let L be a language in N P that is neither empty nor is its complement empty. Since N P = P. we must map strings in Q to a string in ∅ and strings not in Q to a string not in ∅. we cannot do the former since there are no strings in ∅. If w ∈ Q. That means that are strings u and v such that u ∈ L and v ∈ L. Moreover. To prove that L is NP-complete. This is clearly a reduction of Q to L. Run M to determine if w ∈ Q. Else erase the tape and write v in the input position. 2. but are not NP-complete. . A polynomial time reduction of Q to L can be obtained as follows: For a string w 1. Unfortunately. it can be accomplished in polynomial time since the computation of Q is polynomial. To be able to reduce a nonempty language Q to the ∅. b) In part (a) we have shown that every language in N P other than ∅ and its complement is NP-complete. we must show that every language Q in N P is reducible to L in polynomial time. we know that there is a Turing machine M that accepts Q in polynomial time. then erase the tape and write u in the input position. These languages clearly are in P = N P.

AND COOK’S THEOREM .86 CHAPTER 15. P. N P.

In the preceding reduction. This indicates that the value assigned to x4 is irrelevant. When all clauses are removed following the preceding rules. The only truth assignment that satisﬁes this clause is t(x2 ) = 0. we have succeeded in constructing a satisfying truth assignment. If t(x1 ) = 1. one which fails to ﬁnd a satisfying truth assignment and one which requires several steps to complete. Consider the formula (x1 ∨ x2 ) ∧ (¬x2 ∨ ¬x3 ) ∧ (¬x1 ∨ x3 ) ∧ (x1 ∨ ¬x4 ). In this example. Before outlining the strategy. The 2-Satisﬁability Problem is to determine whether an arbitrary 2-conjunctive normal form is satisﬁable. we ﬁrst note that the assignment of a truth value to a variable may be used to reduce the length of a 2-conjunctive normal form formula that needs to be considered for determining satisﬁability. the attempt to construct the assignment fails. either choice will yield an assignment that satisﬁes the formula. no value has been assigned to the variable x 4 . A formula is in 2-conjunctive normal form if it is the conjunction of clauses each of which has two literals. The algorithm constructs a truth assignment if the formula is satisﬁable. then any clause that contains x1 can be deleted since it is satisﬁed by t. Doing the reduction produces (¬x2 ). we set t(x1 ) = 1 and reduced based on this truth assignment. the occurrence of the literal ¬x1 can be deleted from clauses since its value will not satisfy the clause. Since x3 occurs as a singleton. This produces (¬x2 ∨ ¬x3 ) ∧ (¬x1 ∨ x3 ). If not. Moreover. 87 . let us consider two more examples. Applying this reduction yields (¬x2 ∨ x3 ) ∧ (x3 ). We will describe a polynomial-time algorithm that solves the 2-Satisﬁability Problem.Chapter 16 NP-Complete Problems 1. Before describing the algorithm in detail. it is necessary to assign t(x3 ) = 1 to satisfy the formula. We attempt to ﬁnd a satisfying truth assignment for (x1 ∨ x2 ) ∧ (¬x1 ∨ ¬x2 ) ∧ (¬x1 ∨ ¬x3 ) ∧ (x2 ∨ x3 ).

are incorporated into the ﬁnal truth assignment. If the reduction did not fail and produced the formula v . Set T (xi ) = 1 and reduce v. . The eﬀect of this assignment yields (x2 ) ∧ (x4 ∨ x5 ). 3. . 2. The algorithm proceeds as follows: 1. To describe the algorithm we let u = w1 ∧ · · · ∧ wm be a 2-conjunctive normal form formula and V = {x1 . . If the reduction does not fail. Reducing by setting t(x2 ) = 0 produces the contradictory clauses (¬x3 ) ∧ (x3 ). The resulting formula (x4 ∨ x5 ). we then assign the truth values of T to t.Set t(x) = T (x) for all x ∈ var(v) for which T (x) is deﬁned. Set T (xi ) = 0 and reduce v.If v is empty.Set t(x) = T (x) for all x ∈ var(v) for which T (x) is deﬁned.88 Setting t(x1 ) = 1 yields CHAPTER 16. The reduction fails because there is no assignment that can satisfy both of these clauses. the formula is not empty and it does not contain contradictory clauses. The result of assigning t(x1 ) = 1 in (x1 ∨ x2 ) ∧ (¬x1 ∨ x3 ) ∧ (¬x3 ∨ x2 ) ∧ (x4 ∨ x5 ) produces (x3 ) ∧ (¬x3 ∨ x2 ) ∧ (x4 ∨ x5 ). If the reduction did not fail and produced the formula v . Each reduction process is initiated with the selection of a truth assignment for a variable x i . denoted var(v). then . 6. which requires t(x2 ) = 1 in a satisfying truth assignment. and pick a variable xi in var(v). . Set T (x) ↑ for all x ∈ var(v). Our ﬁnal example illustrates the need to select the truth value for more than one variable to produce a satisfying assignment. xn } be the set of variables of u. The objective is to build a satisfying truth assignment t on V. 5. For a formula v. The next step would be to begin the process by setting t(x1 ) = 0 and trying to ﬁnd a satisfying truth assignment with this assignment to x1 . NP-COMPLETE PROBLEMS (¬x2 ) ∧ (¬x3 ) ∧ (x2 ∨ x3 ). we use the reduction procedure to build a truth assignment T on the set of variables in v. then . If the reduction does not produce contradictory clauses. along with of those obtained during the reduction.otherwise set v = v and continue with step 2. Let v = u. this truth assignment. . . 4. We continue the process by selecting a truth assignment for one of the variables in (x4 ∨ x5 ) and applying the reduction procedure. matches neither of the termination conditions. The reduction for T (xi ) = 1 has failed. continue with step 8. . At this point we conclude that there is no satisfying truth assignment that can be obtained by setting t(x1 ) to 1. requiring t(x3 ) = 1 in order to satisfy the formula.

continue with step 8.m . To accomplish the reduction. Consequently. Moreover.m and bi. Thus the algorithm completes in time polynomial to the number of variables and clauses in the formula. construct the sets {¬xi.3 } {xi. n are added to the set S. ai.m } There are 2mn sets of this form. 6.3 ) be a 3-conjunctive normal form formula with m clauses. bi. ai. . . . . since the reduction removes a clause only when it has been satisﬁed.3 ) ∧ · · · ∧ (um. and 7 will produce the truth assignment t = t . The check veriﬁes that every element in S is in the union of the sets in C . 7. We show that the Minimum Set Cover Problem is NP-hard by reducing the 3-Satisﬁability Problem to it. . .1 . . m.1 ∨ um. A guess-and-check strategy that yields a nondeterministic polynomial time solution to the minimum set cover problem is straightforward. {¬xi.1 .3 } . xn } and the symbol uj.m } {xi. bi. that value does not change. . we are assured of the construction of some truth assignment whenever u is satisﬁable. ¬xi. 5. The selection of truth values T (xi ) = t (xi ) in steps 3. ai. and only if. ai. 89 8. we must reformulate truth assignments and clauses in terms of sets. The truth assignment t satisﬁes u.2 ∨ um. The truth assignment t clearly satisﬁes u. ai. select any truth value for t(xj ). .2 .1 } {¬xi. the formula is unsatisﬁable. . At most 2n reductions are possible and each reduction can alter at most m clauses. and ai.1 . . . The variables of u are {x 1 .If v is empty. For each variable xi . . i = 1.3 . We will construct the elements of S and the sets of C in three steps.2 } {¬xi.k represents the kth literal in clause wj .1 } {xi. bi. ai.2 } {xi. i = 1. . .m . bi. . We will then show that S is covered by k sets in C if. For each xj with t(xj ) ↑.1 . The elements xi.3 .1 .3 . and a number k from a 3-conjunctive normal form formula u. n and j = 1. u is satisﬁable. ai.2 . If both reductions failed.2 .m .otherwise set v = v and continue with step 2. 2m for each variable xi . bi.1 ∨ u1. .2 . We begin with the sets that represent truth assignments. The guess nondeterministically selects a subcollection C of C of size k. .3 . . . . . bi. . ai.j . . a collection C of subsets of S.j . . First we will outline the construction of a set S. bi. Assume that u is satisﬁed by some truth assignment t . Once a variable is assigned a truth value in step 4 or 6.SOLUTIONS TO EXERCISES .2 ∨ u1. Let u = w1 ∧ · · · ∧ w m = (u1.4 . The sets are built so that the smallest cover C of S consists of 2mn sets from C. . . bi. bi. ai.m . the algorithm will produce a truth assignment whenever u is satisﬁable. .1 .

j .k } {xi. c2. bi. then add the set {xi.j }.j and s2. .k ’s and c2.k . If the cover contains all the sets with ¬xi. c2. If t(xi ) = 0 and ¬xi is in wj .Otherwise add the sets {xi.k .k . we add the sets {¬xi. ai. s2.j . . Now we add elements and sets used to represent the clauses of u.j . if the sets from the truth setting component all contain xi.j ’s. the elements xt. . s2.If the literal ¬xi occurs in wj . bi.j . then construct set {xi. The elements c1.j ’s or ¬xi. .k ’s.j .k . there are 2mn sets in C that cover S.m and bi. . .k . where t = i and a diﬀerent k is selected for each set.k appear only in these sets. . we see that a cover of S must contain at least 2mn sets from C.k . bi.k . These m − 1 sets simultaneously complete the coverage of the xt. c2.j . are not covered by our sets. For each clause w j .1 .1 . . . s1.1 . s2. .k and c2. this set covers s1.j .k or all of the sets that contain xi. In either case.k } to C . For a clause wj that was satisﬁed by a positive literal xi in the preceding step.j and s2.j . We must add more sets so that these elements will be covered. If the sets from the truth setting component for a variable xi all contain ¬xi.m and bi. since they complete the coverage of the xi.If t(xi ) = 1. we associate this with a truth assignment that assigns 0 to xi. . c1. and only if. the cover that we are building requires mn + m sets: mn from the truth setting sets and m from the clause sets. . . . m(n − 1). If u is satisﬁed by a truth assignment t. . Thus m(n − 1) completion sets are necessary to cover the c’s This completes the construction of S and C from u. In a like manner. .j and adds one set to C . bi.j . where t = i. ai. .m with m of these sets.j . s1. Thus at least one of the three sets generated from wj is required for any cover of S. and s2. s1. s1.j ’s For each variable xi and clause wj . bi.j .m . For one of the satisfying literals we will add a clause set to C . . c1.k . NP-COMPLETE PROBLEMS The preceding sets are the only sets of C that contain the elements ai. Similarly.k } for k = 1. . Each clause wj is satisﬁed by at least one literal. . then construct set {¬xi. This is possible only if we choose all the sets that contain ¬xi.k } to C . For a ﬁxed variable i.k }. For each variable xi . then add {¬xi.j } to C .j ’s are not covered.90 CHAPTER 16.1 . This choice provides our intuition for a truth assignment. To this point.If the literal xi occurs in wj .j ’s.j .j ’s and the c1. . Each of the ai.j . if the cover contains all the sets with xi.k . ai.j are covered by such a selection.1 . We now must show that u is satisﬁable if. we can build a cover of S consisting of 2mn sets as follows. .1 .j .j ’s are not covered. then add the mn sets of the form {¬xi.j ’s.j } to C .j }. then the xi. . These are covered by the addition of the m − 1 completion sets {xt.k . From the form of the sets.j . These are the only sets of C that contain s1. then the ¬xi. s1. We will refer to these sets as the completion sets. we can cover ai. c1. ai. If t(xi ) = 1 and xi is in wj . . s2. we associate this with a truth assignment that assigns 1 to xi. .

j . First we make a copy v1 of v1 . A) into a graph G = (N . y] creates an acyclic path of length n + 2 in G . vi2 . where t = i. .j . . m linking sets. For each arc [vi .j . We can obtain a truth assignment from the truth setting sets in C .j } is in the cover. and only if. We will show that the Longest Path Problem is NP-hard by reducing the Hamiltonian Circuit Problem to it. To reach nodes x and y.j . vi = v1 . v1 ] as illustrated in the diagram below.k }. We construct G from G by adding three nodes and the arcs incident to these nodes. the path must begin with the arc [x. t(xi ) = 1. c1. Assume that G has a tour. the ¬xi is satisﬁes wj . are not covered by the truth setting or clause sets. 8. . s1. G has an acyclic path of length n + 2.1 . Equating v1 and v1 produces a tour v1 . v 1 ] and end with [v1 . m clause sets. vin . if S is covered by 2mn sets from C . v1 ] and [v1 . . otherwise. vn } be the set of nodes of G. . . In the former case. ai. The tour can be written as a path v1 . . s2. A ) in two steps. one set of the form {xi. s1. v1 of length n. To show that the preceding construction is a reduction. Deleting these produces an acyclic path with endpoints v1 and v1 . y]. the xi satisﬁes wj .j . . x y v1 v1 v1 ' v1 v1 ' v2 v4 v2 v4 v2 v4 v3 v3 v3 The construction of G is completed by adding nodes x and y and arcs [x. .SOLUTIONS TO EXERCISES 91 Similarly. add an arc [vi . Since G has n + 3 nodes. c2. The transformation converts a graph G = (N. Now assume that G has an acyclic path of length n + 2. bi. v1 ] and [v1 . Let N = {v1 . we must show that G has a tour if. v1 ] in A. These are covered by the addition of the m − 1 sets of the form {¬xt. vin . s2. and m(n − 1) of the completion sets.k . .k } are in the cover. . If the sets {xi. . Replacing the v1 at the end of the path with v1 and adding the arcs [x. where t = i. vi2 . this path must visit every node. for a clause wj that was satisﬁed by a negative literal ¬xi . . Conversely. then t(xi ) = 0. The cover must consist of mn truth setting sets. The reduction is complete by selecting the number k = n + 2 as the input in the Longest Path Problem. Thus we have a total of mn truth setting sets. y]. For each clause wj . v1 in G. and m(n − 1) completion sets for a cover of S by 2mn sets.j } or {¬xi.j . the elements ¬xt. In the latter case.k .

Let u = w 1 ∧ w2 ∧ · · · ∧ w m be a 3-conjunctive normal form formula with clauses wi = ui. if there is a partition A1 . To show that the Integer Linear Programming Problem is NP-hard. The transformation is accomplished by the mapping Partition Set Function Number of sets Bound A v Multiprocessor Scheduling A l=v k=2 m = v(A)/2 Now we must show that an instance (A. . and deadline m Output: yes. CHAPTER 16. xn } be the set of Boolean variables occurring in w. no. xi ≥ 0 ¬xi ≥ 0 xi + ¬xi ≥ 1 −xi − ¬xi ≥ −1 are satisﬁed only if one of xi and ¬xi is one and the other is 0. v) of the Partition Problem has a solution if. l. the problem is to determine if there an assignment of tasks to processors that ensures that all tasks will be completed by time m. k. We will consider the literals as variables that may assume integer values. m) of the Multiprocessor Scheduling Problem has a solution. b) Following the strategy employed in the proof of the NP-completeness of the Bin Packing Problem. the corresponding instance (A. However. we deﬁne the inequality . NP-COMPLETE PROBLEMS a) The Multiprocessor Scheduling Problem can be converted into a decision problem by adding a deadline for the computations as follows: Input: set of tasks A.3 and let V = {x1 . . satisfaction of the deadline bound ensures us that l(A1 ) ≤ l(A)/2 and l(A2 ) ≤ l(A)/2. .1 ∨ ui. For each clause wi in u. For each pair of literals xi and ¬xi in V. the four equations 1. With the deadline. Assume that there is a subset A ⊆ A such that v(A ) = v(A)/2. assume that the Multiprocessor Scheduling Problem has a solution A 1 and A2 .2 ∨ ui. . x2 . Since A = A1 ∪ A2 . 10. Conversely. Ak such that l(Ai ) ≤ m for all i. we will reduce the Partition Problem to the Multiprocessor Scheduling Problem. 2. Satisfying the 4n inequalities deﬁnes a truth assignment for V. A set of inequalities 1–4) is constructed for each variable xi in V. . we reduce the question of the satisﬁability of a 3-conjunctive normal form formula to that of satisfying a system of integral inequalities. and only if. Then the partition A1 = A .92 9. The reduction transforms an instance of the Partition Problem into an instance of the Multiprocessor Scheduling Problem. otherwise. 4. A2 = A − A is a solution to the corresponding Multiprocessor Scheduling Problem. . length of task function l : A → N number of processors k. l(A) = l(A1 ) + l(A2 ). It follows that l(A1 ) = l(A2 ) = l(A)/2 and A1 is a solution to the Partition Problem. . . 3.

at least 2T /m are more than half full. then the optimal solution consists of all the items in S and this solution is discovered by the greedy algorithm. If a packing produces two bins numbered r and s that are both less than half full. . Clearly. the solution vector x deﬁnes a truth assignment that satisﬁes u. A row of A consists of the coeﬃcients of the left-hand side of one of the 4 · n + m inequalities. Thus we conclude that at most 2T /m = 2 T /m = 2b∗ are required in the solution produced by the ﬁrst-ﬁt algorithm. an } be the set of objects. size function s. Let S = {a1 . Any item with size greater than b can be removed from consideration since it cannot appear in a solution. an }. First we note that at the completion of the ﬁrst-ﬁt algorithm.2 + ui. it was not the ﬁrst-ﬁt algorithm. When encountering the items in bin s. which is a contradiction since T is the sum of the size of all the ai ’s. ui. This inequality is satisﬁed only if one of the literals in the clause is satisﬁed. . there is at most one bin that is less than half full. Let T = i=1 n s(ai ). If all the items ﬁt into the knapsack. a representation of A and b can be constructed in polynomial time from the clause u. Now assume that 2T /m + 1 or more bins are used in the ﬁrst-ﬁt packing. v(a1 )/s(a1 ) ≥ v(a2 )/s(a2 ) ≥ · · · ≥ v(an )/s(an ). The system Ax ≥ b is solvable if. then that packing could not have been produced by the ﬁrst-ﬁt algorithm. and bin size m with the optimal number of bins denoted b∗ .j is the literal in the jth position of clause i. The column vector b is obtained from the right-hand side of the inequalities. By the argument in the preceding paragraph. T can be used to obtain lower bound on the number of bins in the optimal solution: b∗ ≥ T /m . 93 where ui. as required by the greedy algorithm and that every item in the list has size at most b. . be the total size of the items in the set A. . and only if. b the size bound. Thus whatever strategy was used to assign the items. . Thus the Integer Linear Programming Problem is NP-hard. The matrix A has a column for each literal xi and its negation ¬xi . Consider an instance of the Bin Packing Problem with items A = {a1 . .SOLUTIONS TO EXERCISES 5. Thus the total size of the contents is greater than m/2 2T /m ≥ T.3 ≥ 1. 13. 15. and s : S → N and v : S → N be the size and value functions.1 + ui. Also let c ∗ be the value of the optimal solution and cg be the value produced by the greedy algorithm. . We will show that the modiﬁcation to the greedy algorithm produces a 2-approximation algorithm for the Knapsack Problem. We will show that the ﬁrst-ﬁt algorithm generates a packing which requires at most 2b∗ bins. An integer linear programming system Ax ≥ b can be constructed from the 4 · n inequalities whose satisfaction deﬁnes a truth assignment for the variables of V and the m inequalities the represent the clauses. the ﬁrst-ﬁt algorithm would have placed these items into bin r. respectively. . We assume that the items have been sorted by relative value. Assume that packing bin r occurred before packing bin s. .

94 Now we consider the case where CHAPTER 16. . Combining the inequalities. v(a∗ ) ≥ v(ak ) and k−1 i=1 v(ai ) ≤ cg we get max{v(a∗ ). . i=1 The elements a1 . The total value for this problem is i=1 v(ai ) = c . NP-COMPLETE PROBLEMS n s(ai ) > b. . The third step follows from the inequality max{x. cg } k−1 ≥ max v(ak ). Let a∗ denote the item that has the largest value in S. ak provide an optimal solution for a Knapsack Problem with the same k k set S but having size bound b = i=1 s(ai ). Now c∗ ≤ c since any set of items that can be packed into a knapsack with size bound b can also be packed into the larger knapsack with size bound b . Since a∗ has the maximum value of any item in S. Then k−1 v(ai ) ≤ cg ≤ c∗ . i=1 Let ak be the ﬁrst item not able to be put into the knapsack by the greedy algorithm. it follows that v(a∗ ) ≥ v(ak ). .. y} ≥ (x + y)/2 that compares the maximum and the average of two numbers. i=1 k−1 v(ai ) v(ai ) /2 ≥ v(ak ) + i=1 k ≥ i=1 v(ai ) /2 ≥ c /2 ≥ c∗ /2 as desired. a2 .

On reading an a. This strategy was described in the solution to Exercise 9 in Chapter 15. we construct a loop that iteratively doubles the length of a string produced on previous iteration. For input of length 2. the counter on tape 3 is incremented. 3. For an input string an . the computation halts and rejects the input. but it does demonstrate that L is recursive. The strategy of employing a counter tape to terminate computations in which the number of transitions exceeds the bound can be used to construct a Turing machine M that accepts L and halts for all inputs. or 2. The worst case performance for a string of length n occurs with the input string a n . 2. . the computation begins with tape conﬁguration Ban B. A three-tape Turing machine can be designed to accept the language {a i bi | i ≥ 0} in logarithmic space. The computation terminates after reading the entire input string. For computations with input an . the counter on tape 2 uses lg(n) + 1 tape squares. The machine accepting {ai bi | i ≥ 0} 1. the input aa. the strings on tapes 2 and 3 are compared. The objective is to construct a string of length 2 n 95 . the computation reads the initial blank. By Corollary 17. We sketch the design of a one-tape deterministic machine M for which every computation with input of length n > 1 reads exactly 2n tape squares. In this case. and the following blank using 4 tape squares as required.3. . the number of a’s that have been read is recorded on tape 2 and the number of b’s on tape three. The machine M is no longer subject to the space bound. A computation consists of two steps: accumulating the counts and comparing the results. we know that the number of transitions is bounded by c s(n) . . The computation begins by writing a # in tape position 0 and reading the input. The input is on tape 1 and tapes 2 and 3 serve as counters.3. the counter on tape 2 is incremented.On reading a b. where c is determined by the number of states and tape symbols of M. it is rejected. n > 2. The input alphabet for M is {a}. 7.Chapter 17 Additional Complexity Classes 2. The tape bound limits the number of conﬁgurations that can occur in an accepting computation.If an a is read after a b. Using the binary representation. After processing the input. Reads the input on tape 1 and . the input is accepted. If the input string has length 0. 1. .If the strings are identical.Otherwise.

For example. The # and b indicate that two symbols have been processed and the length of the generated string is 4. The ﬁrst a in the input is then changed to b and the second a is changed to a c. the generated string is expanded by adding X’s. (d) After writing the ﬁnal Y or a . The loop that generates the string proceeds as follows: (a) The tape head moves to the right to ﬁnd an a or c. c. (c) One symbol at a time. As previously noted. each symbol b. for input aaaaa. it is changed you Y . Since this string must overwrite the input string. X and a symbol is added to the end of the generated string. . Each time we double the generated string. The meanings associated with the tape symbols are # a. If the next symbol is blank. This produces the tape conﬁguration #bcX for an input string of length 3 and #bcca n−4 for an input of length 4 or more. X Y left tape boundary marker unprocessed input symbol unprocessed input symbol in the generated string processed input symbol elements added to the generated string beyond the n − 1st tape position elements used in the copying The symbols with primes are used in the extension of the generated string. 8. the computation begins by writing a # in tape position 0 and reading the input. (b) The tape head returns to position 0. we will use the additional tape symbols to diﬀerentiate the input from the generated string. ADDITIONAL COMPLEXITY CLASSES on the tape beginning at position 0.96 CHAPTER 17. c .If a blank is encountered. (e) The ﬁrst occurrence of an a or c is changed to a b. For input of length greater than 2. 16. The computation will halt when all the input symbols have been processed. b’s. the tape head returns to position 0.If an a is encountered. (f) The tape head returns to position 0 and the computation continues with step a). we will mark one input symbol as processed. After all the a’s have been changed to b’s or c’s. it is changed to an a . a c. the ﬁnal a is erased and the head returns to tape position 0. c’s and X’s and be referred to as the generated string. the computation halts. a and c to c. and so forth will consist of #. c b. . If none is found. On this pass it changes Y and X to X. The c’s represent unprocessed input that are already in the generated string and the a’s represent unprocessed whose tape positions have not already been incorporated into the string. an X is written. X in the generated string is replaced by a b . the next a is also changed to a c. and b to b. b X. the tape conﬁgurations at the beginning of each loop are #bccaBBBBBBBBBBBB 16 #bbccXXXBBBBBBBBB 16 #bbbcXXXXXXXXXXXB 16 #bbbbXXXXXXXXXXXX 16 . The string beginning at position 0 that has length 4. Otherwise.

z. F) be a Turing machine whose computation have a 2s(n) tape bound. the input alphabets of M and M are identical. [y. d. a. 2]. z. 1]. R] δ(qi . The input and tape alphabets Σ and Γ of M are Σ = Σ. d. R}. 1]) = [qj . a. B) = [qj . d. b. Doubling the amount of available tape does not change the set of languages that can be accepted. b. 2]. When M reads a B on the work tape. z. a. The reduction in amount of tape required by the computations of M is achieved by the storing more information in a single tape symbol. consequently. [z. d. y. By Theorem 17. The computation of M with an input string u simulates that of M with u but reads at most s(n) tape squares on the work tape. z. Σ. a. y. x) = [qj . . a. L] δ(qi . d. z.2. [z. B) = [qj . b. R] Transitions of M δ (qi . The NP-hardness of a P-space complete language follows directly from the deﬁnition of PSpace and the inclusions shown in Figure 17. 1]) = [qj . a. a. Since M is designed to accept L(M).4. M interprets the blank as the triple [B. B. B. The transition function δ of M is obtained from the transition function δ of M as follows: Transition of M δ(qi . a. 2]. d. 2}). d. a. x) = [qj . 2}. B) = [qj . Let Q be a P-Space complete language. 2]) = [qj . b. [x. we can restrict our attention to Turing machines with one work tape. it follows that every language in N P is reducible to Q in polynomial time and. y ∈ Γ and i ∈ {1. b. [z. L] where x. b. The tape alphabet of M has triples [x. Let M = (Q. y. 1] and the transition converts it into a triple. b. b. δ. a tape symbol of M contains two symbols from M. [y. d. y. y. 1]. L] δ (qi . R] δ (qi . and S indicates that the tape head remains stationary in the transition. S] δ (qi . q0 . B) = [qj . d. 2]) = [qj . b.SOLUTIONS TO EXERCISES 97 12. [x. a. The integer value in the ordered triple indicates which of the two symbols from Γ would be scanned by M. 15. 1]. x. B. and Γ = {B} ∪ (Γ × Γ × {1. d ∈ {L. Q is NP-hard. z. L] δ(qi . Γ. [y. y. i] with x. Since N P is a subset of P-space. z are elements of Γ. S] δ (qi . d. S] δ (qi . [y.2. a. This means that Q is in P-Space and that every language in P-Space is reducible to Q in polynomial time. x. [z. We show that a language accepted by a Turing machine M whose computations require at most 2s(n) tape squares can also be accepted by Turing machine M with tape bound s(n).

ADDITIONAL COMPLEXITY CLASSES .98 CHAPTER 17.

The search tree created by Algorithm 18. The subgraph of the graph of the grammar G consisting of all leftmost derivations of length three or less is Level 0 1 2 3 ba B BB abB S AB abAB baB BBB abba abBB ababB ababAB aba aS aB aAB aBB aabB aabAB aaB aaS aaAB aaaS 5.2. 99 .Chapter 18 Parsing: An Introduction 2.1 while parsing ((b)) is given below. The nodes on each level are listed in the order of their generation.

1 will not terminate when run with input string aa and grammar S → A | a | aa A→S The rule S → a establishes Sa as the result of the ﬁrst reduction for the string aa. Algorithm 18. The parse proceeds by alternating reductions with the rule A → S and the rule S → A. . Derivation S ⇒A ⇒T ⇒ (A) ⇒ (T ) ⇒ ((A)) ⇒ ((T )) ⇒ ((b)) Rule S→A A→T T → (A) A→T T → (A) A→T T →b 11.4.100 CHAPTER 18. PARSING: AN INTRODUCTION (b) b T (A) S —A b+T T+T A+T (A) + T (T) + T (A + T) + T b+T+T A+T+T T+T+T (A) + T + T (b) + T ((A)) + T (T + T) + T (A + T + T) + T (T) + T + T (A + T) + T + T b+T+T+T (A) + T + T + T T+T+T+T+T A+T+T+T+T+T ((A)) (T + T) (A + T) (A + T + T) (T + T + T) (A + T + T + T) ((T)) ((A + T)) (b + T) ((A) + T) ((b)) (T) A+T+T+T T+T+T+T A+T+T+T+T The derivation of ((b)) is obtained by following the arcs from the root S to the goal node ((b)).

5 are used to construct the lookahead sets from the FIRST1 and FOLLOW1 sets. aab. cab} {bacc. acab. acc} {cbab. cbab. cab. ccc} {bab} {cab} {ab} b) The FIRST1 and FOLLOW1 sets for the variables of the grammar are Variable S A B FIRST1 a.2. # a a FOLLOW1 λ a. # # To determine whether the grammar is LL(1). Rule S → AB# A → aAb A→B B → aBc B→λ Lookahead set {a. a) Algorithm 19. 6. ccab.Chapter 19 LL(k) Grammars 2. #} {a} {#} Since the lookahead sets for the alternative A rules both contain the symbol a. acab. the grammar is not strong LL(1). The lookahead set for a variable A is the union of the lookahead sets of the A rules.4. the techniques outlined in Theorem 19. ccab.1 is used to construct the FIRST2 sets for each of the variables of the grammar S → ABC### A → aA | a B → bB | λ C → cC | a | b | c. aab. 101 . a) The lookahead sets are given for the rules of the grammar. bccc} {abab. Rule S → ABab S → bAcc A→a A→c B→b B→c B→λ 3. Lookahead set {abab. #} {a} {a.

bb. b. ba. ca.2.2. cb. c#. bc} F L(A) ∅ {a#. ba. b. The rules S → ABC##.1. ac} {aa} {aa. cc} {ca. b#.3 of Algorithm 18. bb. ab. aa} F (B) {λ} {λ. c#. cb. cb. cc} {a#. c. bb} F (C) ∅ {a. ca. B → bB. c#. bb} {λ. ca. cc} F L(A) := F L(A) ∪ trunc2 (FIRST2 (B)FIRST2 (C){##}F L (S)) = F L(A) ∪ {a#. aa} {a.2 and 3. c} The algorithm is traced by exhibiting the sets produced after each iteration. ca.2 of the algorithm. c#. Step 0 1 2 F L(S) {λ} {a#. b#. ab.102 CHAPTER 19. ab. ac} {aa. cc} F L(B) ∅ {##} {##} F L(C) ∅ ∅ ∅ The length two lookahead sets are used to construct the FIRST2 and FOLLOW2 sets. b#. c} {a. cc} {a#} {b#} {c#} . b#. and C → cC produce F L(C) := F L(C) ∪ trunc2 ({##}F L (S)) = F L(C) ∪ {##} F L(B) := F L(B) ∪ trunc2 (F IRST2 (C){##}F L (S)) = F L(B) ∪ {a#.5. cb. b#. bb. b#. LL(K) GRAMMARS The rules of the grammar produce the following assignment statements for step 3. ba. Rule S → ABC## A → aA A→a B → bB B→λ C → cC C→a C→b C→c Lookahead set {aa. bc} {a#. ac} {ba. b. bc} {a#. c#. c#. c#. b. F (S) := F (S) ∪ trunc2 (F (A)F (B)F (C){cc}) F (A) := F (A) ∪ trunc2 ({a}F (A)) ∪ {a}) F (B) := F (B) ∪ trunc2 ({b}F (B)) F (C) := F (C) ∪ trunc2 ({c}F (C)) ∪ {a. ab. bc} F L(B) := F L(B) ∪ F L (B) F L(C) := F L(C) ∪ F L (C) The last two assignment statements may be omitted from consideration since they do not contribute to the generation of F L(B) and F L(C). cb. b. ca. cc} The rules that contain a variable on the right-hand side generate the assignment statements used in steps 3. b} {λ. b#. cb. cc} {a. ac} F (A) ∅ {a} {a. Step 0 1 2 3 F (S) ∅ ∅ {aa. bb. c. b. cb. ca.

b. Let S ⇒ uAv. Let av ∈ FIRSTk−1 (au). shows that v ∈ FIRSTk (u). We will use a proof by contradiction to show every LL(k) grammar is unambiguous. Then au ⇒ avx where length(v) = k − 1 or ∗ length(v) < k − 1 and xλ. A] [q2 . be the initial subderivation that is identical in both derivations of z. State q3 reads the c’s and q2 the b’s with each transition popping the stack. The preceding argument shows that FIRSTk (au) ⊆ {a}FIRSTk−1 (u). δ(q0 . 8. λ] States q0 and q1 read an a and push A onto the stack. 9. Thus our assumption. A) = δ(q3 . a. a) The lookahead sets for the rules of G1 are Rule S → aSb S→A A → aAc A→λ Lookahead set {aj ai ci bj | j > 0. ∗ length(v) = k − 1 or length(v) < k − 1 and x = λ. λ) = δ(q1 . The accepting states are q0 and q2 . To establish part (3) of Lemma 19. λ] [q2 . A pushdown automaton that accepts L(G1 ) is deﬁned by the following transitions. the string ak is a lookahead string for both of the S rules. λ] [q2 . b. c. This implies that there is a derivation u ⇒ vx where vx ∈ Σ∗ .2. λ] [q3 . u ∈ Σ∗ . The exercise asked us to show that every strong LL(k) grammar is unambiguous. A) = δ(q2 .SOLUTIONS TO EXERCISES The lookahead sets of the A rules show that the grammar is not strong LL(2). c. the sets LAk (uAv. A → x2 ). au ⇒ avx and av is in FIRSTk (au). we must show that the identity FIRSTk (au) = {av | v ∈ FIRSTk−1 (u)} 103 holds for all k ≥ 1. A) = [q1 . The two derivations can be written 1) S ⇒ uAv ⇒ ux1 v ⇒ uw = z 2) S ⇒ uAv ⇒ ux2 v ⇒ uw = z. λ) = δ(q1 . This follows immediately since the strong LL(k) grammars are a subfamily of the LL(k) grammars. Let v ∈ ∗ F IRSTk−1 (u) for some k ≥ 1. A → x2 ) are disjoint since G is an LL(k) grammar. This is a contradiction. that G is ambiguous. b. Consequently. A) = δ(q3 . For convenience we let FIRST0 (u) = {λ} for all strings u.2. These derivations show that the string trunck (w) is in both LAk (uAv. The derivation u ⇒ vx. The derivations continue by applying rules A → x1 and A → x2 to the variable A. Then there is some string z that has ∗ two distinct leftmost derivations. A → x1 ) and LAk (uAv. A) = δ(q1 . A] [q1 . λ] [q3 . We must now establish ∗ the opposite inclusion. i ≥ 0} {ai ci | i ≥ 0} {ai ci | i > 0} {λ} ∗ ∗ ∗ ∗ ∗ ∗ For any k > 0. Let G be an LL(k) grammar and assume that G is ambiguous. Thus G1 is not strong LL(k). must be false. obtained by deleting the leading a from the preceding derivation. a. . A → x1 ) and LAk (uAv. 7.

b. abbcc} {caa. producing S → A# A → aB | ZaB B → bBc | λ Z → bZ | cZ | b | c The resulting grammar is not strong LL(1) since there are two Z rules that begin with b and two that begin with c. Left factoring these rules we obtain S → A# A → aB | ZaB B → bBc | λ Z → bX | cY X→Z | λ Y →Z | λ Examining the length one lookahead sets. Four symbols are required to choose the appropriate A rule. bbcc} {acaa. LL(K) GRAMMARS a) The process of transforming the grammar into an equivalent strong LL(1) grammar begins by removing the directly left recursive A rules. aabcaa. aabcaa. To show that G is LL(3).104 11. Rule S → A# A → ab A → Zab B → bBc B→λ Z → bX Z → cY X→Z X→λ Y →Z Y →λ Lookahead set {a. c} {a} {b. c} {a} {b. bcc} One symbol lookahead is suﬃcient to discriminate between the S rules. CHAPTER 19. a) The lookahead sets for the grammar G S → aAcaa | bAbcc A → a | ab | λ are Rule S → aAcaa S → bAbcc A→a A → ab A→λ Lookahead set {aacaa. we see that this grammar is strong LL(1). c} {b} {#} {b} {c} {b. the lookahead sets are constructed for the sentential forms of the grammar. Rule S → aAcaa Sentential form S Lookahead set {aaccaa. c} {a} 13. abcc} {abcaa. acaa} . Thus G is strong LL(4). babbcc. acaa} {babcc.

b. To conclude that these families of grammars are identical. 15. the string ai is a preﬁx of both ai and ai bi . A) = {[q2 . we must show that every LL(1) grammar is also strong LL(1). A) = {[q2 .SOLUTIONS TO EXERCISES S → bAbcc A→a A → ab A→λ A→a A → ab A→λ S aAcaa aAcaa aAcaa bAbcc bAbcc bAbcc {babcc. and wi are in Σ∗ . λ) = {[q0 . The computation reads leading a’s and pushes an A on the stack in state q0 until either the endmarker or a b is read. there are derivations S ⇒ u1 Az1 ⇒ u1 xz1 ⇒ u1 v1 z1 ⇒ u1 v1 w1 S ⇒ u2 Az2 ⇒ u2 yz2 ⇒ u2 v2 z2 ⇒ u2 v2 w2 where a) ui . λ]} δ(q2 . A nondeterministic pushdown automaton that accepts {ai | i ≥ 0} ∪ {ai bi | i ≥ 0} was given in Example 7. vi .1. A]} δ(q0 . a loop is entered that compares the number of b’s with the number of A’s stored on the stack. Since G is not strong LL(1). The pushdown automaton deﬁned by the transitions δ(q0 . λ) = {[q2 . λ) = {[q1 . λ} with accepting states q1 and q2 accepts the language. P) be an LL(1) grammar and assume that it is not strong LL(1). It follows immediately from the deﬁnitions of LL(1) and strong LL(1) grammars that a strong LL(1) grammar G is also LL(1). A) = {[q1 . babbcc. so our assumption that G is not strong LL(1) must be false. a. bbcc} {acaa} {abcaa} {caa} {abcc} {abbcc} {bcc} 105 14. Σ. #. the stack is popped and the string is accepted. There are two cases to consider: when v1 = v2 = λ and when at least one of v1 or v2 is not λ. and d) FIRST1 (v1 w1 ) = FIRST1 (v2 w2 ). the FIRST sets do not distinguish between the rule A → x and A → y. But this is a contradiction. If a b is read. λ. λ]} δ(q0 . b) u1 v1 w1 = u2 v2 w2 . A deterministic approach to recognizing strings of this form uses an endmarker # on the strings. c) A → x and A → y are distinct rules of G. b. If the endmarker is read. Let G = (V. ∗ ∗ ∗ ∗ ∗ ∗ . We will use the preceding derivations to show that the original grammar G is not LL(1). zi ∈ (Σ ∪ V)∗ . For every i > 0. λ} δ(q2 . That is. #.2. λ]} δ(q1 . S.

which demonstrate that LA1 (u2 Az2 . A → y) have the same FIRST1 set and consequently G is not LL(1). assume that v1 = λ. we have two distinct derivations S ⇒ u1 Az1 ⇒ u1 xz1 ⇒ u1 z1 ⇒ u1 w1 S ⇒ u1 Az1 ⇒ u1 yz1 ⇒ u1 z1 ⇒ u1 w1 of the string u1 w1 . ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ . Using these subderivations. Case 2: At least one of v1 or v2 is not λ. In both cases. A → x) and LA1 (u2 Az2 .106 CHAPTER 19. we have shown that a grammar that is strong LL(1) is also LL(1). LL(K) GRAMMARS Case 1: v1 = v2 = λ. Combining this with inclusion of the familty of LL(1) grammars in the family of strong LL(1) grammars yields the equality of these families of grammars. Without loss of generality. Since the lookahead sets LA1 (u1 Az1 . A → x) and LA1 (u1 Az1 . G is not LL(1). From d) above we note that FIRST1 (v1 w1 ) = FIRST1 (v1 ) = FIRST1 (v2 w2 ). both of the rules A → x and A → y initiate derivations of ∗ the form A ⇒ λ. In this case. A → y) are not disjoint. We can build derivations S ⇒ u2 Az2 ⇒ u2 xz2 ⇒ u2 v1 z2 ⇒ u2 v1 w2 S ⇒ u2 Az2 ⇒ u2 yz2 ⇒ u2 v2 z2 ⇒ u2 v2 w2 .

2.Chapter 20 LR(k) Grammars 1. 107 .3. a) The LR(0) contexts for the rules of G S → AB A → aA | b B → bB | a are obtained from the rightmost derivations S ⇒ AB ⇒ Abi B ⇒ Abi a ⇒ aj Abi B ⇒ aj bbi a i j Rule S → AB A → aA A→b B → bB B→a LR(0) contexts {AB} {ai A | i > 0} {ai b | i ≥ 0} {Abi B | i > 0} {Abi a | i ≥ 0} The nondeterministic LR(0) machine of G is constructed directly from the speciﬁcations of Deﬁnition 20.

b λ B λ B→ .A A→ .bB S→ AB. the grammar G is LR(0).a b B→ b. LR(K) GRAMMARS b λ q0 λ S→ .bB B→ .B λ B→ . .AB A→ .aA A→ .B B→ . b q0 S→ .AB A S→ A.108 A→ . B→ b. S→ AB.B B→ .B B B→ bB.a B a B→ a.b a A A→ aA. The deterministic LR(0) machine of G is A→ b.b λ CHAPTER 20.aA A→ .bB B→ .b A a S→ A.A A A→ aA. A→ b.a λ a B→ a. a b A→ a. b B B→ bB.aA a λ A→ a. Since every state with a complete item contains only that item. λ A→ .

b.b. The grammar AE is not LR(0) since A is an LR(0) context of the rule S → A and a viable preﬁx of the rule A → A + T . a) The nondeterministic LR(1) machine is constructed for the grammar S → Ac A → BA | λ B → aB | b The λ-transitions from the item [S → .c.. {c}] The corresponding deterministic LR(1) is .c}] A [S→ A.BA. A in this case.c}] a λ λ [B→ . {c}] since the string that follows B.b. {λ}] [A→ B.A. {c}] B λ λ [B→ . {λ}] λ [A→ .b.. {c}] λ q0 λ [S→ . Similarly the lookahead set {a.BA. {a.c}] b [B→ . c} is generated by λ-transitions from [A → .b. [A→ . {a.aB. {λ}] generate LR(1) items with lookahead set {c} since the symbol c must follow all reductions that produce A. {a.c}] [B→ a.b.b. {a. {a... b.Ac. 7.c}] λ B [B→ aB. b and λ.Ac. {c}] A [A→ BA. {λ}] c [S→ Ac.B.SOLUTIONS TO EXERCISES 109 3. generates a.

{c}] a b B Since the deterministic machine satisﬁes the conditions of Deﬁnition 20. {a.110 [S→ Ac.BA.b. {c}] [B→ ..BA. {c}] [B→ . {a. {λ}] c [S→ A.b.b.c}] [B→ . {c}] [A→ . LR(K) GRAMMARS a [B→ a.aB.c}] B [B→ aB. . {λ}] [A→ . {a.c}] A [A→ BA.b.B. {c}] [A→ . {a.3. {λ}] A q0 [S→ .A.c.Ac.c}] [B→ . {a.b. {a. {c}] [A→ .b.b.c}] [B→ .c}] [B→ . the grammar is LR(1).c}] [B→ ... {a.b.c}] a CHAPTER 20..b. {a.c}] b B [A→ B.b.b.aB.aB..b.5.b. {a.

- Sudkamp Solutions
- Sudkamp Completo
- Numbers chap78
- Sudkamp Languages and Machines Notes 1-Libre
- number theory.pdf
- Languages and Machines (Thomas A. Sudkamp)
- Fundamental Theorem of Finite Abelian Groups
- Complex Number
- 341-2003sol04
- 4-4
- Exam 20120120 Solution
- eigen.ps
- Theory of Computation
- Introduction to Topology
- Lecture 18 - Pushdown Automata - Examples
- Abstract Algebra
- Programming Languages Principles and Paradigms 2nd Edition by Tucker Noonan
- On Hessen Berg Unit Arty Matrices
- 3.3 Push-Down Automata
- C8
- The Theory of Computer Science by J. M Brady
- 321
- parsing
- Projections
- Algorithms and Theory of Computation Handbook (Mikhail Atallah, Ed)
- Zager Math Tutorial
- Solution_Manual Introduction to the Theory of Computation Sipser
- model
- The Annotated Turing
- Vector Spaces

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd