Sie sind auf Seite 1von 113

Instructors Manual

Volume I
Models of Computation
Exploring the Power of Computing

John E. Savage
Brown University

An imprint of Addison Wesley Longman, Inc.

Reading, Massachusetts  Harlow, England  Menlo Park, California
Berkeley, California  Don Mills, Ontario  Sydney
Bonn  Amsterdam  Tokyo  Mexico City

c 1998 Addison Wesley Longman, Inc.

Copyright
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,
or transmitted, in any form or by any means, electronic, mechanical, phtocopying, recording,
or otherwise, without the prior written permission of the publisher. Printed in the United
States of America.

Introduction

This manual contains solutions to all of the problems in the first five chapters of my book.
These 200 problems represent just under 50% of the 427 problems in the book.
I have prepared these solutions because I believe that the material in these chapters except
for the last half of Chapter 2 plus a little bit of Chapter 8 can serve as the basis for an excellent
first course in theoretical computer science. Instructors wishing to investigate this possibility
are encouraged to consult the web site:
http://www.cs.brown.edu/people/jes/book/book.html.

John E. Savage
February 14, 2000

Contents

The Role of Theory in Computer Science 5

1.1

Solutions

Logic Circuits 15

2.1

Solutions

3.1

Solutions

4.1

Solutions

Computability 91

5.1

Solutions

15

37

59

91

The Role of Theory in

Computer Science

1.1 Solutions
MATHEMATICAL PRELIMINARIES

1.1 Problem: Show that the sum S (k) below has value S (k)

= 2k

1:

S (k ) = 2 k 1 + 2 k 2 +    + 2 1 + 2 0
...........................................................................
Solution: The proof is by induction. The base case is k = 1 for which it is immediate
that S (1) = 1 = 21 1. Suppose that S (j ) = 2j 1 for 1  j  k 1. We show that
it holds for j = k. We write S (k) = 2k 1 + S (k 1) = 2k 1 + 2k 1 1 = 2k 1.
SETS, LANGUAGES, INTEGERS, AND GRAPHS

1.2 Problem: Let A = fred, green, blueg, B = fgreen, violetg, and

blue, greeng. Determine the elements in (A \ C )  (B C ).

C = fred, yellow,

...........................................................................

1.3

Solution: (A \ C ) = fred, blueg and B C = fvioletg. Thus, (A \ C )  (B C ) =

f(red, violet), (blue, violet)g.
Problem: Let the relation R  N  N be defined by pairs (a, b) such that a and b
have the same remainder on division by 3. Show that R is an equivalence relation.
5

Chapter 1 The Role of Theory in Computer Science

...........................................................................
Solution: We need to show that this relation is reflexive, symmetric, and transitive. It
is reflexive if (a, a) 2 R which holds because a and a have the same remainder modulo
3. It is symmetric because (a, b) 2 R exactly when both have the same remainder
modulo 3; that is, (b, a) 2 R also. It is transitive because if a and b have the same
remainder modulo as do b and c, then a and c have the same remainder modulo 3 and
(a, c) 2 R.

1.4 Problem: Let R  A  A be an equivalence relation. Let the set E [a] be the elements
in A equivalent under the relation R to the element a. Show that for all a, b 2 A the
equivalence classes E [a] and E [b] are either equal or disjoint. Also show that A is the
union of all equivalence classes.
...........................................................................

Solution: We prove that E [a] and E [b] are either equal or disjoint by contradiction.
We suppose that it is false and show that a contradiction results.

Suppose that c 2 E [a] \ E [b] but E [a] 6= E [b]. Because R is an equivalence relation,
for every element u 2 E [a], uRc. Also, for every element v 2 E [b], vRc. By symmetry
and transitivity, uRv. Thus, every element in E [a] is in E [b] and vice versa; that is,
E [a] = E [b].
Since for each element
equivalence class.

a 2 A, aRa, it follows that every element

of

A is in some

1.5 Problem: In terms of the Kleene closure and the concatenation of sets, describe the
languages containing the following:
a) Strings over f0, 1g beginning with 01.

b) Strings beginning with 0 that alternate between 0 and 1.

...........................................................................
Solution:

a) f01gf0, 1g
b) 0(f10g1 [ f10g)
1.6 Problem: Describe an algorithm to convert numbers from decimal to binary notation.
...........................................................................
Solution: The binary representation of a number is an expression of the form xn 12n 1
+    + x121 + x020 . Thus, if we divide the decimal number by 2 the remainder will
be x0 and the quotient will be xn 1 2n 2 +    + x1 . To find the value of x1 , repeat
the process on the quotient, etc. The computation of the quotient and remainder of a
decimal number upon division by 2 is straightforward.
1.7 Problem: A graph G = (V , E ) can be described by adjacency lists, one list for each
vertex in the graph. The adjacency list for vertex v 2 V is a list of vertices to which
there is an edge from v. Generate adjacency lists for the two graphs of Fig. .1.2.
...........................................................................
Solution: Let Li be the list of vertices to which vertex vi has an edge. For the graph
in Fig. 1.2(a) the lists are the following: L1 = fv3 g, L2 = fv3 g, L3 = fv5 g, and

1.1 Solutions

L4 = fv5 g. For the graph in Fig. 1.2(b) the lists are the following: L1 = fv4 , v5 g,
L2 = fv4 , v5 g, L3 = fv6, v7 g, L4 = fv6, v7 g, L5 = fv8 g, L6 = fv8 g.
TASKS AS FUNCTIONS

1.8 Problem: Let 5 be the set f0, 1, 2, 3, 4g. Let the addition operator  over this set
be modulo 5; that is, if x and y are two such integers, x  y is obtained by adding x
and y as integers and taking the remainder after division by 5. For example, 2  2 =
4 mod 5 whereas 3  4 = 7 = 2 mod 5. Provide a table describing the function
f : 5  5 7! 5.
...........................................................................
x y f
x y f
x y f
Solution:
0 0 0
2 0 2
4 0 4
0 1 1
2 1 3
4 1 0
0 2 2
2 2 4
4 2 1
0 3 3
2 3 0
4 3 2
0 4 4
2 4 1
4 4 3
1 0 1
3 0 3
1 1 2
3 1 4
1 2 3
3 2 0
1 3 4
3 3 1
1 4 0
3 4 2

Z Z Z

1.9 Problem: Give a truth table for the Boolean function whose value is True exactly when
either x or y or both is True and z is False.
...........................................................................
Solution: The truth table for this function, f (x, y, z ), is given below.

x y z f (x, y, z )
0
0

0
0

0
1

0
0

0
0
1
1

1
1
0
0

0
1
0
1

1
0
1
0

1
1

1
1

0
1

1
0

RATE OF GROWTH OF FUNCTIONS

1.10 Problem: For each of the fifteen unordered pairs of functions f and g below, determine
whether f (n) = O(g(n)), f (n) =
(g(n)), or f (n) = (g(n)).
c) n6 ;
e) n3 log2 n;
a) n3 ;
b)

2n log2 n;

d)

n2n;

f ) 22

Chapter 1 The Role of Theory in Computer Science

...........................................................................
Solution: The following is the ordering of functions by their asymptotic size: n3
O(n3nlog n); n3 log n = O(n6); n6 = O(n2n); n2n = (2n log2 n); 2n log2 n
O(22 ).
1.11 Problem: Show that 2:7n2 + 6

=
=

pndlog ne  8:7n2 for n  3.

2

...........................................................................
Solution: Subtracting 2:7n2 from both sides and then dividing each side by 6 we have
the following inequality which is equivalent to the above.

dlog2 ne  n1:5
This inequality certainly holds for n = 1 and 2. We show that it holds for n  3.
Since dlog2 ne  log2 n + 1, if we can show that log2 n + 1  n for n  3, then the
desired result holds because n  n1:5 . Subtracting 1 from both sides of the inequality
log2 n + 1  n, raising both terms to the exponent 2, and dividing them by n we have
the equivalent inequality 1  2n 1 =n. If we can show that this holds, we are done. But
f (n) = 2n 1=n is an increasing function of n for n  3 because f (n + 1)=f (n) =
2n=(n + 1)  1 for n  1. Thus, f (n + 1) is larger than f (n) for all n  1. Because
f (2) = 1 it follows that 1  2n 1=n for n  3, and the desired result follows.
METHODS OF PROOF

Pn

1.12 Problem: Let Sr (n) = j =1 j r denote a sum of powers of integers. Use proof by
induction to show that the following identities on arithmetic series hold:
a)
b)

S2 (n) =
S3 (n) =

n3

+ n2 + n6

n4

+ n2 + n4

3
4

...........................................................................
Solution:
a) Note that S2 (n + 1) = S2 (n)+(n + 1)2 = S2 (n)+ n2 + 2n + 1. We now expand
the expression given above for S2 (n + 1) and show that the two expressions are the
same.

S2 (n + 1) =
=
=

(n + 1)3
3

(n + 1)2
2

(n3 + 3n2 + 3n + 1)

n3
3

n2
2

n
6

+
+

(n + 1 )
6

(n2 + 2n + 1)
2

(n + 1 )
6

+ n2 + 2 n + 1

= S2 (n) + (n + 1)2 = S2 (n) + n2 + 2n + 1

b) Note that S3 (n + 1) = S3 (n)+(n + 1)3 = S2 (n)+ n3 + 3n2 + 3n + 1. We now
expand the expression given above for S3 (n + 1) and show that the two expressions

1.1 Solutions

S3 (n + 1) =
=
=

(n + 1)4
4

(n + 1)3
2

(n + 1)2

(n4 + 4n3 + 6n2 + 4n + 1)

(n + 1)

(n + 1)

(n3 + 3n2 + 3n + 1)

(n + 1)

(n2 + 2n + 1)
4

+ n3 + 3n2 + 3n + 1

COMPUTATIONAL MODELS

1.13 Problem: Produce a circuit and straight-line program for the Boolean function described in Problem 1.9.
...........................................................................
Solution: A table is given below for the function whose value is True exactly when
either x or y or both is True and z is False. Combining the minterms xyz and xyz we
produce the product yz . Similartly, combining xy z and xyz we produce the product
xz . Thus, f has the SOPE expansion yz _ xz which simplifies to (x _ y) ^ z . A
straight-line program for this function is shown below.
x y z f (x, y, z )
g1 := x ;
g2 := y ;
0 0 0 0
g3 := z ;
0 0 1 0
g4 := :g3 ;
0 1 0 1
g5 := g1 _ g2 ;
0 1 1 0
g6 := g5 ^ g4 ;
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 0
1.14 Problem: A state diagram for a finite-state machine is a graph containing one vertex
(or state) for each pattern of data that can be held in its memory and an edge from
state p to state q if there is a value for the input data that causes the memory to change
from p to q. Such an edge is labeled with the value of the input data that causes the
transition. Outputs are generated by a finite-state machine when it is in a state. The
vertices of its state diagram are labeled by these outputs.
Provide a state diagram for the finite-state machine described in Fig. 1.5(b).
...........................................................................
Solution: The state diagram is shown in Figure 1.1.
1.15 Problem: Using the straight-line program given for the full-adder circuit in Section 1.4.1,
describe how such a program would be placed in the random-access memory of the
RAM and how the RAM would run the fetch-and-execute cycle to compute the values produced by the full-adder circuit. This is an example of circuit simulation by a
program.
...........................................................................
Solution: We imagine placing statement i in the ith word of the RAM. This word
specifies the operation to be performed as well as the numbers of the words containing

10

1

q1=1

q0=0

Initial

Figure S.1.1 The FSM associated with Problem 1.14

the arguments needed for the operation. Proceding from the lowest to the highest
numbered statement, the operand(s) of the current statement is (are) fetched and the
operation evaluated. The value of the i operation is used to replace its description
that was previously stored in the ith word. This program executes O(n) steps on a
straight-line program with n statements.
1.16 Problem: Describe the actions that could be taken by a Turing machine to simulate a
circuit from a straight-line program for it. Illustrate your approach by applying it to the
simulation of the full-adder circuit described in Section 1.4.1.
...........................................................................
Solution: Assume that the statements of the straight-line program are placed in successive cells just as described for the RAM in the solution to Problem 1.15. However, since
addressing of cells is not possible, it is necessary for the TM to search from the current
statement backward to find the values of its operands. Once it finds these operands,
it replaces the current word with the statement number followed by the value of the
operation.
The backward search for operands is facilitated by representing statement numbers in
unary (by a string of 1s) and by placing markers on statement numbers. This allows
the TM to bounce back and forth between statement numbers preceding the current
statement to find the statement that contains the value in question.
If the program has n steps, this procedure uses O(n2 ) units of time for each step because
O(n) steps are used for each of the O(n) digits in the unary representation of the
current statement number. Since there are n statements to be simulated, this algorithm
will run in time O(n3 ).
1.17 Problem: Suppose you are told that a function is computed in four time steps by a
very simple finite-state machine, one whose logic circuit (but not its memory) can be
realized with four logic gates. Suppose you are also told that the same function cannot
be computed by a logic circuit with fewer than 20 logic gates. What can be said about
these two statements? Explain your answer.
...........................................................................
Solution: Given a function that is computed in four time steps by finite-state machine
whose logic circuit can be realized with four logic gates, we know that it has a minimal
circuit size that is at most 16 because of the construction based on unwinding the FSM
loop. If you are told that this function cannot be computed by a logic circuit with fewer

1.1 Solutions

11

than 20 logic gates, there is a contradiction between this statement and the above cited
construction.
1.18 Problem: Describe a finite-state machine that recognizes the language consisting of
those strings over f0, 1g that end in 1.
...........................................................................
Solution: This machine has two states, the initial state q1 and an accepting state q2 . In
q1 on input 1, move to q2 and to q1 on input 0. In q2 on input 1, move to q2 and to q1
on input 0.

1.19 Problem: Determine the language generated by the context-free grammar G = (N , T ,

R, S) where N = fS, M, N g, T = fa, b, c, dg and R consists of the rules given below.
a) S ! MN
d) N ! cNd
b) M ! aMb
e) N ! cd
c) M ! ab
...........................................................................
Solution: It is straightforward to show by induction that all the strings over the alphabet of terminals and non-terminals generated by this grammar are of the form
ar Mbr cs Nds for r, s  0. Thus, the terminal strings are of the form ak bk cl dl for
k, l  1.
COMPUTATIONAL COMPLEXITY

1.20 Problem: Using the rules for the red pebble game, show how to pebble the FFT graph
of Fig. 1.10 with five red pebbles by labeling the vertices with the time step on which it
is pebbled. If a vertex has to be repebbled, it will be pebbled on two time steps.
...........................................................................
Solution: The time of pebblings of vertices when three and five pebbles are used are
shown in Figure 1.10.
1.21 p
Problem: Suppose that you are told that the n-point FFT graph can be pebbled with
n pebbles in n=4 time steps for n  37. What can you say about this statement?
...........................................................................
Solution: On page p
25 of the book it is said that S and T must satisfy (S + 1)T 
n2 =24. Since S = n and
p T = n=4, it follows that the inequality must be satisfied
which is equivalent to 6( n + 1)  n. But the left-hand side has value more than 42
whereas the right-hand side has value 37 for n = 37, a contradiction.
1.22 Problem: You have been told that the FFT graph of Fig. 1.10 cannot be pebbled with
fewer than three red pebbles. Show that it can be pebbled with two red pebbles in
the red-blue pebble game by sketching how you would use blue pebbles to achieve this
objective.
...........................................................................
Solution: To pebble an internal vertex v place a red pebble on each of immediate
predecessors and slide one of these pebbles to v. If the next vertex to be pebbled does
not have vertex v as one of its predecessors, replace the red pebble on v with a blue
pebble.

12

0

a1

0 0

a1

c2

c1

b2
c2

0 0

0 0

Step 1

a0

a1
b1
c2

0
0

c2

a0
b1
c1

0
0

c1

a1
b0
c1

b1
c0

b0

Step 2
0
0

b0

c0
a0
b0
c0

Step 3
0

Step 4
0
0

0 0

c0

Figure S.1.2 A systolic for Problem 1.23.

PARALLEL COMPUTATION

1.23 Problem: Using Fig. 1.12 as a guide, design a systolic array to convolve two sequences
of length two. Sketch out each step of the convolution process.
...........................................................................
Solution: See Fig. S.1.2.
1.24 Problem: Consider a version of the PRAM consisting of a collection of RAMs (see
Fig. 1.13 also shown here as Fig. S.1.3) with small local random-access memories that
repeat the following three-step cycle until they halt: a) they simultaneously read one
word from a common global memory, b) they execute one local instruction using local
memory, and c) they write one word to the common memory. When reading and
writing, the individual processors are allowed to read and write from the same location.
If two RAMs write to the same location, they must be programmed so that they write
a common value. (This is known as the concurrent-read, concurrent-write (CRCW)
PRAM.) Each RAM has a unique integer associated with it and can use this number to
decide where to read or write in the common memory.
Show that the CRCW PRAM can compute the AND of n Boolean variables in two
cycles.
Hint: Reserve one word in common memory and initialize it with 0 and assign RAMs
to the appropriate memory cells.
...........................................................................
Solution: One memory location in global memory is reserved for each variable and one
additional location is reserved for the value of the function. This location is initialized
at 1. One PRAM processor is assigned to each location containing a variable. It reads its

1.1 Solutions

13

Common Memory

RAM

p1

RAM

p2

RAM

pn

Figure S.1.3 The PRAM model is a collection of synchronous RAMs accessing a common
memory.

variable on one cycle and writes 0 to the location containing the value of the function
if its variable is 0. Thus, all the processors that write, write a common value.

Logic Circuits

2.1 Solutions
MATHEMATICAL PRELIMINARIES

2.1 Problem: Show that the following identities on geometric series hold:

P0 (s) =
P1(s) =

s
X

j =0
s
X
j =0

aj =

aj j =

(as+1 1)
(a 1)
(a

1)2

(sas+1

(s + 1)as + 1)

...........................................................................
Solution: (a) The proof is by induction. Clearly, P0 (0) = 1 both in the sum and in
the putative closed form for the sum. Assume that

P0 (j ) =
for 0  j

s

(aj +1 1)
(a 1 )

P0(s) = P0(s

1) + as

= ((as

= s as well.
1 ) + (a

1)as ) =(a

15

1)

16

log n

3 :0

log(n + 1)

2 :5
2 :0
1 :5
1 :0
0 :5

n
1

by

ln x dx

and above by

ln(x +

P
is
8

10 11 12 13

n
k =1 ln k

1) dx.

(b) This proof is by direct manipulation. Observe that

P1(s) + (s + 1)as+1 =
=

s+1
X
j =0
s
X
k=0

aj j =

s
X

ak+1(k + 1)

k=0
s
X

ak+1 k +

k=0

The result follows by algebraic manipulation.

2.2 Problem: Derive tight upper and lower bounds on the factorial function n! = n(n
1)    3 2 1.
...........................................................................
Solution: From the caption on Fig. 2.23 (repeated as Fig. S.2.1) it follows that
Z n
1

ln x dx  ln n! 

Z n
1

ln(x + 1) dx

Rb

The integral a ln x dx has the closed form solution b(ln b

1) a(ln a
1 ). A
R n+1
ln y dy. It
change of variable in the second integral shows that it is equivalent to 2
follows that

n ln n (n

1)  ln n!  (n + 1) ln(n + 1)

(n

Exponentiating both sides we have the following bounds:

1
nne (n 1)  n!  (n + 1)(n+1)e (n 1)
4

1)

ln 4

2.1 Solutions

(a)

17

(b)

Figure S.2.2 Complete balanced binary trees a) of depth two and b) depth 4.

2.3 Problem: Let T (d) be a complete balanced binary tree of depth d. T (1), shown in
Fig. 2.24(a) (Fig. 2.24 is repeated here as Fig. S.2.2), has a root and two leaves. T (d) is
obtained by attaching to each of the leaves of T (1) copies of T (d 1). T (3) is shown
in Fig. 2.24(b) (and in Fig. S.2.2(b)).
a) Show by induction that T (d) has 2d leaves and 2d 1 non-leaf vertices.
b) Show that any binary tree (each vertex except leaves has two descendants) with n
leaves has n 1 non-leaf vertices and depth at least dlog2 ne.

...........................................................................
Solution:

a) Let L(d) and N (d) be the number of leaves and non-leaf vertices in T (d) respectively. By induction we show that L(d) = 2d and N (d) = 2d 1, respectively.
The base case has L(1) = 2 and N (1) = 1 which follows by inspection of T (1).
For the inductive hypothesis we assume that L(e) = 2e and N (e) = 2e 1 for
e  d 1. Since T (d) is formed by attaching two copies of T (d 1) to the leaves
of T (1), it follows that L(d) = 2L(d 1) and N (d) = 1 + 2N (d 1) from
which the result follows.
b) We show by induction that every binary tree with n leaves has n 1 non-leaf
vertices. The statement is true for T (1) which has two leaves. Suppose it is true
for every binary tree with n 1 leaves. Given a binary tree with n leaves, there
must exist some non-leaf vertex both of whose subtrees consist of leaf vertices. (If
not, at least one subtree of every vertex is not a leaf vertex and the graph has cycle.)
Remove such a non-leaf vertex and replace it by a leaf. Since the new tree has n 1
leaves, it n 2 non-leaf vertices and the original tree must have had n 1 non-leaf
vertices.
It is an easy proof by induction that a binary tree of depth d has at most 2d leaves.
The base case applies to T (1). Let 2d 1 < n  2d . Since a binary tree of depth
d has two subtrees each of which has depth at most d 1, a binary tree with n
leaves cannot have depth less than d 1 because such a tree has insufficiently many
leaves. Thus, it must have depth at least d where by definition d = dlog2 ne.

18

BINARY FUNCTIONS AND LOGIC CIRCUITS

2.4 Problem:
a) Write a procedure EXOR in a language of your choice that writes the description
of the straight-line program given in equation (2.2).
b) Write a program in a language of your choice that evaluates an arbitrary straightline program given in the format of equation (2.2) in which each input value is
specified.
...........................................................................
Solution:
a) This is straightforward. Just use one print statement for each line of the straightline program.
b) If it is known in advance how many statements the program contains, then a language such as Pascal, which does not have dynamic storage allocation, can be used.
Create an array that will contain the value produced at each step of the program.
Assume the statements in the straight-line program are provided in an order such
that each statement uses operands that have been produced earlier. When a new
statement arrives, use the array to find the value of its operands. Compute the
value of the operator on these operands and store its value in the array.
If the length of the input is not known in advance, Scheme, or some other language
such as C which supports dynamic storage allocation must be used.
2.5 Problem: A set of Boolean functions forms a complete basis
if a logic circuit can be
constructed for every Boolean function f : Bn 7! B using just functions in
.
a) Show that the basis consisting of one function, the NAND gate, a gate on two
inputs realizing the NOT of the AND of its inputs, is complete.

b) Determine whether or not the basis fAND, OR g is complete.

...........................................................................
Solution:

a) Since the NAND gate computes the function fnand (x, y) = x ^ y , the NOT function, fnot (x) = x, can be realized from fnand (x, x). Applying this to the output
of fnand (x, y), the AND gate is realized. Finally, by DeMorgans rule it follows
that fnand (x, y) = x _ y from which it follows that fnand (x, y ) is the function
computed by the OR gate. Thus, from the NAND gate we can realize each of the
gates in the standard basis, which we know is complete.

b) The basis fAND, OR g is not complete because the NOT gate cannot be realized by
it. To show this we show by induction that every function computed by a circuit
over this basis is monotone, that is, increasing any input from 0 to 1 cannot cause
the output to decrease from1 to 0. This is true for any one input gate. Consider
the last gate of the circuit and show that it must also hold in this case. But the
NOT gate is non-monotone.

2.6 Problem: Show that the CNF of a Boolean function f is unique and is the negation of
the DNF of f .

2.1 Solutions

19

...........................................................................
Solution: Two CNFs that differ must differ in a maxterm; that is, one has the maxterm
and the other does not. Since each maxterm in a CNF has value 0 on exactly one set
of values for its variables, one CNF represents a function with value 0 on this set of
values whereas the other represents a function with value 1. It follows that the CNF of
a function is unique.
To show that the CNF is the negation of the DNF, observe that the former is the
conjunction (AND) of maxterms (ORs of literals) while the latter is the disjunction
(OR) of minterms (ANDs of literals). By DeMorgans rules, the negation of the OR of
terms is the AND of their complements. Also, by these rules the complement of the
AND s of literals is the OR of their complements. In this case, the last terms that are
complemented are literals whose complements are also literals and the result follows.
2.7 Problem: Show that the RSE of a Boolean function is unique.
...........................................................................
Solution: To show that the ring-sum expansion RSE of a Boolean function f is unique
we suppose that f has two distinct RSEs, E1 and E2 , and show that a contradiction
results. The RSE consists of the EXCLUSIVE OR of the product (AND) of variables of
f . Let the degree of a product be the number of variables in it. Since E1 and E2
differ, there must be a smallest degree, say d, such that one of them, say E1 , contains a
product of this degree but the other does not. It follows that both E1 and E2 contain
a common expression Ec consisting of the EXCLUSIVE OR of terms of degree d 1
or less. If we delete these products from E1 and E2 giving F1 and F2 , we have two
different RSEs for g = f  Ec. If we can show that g has a unique RSE, then the same
applies to f .
The lowest degree products in F1 and F2 all have degree d. Let p be a product of degree
d in F1 but not F2 . Set all variables in p to 1 and the rest to 0. Not only does this set to
0 all products of degree d except for p, it also sets to 0 all products of higher degree. It
follows that F1 has value 1 but F2 has value 0. Thus, g cannot be represented correctly
by both F1 and F2 and we arrive at a contradiction. Thus, g (and f ) have a unique
RSE.

(n)

2.8 Problem: Show that any SOPE (POSE) of the parity function f has exponentially
many terms.
...........................................................................
Solution: First, consider the DNF of the parity function. Each function of n variables
has a truth table with 2n rows. For each value for x1 , x2 , : : : , xn 1 ,

f(n) (x1 , x2 , : : : , xn

1, 0

) 6= f(n) (x1 , x2, : : : , xn

1, 1

It follows that for exactly half of the rows the function has value 1. Thus, the DNF of
a parity function has 2n =2 minterms.
The DNF is just a special case of a SOPE. To prove that every SOPE has exponentially
many terms it suffices to show that no product in the SOPE has less than n variables,
or what it is equivalent, that the DNF cannot be simplified.

20

Chapter 2 Logic Circuits

(n)

Consider the SOPE of the parity function f and suppose that it contains the product
xc11 ^ xc22 ^    ^ xcrr for r < n where xci i = xi if ci = 1 and xci i = xi if ci = 0.
Give values to these variables so the the product has value 1. Then the SOPE has value
(n)
1. Also set the variables not in this product to the values which cause the f to have
value 1. Now change the value of a variable not in the product. This does not cause a
(n)
change in the value of the SOPE but it does cause a change in the value of f . Thus,
(n)
a SOPE with a product of less than n variables cannot represent f .

(n)

The proof for the POSE is exactly the dual. f has value 0 on exactly half the points
in its domain. If some clause does not include all variables, it can be set to 0 without
fixing all the variables. Thus, the POSE remains 0 as one of the remaining variables is
(n)
varied, contradicting the definition of f .

(n)

2.9 Problem: Demonstrate that the RSE of the OR of n variables, f_ , includes every
product term except for the constant 1.
...........................................................................
(n)
Solution: The function f_ (x1 , x2 , : : : , xn ) has value 1 on every n-tuple of weight
k  1. We show by contradiction that every product term except the constant term is
(n)
in the RSE of this function. To show that the constant term in the RSE of f_ is 0,
observe that all product terms are 0 when every variable is 0.
Suppose xi1 xi2    xik is the product term containing the fewest variables that is missing from the RSE. That is, every product term containing more than k variables is in
the RSE. Now set each variable except those in this product to 0. This causes every
term containing more than k variables to have value 0. Since there is only one product
term containing these k variables, if we set them to 1 and all other variables to zero,
every product term in the RSE except for xi1 xi2    xik is 0. However, since this term
is missing from the RSE, it must have value 0, contradicting the assumption that it is
the RSE of the OR function.

(n)

2.10 Problem: Consider the Boolean function f mod3 on n variables, which has value 1
when the sum of its variables is zero modulo 3 and value 0 otherwise. Show that it has
exponential-size DNF, CNF, and RSE normal forms.
Hint: Use the fact that the following sum is even:
X
0

j k

3k
3j

...........................................................................
Solution: We show exponential-size DNF and CNF by showing that the number of 1s
(n)
and 0s in the truth table for f mod3 is exponential. The number of 1s plus the number
n
of 0s is 2 . We give upper and lower bounds on the number of 1s from which these
results follow.
Ckn is the binomial coefficient defined below. The identity given for it holds when
1  k  n 1, as can be easily shown.

Ckn =

 

n
= Ckn 1 + Ckn
k

1
1

2.1 Solutions
From this we have the following identity and bounds when 2  k

n

21
2:

Ckn = Ckn 2 + 2Ckn 12 + Ckn 22

Ckn  2(Ckn 2 + Ckn 12 + Ckn 22)
Ckn  Ckn 2 + Ckn 12 + Ckn 22
Pbn=3c

(n)

Let f (n) = j =0 C3nj . Thus, the number of 1s in the truth table for f mod3 is f (n)
and the number of 0s is 2n f (n). We show this implies that both the number of
(n)
1s and the number of 0s grow exponentially with n and the DNF and CNF of f mod3
have expontial size.
Let (x) be the Dirac delta function which is 1 if x > 0 and 0 if x = 0. If 2  3j 
n 2, we have 1  j  b(n 2)=3c. Let a = bn=3c and b = b(n 2)=3c. Then,
a b = 1 unless n = 3k + 2 for some integer k in which case a b = 0. It follows
that

f (n) =




P
b
n 2
n
j =1 C3j + 2C3j
P
b
n
n 2
2
j =1 C 3 j + C 3 j
2n 1 + Cnn 2



+ C3nj 22 + 1 + (a b)C3na

n 2  + 1 + (a b)C n
2
3a
1 + C3 j 2

2
1

Here we use the fact that C3na is an decreasing function of a and that 3a  n
because

2. Also,

3b + 1 = 3b(n

3
=n

2)=3c + 1

2 2
+1
3
3
4+1=n 3

we have the following lower bound:

f (n) 
=

Pb
n 2
n 2
n
j =1 C3j + C3j 1 + C3j
P
n 2
n 2
2n 2
j =3b+1 Cj
2n 2 n + 2

2
2

+1

n + 2  f (n)  2n 1 + n(n 1)=2.

(n)
Consider next the RSE for f mod3 . The RSE can be written as the integer sum modulo

Thus, 2n

two of a constant and products of one or more variables that appear in the RSE. If we
(n)
set all variables of f mod3 to 0, the function has value 1. Since all products in the RSE
are zero, it follows that the constant is 1. We can show by considering cases that all
products of one and two variables are present. Since the function is symmetric, if a
product of k variables appears in the RSE, all products of k variables appear.
As our inductive hypothesis we assume that all products of 3k variables are absent in
the RSE but all products of 3k 1 and 3k 2 variables are present. This holds for
k = 1. We assume it holds for k  l and show it holds for k = l + 1. There are Cjn

22

Chapter 2 Logic Circuits

products of j variables. Let P (j ) be 1 if products of j variables are in the RSE and let
it be 0 otherwise. It follows that
0

P (j )Cjm A

jX
=m
j =0

mod 2 =

(n) = 1
f mod
3
(n) = 0
f mod
3

1
0

when m is the number of 1s among the n variables. We use this identity to show by
induction that P (j ) is 0 when j = 3k and 1 otherwise. Note that if P (j ) = 1 for all
values of j , the sum on the left-hand side is 2m . Given our inductive hypothesis, we
have the following three identities:
jX
=m
j =0

P (j )Cjm = 2m + 1

k
X
j =0

C33jk P (m)

that
0
@

jX
=m
j =0

(n)

Since f mod3 is 0 when m = 3k + 1, 3k + 2 but 1 when

P (3k + 1) = 1, P (3k + 2) = 1 but P (3k + 3) = 0.

2.11 Problem: Show that every Boolean function f (n)

follows:

m = 3k + 3, we have that

Bn 7! B can be expanded as

f (x1 , x2 , : : : , xn ) = x1 f (1, x2 , : : : , xn) _ x1 f (0, x2 , : : : , xn)

Apply this expansion to each variable of f (x1 , x2 , x3 ) = x1 x2 _ x2 x3 to obtain its
DNF.
...........................................................................
Solution: To establish that every Boolean function f (n) : Bn 7! B has this expansion
we show that the left and right-hand sides have the same value when x1 = 1 and
x1 = 0, which is clearly the case.
Applying this expansion to f (x1 , x2 , x3 ) = x1 x
2 _ x2x3 we generate the following
sequence of expansions, the last of which is its DNF.

f (x1 , x2 , x3) = x1x2 _ x2x3

= x1(x2 _ x2x3 ) _ x1(x2 x3)
= x1(x2 (x3) _ x2 (1)) _ x1x2 x3
= x1x2 x3 _ x1x2 _ x1x2 x3
= x1x2 x3 _ x1x2 (x3 _ x3 ) _ x1x2 x3
= x1x2 x3 _ x1x2 x3 _ x1 x2 x3 _ x1 x2x3
2.12 Problem: In a dual-rail logic circuit 0 and 1 are represented by the pairs (0, 1) and
(1, 0), respectively. A variable x is represented by the pair (x, x). A NOT in this
representation (called a DRL- NOT) is a pair of twisted wires.

2.1 Solutions

23

a) How are AND (DRL- AND) and OR (DRL- OR) realized in this representation? Use
standard AND and OR gates to construct circuits for gates in the new representation. Show that every function f : Bn 7! Bm can be realized by a dual-rail logic
circuit in which the standard NOT gates are used only on input variables (to obtain
the pair (x, x
)).
b) Show that the size and depth of a dual-rail logic circuit for a function f : Bn 7! B
are at most twice the circuit size and at most one more than the circuit depth of f
over the basis fAND, OR, NOTg when the n NOTs on inputs are not counted.
...........................................................................
Solution: Let x and y denote either input variables or the values of two gates in a
circuit for f . If x and y are represented by (x, x
) and (y, y), respectively, their AND is
represented by (x ^ y, x ^ y), that is, by (x ^ y, x _ y). Thus, the DRL- AND is realized
from (x, x
) and (y, y) by a circuit containing one AND and one OR. Similarly, the OR
of the two variables is represented by (x _ y, x _ y ) or, equivalently, by (x _ y, x ^ y).
Thus, the DRL- OR is realized by a circuit with one AND and one OR. Since this
construction only adds depth at the input (to compute the negation of inputs), and
at most doubles the number of ANDs and ORs (negations do not require gates), the
following bounds apply:

CDRL (f )  2C
(f )
DDRL (f )  D
(f ) + 1
2 Problem: A function f : Bn 7! B is monotone if for all 1  j  n, f (x1 , : : : , xj 1 ,
0, xj +1 , : : : , xn )  f (x1 , : : : , xj 1 , 1, xj +1 , : : : , xn ) for all values of the remaining
variables; that is, increasing any variable from 0 to 1 does not cause the function to
decrease its value from 1 to 0.
a) Show that every circuit over the basis
mon = fAND, OR g computes monotone
functions at every gate.
b) Show that every monotone function f (n) : Bn 7! B can be expanded as follows:

f (x1 , x2 , : : : , xn ) = x1 f (1, x2 , : : : , xn) _ f (0, x2 , : : : , xn )

Show that this implies that every monotone function can be realized by a logic circuit
over the monotone basis
mon = fAND, OR g.
...........................................................................
Solution:
a) We give a proof by induction. Clearly, AND and OR gates compute monotone
functions. Assume that every circuit containing g 1 or fewer gates over the basis

mon computes monotone functions at every gate. Consider the output gate of
a circuit over this basis that has g gates. Since both of its inputs are the values of
monotone functions and the gate itself is monotone, the function computed by the
complete circuit is monotone.
b) Because f (n) : Bn 7! B is monotone, if f (0, x2 , : : : , xn ) = 1, then f (1, x2 , : : : , xn)
is also 1. Thus, the left- and right-hand sides of the following equation both have

24

Chapter 2 Logic Circuits

the same value when x1

= 0 and when x1 = 1.

f (x1 , x2 , : : : , xn ) = x1 f (1, x2 , : : : , xn) _ f (0, x2 , : : : , xn )

SPECIALIZED FUNCTIONS

2.14 Problem: Complete the proof of Lemma 2.5.3 by solving the recurrences stated in
Equation (2.4).
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . .. . . . . . . . . . . . . . .
(n)
(n)
Solution: Let (n) = C
0 fencode and  (n) = D
0 fencode . Then, (1) =

(n) (x ) = (x , x ) and we do not count negations. The recur (1) = 0 because fencode
1
1 1
rences in question are the following:

(n)  2n 1 + (n 1)
 (n)  max(n 1,  (n 1) + 1)
We assume as inductive hypotheses the following for m  n

(2.1)
(2.2)
1:

(m)  2m+1 (m + 3)
 (m )  m 1
These inequalities are satisfied for the base case. It is straightforward to show that the
inductive hypothesis holds for m = n.

2.15 Problem: Design a multiplexer circuit of circuit size 2n+1 plus lower-order terms when
n is even.
Hint: Construct a smaller circuit by applying the decomposition given in Section 2.5.4
of the minterms of n variables into minterms on the two halves of the n variables.
...........................................................................
Solution: Output variable yj of the decoder is associated with a minterm x(c) where
j = jcj. In addition, x(c) can be written as the product of two minterms u(a) and
v(b) where the n-tuple x is decomposed as an n=2-tuple u and an n=2-tuple v. The
constant n-tuple c is decomposed into corresponding n=2-tuples a and b. Thus, jcj =
(n)
2n=2 jaj + jbj. This leads to the following representation of fmux :

(n) (x, y) =
fmux

a,b
_

u(a )

v (b) ^ y2n=2 jaj+jbj

!!

Let the minterms fv(a) g and fv(b) g be realized by two decoder circuits computing

(n=2) . Each of the 2n=2 inner sums (indexed by a) can be formed with 2n=2 ^s and
fdecode
2n=2 1 _s for a total of 2n+1 2n=2 additional two-inputs gates and depth (n=2)+ 1.
The outer sum can be formed with 2n=2 additional ^s and 2n=2

1 additional _s and

2.1 Solutions

25

additional depth (n=2) + 1. The depth of the resulting circuit is n + 2 plus the depth
of a circuit for the minterms fv(b) g. Using the bounds given in Lemma 2.5.4 we

(n)

have that for n divisible by four the multiplexer function fmux : B2 +n 7! B can be
realized with the following circuit size and depth over the basis
0 = f^, _, :g :

(n) )  2n+1 + 3  2(n=2) + (2n

C
(fmux
(n) )  n + dlog ne + 3
D
(fmux
2

4)2(n=4)

2.16 Problem: Complete the proof of Lemma 2.11.1 by establishing the correctness of the
inductive hypothesis stated in its proof.
...........................................................................
Solution: The recurrences of the proof are shown below.

D
(cj(l++11) ) = max(D
(cj(l+1) ) + 2, D
(s(jl) ) + 3)
D
(sj(l+1) ) = max(D
(c(jl+1) ) + 1, D
(s(jl) ) + 2)
(l)

(2.3)
(2.4)

(l)

for 2  l and 0  j  l 1 where sl 1 = cl 1 . The solutions to these recurrences

are the following which we establish by induction on k and j for 2  k:

D
(c(rk)) = 2(k + r)
D
(s(tk) ) = 2(k + t)

3
2

1rk
0tk

(2.5)

(2.6)

(2 )

(k)

The base case is k = 2 for which we have D

(s0 ) = 2 and D
(c1 ) = 3 by
(k )
examination of Fig. 2.21(a). It is straightforward to show that D
(s0 ) = 2k 2 for
(
k)
all k by noting that D
(c0 ) = 0 for all k  2 and applying (2.4). It follows from
(k )
(2.3) by induction that D
(c1 ) = 2k 1.
We show that (2.5) and (2.6) hold by induction on k as follows. The base case is k = 2.
Assume that it holds for all values of r and t in the indicated ranges for k = m 1. We
show it holds for k = m by inducting on l for r = l + 1 and t = l in (2.5) and (2.6).
The base case is l = 0 which is established in the previous paragraph. Assume (2.5)
and (2.6) hold for l = p 1, 1  p  k 1 and show that it holds for l = p. But
this follows directly from substitution in (2.3) and (2.4). This completes the double
induction.
2.17 Problem: The binary sorting function is defined in Section 2.11. Show that it can be
realized with a circuit whose size is O(n) and depth is O(log n).
...........................................................................
(n)
Solution: The binary sorting fsort : Bn 7! Bn has the following size and depth
bounds where k = dlog(n + 1)e

(n) )  9 2k 5k 11 + k
C
(fsort
(n) )  6k 4 + dlog ke
D
(fsort
2

2k+1

26

Chapter 2 Logic Circuits

To show this, choose 2k 1 1 < n  2k 1 and construct a circuit for these functions
on 2k 1 inputs by filling in the remaining variables with 0. These are stripped off
later.
The proof of Theorem 2.11.1 gives a construction for the elementary symmetric functions for m = 2k 1 which can be translated into a circuit for the threshold functions
on the same number of inputs (the binary sorting function) by performing a prefix
computation on the elementary symmetric functions with the OR function. That is,
m(m) = e(mm) , m(m)1 = e(mm)1 _ e(mm) , : : : , 1(m) = e1(m) _    _ e(mm)1 _ e(mm) .
From Theorem 2.6.1 it follows that with 2m 2 additional ORs and additional depth
2dlog2 me + 1 the sorting function on m, inputs can be realized.

LOGICAL FUNCTIONS

(n)

2.18 Problem: Let fmember : B(n+1)b

7! B be defined below.

(n) (x , x , : : : , x , y) =
fmember
1
2
n
where xi , y

1 xi = y
0 otherwise

for some 1  i  n

2 Bb and xi = y if and only if they

agree in each position.




Obtain good upper bounds to

(n)
C
fmember

and D

(n)
fmember

by constructing a

circuit over the basis

= f^, _, :, g.
...........................................................................
(n)
(b)
Solution: Consider the function fmatch : B2n 7! B where fmatch (x, y) has value 1
(n)
only when x = y. The following is a formula for fmatch because (xi  yi ) has value 1
only when xi and yi have the same value.

(b) (x, y) = (x  y ) ^ (x  y ) ^    ^ (x  y )
fmatch
1
1
2
2
b
b
Applying DeMorgans rule this can be written as the NOT of the OR of the terms
(b) can be realized by a circuit with b
(xi  yi ) for 1  i  n. The function fmatch
EXCLUSIVE ORs, one NOT , b
1 ORs or a total of 2b gates and depth dlog2 be + 2.

(n)

Also, we can write fmember as follows:

(n)
(b) (x , y) _ f (b) (x , y) _    _ f (b) (x , y)
fmember
(x1 , x2, : : : , xn, y) = fmatch
1
match 2
match n
(n)

(b)

Thus, the function fmember can be realized with n instances of fmatch and an OR-tree
on n inputs having n 1 two-input ORs and depth dlog2 ne. Thus, we have the
(n)
following size and depth bounds on fmember over the basis
= f^, _, :, g:

(n)
C
(fmember
)  2nb + n 1
(
n)
D
(fmember )  dlog2 be + 2 + dlog2 ne
2.19 Problem: Design a circuit to compare two n-bit binary numbers and return the value
1 if the first is larger than or equal to the second and 0 otherwise.

2.1 Solutions

27

Hint: Compare each pair of digits of the same significance and generate three outcomes, yes, maybe, and no, corresponding to whether the first digit is greater than,
equal to or less than the second. How can you combine the outputs of such a comparison circuit to design a circuit for the problem? Does a prefix computation appear in
your circuit?
...........................................................................
Solution: Let the two binary numbers be x = (xn 1 , xn 2 , : : : , x0 ) and y = (yn 1 ,
yn 2 , : : : , y0 ). Let g(x, y) = (zy , zm ) where zy = xy and zm = xy _ x y. Then,
zy = 1 when x > y and zm = 1 when x = y; that is, (zy , zm ) characterize two of
the three possible outcomes. The third result can be calculated from these two since
zn = zy _ zm .

(j ) (j )

(j )

Let (zy , zm ) = g(xj , yj ). Then zy = 1 if the number corresponding to the j least

significant bit of x is larger than that corresponding to the j th least significant bit of y.
(j )
Also, zm = 1 if j th most significant bits of the two numbers are equal.
Let (k) (x, y ) have value 1 if the integer denoted by bits 0 through k of x is larger
than that of y and 0 otherwise. We can write the following recurrence for (n) :

(n 1) = zy(n 1) _ zm(n 1) (n 2)
Also, (0)

(n 2)z (n 3) _ z (n 1)z (n 2)z (n 3)z (n 4)

(n 1) = zy(n 1) _ zm(n 1)zy(n 2) _ zm(n 1)zm
y
m
m
m
y
_ zm(n 1)zm(n 2)    zm(1) zy(0)
It follows that (n 1) can be computed by first performing the prefix computation
(n 1) , z (n 1)z (n 2), : : : , z (n 1)z (n 2)    z (1)), ANDing the results with the yes
(1, zm
m
m
m
m
m
(n 1) , z (n 2), : : : , z (0) , and ORing the results.
values zy
y
y
PARALLEL PREFIX

2.20 Problem:

a) Let copy

: S 2 7! S be the operation

a copy b = a
Show that (S , copy ) is a semigroup for S an arbitrary non-empty set.
b) Let  denote string concatenation over the set f0, 1g of binary strings. Show that
it is associative.
...........................................................................
Solution:
a) We must show that copy is associative. This holds if for three values a, b and c in
S we have:

Since a copy

28

Chapter 2 Logic Circuits

b) This result follows because (a  b)  c = a  (b  c).

2.21 Problem: The segmented prefix computation with the associative operation on a
value n-vector x over a set S , given a flag vector  over B, is defined as follows: the
value of the ith entry yi of the result vector y is xi if its flag is 1 and otherwise is the
associative combination with of xi and the entries to its left up to and including the
first occurrence of a 1 in the flag array. The leftmost bit in every flag vector is 1. An
example of a segmented prefix computation is given in Section 2.6.
Assuming that (S , ) is a semigroup, a segmented prefix computation over the set
S  B of pairs is a special case of general prefix computation. Consider the operator

on pairs (xi, i ) of values and flags defined below:

((x1, 1 )
(x2, 2 )) =

2 = 1
(x 2 , 1 )
(x1 x2, 1 ) 2 = 0

Show that ((S , B),

) is a semigroup by proving that (S , B) is closed under the operator
and that the operator
is associative.
...........................................................................
Solution: The operation is closed because (S , ) is a semigrup. So we need only show
that
is associative over S  B. Consider three pairs (x1 , 1 ), (x2 , 2 ) and (x3 , 3 )
in S  B. We have that

((x1 , 1 )
(x2, 2 ))
(x3, 3 ) =
=

8
>
<
>
:

(x2 , 1)
(x3, 3 )
2 = 1
(x1 x2, 1 )
(x3 , 3 ) 2 = 0
3 = 1
(x3, 1)
(x2 x3, 1)
2 = 1 and 3 = 0
(x1 x2 x3, 1 ) 2 = 0 and 3 = 0

On the other hand the following holds and the operation is associative.

(x1 , 1
((x2 , 2 )
(x3 , 3 )) =
=

(
8
>
<
>
:

3 = 1
(x1 , 1)
(x3 , 3)
(x1 , 1)
(x2 x3, 2) 3 = 0
(x3, 1)
3 = 1
(x2 x3, 1)
2 = 1 and 3 = 0
(x1 x2 x3, 1 ) 2 = 0 and 3 = 0

2.22 Problem: Construct a logic circuit of size O(n log n) and depth O(log2 n) that, given
a binary n-tuple x, computes the n-tuple y containing the running sum of the number
of 1s in x.
...........................................................................
Solution: Since integer addition is associative, the running sum of the 1s in x can be
computed by a prefix circuit in which the operator is an adder. Since the maximum
number of 1s is n, the adder need only be designd for (log2 n)-bit numbers. A carry

2.1 Solutions

29

lookahead adder for numbers of such length has size O(log n) and depth O(log log n).
A prefix circuit to perform the addition operation has O(n) adders in a circuit in which
the longest path contains O(log n) adders. Thus, this problem can be solved by a
circuit of size O(n log n) and depth O(log n log log n).
2.23 Problem: Given 2n Boolean variables organized as pairs 0a or 1a, design a circuit that
moves pairs of the form 1a to the left and the others to the right without changing their
relative order. Show that the circuit has size O(n log2 n).
...........................................................................
Solution: We sketch a circuit to move the pairs 0a to the right. A parallel circuit can
be constructed to move pairs 1a to the left.

Let there be n positions holding pairs xa. Number the positions 1 to n from left
to right. Consider a pair in position k. A prefix computation is done from right to
left that assigns to position k an integer which is the number of pairs with 0 as the
first component to its right including itself. In Problem 2.22 we describe a circuit to
compute a running sum of 1s which can be used for this purpose. If position k has a
sum rk and holds the rk th 0-pair from the right, the quantity sk = n k + 1 rk
specifices the number of positions that this pairs needs to shift right in order to arrive
in its final position.
Represent sk as a binary number. We now design shifting circuits to move each pairs
(and a corresponding shift value) to the right. The first shifting circuit shifts the kth
pair 0 places if the least significant bit of sk is 0 and 1 place if it is 1. We shift sk from
which the least significant bit is deleted along with the kth pair. We repeat this process
with a circuit that shifts the kth pair by 0 or 2 places depending on whether the second
least significant bit of sk is 0 or 1. Along with the kth pair, we carry sk from which the
two least significant bits are dropped. This process is repeated with shifters that shift by
increasing powers of two. It is not hard to show that this process does not result in two
or more pairs arriving at the same location at the same time.
A circuit constructed in this fashion has size O(n log2 n) because each shifting stage
can be realized by a circuit of size O(n log n) and there are log n stages and because
the computation of the quantities sk for 1  k  n can be done with a circuit of size
O(n log n).
2.24 Problem: Linear recurrences play an important role in many problems including the
solution of a tridiagonal linear system of equations. They are defined over near-rings,
which are slightly weaker than rings in not requiring inverses under the addition operation. (Rings are defined in Section 6.2.1.)
A near-ring (R, , +) is a set R together with an associative multiplication operator 
and an associative and commutative addition operator +. (If + is commutative, then
for all a, b 2 R, a + b = b + a.) In addition,  distributes over +; that is, for all
a, b, c 2 R, a  (b + c) = a  b + a  c.
A first-order linear recurrence of length n is an n-tuple x = (x1 , x2 , : : : , xn) of
variables over a near-ring (R, , +) that satisfies x1 = b1 and the following set of
identities for 2  j  n defined in terms of elements faj , bj 2 R j 2  j  ng:

xj = aj  xj 1 + bj

30

Chapter 2 Logic Circuits

Use the ideas of Section 2.7 on carry-lookahead addition to show that xj can be written

xj = cj  x1 + dj
where the pairs (cj , dj ) are the result of a prefix computation.
...........................................................................
Solution: We can solve for x3 in terms of x1 by substituting the value for x2 into its
recurrence. Similarly, for j  4 we can substitute the value of xj 1 into the recurrence
for xj . Assume we can write xj for 2  j  r 1 in terms of x1 and coefficients
(cj , dj ), as shown below.

xj = cj x1 + dj
Clearly (c2 , d2 ) = (a2 , b2 ). Substituting xr

into the recurrence for xr we have

xr = cr  x1 + dr

(2.7)

where

cr = ar  cr 1
dr = br + ar  dr

We can treat (cr , dr ) as the result of a new product operation  that combines the pairs
(ar , br ) and (cr 1 , dr 1 ), as indicated below.

1,

dr 1 )

(x1 , y1 )  (x2 , y2 ) = (x1  x2, y1 + x1  y2 )

To show that this operation  is associative we consider three pairs (x1 , y1 ),
and (x3 , y3 ) and their combinations shown below.

(x2, y2 ),

((x1, y1 )  (x2, y2 ))  (x3 , y3 ) = (x1  x2, y1 + x1  y2 )  (x3, y3 )

= ((x1  x2)  x3 , y1 + x1  y2 + (x1  x2)  y3)
= (x1  (x2  x3), y1 + x1  (y2 + (x2  y3 ))
= (x1 , y1 )  ((x2 , y2 )  (x3, y3 ))
Here we have used the associativity of  and the associtivity of + and the fact that it distributes over . Note that the proof of associativity of  for first-order linear recurrences
does not require commutativity of the addition operator +.
Since the set S of pairs f(a, b)g for a, b 2 R under the operation  is closed, (S , )
forms a semigroup. Thus, the prefix computation on the pairs ((a2 , b2 ) , (a3 , b3 ) , : : : ,
(an , bn )) produces the pairs ((c2 , d2) , (c3, d3 ) , : : : , (cn, dn )). From these pairs and
x1 the values (x2 , x3, : : : , xn) can be computed from (2.7).

2.1 Solutions

31

ARITHMETIC OPERATIONS

2.25 Problem: Design a circuit that finds the most significant non-zero position in an n-bit
binary number and logically shifts the binary number left so that the non-zero bit is in
the most significant position. The circuit should produce not only the shifted binary
number but also a binary representation of the amount of the shift.
...........................................................................
Solution: Let the n-bit binary number be x = (xn 1 , xn 2 , : : : , x0 ). From this
number we generate a vector y such that yj = 0 for j 6= k and yk = 1 if xk is the
most significant non-zero digit of x. Consider tk = xn 1 _    _ xk+1 . It is 0 if no
higher order position than the kth contains a 1 and 1 otherwise. Thus, if tk = 0 and
xk = 1 then yk = 1. Otherwise, yk = 0. Thus, yk = tk ^ xk . Thus, to compute yk
for 0  k  n 1 we can perform the prefix computation on the vector (xn 1 , xn 2 ,
: : : , x0) with the OR operation to produce (tn 1, tn 2, : : : , t0) (note that tn 1 = 0)
after which we combine tk and xk as indicated to produce y.
Once y has been produced we convert from this positional notation to binary notation
using the encoder circuit of Section 2.11.1 and then use this result as the value of the
amount of shift in a logical shifting circuit.
The size and depth of the circuit to compute y are O(n) and O(log n). The same
bounds apply to the encoder circuit. The shifting circuit has size O(n log n) and depth
O(log n). Thus, the combined circuit has size O(n log n) and depth O(log n).

2.26 Problem: Consider the function [j , k] = [j , k 1]  [k, k] for 1  j < k  n 1,
where  is defined in Section 2.7.1. Show by induction that the first component of
[j , k] is 1 if and only if a carry propagates through the full adder stages numbered
j , j + 1, : : : , k and its second component is 1 if and only if a carry is generated at one
of these stages, propagates through subsequent stages, and appears as a carry out of the
kth stage.
...........................................................................
Solution: The carry out of the j th stage, cj +1 , is defined in terms of the carry in and
the propagate and generate bits as follows:

cj +1 = pj cj _ gj
Substituting into this expression that for cj +2 we have

cj +2 = pj +1pj cj _ pj +1 gj _ gj +1
Clearly, a carry is propagated through stage j and j + 1 if pj +1 pj = 1 and generated
in these stages if pj +1 gj _ gj +1 = 1. This is the base case for induction. Given that 
is associative, as stated in Section 2.7.1, we need only show to show that if [j , k 1]
satisfies the inductive hypothesis, then so does [j , k] = [j , k 1]  [k, k]. But this
follows from the above analysis.
2.27 Problem: Give a construction of a circuit for subtracting one n-bit positive binary
integer from another using the twos-complement operation. Show that the circuit has
size O(n) and depth O(log n).

32

Chapter 2 Logic Circuits

...........................................................................
Solution: The 2s complement of an n-bit binary number is obtained by complementing each bit and adding 1 to the result. To add a negative number x to a positive
number y, complement each bit of x with NOT gates, and add the complement of x,
y, and 1 using a carry lookahead adder to which 1 is supplied as the carry in to the first
stage. The combined circuit has size O(n) and depth O(log n).

2.28 Problem: Complete the proof of Theorem 2.9.3 outlined in the text. In particular,
solve the recurrence given in equation (2.10).
...........................................................................
Solution: We have the following two recurrences in which C (1) = D(1) = 1 and
c, d  1.

C (n)  3C (n=2) + cn
D(n)  D(n=2) + d log2 n
When n = 2k , let (k) = C (n) and (k) = D(n). We claim that (k)  c(3k+1
2k+1 ). The first claim holds when k = 0 since c  1 and for larger k as can be seen
by straightforward substitution. We claim that (k)  d(k(k + 1)=2). The first claim
holds when k = 0 since d  1 and for larger k as can be seen by straightforward
substitution.
2.29 Problem: Show that the depth bound stated in Theorem 2.9.3 can be improved from
O(log2 n) to O(log n) without affecting the size bound by using carry-save addition
to form the six additions (or subtractions) that are involved at each stage.
Hint: Observe that each multiplication of (n=2)-bit numbers at the top level is expanded at the next level as sums of the product of (n=4)-bit numbers and that this type
of replacement continues until the product is formed of 1-bit numbers. Observe also
that 2n-bit carry-save adders can be used at the top level but that the smaller carry-save
adders can be used at successively lower levels.
...........................................................................
Solution: This algorithm has O(log n) stages of addition each of which is a carry-save
addition. The output of the last stage is a pair of binary numbers whose sum is the
desired result. Put these last two binary numbers into a carry lookahead adder. Since
each of the O(log n) stages except that last has a fixed depth and the last has depth
O(log n), the depth of this circuit is O(log n).
2.30 Problem: Residue arithmetic can be used to add and subtract integers. Given positive
relatively prime integers p1 , p2 , : : : , pk (no common factors), an integer n in the set
f0, 1, 2, : : : , N 1g, N = p1p2    pk , can be represented by the k -tuple n =
(n1 , n2 , : : : , nk ), where nj = n mod pj . Let n and m be in this set.
a) Show that if n 6= m, n 6= m.
b) Form n + m by adding corresponding j th components modulo pj . Show that
n + m uniquely represents (n + m) mod N .
c) Form n  m by multiplying corresponding j th components of n and m modulo
pj . Show that n  m is the unique representation for (nm) mod N .

2.1 Solutions

33

...........................................................................
Solution:
a) We show that if n 6= m, n 6= m by contradiction. If n = m, then (ni mi ) =
(n m) mod pi = 0 for 1  i  k. Since (n m) is divisible by p1 , p2, : : : , pk ,
it is divisible by their product N . Since n m lies between -(N-1) and N-1,
the only integer in this range divisible by N is 0 and n = m, contradicting the
assumption that n 6= m.
b) Write n + m = qN +(n + m) mod N where q is the quotient on dividing n + m
by N . The remainder (n + m) mod N has value between 0 and N 1. The ith
component of n + m is

(n mod pi + m mod pi ) mod pi = (n + m) mod pi

= (qN + (n + m) mod N ) mod pi
= (n + m) mod N mod pi
The latter follows because N is divisible by pi . By a) this representation is unique.
c) Let n  m denote the tuple obtained by multiplying the corresponding j th components of n and m modulo pj . The proof of this result is analogous to that for
b), namely, we observe that we can write n = qnpi + (n mod pi ) (and a similar
expression for m). Thus,

nm = p2i qnqm + pi (qn(m mod pi ) + qm (n mod pi)) + (m mod pi)(n mod pi )

It follows that nm mod pi is the product (m mod pi )(n mod pi ) mod pi . Because we can write nm = qN + (nm mod N ) and N is divisible by pi , n  m
is the unique representation for (nm) mod N .
2.31 Problem: Use the circuit designed in Problem 2.19 to build a circuit that adds two
n-bit binary numbers modulo an arbitrary third n-bit binary number. You may use
known circuits.
...........................................................................
Solution: Let the three numbers be x, y, and z. The sum of the first two n-bit
numbers cannot exceed 2n+1 1. Let z be fixed. To compute the remainder of this
sum modulo z , subtract z at most k = d(2n+1 1)=z e times from the sum. Thus,
a circuit for the problem in question can be constructed from an adder the output to
which is passed through k circuits each of which compares the result of the previous
stage with z using the circuit Problem 2.19 and subtracts z if it is larger. Subtraction
is done by adding the twos complement of z .
What kind of circuit should be constructed when z is not known in advance? How
about a division circuit?
2.32 Problem: In prime factorization an integer n is represented as the product of primes.
Let p(N ) be the largest prime less than N . Then, n 2 f2, : : : , N 1g is represented
by the exponents (e2 , e3 , : : : , ep(N ) ), where n = 2e2 3e3 : : :p(N )ep(N ) . The representation for the product of two integers in this system is the sum of the exponents of their
respective prime factors. Show that this leads to a multiplication circuit whose depth
is proportional to log log log N . Determine the size of the circuit using the fact that
there are O(N= log N ) primes in the set f2, : : : , N 1g.

Chapter 2 Logic Circuits

34

...........................................................................
Solution: The largest exponent in the prime factorization of any integer in the set is at
most log2 N . When represented in binary such numbers require O(log log N ) bits. It
follows that exponents can be added with carry lookahead adders with O(log log N )
gates per exponent for a total of O(N log log N= log N ) gates. The depth of each of
these adders is O(log log log N ).

2.33 Problem: Construct a circuit for the division of two n-bit binary numbers from cir(n)
(n)
cuits for the reciprocal function frecip and the integer multiplication function fmult .
Determine the size and depth of this circuit and the accuracy of the result.
...........................................................................
Solution: To divide x by y , form the reciprocal of y and multiply by x. The size
and depth of the resulting circuit is the sum of the sizes and depths of circuits for the
reciprocal and multiplication functions.
The reciprocal r of the integer u (we assume that u  2n 1 ) satisfies ru = 22n s
where 0  s  u. Thus, the product v with r satisfies vr = (v=u)22n vs. Thus,
the error in the result is at most vs. When normalized by dividing 22n, it is at most
uv=22n.

2.34 Problem: Let f : Bn 7! Bkn be an integer power of x; that is, f (x) = dxk e for
(m)
some integer k. Show that such functions contain the shifting function fshift as a
subfunction for some integer m. Determine m dependent on n and k.
...........................................................................
Solution: Let n be divisible by 4. Consider first the case k = 2. Since x is a binary ntuple, decompose it into two (n=2)-tuples, a high-order (n=2)-tuple b and a low-order
(n=2)-tuple a. We can then write the corresponding integer jxj as follows:

f (x) = 2njbj2 + 2n=2+1jbjjaj + jaj2

Now let jbj = 2t for n=4  t  n=2 1 and let jaj  2n=4 1. It follows that the
middle term in the expansion of f (x ) contains n=4 bits that are free to vary and they
do not overlap the bits in the high order or low order terms. The middle n=4 bits are
shifted by 0 through n=4 1 positions. Thus, m = n=4.
To generalize this result for k which is a power of two, expand jxj as follows:
jxj = 2l jbj + jaj
for jaj  2l 1 and jbj  2n l 1. Use the binomial theorem to expand (jbj2l + jaj)k
and choose jaj and jbj such that the term kjbjk 1jaj does not overlap with any other
term in the expansion. When jbj is a power of 2, this term represents a shift of jaj by
1 places. To meet these conditions we require that kjbjk 1 jaj be less than jajk
and more than k2 jbjk 2jaj2 . Both conditions are satisfied if kjaj < jbj. If we choose
jaj  2l=2 1 and jbj  2(n l)=2, the inequality holds for l  (n=2) dlog2 ke.
Since the multiplier of jaj in kjbjk 1jaj is kjbjk 1 , when jbj is a power of two, jaj is
shifted in steps of size k 1. Thus, m = b(n (n l))=(k 1)c = bl=(k 1)c.

2.1 Solutions

35

2.35 Problem: Let f : Bn 7! Bn be a fractional power of x of the form f (x) = dxq=2 e,

(m)
0 < q < 2k < log2 n. Show that this function contains the shifting function fshift as
a subfunction. Find the largest value of m for which this holds.
...........................................................................
Solution: Analogous to the previous problem, the approach is to compute a Taylor
series expansion of the function.
k

Machines with Memory

3.1 Solutions
MATHEMATICAL PRELIMINARIES

3.1 Problem: Establish the following identity:

S (k ) =

k
X
j =0

j 2 j = 2 (k

1 )2 k + 1

...........................................................................
Solution: The proof is by induction. The result holds for k = 0. Assume it holds for
k  m 1 and show that it holds for k = m. The following relationship follows from
the definition.

S (m) = S (m

1) + m2m

= 2 (m

2)2m

N N

N N

+ 1 + m2 m

3.2 Problem: Let p :

7! and q : 7! be polynomial functions on the set of
non-negative integers. Show that p(q(n)) is also a polynomial in n.
...........................................................................
Solution: Since p and q are polynomial functions, there exist integers r and s such
that p(n)  a1 nr + b1 and q(n)  a2 ns + b2 for positive integers a1 , a2 , b1 , b2 .
Then, p(q(n))  a1 (a2 ns + b2 )r + b1 . Since (anr + b)s  (a + b)s nrs , the desired
conclusion follows.

37

38

Chapter 3 Machines with Memory

FINITE-STATE MACHINES

3.3 Problem: Describe an FSM that compares two binary numbers supplied as concurrent
streams of bits in descending order of importance and enters a rejecting state if the first
string is smaller than the second and an accepting state otherwise.
...........................................................................
Solution: The FSM has accept and reject states, qa and qr . Once it enters one of these
states, it returns to it on all inputs. It also has a maybe state, qm , which is also the
initial state. If the FSM is in qm and the first of the two input bits is 1 and the other
0, it enters qa. It enters qr if the first bit is 0 and the second 1. In the remaining cases
(both inputs are the same), it returns to qm .

3.4 Problem: Describe an FSM that computes the threshold-two function on n Boolean
inputs that are supplied sequentially to the machine.
...........................................................................
Solution: This FSM has three states, q0 , the initial state and that state the FSM is in if
it all prior input bits are 0, q1 , the state in which it resides if it has seen a single 1 among
its input bits, and q2 , the (final) state it enters if it has seen two or more 1s among its
input bits. If in q0 the FSM receives an input of 1, it enters q1 . Otherwise it returns to
q0. If in q1 the FSM receives an input of 1, it enters q2. Otherwise it returns to q1. In
q2 the FSM returns to q2 on all inputs.

3.5 Problem: Consider the full-adder function fF A (xi , yi , ci ) = (ci+1 , si ) defined below
where + denotes integer addition:
2ci+1 + si

= xi + yi + ci

Show that the subfunction of fF A obtained by fixing ci = 0 and deleting ci+1 is the
EXCLUSIVE OR of the variables xi and yi .
...........................................................................
Solution: The value of the output s1 is 1 if the number of 1s among the three input
xi , yi , and zi is odd. This is exactly the parity function.
3.6 Problem: It is straightforward to show that every Moore FSM is a Mealy FSM. Given
a Mealy FSM, show how to construct a Moore FSM whose outputs for every input
sequence are identical to those of the Mealy FSM.
...........................................................................
Solution: Given a Mealy FSM M = (, , Q, , , s, F ), we construct a Moore FSM
M 0 = (, , Q  , 0 , 0 , s0 , F 0). In M 0, each state is a pair [q=b], q a state of M
and b an output symbol that can occur on a transition into state q. [qi=bj ] 2 F 0, the
final states of M 0 , if qi 2 F . The initial state of M is s0 = [s=bk ] for bk an arbitrary
output letter of M .
For every c 2 let 0 ([q=c], a) = [q0=b0] where q0 = (q, a) and b0 = (q, a).
Also, let 0 ([q0=b0]) = b0. Then, if output b0 is produced on a transition by the Mealy
machine M to state q0, the Moore machine M 0 enters state [q0=b0] and produces the
same output. The state [q0 =b0] captures the fact that M has entered state q0.
Because the successor to M 0s state [q=c] is the same for every output symbol c, each
transition from [q=c] by M 0 corresponds to a transition from state q by M .

3.1 Solutions
1

0
Start

qa

qb
1

qc

qd

39

qe

qf

0
1

Figure S.3.1 The figure associated with Problem 3.7.

We show by induction that M 0 produces the same output sequence for a given input
sequence as does M . The statement is true for input sequences of length one when
the two machines are in their initial states. We assume that it is true for states that
are reachable from initial states by input sequences of length k or less and show that it
holds for input sequences of length k + 1. Since for every output letter c, the transition
from state [q=c] of M 0 on input a generates the same output as does M in state q with
input a, the conclusion follows.
3.7 Problem: Find a deterministic FSM that recognizes the same language as that recognized by the nondeterministic FSM of Fig. 3.8.
...........................................................................
Solution: The method of Section 4.2 is used to construct the FSM shown in Figure S.3.1. Here qa = fq0 g, qb = fq0 , q1 g, qc = fq0 , q1 , q2 g, qd = fq0, q3 g,
qe = fq0, q1, q4 g, and qf = fq0, q5g.
3.8 Problem: Write a program in a language of your choice that writes the straight-line
program described in Fig. 3.3 for the FSM of Fig. 3.2 realizing the EXCLUSIVE OR
function.
...........................................................................
Solution: The next-state/output function for the FSM of Fig. 3.2 is given in Equation 2.2. We need only write a program that writes this equation T times, T an input
parameter, that changes the the numbers of the statements in this equation so that every
statement is given a unique number.
SHALLOW FSM CIRCUITS

3.9 Problem: Develop a representation for states in the m-word, b-bit random-access
memory so that its next-state mappings form a semigroup.
Hint: Show that the information necessary to update the current state can be succinctly
described.
...........................................................................
Solution: Consider the random-access memory unit with M b-bit words and one b-bit
output word. It has 2(M +1)b states. There is one next-state mapping for the no-op
command; it maps the current state to itself. There are M 2b next-state mappings
corresponding to writing a word in memory. For each such mapping the contents of
one word is replaced by a new word. Thus, all states differing only in the word that

40

Chapter 3 Machines with Memory

is replaced have the same successor state. Finally, the read command maps the current
state to the state that differs only in the value of the output word.

3.10 Problem: Show that matrix multiplication is associative.

...........................................................................
Solution: The goal is to show that A(BC ) = (AB )C where A, B , and C are n  n
matrices. Applying the definition of matrix multiplication twice and using the associativity of addition and multiplication over a ring, we have the following derivation:

(A(BC ))r,s =
=
=
=

n
X
i=1
n
X

ar,i (BC )i,s

ar,i (

n
X

bi,j cj ,s )
i=1
j =1
n X
n
X
( ar,i bi,j )cj ,s
j =1 i=1
n
X
(AB )r,j cj ,s
j =1

= ((AB )C )r,s
SEQUENTIAL CIRCUITS

3.11 Problem: Show that the circuit of Fig. 3.15 computes the functions defined in the
tables of Fig. 3.14.
Hint: Section 2.2 provides a method to produce a circuit from a tabular description of
a binary function.
...........................................................................
Solution: We can use the hint and design a circuit from the tables for and . The
function  is trivial because it produces as output the value of the second component
in the representation (c, s) for the state of the FSM. By inspection the value of the
function on the state is the same for both values of the input s. Thus, it depends
on the values of c, u, and v. Again by inspection, the value of (c , s ) is the binary
representation of the number of 1s among the three inputs c, u, and v. Since, as shown
in Section 2.7, this function is computed by the Full Adder circuit, the sequential circuit
of Fig. 3.15 does realize the functions described in the tables of Fig. 3.14.
3.12 Problem: Design a sequential circuit (an electronic lock) that enters an accepting state
only when it receives some particular four-bit sequence that you specify.
...........................................................................
Solution It is useful to assume that the FSM has a reserved input letter (a master
key) that resets the FSM to the initial state. Design an FSM that has five states,
q0, q1 , q2, q3 , q4. We describe its action on all input letters except for the master key.
Advance from qi to qi+1 , i = 0, 1, 2, 3, if four successive input letters are correct. If
any one of them is incorrect, enter the reject state q4 . Since q3 is the accept state,
return to it on all inputs. Also return to q4 on all inputs.

3.1 Solutions

41

x7 x6 x5 x4 x3 x2 x1 x0

Compare to p

Adder

+1

x7 x6 x5 x4 x3 x2 x1 x0
Figure S.3.2 A modulo-p counter.

3.13 Problem: Design a sequential circuit (a modulo-p counter) that increments a binary
number by one on each step until it reaches the integer value p, at which point it resets
its value to zero. You should assume that p is not a power of 2.
...........................................................................
Solution: We construct such a circuit from a register, an adder and comparator circuit,
as suggested by Fig. S.3.2. At each step one is added the contents of the register. The
result is compared to p and if it equals p in binary, the output of the comparator is set
to zero. This is ANDed with the word produced by the adder and sent to the register.
Thus, the register is zeroed if one more than its current contents is equal to p.
To determine if the new sum is exactly p, we need only compare the bits of p individually with the bits of the new sum. If they are all equal, the sum has been reached. A
circuit to compare two bits for equality and return 0 if they are not equal is the negation
of the EXCLUSIVE OR gate. An AND tree circuit returns a value of 0 if one or more of
the comparisons returns 0.
3.14 Problem: Give an efficient design of an incrementing/decrementing counter, a sequential circuit that increments or decrements a binary number modulo 2n. Specify
the machine as an FSM and determine the number of gates in the sequential circuit in
terms of n.
...........................................................................
Solution: An incrementing and decrementing counter can be realized by an adder that
either adds 1 or -1 to the current value. Since the result must be the sum modulo 2n, if
the sum reaches 2n, it must be reduced to 0. But this is exactly what a traditional adder

42

Chapter 3 Machines with Memory

does by dropping the most significant bit. To add -1 we take the 2s complement of 1
obtained by complementing every bit and adding 1. This is the all 1s n-tuple. Thus, if
the sum is 0 and -1 is added, a traditional adder produces the result which is the all 1s
n-tuple. Since this is the representation for 2n 1, this is exactly the desired result.
Thus, we can construct an incrementing/decrementing counter modulo 2n from an nbit register, an adder and gates that add either the n-tuple 00    01 or 11    11 to the
current value in the register. The control input can be used as an input to a multiplexer
that selects of these two binary tuples to be input to the adder.

RANDOM-ACCESS MACHINES

3.15 Problem: Given a straight-line program for a Boolean function, describe the steps
taken to compute it during fetch-and-execute cycles of a RAM. Determine whether
jump instructions are necessary to execute such programs.
...........................................................................
Solution: See the solution to Problem 2.4(b).
3.16 Problem: Consulting Theorem 3.4.1, determine whether jump instructions are necessary for all RAM computations. If not, what advantage accrues to using them?
...........................................................................
Solution: Since every T -step RAM computation can be done by a straight-line program, jump instructions, which are not used by straight-line programs, are not necessary. They are very useful, nonetheless, because they can shorten the length of computations.
3.17 Problem: Sketch a RAM program using time and space
of the form f0m 1m j 1  m  ng.

O(n) that recognizes strings

...........................................................................
Solution: Let the RAM CPU have a ternary input alphabet with letters 0, 1 and 2.
Let 2 denote the end of the string. Use a register or memory location to count up the
number of 0s received as input to the CPU. If the first input is not 0, reject the string.
Otherwise, add 1 to the register every time another 0 is received as input. On receiving
the first 1, decrement the value in the register. If no 1s are received or if the register is
not zero on receiving the input 2, reject the string. Otherwise, accept it.

ASSEMBLY-LANGUAGE PROGRAMMING

3.18 Problem: Write an assembly-language program in the language of Fig. 3.18 to subtract
two integers.
...........................................................................
Solution: To subtract a from b, halt if a is zero and otherwise decrement both a and b.
An assembly language program is given below.

3.1 Solutions
R0
N0

N1

R0

R1

R1 JMP N1
DEC R0
DEC R1
JMP+ N0
CONTINUE

43

Comments
End if R1 = 0
Decrement R0
Decrement R1
Repeat

3.19 Problem: The assembly-language instructions of Fig. 3.18 operate on integers. Show
that the operations AND, OR, and NOT can be realized on Boolean variables with these
instructions. Show also that these operations on vectors can be implemented.
...........................................................................
Solution: The following programs realizes AND, OR, and NOT under the assumption
that registers contain the integers 0 or 1. R2 and R3 contain the input data.
R4

R2 AND R3

N1
N2

R0
0
R1
1
R2 JMP N1
R3 JMP N1
R4
R1
JMP N2
R4
R0
CONTINUE

R4

N1
N2
R4

R2 OR R3
R0
0
R1
1
R4
R0
R2 JMP N1
R4
R1
JMP N2
R3 JMP N2
R4
R1
CONTINUE
NOT

R2

Comments
R0 initialized to 0
R1 initialized to 1
R4 is 0 if R2 = 0
R4 is 0 if R2 = 0
R4 has value 1
Computation complete
R4 has 0
Comments
R0 initialized to 0
R1 initialized to 1
Initialize R4 to 0
R4 = R3
R4 has value 1
Computation complete
R4 = R2 = R3 = 0
R4 has value 1
Comments

R0
0
Register R0 initialized to 0
R1
1
Register R1 initialized to 1
R2 JMP N1 R4 is 0 if R2 = 1
R4
R0
R4 has value 0
JMP N2
Computation complete
N 1 R4
R1
R4 has value 1
N2 CONTINUE
Vector AND, OR and NOT can be performed on Boolean vectors using the above program fragments if we represent the vectors as binary numbers and then encode them as
integers. The next bit in a Boolean result vector can be inserted by shifting the current

44

Chapter 3 Machines with Memory

vector left and then adding the new bit to it with a conditional increment operation.
Shifting a vector left one place can be done by adding the vector to itself which is equivalent to multiplying it by 2. The next bit in a Boolean vector can be obtained as the
remainder in the division of the vector by 2. Division of one number by another can
be done by subtracting the second as many times as necessary so that the remainder is
either 0 or 1, adding 1 to a register each time to produce the quotient. Subtraction of b
from a can be done by taking the 2s complement representation of b and adding it to
a. Testing for a remainder of 0 or 1 can be done by testing for 0 or decrementing by 1
and then testing for 0.

3.20 Problem: Write an assembly-language program in the language of Fig. 3.18 to form xy
for integers x and y.
...........................................................................
Solution: An algorithm for this problem starts with temporary result of x, decrements
y and, if not zero, multiplies the temporary result by x. Thus, we put x into register R0
and R1 and the value y into register R2 . We decrement R2 , and if not zero, multiply
R1 times R0 and leave the result in R0 . To multiply the contents of R0 and R1 , we copy
R0 into R4 and R1 into R5 . We then decrement R5 and, if the result is not zero, add
R4 to R0 . To add R4 to R0 we save R4 in R6 and increment R0 once and decrement R6
once until R6 is 0.
R0

N0

N1

N2
N5

R0

+ R1
x
x
y

R0
R1
R2
DEC R1
R1 JMP N5
R4
R0
R5
R1
DEC R5
R5 JMP N5
R6
R4
R6 JMP N1
INC R0
DEC R6
JMP+ N1
CONTINUE
JMP+ N0
CONTINUE

Comments
R0 contains x initially
R0 contains x initially
R2 contains y initially
Decrement R1
Exit if the last value of R1 is 1
Start multiplication by saving R0
Save R1
Decrement R5
Exit if R5 is 0
Copy R4 to start multiplication by addition
End if R6 = 0
Increment R0
Decrement R6
Repeat incrementing and decrementing
Repeat multiplication by x

3.21 Problem: Show that the assembly-language instructions CLR Ri , Ri Rj , JMP+ Ni ,

and JMP Ni can be realized from the assembly-language instructions INC, DEC,
CONTINUE, Rj JMP+ Ni, and Rj JMP Ni .
...........................................................................
Solution: The program for CLR Ri is straightforward and given below. The one for
Ri Rj clears registers Ri and Rk and then procedes to increment both of them while

3.1 Solutions

45

decreasing Rj to 0 which results in copying Rj into both Ri and Rk . It then restores

Rj to its original value by copying Rk into Rj .
CLR Ri
N0

N1

N2

N3

N4

Comments

Ri JMP N1
DEC Ri
0 JMP+ N0
CONTINUE

If Ri = 0, exit
Decrement Ri
Check if Ri is zero

Ri

Comments

Rj

CLR Ri
CLR Rk
Rj JMP N4
INC Ri
INC Rk
DEC Rj
Rj JMP N3
0 JMP+ N2
INC Rj
DEC Rk
Rk JMP+ N4
0 JMP N3
CONTINUE

Invoke routine for CLR

Invoke routine for CLR
Done if Rj = 0
Increment Ri
Increment Rk
Decrement Rj
If Rj is zero, restor its value
Rj is not yet zero; continue copying process
Increment Rj
Decrement Rk
Done if Rk is 0
Resume copying Rk into Rj

JMP Ni

Comments

CLR Rk
Rk JMP Ni

Invoke routine for CLR

Jump is unconditional

JMP+ Ni

Comments

CLR Rk
Rk JMP+ Ni

Invoke routine for CLR

Jump is unconditional

TURING MACHINES

3.22 Problem: In a standard Turing machine the tape unit has a left end but extends indefinitely to the right. Show that allowing the tape unit to be infinite in both directions
does not add power to the Turing machine.
...........................................................................
Solution: To answer this question we show that for every two way infinite TM M1 =
( 1 , Q1 , 1 , s1 , h1 ) there is an equivalent standard TM M2 = ( 2 , Q2, 2 , s2 , h2 ). M2
has its tape split into an upper and lower track so each tape cell can hold two symbols.
On the leftmost position we place a special end of tape marker .. We start simulating
M1 on the upper track of the tape of M2 . When there is a transition that will move the
head to the left of the marker . we change tracks and start simulating M1 on the lower
track of M2 .
The tape of M2 is just a folded version of the tape of M1 . The alphabet 2 is ( 1 
1 ) [ f.g [ . For each transition 1 (q , x) = (r , y ), where x, y 2 Q1 , x 2
1,

46

Chapter 3 Machines with Memory

0
1 [ fL, Rg we construct two new transitions 2 and 2 that correspond to the
simulation of 1 in the upper and lower track respectively. 2 and 20 read and write pairs
of symbols and the head movements of 20 are inverted. M2 has also two transitions for
changing tracks when the head reaches the symbol ..

y2

3.23 Problem: Describe in detail a Turing machine with unlimited storage capacity that
recognizes the language f0m 1m j1  mg.
...........................................................................
Solution: Start with the machine in state qstart with the head positioned in the leftmost cell of the tape. If the symbol under the head is different from 0 then go to state
qreject. If it is a 0 then replace it with (blank symbol), go to state qfind1 and move
the head one cell to the right.
In state qfind1 move the head to the right until it finds a 1. In this case go to state
qskip1 and move the head one cell to the right. If it finds a while in state qfind1 then
go to state qreject.
In state qskip1 move the head to the right until it reads the first blank symbol. Then
move the head one cell to the left, replace the 1 with and go to state qfind0 . If while
in state qskip1 if encounters a 0 then go to state qreject.
In state qfind0 move the head to the left until it finds a 0 and then go to state qskip0.
If it reads a before a 0 check if the symbol to the right is also a . If so go to state
qaccept; otherwise go to state qreject (there are more 1s than 0s in the input).
In state qskip0 move the head to the left until it finds the first . Then move the head
to the right one cell, replace the 0 with a , go to state qfind1 .

3.24 Problem: Sketch a proof that in O(n4 ) steps a Turing machine can verify that a particular tour of n cities in an instance of the Traveling Salesperson Problem satisfies the
requirement that the total distance traveled is less than or equal to the limit k set on
this instance of the Traveling Salesperson Problem.
...........................................................................
Solution: For concreteness, let an instance of TSP be represented by a tuple of the
form (n; d1,2 , d1,3 , : : : , d1,n; d2,3, : : : , d2,n; : : : ; dn 1,n, D). Such a string is in the
language TSP if there is an ordering of the n cities such that the length of the tour is
at most D. Imagine writing this string on a three-track tape. Let the j th semicolon on
the top track be associated with the j th city. Thus, the distances to the right of the j th
semicolon are the distances from the j th city to all higher numbered cities. Also, let
the ith comma following the j semicolon be associated with the city whose number is
i more than j . Let l be the length of the instance of TSP. It follows that l = O(n2 ).
Assume that such a string is in TSP and that a choice agent writes on the second track
below the j th semicolon an integer i such that the tour passes through the j th city by
entering via edge (j , j + i) or edge (j + i, j ). Moving generally left to right, advance
a marker on the third tape from the j th semicolon to the (i 1)st comma to its right,
thereby identifying the length of an edge in the tour. This process takes time O(n2 )
because O(n) steps are needed to move each marker from a semicolon to a comma.
Thus, the time is O(l), linear in the length of the input string.
Now using the third track to mark cells, copy each of the tour edge lengths to the blank
portion to the right of the string and add them up. Moving distances takes time O(l2 )

3.1 Solutions

47

because there are at most l characters in all n distances and at most l cells must be
crossed to move each one of them. The addition of two numbers placed on contiguous
tape cells can be done in time proportional to the square of their length with their sum
placed in the
cells occupied by the number to the right. Thus, the addition can be done
Pn
in time O( i=1 li2 ) where the lengths l1 , l2 , : : : , ln sum to at most l. Thus, this time
bound is O(l2 ).
Finally, compare the result to D by bouncing back and forth between the string representing D and the sum. Since the portion of the occupied tape has length O(l), this
takes time O(l2 ) which is O(n4 ).
3.25 Problem: Design the additional circuitry needed to transform a sequential circuit for
a random-access memory into one for a tape memory. Give upper bounds on the size
and depth of the next-state and output functions that are simultaneously achievable.
...........................................................................
Solution: Since the head can only move from one cell to at most a cell with a number
that is one higher or lower than the current number, we modify the random-access
memory by adding circuits to add 1, 0, or 1 to the current value of the memory
address register. This adds at most O(b) gates to the circuit size and at most O(log b)
to the depth of the next-state/output functions for the random-access memory unit.
3.26 Problem: In the proof of Theorem 3.8.1 it is assumed that the words and their addresses in a RAM memory unit are placed on the tape of a Turing machine in order of
increasing addresses, as suggested by Fig. 3.40 (which is repeated as Fig. S.3.3). The addresses, which are dlog me bits in length, are organized as a collection of ddlog me=be
b-bit words. (In the example, b = 1.) An address is written on tape cells that immediately precede the value of the corresponding RAM word. A RAM address addr is
stored on the tape to the left in the shaded region.
Assume that markers can be placed on cells. (This amounts to enlarging the tape alphabet by a constant factor.) Show that markers can be used to move from the first
word whose RAM address matches the ib most significant bits of the address a to the
next one that matches the (i + 1)b most significant bits. Show that this procedure can
be used to find the RAM word whose address matches addr in O((m=b)(log m)2 )
Turing machine steps by a machine that can store in its control unit only one b-bit
subword of addr.
...........................................................................
Solution: Assume that the addresses placed on the tape appear in increasing order
from left to right. Assume that the first b-bit word in an address is the most significant
word and that successive such address words have decreasing significance. Also, assume

}


}


w3

w2

w1

w0

}


}
0

Figure S.3.3 A TM tape with markers on words and the first bit of each address.

48

Chapter 3 Machines with Memory

that the first and most significant word in each address is marked (as indicated by the
diamonds in Fig. S.3.3) as are the data words (as indicated by the spades in Fig. S.3.3).
Let the address addr be specified as q = ddlog me=be b-bit words stored in decreasing
significance from left to right in the first q tape cells and let its most significant word
be marked.
The TM reads the currently marked word, c, of addr and moves right to the first
marked address word associated with data whose value agrees with c, erasing all previous
marks as it advances. This process places the head over the first cell whose value agrees
with c. If the next cell does not contain a RAM data word, it now travels to the right
end of the non-blank portion of the tape advancing this and subsequent address marks
on address words one cell to the right. This insures that on the next pass it will compare
the word of next lower significance in addr with the appropriate word in the simulated
RAM. This process repeats until all words of addr have been marked in which case the
leftmost mark on an address word is that of the RAM word whose address is addr.
The random-access memory has m b-bit words and a tape cell can hold a b-bit word
as well as a mark. Each address occupies q tape cells whereas the corresponding data
occupies one cell. Thus, there are O(mq) cells used to store addresses and data for
entries in the random-access memory. Thus, one pass over the tape can be done in
O(mq) steps. The number of passes over the tape is equal to the number of words in
one address, which is q. It follows that the data whose address matches addr can be
found in O(mq2 ) steps. But O(mq2 ) = O(m(log m=b)2 ), which is smaller than the
bound requested.

3.27 Problem: Extend Problem 3.26 by demonstrating that the simulation can be done with
a binary tape symbol alphabet.
...........................................................................
Solution: This problem can be addressed as follows: Given an arbitrary tape alphabet,
find a binary encoding of its letters. Extend the tape alphabet to include the special
symbols such as semicolon and comma as well as the marked versions of these symbols.
Now choose any encoding of this alphabet that assigns a fixed number of bits to every
letter. The control unit is then designed to read this fixed number of bits before it
simulates the state transition that would be performed by the original machine. After
making this transition, it procedes to replace the fixed-length bit string on its tape with
the encoding of the new symbol that is written by the original Turing machine. Also, if
necessary, it moves the head by the number of binary tape cells needed to place it over
the first cell corresponding to the original head placement.
3.28 Problem: Extend Theorem 3.8.1 to show that there exists a Turing machine that can
simulate an unbounded-memory RAM.
...........................................................................
Solution: When the RAM is simulated an address may be produced that is larger
than an existing address. This requires the creation of new address/data pairs for the
higher indexed RAM locations as well as expanding the address portions of existing
address/data pairs. The existing address/data pairs must have inserted as their new
most significant b-bit words the value 0.
The address/data pairs for the enlarged set of address can be created as follows: a) shift
the existing address/data pairs words right to create space for the new most significant

3.1 Solutions

49

b-bit words each of word (a procedure for this purpose is sketched below) and insert the
word 0 in the most significant place in each of these address words; b) for each of the
2b b-bit words except the first, copy the newly expanded originally address/data pairs
and replace their most significant b-bit words by the appropriate value and replace their
data words by 0.
A Turing machine that shifts the contents of contiguous tape cells to the right by one
place is easily realized by placing marks on the cell to the left of the leftmost character
to be shifted as well as the rightmost character to be shifted. It then moves to the right
marker, copies the value under the head to the cell to the right. If the cell to the left of
the marked cell contains the left marker, the process is complete; otherwise, the process
continues at the right marker.
3.29 Problem: Sketch a proof that every Turing machine can be simulated by a RAM program of the kind described in Section 3.4.3.
Hint: Because such RAM programs can only have a finite number of registers, encode
the contents of the TM tape as a number to be stored in one register.
...........................................................................
Solution: We must show that the control and tape units can be simulated. We use a
finite number of RAM registers for this purpose. While this may not seem possible, we
note that no restriction is placed a priori on the size of the largest integer that can be
held in a RAM register. As shown below, we use a finite number of registers to record
the state transitions and two registers to record the contents of the tape.
Because a control unit is an FSM defined by a next-state/output function, we need only
describe a procedure to compute any fixed function with a RAM program. We reasoned
in Problem 3.27 that tape alphabet can be binary. We make this assumption here.
To simulate an FSM with binary input alphabet, let the FSM state be represented by
an integer in register R0 . Let the input and output be located in registers R1 and R2 .
Form a state/input pair by shifting R0 left one place (add it to itself ) and adding R1 to
it.
Let q be the number of state/input pairs of the FSM (values in R0 after the above
operation). These pairs are associated with the integers 0, 1, 2, : : : , q 1. To compute
the next state of the FSM, we write a RAM program to jump to location Nk when R0
contains k. In this location, we place an instruction of the form R0
r which causes
r to be the next state. We follow this by an instruction of the form R2 s which
produces the output s. The next instruction jumps back to the first instruction of the
program.
To cause the RAM program to jump to location
following code fragment:

k when

50

Chapter 3 Machines with Memory

R0 JMP N0
DEC R0
R0 JMP N1
DEC R0
R0 JMP N2

::
:

Go to N0 if R0 = 0
Decrement R0
Go to N1 if R0 = 1
Decrement R0
Go to N2 if R0 = 2

DEC Rq 1
Decrement R0
R0 JMP Nq 1 Go to Nq 1 if R0 = q 1
We simulate a binary tape unit by two integers, one of which represents the binary
sequence to the left of the tape head and the other which represents the binary sequence
to its right. In the solution to Problem 3.28 we give a procedure to shift a binary
sequence left one place, adding in a new bit as does. Also, we give a procedure to shift a
binary sequence right one place, reading the rightmost bit that is produced by this shift.
We use these procedures to simulate the movement of the head on the tape. Together
with the description of the control unit, we have a program that simulates an arbitrary
Turing machine.
COMPUTATIONAL INEQUALITIES FOR TURING MACHINES

3.30 Problem: Show that a one-tape Turing machine needs time exponential in n to compute most Boolean functions f : Bn 7! B on n variables, regardless of how much
memory is allocated to the computation.
...........................................................................
Solution: This follows from Corollary 3.9.1 and Theorem 2.12.1. Corollary 3.9.1
shows
C
(f ) is at least
p that the time T compute a Boolean function f with circuit size
n

( C
(f )) while Theorem 2.12.1 shows that C
(f ) is at least 2n (1 ) 2n2 for
most Boolean functions on n variables.

3.31 Problem: Apply Theorem 3.2.2 to the one-tape Turing machine that executes T steps.
Determine whether the resulting inequalities are weaker or stronger than those given in
Theorem 3.9.2.
...........................................................................
Solution: We derive an upper bound on the number of states which a TM can assume
that executes T steps and uses m cells. We then apply Theorem 3.2.2.
The number of distinguishable states of the tape unit is at most the product of the
number of different positions for the tape head (m) with the sum of the maximum
number of different values that can be assumed by the first j cells of the tape for 1 
j  m (a blank can reside to the right of the j th cell). Since each cell can have at most
jj values, the tape can assume at most the following number of values.
m
X
j =1

It follows that the T -step TM can have at most

number of states of the control unit. Let a 
most mam+2 states.

1)


mjjm+2 jQj states where jQj is the

max(jj, jQj). Then, the TM has at

3.1 Solutions

51

From Theorem 3.2.2 and the fact that two n  n matrices can be multiplied with
O(n3 ) operations, since n = mam+2 in this case, the following bounds hold:

C
(f ) = O(m3 a3(m+2) T )
D
(f ) = O(m log T )
The first inequality given above, which is of the form O(S 3 bS T ), is weaker than the
first inequality given in Theorem 3.9.2, whereas the second given above, which is of the
form O(S log T ), is stronger than the second in Theorem 3.9.2 when S log T is much
smaller than T log S .

3.32 Problem: Write a program in your favorite language for the procedure WRITE OR(t,
m) introduced in Fig. 3.27.
...........................................................................
Solution: For the tth step, 1  t  T , this program writes a straight-line program for
the vector OR of vj ,t, 0  j  m 1, where j indexes a memory cell and vj ,t is a
binary b-tuple. Let vj ,t,r be the rth bit, 1  r  b, in the j th cell on the tth step.
Then, for each value of j and t the OR must be taken of vj ,t,r . This can be done by
taking the OR of vj ,t,1 and vj ,t,2 , taking the OR of the result with vj ,t,3 , etc.
3.33 Problem: Write a program in your favorite language for the procedure WRITE CELL
CIRCUIT (t, m) introduced in Fig. 3.27.
Hint: See Problem 2.4.
...........................................................................
Solution: This program is straightforward.
FIRST P-COMPLETE AND NP-COMPLETE PROBLEMS

3.34 Problem: Show that the language MONOTONE CIRCUIT VALUE defined below is
P-complete.
MONOTONE CIRCUIT VALUE

Instance: A description for a monotone circuit with fixed values for its input variables
and a designated output gate.
Answer: Yes if the output of the circuit has value 1.
Hint: Using dual-rail logic, find a way to translate (reduce) a string in the language
CIRCUIT VALUE to a string in MONOTONE CIRCUIT VALUE by converting in logarithmic space (in the length of the string) a circuit over the standard basis to a circuit
over the monotone basis. Note that, as stated in the text, the composition of two
logarithmic-space reductions is a logarithmic-space reduction. To simplify the conversion from non-monotone circuits to monotone circuits, use even integers to index
vertices in the non-monotone circuits so that both even and odd integers can be used
in the monotone case.
...........................................................................
Solution: Dual-rail logic circuits are described in Problem 2.12. A standard circuit is
converted to a dual-rail one by replacing each wire by a pair of wires carrying a value and

52

Chapter 3 Machines with Memory

its complement. Let steps in the original straight-line program be given even numbers.
If step 2j carried the value x, in the new program step 2j carries the value x and step
2j + 1 carries the value x.
As a consequence of this representation, input step (2k read 2i) is replaced by the steps
(2k read 2i) and (2k + 1 read 2i + 1) where the input 2i and 2i + 1 are complements.
Each NOT gate is replaced by a twisted pair of wires, and AND and OR gates are each
replaced by one AND and one OR gate. Step (2k NOT 2i) in the original program is
replaced by steps (2k = 2i + 1) and (2k + 1 = 2i). (The equality operator can be
realized by either AND or OR with two identical arguments.) This assignment realizes
the DRL- NOT as a pair of twisted wires. Step (2k AND 2i 2j ) is replaced by the steps
(2k AND 2i 2j ) and (2k + 1 OR 2i + 1 2j + 1). Similarly, (2k OR 2i 2j ) is replaced by
the steps (2k OR 2i 2j ) and (2k + 1 AND 2i + 1 2j + 1).
Given a straight-line program, the generation of the new steps from the original steps
can be done by examining one step at a time and performing computations to increment variables. This represents an amount of space that proportional to the length of
the numbers involved. Since these numbers refer to steps in the program, they have
a length logarithmic in the length of the program. It follows that the translation can
be done deterministicaly in logarithmic working space. Because a straight-line program evaluates to 1 if and only if its monotone equivalent evaluates to 1, a straight-line
program is in CIRCUIT VALUE if and only if the corresponding dual-rail straight-line
program is in MONOTONE CIRCUIT VALUE. Since every language in P can be reduced to CIRCUIT VALUE in logspace and the composition of two logspace reductions
is a logspace reductions, every language in P can be reduced to MONOTONE CIRCUIT
VALUE in logspace. Thus, MONOTONE CIRCUIT VALUE is P-complete.

3.35 Problem: Show that the language FAN - OUT 2 CIRCUIT SAT defined below is NPcomplete.
FAN - OUT 2 CIRCUIT SAT Instance: A description for a circuit of fan-out 2 with fixed
values for its input variables and a designated output gate.
Answer: Yes if the output of the circuit has value 1.

Hint: To reduce the fan-out of a vertex, replace the direct connections between a gate
and its successors by a binary tree whose vertices are AND gates with their inputs connected together. Show that, for each gate of fan-out more than two, such trees can be
generated by a program that runs in polynomial time.
...........................................................................
Solution: Since it is desirable but not necessary that a step in a straight-line program
operate on the results of previous steps, we simplify the design of a translator by dropping this restriction.
Since a polynomial-time RAM program runs in polynomial time on a Turing machine,
we describe a RAM translator program. This program visits every step of the straightline program and collects the largest integer, L, used to identify a step. It then adds a
field to each step that points to the root of a binary tree. For example, if (n OP p q) is
a step, a field is added immediately after n that is initially to form (n OP p q).
The program revisits each step of the straight-line program for the purpose of reducing
the fan-out. Consider the first step (the sth) that contains a reference to another step.

3.1 Solutions

53

If step s refers to step n, increment L, replace in step n with the new value of L,
replace n in step s by L, and create the step (L = n), a new step that says the value
of step L is the value of step n. (The equality operator can be realized by either AND
or OR with two identical arguments.)
This additional step does not change the fan-out of step n but it does cause step s to
refer to this new step which has fan-out one. When a step is encountered that refers
to a step n for which the new field is not but is some integer, say r, the program
increments L, visits step r, and if its second field is not but is r1 visits step r1 and its
possible successors until a step is found whose second component does contain . At
this point the program replaces by L and creates a new record of the form (L = q)
where q is the label of the last step visited. The last operation reduces the fan-out of
step n by one and increases the fan-out of step q from 1 to 2. Following in this fashion,
the fan-out of every step is reduced to 2.
Let fi be the fan-out of step i. Because a constant number of steps is performed for the
first step pointing to step i, and O(t) steps on the tth step to point to step i, the number
P
of operations performed, T , is proportional to S = ki=1 fi + (fi 1) +    + 1,
where k is the number of different steps in the original program. Here t  fi and
P
N = ki=1 fi both the number of times that some step is used by another step and the
number of times labels appear in steps. Thus, N is at most twice the number of steps
and T is at most the square of the number of steps.
3.36 Problem: Show that clauses given in the proof of Theorem 3.9.7 are satisfied only
when their variables have values consistent with the definition of the gate type.
...........................................................................

3.37

Solution: Let gi be the value of the result of step i. The function associated with
(i READ x) is gi = x. But the clauses (g i _ x) and (gi _ x) are both 1 when both
gi and x are 1 or both g i and x are 1. These are exactly the conditions under which
gi = x.
The function associated with (i NOT j ) is gi = gj . But the clauses (gi _ g j ) (gi _ gj )
are both 1 when gi is 1 and gj is 1 (that is, gj is 0), or gi is 0 and gj is 1. These are
exactly the conditions under which gi = gj .
The function associated with (i OR j k) is gi = gj _ jk . But the clauses (gi _ gj ),
(gi _ gk ), and (gi _ gj _ gk ) are all True when either gi is 1 and one of gj and gk is
True or when gi is 0 and both g j and gk are 1. These are exactly the conditions under
which gi = gj _ jk .
The function associated with (i AND j k) is gi = gj _ jk . But the clauses (g i _ gj ),
(g i _ gk ), and (gi _ gj _ gk ) are all True when gi is 1 and both gj and gk are True or
when gi is 0 and either gj or gk are False.
The function associated with (i OUTPUT j ) is 1 exactly when gj is 1 which is exactly
the condition under which the clause (gj ) is True.
Problem: A circuit with n input variables fx1 , x2 , : : : , xng is satisfiable if there is
an assignment of values to the variables such that the output of the circuit has value
1. Assume that the circuit has only one output and the gates are over the basis
=
fAND , OR, NOT g.

54

Chapter 3 Machines with Memory

a) Describe a nondeterministic procedure that accepts as input the description of a
circuit in POSE and returns 1 if the circuit is satisfiable and 0 otherwise.
b) Describe a deterministic procedure that accepts as input the description of a circuit
in POSE and returns 1 if the circuit is satisfiable and 0 otherwise. What is the
running time of this procedure when implemented on the RAM?
c) Describe an efficient (polynomial-time) deterministic procedure that accepts as input the description of a circuit in SOPE and returns 1 if the circuit is satisfiable
and 0 otherwise.
d) By using Boolean algebra, we can convert a circuit from POSE to SOPE. We can
then use the result of the previous question to determine if the circuit is satisfiable.
What is the drawback of this approach?
...........................................................................
Solution: a) A non-deterministic procedure for determining if a circuit in POSE is
satisfiable chooses the values for x1 , x2 , : : : , xn for which the circuit has output 1 and
then verifies that this assignment is correct by computing the values at the gates.
b) A deterministic procedure for determining if a circuit in POSE is satisfiable would
iterate through all the possible values of the inputs x1 , x2 , : : : , xn . As there are 2n
possible assignments this procedure will run in exponential time.
c) A circuit of SOPE is satisfiable if it has a product not containing both an input
variable and its complement. This can be determined by scanning the description of
the circuit. This procedure will run in polynomial time.
d) A procedure for converting from a circuit in POSE to a circuit in SOPE would
normally require exponential time. If this procedure would take polynomial time then
there will be a deterministic polynomial time procedure for determining if a circuit in
POSE is satisfiable and we would have a polynomial-time solution to P = NP .

CENTRAL PROCESSING UNIT

3.38 Problem: Write an assembly-language program to multiply two binary numbers using
the simple CPU of Section 3.10. How large are the integers that can be multiplied
without producing numbers that are too large to be recorded in registers?
...........................................................................
Solution: Since the product of two n-bit binary numbers is a 2n-bit number and our
simple CPU can hold 16-bit numbers, we limit the multiplication program to 8-bit
numbers.
The program of Figure S.3.4 uses the algorithm of Section 2.9; that is, it multiplies v
by u by adding v2j to a running sum if uj = 1. We form v2j by shifting v left on
successive cycles using CIL. Determination of the values of uj , 0  j  7 can be done
in a variety of ways. We choose to form the AND of u with 8 different Boolean vectors
containing a single 1 and then use JZ to jump to determine whether to add the current
shifted version of v.
Location ADDR 8 holds v and shifted versions thereof. Location ADDR 9 holds u.
The partial sum is stored in location ADDR 10. The 8 Boolean vectors containing
a single 1 in the 8 least significant places are stored in locations ADDR 0, ADDR 1,
: : : , ADDR 7. In this program we use indirect addressing and use location ADDR 11

3.1 Solutions
ORG

CLA
N0

Clear AC
ADDR 10

Initialize partial sum to 0

LDA

ADDR 9

Load AC with u

ADDR 11

Expose bit 0 of u

N1

LDA

ADDR 10

ADD

ADDR 8

STA

ADDR 10

JZ

CLA
ADD

Clear AC
ADDR 8

Load v2j
Shift v2j left

CIL
N1

STA
AND

STA

ADDR 8

Store v2j +1

LDA

ADDR 11

ADD

ADDR 0

STA

ADDR 11

Store new address for indirection

ADD

ADDR 12

Add 2s complement of 7

N0

Repeat the addition if not done

JZ

55

END
Figure S.3.4 A program to add two binary 8-bit numbers.

for the address at which to find the address of the next Boolean vector to AND with
u. Indirection can be avoided by replicating code. The 2s complement of 7, the
highest addressed used for a Boolean vector, is stored in location ADDR 12. This value
is added to the address used for indirection. When the sum is zero, this address has
reached the value 7.
3.39 Problem: Assume that the simple CPU of Section 3.10 is modified to address an unlimited number of memory locations. Show that it can realize any Boolean function by
demonstrating that it can compute the Boolean operations AND, OR, and NOT.
...........................................................................
Solution: We slightly modify the programs given in the solutions to Problem 3.19 to
account for the differences in syntax between RAM assembly language statements and
the simple CPU assembly language statements.

3.40 Problem: Design a circuit to produce the timing variables tj , 1  j  k, of the simple
CPU. They must have the property that exactly one of them has value 1 at a time and
they successively become 1.

56

Chapter 3 Machines with Memory

Hint: Design a circuit that counts sequentially modulo k, an integer. That is, it increments a binary number until it reaches k, after which it resets the number to zero. See
Problem 3.13.
...........................................................................
Solution: The solution to Problem 3.13 describes a circuit to realize a counter modulop. We use such a counter to count to modulo-k and supply the output to a decoder
with at least k outputs. The first k outputs are the desired values.

3.41 Problem: Complete the design of the CPU of Section 3.10 by describing circuits for
PC, MAR, MDR, OPC, INR, and OUTR.
...........................................................................
Solution: Fig. 3.38 shows the microinstructions affecting each register. Those affecting AC are used to construct the circuit shown in Figure 3.39. The control variables
affecting the registers PC, MAR, MDR, OPC, INR, and OUTR are described on page
144.
When L(PC, PC+1) = 1, the value of PC is incremented and when L(PC, MDR) =
1, PC is given the value of MDR. Thus, the input to the register PC is a multiplexer
one input of which is MDR and the other is an adder that has as one input the current
value of PC and as a second the value 1.
When L(MAR, PC) = 1, MAR is given the value of PC and when L(MAR, MDR) =
1 it is given the value of MDR. Thus, MAR has as input a multiplexer whose two inputs
are PC and MDR.
When L(MDR, M) = 1, MDR is given the value M read from memory and when
L(MDR, AC) = 1, MDR is given the value of AC. Thus, MDR has as input a multiplexer whose two inputs are MDR and AC.
When L(OPC, MDR) = 1, OPC is given the value of MDR. Thus, the OPC has as
input the value of MDR each bit of which is ANDed with L(OPC, MDR).
When L(OUTR, AC) = 1, OUTR is given the value of AC. Thus, the OUTR has as
input the value of AC each bit of which is ANDed with L(OUTR, AC).
The register INR always supplies its input to the multiplexer feeding AC.
3.42 Problem: Show that an indirect store operation can be simulated by the computer of
Section 3.10.
Hint: Construct a program that temporarily moves the value of AC aside, fetches the
address containing the destination for the store, and uses Boolean operations to modify
a STA instruction in the program so that it contains the destination address.
...........................................................................
Solution: The code shown in Fig. S.3.5 is self-modifying. It simulates STA I A 1,
indirect addressing through address A 1, by moving the contents of AC to location
A 0, loading AC with A 1 which contains the new address A NEW in its twelve least
significant positions, and ANDs this word with the bit pattern 0110(1)16 where (0110)
is the binary encoding for STA. The result is the bit pattern for STA A NEW. This
value is stored in location Y (replacing NOOP) which immediately follows the next
instruction, LDA A 0, which restores the value of AC. Since the instruction that is
executed after LDA A 0 is the new instruction STA A NEW, this piece of assembly
language code realizes STA I A 1 through self modification.

3.1 Solutions

ORG

STA

A0

Store AC temporarily.

LDA

A1

Load address A NEW on which to indirect.

AND

A2

A 2 contains (STA)11...1.

STA

LDA

A0

NOOP

57

Store new instruction, namely, STA A NEW.

Restore AC to original value.
Program executes STA with address A NEW.

END
Figure S.3.5 A program to realize indirect addressing with STA.

3.43 Problem: Write an assembly-language program that repeatedly examines the input
register until it is nonzero and then moves its contents to the accumulator.
...........................................................................
Solution: This problem has a simple solution. Repeatedly jump back to the instruction
(IN) that loads AC until AC is non-zero.
3.44 Problem: Sketch an assembly-language program to emulate a target CPU by a host
CPU under the assumption that each CPUs instruction set supports indirection. Provide a skeleton program that reads an instruction from the target instruction set and
decides which host instruction to execute. Also sketch the particular host instructions
needed to emulate a target add instruction and a target jump-on-zero instruction.
...........................................................................
Solution: The general idea here is that after reading an instruction from a program for
the target CPU, the host CPU branches to a small set of assembly language instructions
to emulate the target CPU instruction. Let each target CPU instruction contain three
fields, a bit pattern for an instruction name, an address, and a bit denoting whether or
not addressing is indirect.
The target and emulator programs are located in separate portions of memory. The
emulator program reserves a location for the address of the current instruction of the
target program. The emulator moves the current values of its registers aside to reserved
locations. Using indirection, it loads the first target instruction into the accumulator.
It then stores the accumulator in a reserved location so that it can perform multiple

ORG
Y

IN
JZ

Program is stored at location X.

Load AC with the value of IN.

Continue unless AC is zero.

END
Figure S.3.6 A program that loads AC with the value of IN when IN becomes non-zero.

58

Chapter 3 Machines with Memory

operations on it after which it increments the address of the current instruction of the
target program. It then begins to emulate the current target instruction.
The emulator loads the target instruction three times, ANDing it with bit vectors to decompose into its three parts which it stores in three memory locations. (If host addresses
are stored in different bit locations than are target addresses, some address manipulation will be necessary.) It then tests the instruction bit pattern against the possible target
instruction bit patterns and executes the comparable host instruction with a direct or
indirect address depending on the value of the indirect bit. This comparison process
could be done by successively substracting each of the possible instruction bit patterns
until one is found that causes the accumulator to be zero.
If the target instruction is a direct add, the host reloads all registers and then executes its
add instruction indirectly through the address of the location that contains the address
portion of the target instruction. If the target instruction uses indirection, an indirect
load through the register containing the address portion of the target instruction is
needed to load the address at which to execute the instruction. The steps for a direct
add are then followed. Similar steps are followed for a jump-on-zero target instruction.

Finite-State Machines and

Pushdown Automata

4.1 Solutions
FSM MODELS

4.1 Problem: Let M = (, , Q, , , s, F ) be the FSM model described in Definition 3.1.1. It differs from the FSM model of Section 4.1 in that its output alphabet
has been explicitly identified. Let this machine recognize the language L(M ) consisting of input strings w that cause the last output produced by M to be the first letter in
. Show that every language recognized under this definition is a language recognized
according to the final-state definition in Definition 4.1.1 and vice versa.
...........................................................................
Solution: Given an FSM M described in Definition 3.1.1 that recognizes the language
L of strings that cause M to produce as its last output the first letter a in , make the
states in M that produce such letters final states. This new version of M is an instance
of an FSM given by Definition 4.1.1 that recognizes L. Similarly, given an FSM given
by Definition 4.1.1 that recognizes L, change its definition so that it outputs letter a in
each state that is designated as final. This new machine conforms to Definition 3.1.1
and recognizes L.
4.2 Problem: The Mealy machine is a seven-tuple M = (, , Q, , , s, F ) identical
in its definition with the Moore machine of Definition 3.1.1 except that its output
function  : Q   7! depends on both the current state and input letter, whereas
the output function  : Q 7! of the Moore FSM depends only on the current
59

60

Chapter 4 Finite-State Machines and Pushdown Automata

state. Show that the two machines recognize the same languages and compute the same
functions with the exception of .
...........................................................................
Solution: The solution to this problem is the same as solution to Problem 3.6.
4.3 Problem: Suppose that an FSM is allowed to make state -transitions, that is, state
transitions on the empty string. Show that the new machine model is no more powerful
than the Moore machine model.
Hint: Show how -transitions can be removed, perhaps by making the resultant FSM
nondeterministic.
...........................................................................
Solution: Let state q1 have an -transition to state q2 and suppose that there is a transition into state q1 on input a (which itself could be ) from q0 . Observe that the
transition from q1 to q2 on input  can be replaced by a transition from q0 to q2 on
input a = a without changing the set of strings that are accepted by the FSM. Each
application of this procedure reduces the number of -transitions without changing the
language recognized. Applying this procedure as many times are there are -transitions
converts the FSM to one without any -transitions recognizing the same language.

EQUIVALENCE OF DFSMS AND NFSMS

4.4 Problem: Functions computed by FSMs are described in Definition 3.1.1. Can a
consistent definition of function computation by NFSMs be given? If not, why not?
...........................................................................
Solution: A function must have one value for each point in its domain. Given an
NFSM there may be many different output sequences for each input sequence as the
choice agent ranges over all of its corresponding sequences. It is not clear how to choose
which value to assign to each input sequence. In the case of language recognition by
an NFSM, our goal is to get the machine to enter a final state or to produce a specific
value, say 1. In this case the issue is whether a choice input can produce this result, for
which there is no ambiguity; either it can or it cannot. Hence, language recognition
can be defined unambiguously for NFSMs even if function computation cannot.
4.5 Problem: Construct a deterministic FSM equivalent to the nondeterministic FSM
shown in Fig. 4.32 (repeated here as Fig. S.4.1).
...........................................................................
Solution: The next-state tables for the equivalent machine, Mequiv , is given in Figure S.4.2. All states are final states except for fq0 g and fq0 , q2 g.
REGULAR EXPRESSIONS

4.6 Problem: Show that the regular expression 0(0 10)+ defines strings starting with 0
and containing at least one 1.
...........................................................................
Solution: Let r = (0 10). Then 0r+ denotes the strings beginning with 0 followed
by strings denoted by r+ which is r + r2 + r3 +    + rk +    . A string in rk contains k

4.1 Solutions
0
Start

61

q0

q1
1

0, 1

q2

1
0

0, 1

q3

Figure S.4.1 A nondeterministic FSM.

1s with zero or more 0s between them. It follows that r+ describes strings containing
at least one 1 and 0r+ denotes strings beginning with 0 and containing at least one 1.

4.7 Problem: Show that the regular expressions 0 , 0(010 )+ , and 1(0 + 1) partition
the set of all strings over 0 and 1.
...........................................................................
Solution: It follows from Problem 4.6 that 0(0 10 )+ denotes strings beginning with
0 and containing at least one 1. Clearly, 1(0 + 1) denotes strings beginning with 1 and
containinng 0s and 1s. The only remaining case is that of strings that do not contain
any 1s, that is, just 0s, namely, 0.
4.8 Problem: Give regular expressions generating the following languages over  = f0, 1g:
a) L = fw j w has length at least 3 and its third symbol is a 0g
b) L = fw j w begins with a 1 and ends with a 0g
c) L = fw j w contains at least three 1sg
...........................................................................
Solution:
a) (0 + 1)2 0(0, 1)
b) 1(0 + 1) 0
c) (0 10 )3 (0 + 1)

4.9 Problem: Give regular expressions generating the following languages over  = f0, 1g:
a) L = fw j w is any string except 11 and 111g
b) L = fw j every odd position of w is a 1g
...........................................................................
Solution:
a) To construct the complement of a set, construct an FSM that accepts the set and
take the union of the regular expressions associated with the non-accepting states.
An FSM accepting 11 + 111 has states q1 , q2 , q3 , q4 , and q5 where q1 is the initial

62

Chapter 4 Finite-State Machines and Pushdown Automata

qequiv
a Mequiv (qequiv , a)
fq0g
0 fq0 , q2 g
fq0g
1 fq1 , q2 g
fq0, q2g
0 fq0 , q2 , q3 g
fq0, q2g
1 fq1 , q2 g
fq1, q2g
0 fq0 , q1 , q3 g
fq1, q2g
1 fq0 , q1 g
fq0, q1g
0 fq0 , q1 , q2 g
fq0, q1g
1 fq0 , q1 , q2 g
0 fq0 , q1 , q2 , q3 g
fq0, q2, q3 g
fq0, q2, q3 g
1 fq1 , q2 g
fq0, q1, q3 g
0 fq0 , q1 , q2 g
fq0, q1, q3 g
1 fq0 , q1 , q2 g
fq0, q1, q2 g
0 fq0 , q1 , q2 g
fq0, q1, q2 g
1 fq0 , q1 , q2 g
fq0, q1, q2 , q3g 0 fq0, q1 , q2, q3 g
fq0, q1, q2 , q3g 1 fq0, q1 , q2, q3 g
Figure S.4.2 The next-state table of the DFSM equivalent to that given in Fig S.4.2.

state and the FSM advances from q1 to q2 , from q2 to q3 , and from q3 to q4 , on

input 1 and moves from each of these states to q5 on 0. It returns to q5 on 0 and 1.
By inspection we can see that the regular expression describing strings other than
11 and 111 is (0 + 10 + 110 + 1110)(0 + 1) .
b) The first position contains 1. If the string has odd length, it ends in 1 and is
represented by 1((0 + 1)1) . If it has even length, it is represented by 1((0 +
1)1) (0 + 1). Thus, all such strings are represented by 1((0 + 1)1) ( + 0 + 1).

4.10 Problem: Give regular expressions for the languages over the alphabet f0, 1, 2, 3, 4, 5,
6, 7, 8, 9g describing positive integers that are:
a)
b)
c)
d)

even
odd
a multiple of 5
a multiple of 4

...........................................................................
Solution: Let  = f0, 1, 2, 3, 4, 5, 6, 7, 8, 9g.

a)  (2 + 4 + 6 + 8)
b)  (1 + 3 + 5 + 7 + 9)
c)  5
d) We assume that the digits are supplied least significant digit first. We note that
if the sequence has three or more digits, the integer can be written in the form
q  100 + r where q is the quotient and r the remainder under division by 100.
Since 100 is divisible by 4, the integer is divisible by 4 if and only if the remainder
is. Thus, we need only represent sequences of length 1 or 2 that are divisible by 4.

4.1 Solutions

63

The sequences of length 1 are (0 + 4 + 8). By enumeration we can see that those of
length 2 either have 0, 4 or 8 as the least significant digit and an even second least
significant digit or have 2 or 6 as the least significant digit and an odd second least
significant digit. Let E = f0, 2, 4, 6, 8g and O = f1, 3, 5, 7, 9g. It follows that the
integers divisible by 4 are denoted by (0 + 4 + 8)+(0 + 4 + 8)E  +(2 + 6)O .
4.11 Problem: Give proofs for the rules stated in Theorem 4.3.1.
...........................................................................
Solution: In the following let R, S and T be the sets denoted by the regular expressions
r, s, and t.
(1) The concatenation of any set with the empty set ; is the empty set.

(2) The concatenation of any set R with the set containing the empty string  is the
set itself.
(3) The union of any set R with the empty set is R itself.

(5) The union of two sets R and S is commutative; that is, R [ S

(6)

(7)

(8)
(9)

= S [ R.
R(S [ T ) = RS [ RT because the concatenation of a set R with the union,
S [ T , of two sets is the set of strings that have R as prefix and strings from either
S or T as suffix.
(R [ S )T = RT [ ST because the concatenation of the union of two sets, S [ T ,
with a set R is the set of strings that have R as suffix and strings from either S or
T as prefix.
R(ST ) = (RS )T because the concatenation operation is associative.
;  =  because the Kleene closure contains  and the concatenation of the empty
set with itself, which is the empty set.

(10)  =  because the Kleene closure contains  and the concatenation of the empty
string with itself, which is the empty string.

= r because the expression on the left is equal to ( + r) + ( + r)2 +

   + ( + r)k +    and  and r are in r and, by induction on k, ( + r)k is
contained in r + r2 +    + rk .
( + r) = r follows from (11) since ( + r) and ( + r)+ differ only in that
the former is guaranteed to contain .
We establish r ( + r) = r , the other identity being shown in the same fashion.
We observe that r  = r and r = r+ . Applying (6) we have the desired result.
r s + s = rs follows because s is included in r s.
r(sr) = (rs) r follows because the former can be written as r( + (sr) +
(sr)2 +    ) and the latter can be written as ( + (rs) + (rs)2 +    )r. Clearly,
r = r and r(sr)k = (rs)k r.
We show that (r + s) = (r s) r , the other identity being shown in a similar
fashion. Note that (r + s)2 = r2 + rs + sr + s2 . Because (r + s)k is the union
of all sequences of length k over the set fr, sg, (r + s) contains alternations of
sequences of rs and ss.

(11) ( + r)+

(12)
(13)
(14)
(15)

(16)

64

Chapter 4 Finite-State Machines and Pushdown Automata

Now consider (r s) r . We write r s as follows:

r s =

X
k1

rk s
1

Thus, (r s) can be written as follows:

X
X
(r s) =  + rk s +
rk srk s +
1

k1

k1 ,k2

X
k1 ,k2 ,k3

rk srk srk s +   
1

That is, (r s) is alternating sequence of rs and ss ending in s. Concatenating

this with r on the right, we have the desired conclusion.

4.12 Problem: Show that  + 01 +(010)(10 + 010) ( + 1 + 01) and (01 + 010) describe
the same language. (The statement of this problem in the book was in error.)
...........................................................................
Solution: Let p =  + 01 + (010)(10 + 010) ( + 1 + 01). Using the rule that
r(s + t) = rs + rt we have the following:

p =  + 01 + (010)(10 + 010) + (010)(10 + 010)(1 + 01)

Using the rule r(sr) = (rs) r we rewrite (10 + 010)(1 + 01) in the last term on
the right as follows:

(10 + 010)(1 + 01) = ((1 + 01)0)(1 + 01)

= (1 + 01)(0(1 + 01))
= (1 + 01)(01 + 001)

0(10 + 010)

= 0((1 + 01)0)
= (0(1 + 01))0
= (01 + 001) 0

p =  + 01 + (01)(01 + 001)0 + (010)(1 + 01)(01 + 001)

=  + 01 + (01)(01 + 001)0 + (01)(01 + 001)(01 + 001)
=  + 01( + (01 + 001)0 + (01 + 001)(01 + 001))
=  + 01((01 + 001) 0 + (01 + 001) )
=  + 01(01 + 001)(0 + )
=  + 01(( + 0)01) (0 + )
=  + (01( + 0)) (01)(0 + )
=  + (01 + 010) (010 + 01)
= (01 + 010)

4.1 Solutions

65

0
0
0

q2

q1

0
0

q4

0
0

q3

q5

4.13 Problem:

a) Find a simple nondeterministic finite-state machine accepting the language (01 [

001 [ 010) over  = f0, 1g.
b) Convert the nondeterministic finite state machine of part (a) to a deterministic
finite-state machine by the method of Section 4.2.
...........................................................................
Solution:
a) We begin by constructing a 5-state NFSM that recognizes 01, 001, and 010. We
then transform it into a machine recognizing (01 [ 001 [ 010) over  = f0, 1g
using the procedure given in Theorem 4.4.1. The resulting NFSM is shown in
Fig. S.4.3.

b) Let fi, : : : , kg denote the set fqi, : : : , qk g. The following table shows the sets
of states that arise when applying this method to the NFSM of Fig. S.4.3. The
resulting DFSM is shown in Fig. S.4.4.
State

a
c
e

State Set

f1g
f2g

f2, 3, 5g

State State Set

b
d
f

f2, 3g
f4g
;

4.14 Problem:

a) Let  = f0, 1, 2g, and let L be the language over  that contains each string
w ending with some symbol that does not occur anywhere else in w. For example, 011012, 20021, 11120, 0002, 10, and 1 are all strings in L. Construct a
nondeterministic finite-state machine that accepts L.
b) Convert the nondeterministic finite-state machine of part (a) to a deterministic
finite-state machine by the method of Section 4.2.

66

Chapter 4 Finite-State Machines and Pushdown Automata

1
1

f
0, 1
Figure S.4.4 NFSM associated with Problem 4.13(b).

...........................................................................
Solution:
a) After fixing the last letter in a string, all strings over the remaining symbols in the
alphabet are possible. Thus, the following regular expression describes such strings:

(

1) 1 + (

2) 2 + (

3) 3

Here  i is the set of letters in  other than i.

An NFSM for this set of expressions is shown in Fig. S.4.5.
b) Applying the the method of Section 4.2 to the NFSM of Fig. S.4.5 we create states
associated with sets of its states. Let fi, : : : , kg denote the set fqi, : : : , qk g. The
corresponding DFSM shown in Fig. S.4.6.
4.15 Problem: Describe an algorithm to convert a regular expression to an NFSM using the
proof of Theorem 4.4.1.
...........................................................................
Solution: A recursive procedure CONSTRUCT to convert a regular expression (r.e.)
into an NFSM parses the r.e. If it contains a single non-terminal, it constructs a simple
NFSM as shown in Fig. 4.6. If the r.e. is compound, it identifies the outermost operator, applies CONSTRUCT to the component(s) of the compound expression and then
assembles the complete machine from the machine(s) generated by CONSTRUCT
using the rules given in the proof of the theorem.
A representation of NFSMs is needed to insure that the assembly of a larger NFSM
from smaller ones is easy. This is facilitated by making explicit in the representation the
initial and final states.

4.1 Solutions

67

2, 3

q2

2, 3
1, 3

q1

q3

1, 3

q5

1, 2, 3

1, 2

1, 2

q4

Figure S.4.5 An NFSM for Problem 4.14(a).

1, 2, 3
1, 2

f4g

f3, 4g
1

f3, 4, 5g

f1g

f2, 4, 5g

f4, 5g
f3, 5g

1
2

2
3

1, 3
1

f2g

2
2, 3

f2, 5g
2

f2, 3g
3
Figure S.4.6 A NFSM for Problem 4.14(b).

f3g

f2, 4g
3

1, 2, 3

1, 3

f2, 3, 5g

f5g

3
3

1, 2
3

2, 3

68

4.16 Problem: Design DFSMs that recognize the following languages:

a)
b)
c)

a bca
(a + c) (ab + ca)b
(a b (b + c) )

...........................................................................
Solution: For each of these problems we used the techniques of Theorem 4.4.1. We
do not detail them because they are straightforward.
a) This DFSM M has four states, a reject state, qR, and three other states, an initial
state q1 to which M returns on input a, and state q2 reached from q1 on input b,
q3, the final state, reached from q2 on input c. M returns to q3 on input a. All
other transitions from these states are to state qR.
b) This DFSM M has five states, a reject state, qR , and four other states, an initial
state q1 to which M returns on input a or c, and states q2 reached from q1 on input
b and q3 reached from q1 on input c. M enters q4, the final state, from q2 on input
b or from q3 on input a. M returns to q4 on input b. All other transitions from
these states are to state qR .
c) We construct a DFSM M with five states that accepts a b (a + b) . M has a
reject state, qR, and four other states q1 , q2 , q3 , and q4 , all of which are final states.
q1 is the initial state. M enters q2 from q1 on input a and returns to q2 on input
a. It enters q3 from q2 on input b and returns to q3 on input b. M enters q4 on
either b or c from q3 and returns to q4 on input b or c. The machine M  accepting
(a b (b + c) ) is constructed in the standard way.

4.17 Problem: Design an FSM that recognizes decimal strings (over the alphabet f0, 1, 2,
3, 4, 5, 6, 7, 8, 9g representing the integers whose value is 0 modulo 3.
Hint: Use the fact that (10)k = 1 mod 3 (where 10 is ten) to show that (ak (10)k +
ak 1(10)k 1 +    + a1 (10)1 + a0 ) mod 3 = (ak + ak 1 +    + a1 + a0) mod 3.
...........................................................................
Solution: We design a DFSM M that uses the fact given in the hint. It has three
states corresponding to the running value of (ak + ak 1 +    + a1 + a0 ) mod 3 as a
function of k. (See Fig. S.4.7.) The states are q0 , q1 , and q2 . q0 is the initial state and
the only final state. Thus, as expected, the empty string is accepted. The values of each
input, ai , are grouped into three classes, those that have value 0 modulo 3, namely,
f0, 3, 6, 9g, those that have value 1 modulo 3, namely, f1, 4, 7g, and those that have
value 2 modulo 3, namely, f2, 5, 8g. Let a0 , a1 , and a2 denote regular expresions for
the value in each of the corresponding sets. Then, M returns to qi on input a0 , for
j = (i + 1) mod 3 advances from qi to qj on a1, and for j = (i + 2) mod 3 advances
from qi to qj on a2 .

4.18 Problem: Use the above FSM design to generate a regular expression describing those
integers whose value is 0 modulo 3.
...........................................................................
Solution: For this problem increase the index on each state by 1; that is, qj becomes
qj +1 for 0  j  2. If we use the approach of Section 4.4.2, we develop a regular
expression describing the strings that take the FSM M from state qi to qj for each i and

4.1 Solutions

a0

a0

a1
a2

q0

a0

a1
a2

q1

a0 = f0, 3, 6, 9g
a1 = f1, 4, 7g
a2 = f2, 5, 8g

q2

a2

a1

69

j . Since only state q1 is an accepting state, we will do unnecessary work if we compute

(k)
(3 )
all the entries in Ri,j for each k. Since r1,1 is defined as shown, we work backward
from the expression to find the regular expressions needed to solve this problem.


(3) = r(2) + r(2) r(2)  r(2)
r1,1
1,1
1,3
3,3
3,1

(2) (2) (2)

(2 )

This implies that we need only compute r1,1 , r1,3 , r3,3 , and r3,1 . The expressions for
these regular expressions are given below.


(2) = r(1) + r(1) r(1)  r(1)
r1,1
1,1
1,2
2,2
2,1


(2) = r(1) + r(1) r(1) r(1)
r1,3
1,3
1,2
2,2
2,3


(2) = r(1) + r(1) r(1) r(1)
r3,3
3,3
3,2
2,2
2,3


(
2)
(
1)
(
1)
(
1)
(1)
r3,1 = r3,1 + r3,2 r2,2 r2,1

(1)

(0 )

The values of ri,j are computed from ri,j which are read from the state diagram for
(0)
(1)
(1)
the machine M . The values of ri,j and ri,j are shown below. We compute ri,j using

(0)) r(0) .
ri(,1j) = ri(,0j) + ri(,10) (r1,1
1,j

ri(,0j)
1
2

r (1 )
i,j

1
2
3

3
1

 + a0 a1
a2
a2  + a0 a1
a1
a2  + a0
2

a0
a0 a1
a0 a2
a2 a0  + a0 + a2a0 a1 a1 + a2a0 a2
a1 a0
a2 + a1a0 a1  + a0 + a1a0 a2

70

Chapter 4 Finite-State Machines and Pushdown Automata

We then have the following expressions for the regular expressions in question.

(2) = a + a a (a + a a a ) a a
r1,1
2 0 1
2 0
0
0 1 0
(
2)




r1,3 = a0 a2 + a0 a1 (a0 + a2 a0 a1) (a1 + a2 a0 a2)
(2) = ( + a + a a a ) + (a + a a a )(a + a a a ) (a + a a a )
r3,3
0
1 0 2
2
1 0 1
0
2 0 1
1
2 0 2
(
2)





r3,1 = a1a0 + (a2 + a1a0 a1 )(a0 + a2 a0 a1) a2a0
(3 )

The expression for r1,1 can be composed from these expressions.

Another approach to this problem that can be simpler is to associate the regular expression Ei with the input strings that carry M from the initial state to the state qi . We
can then write the following equations for these regular expressions which show how
one r.e. can be expresed in terms of the other r.e.s in one-step.

E1 =  + E1a0 + E2 a2 + E3a1
E2 = E1a1 + E2a0 + E3 a2
E3 = E1a2 + E2a1 + E3 a0
The empty string appears on the righthand side of the first equation because state q1 is
the initial state which means that M can reach q1 from the initial state on the empty
letter. An equation of the form for the r.e. E

E = + E
denotes strings in plus the strings denoted by E concatenated with the strings in
. That is, E has as solution E = ( ) . To see this, consider a string in ( ) .
It follows that such strings are either in or in k for some k  1. Thus, they
satisfy E = + E . On the other hand, consider a string w in the set E satisfying
E = + E . It follows that w is either in or in E . To be in E , it must be in E
concatenated with . But this means that it is either in or in E ( )2 . Continuing in
this fashion, we see that w is in ( ) . Thus, the solution to E = + E are exactly
the strings in ( ) .
This observation can be used to solve the above equations. That is,

E3 = (E1a2 + E2 a1)a0
Substituting this into the equation for E2 we have

E2 = E1a1 + E2a0 + (E1 a2 + E2a2 )a0 a2

= E1(a1 + a2a0 a2 ) + E2(a0 + a2 a0 a2 )

E3 = E1 (a2a0 + (a1 + a2a0 a2 )(a0 + a1 a0 a2) a1a0 )

4.1 Solutions
Substituting the expressions for
lowing expression for E1 .

71

E1 =  + E1 (a0 + (a1 + a2 a0 a2 )(a0 + a1 a0 a2) a2 +

(a2a0 + (a1 + a2a0 a2 )(a0 + a1 a0 a2) a1a0 )a1 )
=  + E1 (a0 + (a1 + a2 a0 a2 )(a0 + a1 a0 a2) (a2 + a1a0 a1 ) + a2a0 a1 )
= ((a0 + a2 a0 a1) + (a1 + a2a0 a2 )(a0 + a1 a0 a2) (a2 + a1 a0 a1))
This expression for E1 is different from that derived earlier. Showing the equivalence
between regular expressions can be challenging.
4.19 Problem: Describe an algorithm that constructs an NFSM from a regular expression r
and accepts a string w if w contains a string denoted by r that begins anywhere in w.
...........................................................................
Solution: The solution to this problem is a DFSM that accepts all strings that contain
a string in the set denoted by r. Let M be a DFSM that accepts strings denoted by r.
We construct an NFSM for the problem by adding to each state of M an edge back
to M s initial state for each input letter. This machine nondeterministically decides on
each input letter whether to restart the search for a letter in r. Thus, if the input string
contains a substring denoted by r, this NFSM will accept it.
THE PUMPING LEMMA

4.20 Problem: Show that the following languages are not regular:
a) L = fanban j n  0g
b) L = f0n12n0n j n  1g
c) L = fanbncn j n  0g
...........................................................................
Solution:
a) Let L be recognized by an m-state FSM. Apply the pumping theorem to a string
with n  m. Pumping up the string adds as to the beginning of the string,
violating the condition that the number of as before and after b is the same.
b) Same as in (a).
c) Same as in (b).
4.21 Problem: Strengthen the pumping lemma for regular languages by demonstrating that
if L is a regular language over the alphabet  recognized by a DFSM with m states and
it contains a string w of length m or more, then any substring z of w (w = uzv) of
length m can be written as z = rst, where jsj  1 such that for all integers n  0,
ursntv 2 L. Explain why this pumping lemma is stronger than the one stated in
Lemma 4.5.1.
...........................................................................
Solution: The only change to the proof of Lemma 4.5.1 is to observe that the number
of states entered by the DFSM immediately before the first letter in z and following
each letter of this string is m + 1. By the pigeonhole principle some state among these
states is repeated. The substring s that takes the DFSM from this state to itself can

Chapter 4 Finite-State Machines and Pushdown Automata

72

be removed or inserted into z an arbitrary number of times and the DFSM will reach
exactly the same final state. Thus, the original string w is accepted if and only if the
string ursn tv is accepted.

4.22 Problem: Show that the language L = faibj j i > j g is not regular.
...........................................................................
Solution: Apply the version of the pumping lemma given in Problem 4.21 to the
substring bj in aibj . If the language is regular, then by pumping up bj sufficiently
many times the new suffix will have more bs than as, contradicting the definition of
the language.
4.23 Problem: Show that the following language is not regular:
a)

funzvm zwn+m j n, m  1g

...........................................................................
Solution:
a) Apply the version of the pumping lemma given in Problem 4.21 to the substring
un for n equal to the number of states of the DFSM that is supposed to recognize
this string. Clearly, by deleting the appropriate substring s the resulting string does
not have a number of us and v equal in number to the number of ws.
PROPERTIES OF REGULAR LANGUAGES

4.24 Problem: Use Lemma 4.5.1 and the closure property of regular languages under intersection to show that the following languages are not regular:
a)
b)
c)

fwwR j w 2 f0, 1g g

fww j where w denotes w in which 0s and 1s are interchangedg
fw j w has equal number of 0s and 1sg

...........................................................................
Solution:

a) If fwwR j w 2 f0, 1g g is regular, then its intersection with a regular language
is also regular. Intersect it with the regular language 0 1 0 . The result is the
language f0n12m 0ng. By the pumping lemma, the substring 0n can be pumped
up to give a string not in the language. Contradiction.
b) We apply the same reasoning as in (a) except that we intersect fwwg with the
regular expression 0 1 . The result is the language f0n1n g which is not regular.
c) As in (b), intersect fw j w has equal number of 0s and 1sg with 0 1 giving
f0n1n g which is not regular.
4.25 Problem: Prove or disprove each of the following statements:
a)
b)
c)
d)
e)

Every subset of a regular language is regular

Every regular language has a proper subset that is also a regular language
If L is regular, then so is fxy j x 2 L and y 62 Lg
If L is a regular language, then so is fw : w 2 L and wR 2 Lg
fw j w = wR g is regular

4.1 Solutions

73

...........................................................................
Solution:

a) False. The language f0n1ng, which is not regular, is a subset of 0 1 .

b) True. f;g is a proper subset of all regular languages.
c) False. The language f0n1m 0p g is regular, as is easily shown. However, the language formed by intersecting strings in this set with the reverse of strings in this
set is the language f0n1m 0ng which can easily be shown not to be regular by the
pumping lemma.
d) False. let 0 and 1 be two letters in the alphabet of L = fw j w = wR g. If L
is regular, intersect it with the regular language 0 1 0 . The result is the language
f0n12m 0ng, which is not regular.
STATE MINIMIZATION

4.26 Problem: Find a minimal-state FSM equivalent to that shown in Fig. S.4.8.
...........................................................................
Solution: To find the minimal-state FSM we construct the n for n  0. Clearly,
0 = ffq0, q1 , q2g, fq3gg. Since the successors of q0 and q2 under both inputs are in
the same equivalence class of 0 , but the successor to q1 under input 1 is in a different
equivalence class from the successors to q0 and q2 , 1 = ffq0 , q2 g, fq1 g, fq3 gg. Since
the successors to q0 and q2 under input 0 are in different equivalence classes of 1 , it
follows that 2 = ffq0 g, fq1 g, fq2 g, fq3 gg. Since this equivalence relation cannot be
further refined, we find that the FSM in question is itself a minimal machine.
4.27 Problem: Show that the languages recognized by M and M are the same, where 
is the equivalence relation on M defined by states that are indistinguishable by input
strings of any length.
...........................................................................

0
Start

q0

q1

q2

q3
0

74

Chapter 4 Finite-State Machines and Pushdown Automata

Solution: Two states p and q of M are equivalent (they satisfy p  q) if and only if
for all input strings u 2  of all lengths either both (p, u) and (q, u) are in F (the
final states) or both are not in F . Since the strings that are accepted by M (M ) are
those strings that take M (M ) from its initial state to a state in F , it follows that those
strings that are accepted by M are the same as those accepted by M .

4.28 Problem: Show that the equivalence relation RL is right-invariant.

...........................................................................
Solution: u, v 2  are equivalent under RL if and only if for each z 2  , either
both uz and vz are in L or both are not in L.
An equivalence relation R is right-invariant if for all w 2  , uwRvw.
To show that RL is right-invariant we must only show that for all w 2  , uwRLvw.
But for uwRL vw requires that either both uwz and vwz are in L or both are not in
L. But this is guarenteed by the definition of RL since wz is just another string over
 .

4.29 Problem: Show that the equivalence relation RM is right-invariant.

...........................................................................
Solution: uRM v if and only if (s, u) = (s, v ). But RM is right-invariant if for
all w 2  , uwRM vw. However, by the definition of RM the state reached by M
under uw and vw are the same which implies that RM is right-invariant.

4.30 Problem: Show that the right-invariance equivalence relation (defined in Definition 4.7.2)
for the language L = fanbn j n  0g has an unbounded number of equivalence
classes.
...........................................................................
Solution: If the equivalence relation RL for L had a finite number of equivalence
classes, L would be recognized by an FSM, which is impossible since L is not regular
by the pumping lemma.
4.31 Problem: Show that the DFSM in Fig. 4.20 is the machine ML associated with the
language L = (101 + 0) .
...........................................................................
Solution: We construct a machine for L and then find a minimal-state machine equivalent to it. We first construct a machine for 10 0 + 0 and then build one for L. An
NFSM machine for 10 0 + 0 has states q0 , q1 , and q2 . q0 is the initial state which has a
transition to q1 on 1 and to q2 on 0. The transitions from q1 are to q2 on 1 and to itself
on 0. q2 is an accepting state. The machine constructed to realize L has a new initial
state q3 which is accepting and has transitions to q1 on 1 and to q2 on 0. The transitions
from q2 are to itself on 0 and to q1 on 1. The equivalence relation R on this machine
is easily seen to have two equivalence classes, ffq2 , q3 g, fq1 gg, with transitions from
these classes that are isomorphic to those given in Fig. 4.20. The initial state is fq2 , q3 g
and the final state is fq1 g.
PUSHDOWN AUTOMATA

4.32 Problem: Construct a pushdown automaton that accepts the following language: L =
fw j w is a string over the alphabet  = f(, )g of balanced parenthesesg.

4.1 Solutions

75

...........................................................................
Solution: A sketch of this PDA: whenever a left parenthesis, (, is seen, push it onto the
stack. Whenever a right parenthesis, ), is seen, pop the stack. If ever the blank input
symbol is encountered when the stack is non-empty or the empty stack is encountered
when the tape symbol is non-blank, enter the reject state. Otherwise, enter the accept
state.
4.33 Problem: Construct a pushdown automaton that accepts the following language: L =
fw j w contains more 1s than 0sg.
...........................................................................
Solution: The PDA keeps track of the difference in the number of 0s and 1s. Push
the first letter on the stack. If it is 1, continue to push 1s, popping a 1 when a 0 is
encountered. If the first letter is 0, continue to push 0s, popping when a 1 is encountered. Initially and at all subsequent times the number of characters on the stack is the
difference in the number of 0s and 1s. It follows that if the blank input symbol is
encountered on the input tape when the stack either empty or has a 1 at the top, then
the input has more 1s than 0s. Otherwise, it has more 0s than 1s.
PHRASE STRUCTURE LANGUAGES

a)
b)

fww j w 2 fa, bg g

f02i j i  1g

...........................................................................
Solution:
a) Let G = (N , T , R, S ), where
consists of the following rules:

a)
b)
c)
d)
e)

S
L
L
L
TA

! LR
! aL A
! bL B
! T
! TC

N = fS, A, B, C, D , L, R, Tg, T = fa, bg and R

f)
g)
h)
i)

TB
CA
DA
CB

!
!
!
!

TD
AC
AD
BC

j)
k)
l)
m)

DB
CR
DR
TR

! BD
! Ra
! Rb
! 

The first rule places R on the right. It then rewrites L as a string w of terminals followed by the string W which mirrors w in that the terminals a and b are replaced
by the non-terminals A and B. Rule d) starts a process that reverses the order of the
non-terminals in W . Rules e) and f ) convert A and B into C and D, respectively,
which by rules g)-j) pass to the right of each letter in W until they reach R. At this
point they are converted by rules k) and l) to a and b respectively and R is shifted
left. Since no rule is given to rewrite strings of C and D, if a string of terminals is
to be produced, all instances of these non-terminals must migrate to the right end
of the string.
b) Let G = (N , T , R, S ), where N = fS, A , F, Tg, T = f0g and R consists of the
following rules:

76

Chapter 4 Finite-State Machines and Pushdown Automata

d) A0 ! 00A
! FT
F ! FA0
e) AT ! T
F ! 
f) T ! 
The rule F ! FA 0 generates instances of A each of which doubles the number of
instances of 0 to its right using the rule A 0 ! 00A. This insures that the number
a)
b)
c)

of 0s is equal to 2k where k is the number of instances of A that are generated.

4.35 Problem: Show that the following language can be described by a phrase-structure
grammar:

fai j i is not primeg

...........................................................................
Solution: Because i, the number of instances of a, is not a prime, we give a grammar
that first generates a representation in unary notation for two or more integers. We
then give rules that form the product of the rightmost two integers, leaving the result
in unary notation. These rules also from the product of the remaining integers, if any,
with the product produced so far.
The grammar is G = (N , T , R, S ), where N = fS, A , D, I , J, L , Q , R g, T = fag and
R consists of the following rules:

a)
b)
c)
d)
e)
f)

S
Q
Q
I
I
J

! LQJ
! QQ
! ID
! IA
! AA
! Ja

g)
h)
i)
j)
k)

!
A da !
Aa
!
DD
!
LD
!
J

aa
d Aa
aaA
D

Rule a) must be followed by b) which together produce a string of the form LQm 1 J .
J and each instance of Q will be rewritten as integers  2 in unary notation. Rule c)
rewrites Q as I followed by the separator D. I is rewritten by rules d) and e) as A kr for
some integer kr  2. Also, J is rewritten by rules f ) and g) as ak1 for some integer
k1  2. Rules h) moves the non-terminal A past D when the terminal a is to its right.
By rule i) each non-terminal copies each instance of a to its right. As a consequence,
terminals are produced from right to left and the number of terminals resulting from m
consecutive integers is their product. After all non-terminals between two consecutive
instances of D or to the left of the leftmost D move to the right of D, two consecutive
instances of D result, which by rule j) is rewritten as D. Finally, rule k) annihilates the
remaining instance of D. If this is not the last rule to be applied, the resulting string
will not be a string of terminals.
CONTEXT-SENSITIVE LANGUAGES

4.36 Problem: Show that every context-sensitive language can be accepted by a linear
bounded automaton (LBA), a nondeterministic Turing machine in which the tape
head visits a number of cells that is a constant multiple of the number of characters in
the input string w.

4.1 Solutions

77

Hint: Consider a construction similar to that used in the proof of Theorem 5.4.2.
Instead of using a second tape, use a second track on the tape of the TM.
...........................................................................
Solution: Consider a string w whose membership in a context-sensitive language L(G)
with grammar G is to be tested. Let it be written on the tape of an NTM. If w is in
L(G), each rule of G that is used to derive w leads to strings whose lengths are nondecreasing. Thus, the length of each intermediate string is no more than the length of
w. As a consequence, we can use a second track on the tape of the LBA to write out the
possible intermediate strings leading to w. Initially the start symbol is placed beneath
the leftmost input symbol and rules are applied nondeterministically. Substrings may
need to be shifted right to provide space for a rewriting, a task readily performed in the
space available. If a string is produced that is longer than w, contains non-terminals
that cannot be further expanded, or is a terminal string that is not equal to w, it
is rejected. Otherwise, it is accepted. It follows that if w 2 L(G), there exists a
nondeterministic LBA that accepts it. Since each track of a tape is equivalent to a fixedlength expansion of the original tape, a context-free language can be accepted by an
LBA.
4.37 Problem: Show that every language accepted by a linear bounded automaton can be
generated by a context-sensitive language.
Hint: Consider a construction similar to that used in the proof of Theorem 5.4.1 but
instead of deleting characters at the end of TM configuration, encode the end markers
[ and ] by enlarging the tape alphabet of the LBA to permit the first and last characters
to be either marked or unmarked.
...........................................................................
Solution: Two ideas are used in this proof beyond those used in Theorem 5.4.1,
namely, the number of tape cells used is a constant multiple of the length of the input, and the end markers [ and ] can be encoded by extending the tape alphabet. All
of the rules of a phrase structure grammar except for b)-e) are length preserving or increasing. Rules b) and d) are not necessary because the assumption that the TM is a
linear bounded automaton. Rules c) and e) can be eliminated by expanding the tape
alphabet.
4.38 Problem: Show that the grammar G1 in Example 4.9.1 is context-sensitive and generates the language L(G1 ) = fanbncn j n  1g.
...........................................................................
Solution: Grammar G1 is context-sensitive because each rule is length preserving or
increasing. To show that it generates fan bncn j n  1g, observe that rule a) is the first
to be applied. Before S is is replaced by b) for the last time, S can be replaced multiple
times by a). While other rules can be applied after one application each of a) and b),
there is no loss in generality in assuming that the first rules applied consist of k 1

applications of a) followed by one of b) yielding the derivation C ) ak (BC)k . Rule
c) or d) can be applied to this result. If rule f ) is applied and the character on the right
of C is not C, the rewriting halts. Thus, the derivation produces a terminal string only
if all instances of C move to the right of all instances of B. Thus, terminal strings are

78

Chapter 4 Finite-State Machines and Pushdown Automata

derived only if rule c) is applied k times to move instances of C to the right of instances
of B. This action has the effect of generating the string of the form an bncn .

4.39 Problem: Show that the language f02 j i  1g is context-sensitive.

...........................................................................
Solution: Let G = (N , T , R, S ), where N = fS, A , F, T g, T = f0g and R consists
of the following rules:
i

a)
b)
c)

!
!
!

d)
e)
f)

!
!
!

FA

00
F
A
A0
00A
Rule a) generates 0. Rules b), c) and d) are used to generate longer strings. Rules b)
and c) can only be used once. If rule d) is used k 1 times, the string derived from S
will contain k instances of A . Each instance of A doubles the number of 0s to its right
where T serves not only as a terminator but a representative for one instance for 0. It
follows that strings of 2k 0s are generated by this grammar.
S

FT

AT

4.40 Problem: Show that the context-sensitive languages are closed under union, intersection, and concatenation.
...........................................................................
Solution: Without loss of generality let G1 and G2 be context-sensitive grammars
with disjoint sets of non-terminals. Each of the rules in their grammars is not length
decreasing. Let S1 and S 2 be the start symbols of these grammars.
A grammar generating the union of L(G1 ) and L(G2 ) has a new non-terminal S3 and
the rules S3 ! S1 and S 3 ! S2 as well as those in G1 and G2 . The new rules are not
length decreasing and they generate the terminal strings in one or the other or both of
the languages.
To show that CSLs are closed under intersection, create two tracks on the tape of a
LBA and write the input string, w, on both tracks. On the first track simulate the LBA
recognizing L(G1 ). If it accepts w, repeat the process on the second track with the
LBA accepting L(G2 ), accepting w if this LBA accepts the string. The strings accepted
by both machines are in the intersection of L(G1 ) and L(G2 ). Since the two tracks
expand by a constant factor the amount of storage needed to recognize either language,
the intersection is also recognized by an LBA.
For CFLs we generated a grammar for their concatenation by adding a new nonterminal S 3 and the rule S3 ! S1 S2 . Since rules are invoked only to replace single
non-terminals, the fact that non-terminals are disjoint insures that replacements in the
derivations from S2 and S3 are disjoint. However, in the case of CSLs, since the set of
terminals may not be disjoint and CSLs rewrite strings rather just single non-terminals,
some rule may be invoked on the derivations from S1 S2 that would not be invoked
from just S1 or S 2 alone. To fix this problem, in every derivation G1 that has a terminal
a replace it by a unique non-terminal Na and add the rule Na ! a. If a is also a
terminal of G2 , replace a by Ma where Ma is a non-terminal distinct from Na . Since
the non-terminals of the two grammars are disjoint, it follows that the left-hand side
of every rule in both languages are disjoint. Thus, rules of G1 apply only to strings
derived from S1 and rules of G2 apply only to strings derived from S2 .

4.1 Solutions

79

CONTEXT-FREE LANGUAGES

4.41 Problem: Show that language generated by the context-free grammar

ple 4.9.3 is L(G3 ) = fcan cancbm cbm c j n, m  0g.

G3

of Exam-

...........................................................................
Solution: After application of rule a), rule b) is applied some number of times, say
n 1, (including 0) after which rule c) is applied. Interlaced with the application of
rules b) and c) can be rules d) and e) in which d) can be applied some number of times,
say m 1, (including 0) after which rule e) is applied. Under these conditions balanced
pairs of n instances of a are produced separated by c as are balanced pairs of m instances
of b are produced separated by c. The latter follow the former.
4.42 Problem: Construct context-free grammars for each of the following languages:
a)
b)
c)

fwwR j w 2 fa, bg g

fw j w 2 fa, bg, w = wR g
L = fw j w has twice as many 0s as 1sg

...........................................................................
Solution:
a) The strings in this language have even length

a)
b)
c)

S
S
S

! aS a
! b Sb
! 

b) The strings in this language can have even or odd length. Those of even length are
written as xxR whereas those of odd length are written as xcxR for c equal to a
or b. The following rules generate strings of these two types.

! aS a
S ! b Sb
S ! a
S ! b
S ! 
The rule S ! 001S generates twice as many 0s as 1s but the former precede the
a)
b)
c)
d)
e)

c)

latter. To generate strings that have twice as many 0s as 1s, a grammar suffices that
has one rule such as the one above for each permutation of the four characters on
the right-hand side plus the rule S ! .

4.43 Problem: Give a context-free grammars for each of the following languages:
a)
b)

fw 2 fa, bg j w has twice as many as as bsg

far bs j r  s  2rg

...........................................................................
Solution:
a) Same as given in the solution to Problem 4.42(c) except that 0 is replaced by a and
1 by b.

Chapter 4 Finite-State Machines and Pushdown Automata

80

b)

a)
b)
e)

S
S
S

! aS b
! aSbb
! 

REGULAR LANGUAGES

4.44 Problem: Show that the regular language G4 described in Example 4.9.4 is L(G4 )
(01) 0.

...........................................................................
Solution: There is only one rule that can be applied after a), namely c). The composition of these two rules generates the string 01 B. Similarly, there is only one rule that
can follow d), namely, c), implying that B can be rewritten either as 0 or 01 B. Thus, the
following rules generate the same strings as do the rules of G4 : a) S ! 0, b) S ! 01B,
c) B ! 0, and d) B ! 01B. It follows that the language L(G4 ) contains 0 and strings
resulting from one application of b) and k 1 instances of d). That is, the language
contains only strings of the form (01)k 0 for k  0.

4.45 Problem: Show that grammar G = (N , T , R, S ), where N

and the rules R are given below, is regular.

a)
b)
c)

S
S
S

! abA
! baB
! B

d)
e)

S
A

! 
! bS

f)
g)

B
A

= fA , B , Sg, T = fa, bg

! aS
! b

Give a derivation for the string abbbaa.

...........................................................................
Solution: The following rules generate this string: a), e), b), f ), and d).
To show that this grammar is regular, we demonstrate that it can be put into the standard normal form, namely, right-hand sides that have a terminal followed by a nonterminal or a single terminal. We do this by replacing the rule S ! abA by the two
rules S ! aD and D ! bA and replacing the rule S ! baB by the two rules S ! bE
and E ! aB. Also, we delete the rule S !  and add the rules obtained by deleting S
from the right-hand sides of rules containing it, namely, A ! b and B ! a.

4.46 Problem: Provide a regular grammar generating strings over f0, 1g not containing 00.
...........................................................................

Solution: The rules S ! 0, S ! 1, S ! 0A, A ! 1S, and S ! 1S generates

these strings because, unless the string 0 is generated, each time that 0 is generated it is
followed by 1. Otherwise, no restrictions are placed on the strings.
4.47 Problem: Give a regular grammar for each of the following languages and show that
there is a FSM that accepts it. In all cases  = f0, 1g.
a)
b)

L = fw j the length of w is oddg

L = fw j w contains at least three 1sg

4.1 Solutions

81

...........................................................................
Solution:
a)

S ! 0 and S ! 1 generate strings of length 1. Using as our inductive hypothesis

that S generates strings of odd length, the following rules generate an arbitrary
string of even length followed by S, which satisfies our inductive hypothesis.
a) S ! 0
d) S ! 1A
b) S ! 1
e) A ! 0S
c) S ! 0A
f ) A ! 1S

b) The rules A ! 0A , A ! 1A , and A !  generate all strings over f0, 1g, as can be
easily shown. It follows that the rule S ! A 1A 1A1A generates all strings containing
at least threes 1s.
REGULAR LANGUAGE RECOGNITION

4.48 Problem: Construct a finite-state machine that recognizes the language generated by
the grammar G = (N , T , R, S ), where N = fS , X , Yg, T = fx, yg, and R contains
the following rules: S ! xX, S ! y Y, X ! y Y, Y ! xX, X ! , and Y ! .
...........................................................................
Solution: An NFSM recognizing this language has states labled S, X, and Y. Transitions
from S to X and Y are made on inputs x and y, respectively. From X there is a transition
to Y on input y. From Y there is a transition to X on input x. To convert this NFSM
to a DFSM, add an edge to a reject state from each of the states of this NFSM for
transitions that are missing. Both states X and Y are final states.
4.49 Problem: Describe finite-state machines that recognize the following languages:
a)
b)

fw 2 fa, bg j w has an odd number of asg

fw 2 fa, bg j w has ab and ba as substringsg

...........................................................................
Solution:
a) The FSM of Fig. 3.2 with 0 and 1 replaced by b and a recognizes this language.
b) A string containing both ab and ba satisfies one of the following four conditions:
a) ab appears before ba, b) ba appears before ab, c) it contains aba, or d) it contains bab. We design NFSMs for each condition and take the union of these four
NSFMs.
An NFSM for a) has five states, q1 , q2 , q3 , q4 , and q5 . It can remain in q1 on inputs
a and b or can enter q2 on a. It then moves to q3 on input b, possibly remaining
there on either input and then moving to q4 on b followed by a move to q5 on a
where it remains on all inputs. An NFSM for b) has the same structure except that
the role of a and b are interchanged. An NFSM for c) has four states. It can remain
in the first one one each input or move to the second, third and fourth states on
inputs a, b and a, respectively, where it remains on all inputs. An NFSM for d) has
the same structure as that for c) except that the role of a and b are interchanged.
An NSFM for all four cases is obtained by coalescing the initial and final states for
each of the machines.

82

Chapter 4 Finite-State Machines and Pushdown Automata

4.50 Problem: Show that, if L is a regular language, then the language obtained by reversing
the letters in each string in L is also regular.
...........................................................................
Solution: If the DFSM M recognizing L has a single final state, a DFSM MR for the
reverse of L is obtained by reversing the directions of all edges in M and letting M s
final state be the initial state of MR . This machine, which is an NFSM, can enter MR s
final state on the input w if and only if the reverse of w will take M from its initial
state to M s final state. When M has more than one final state, reverse all the edges of
M to form MR . Let the initial state of MR be a new state that has an edge to every
state from which there is an edge to a final state of M . This initial state of MR allows
any final state of M to be reached by the original input. It follows that the reverse of
strings in L is also regular.

4.51 Problem: Show that, if L is a regular language, then the language consisting of strings
in L whose reversals are also in L is regular.
...........................................................................
Solution: The strings in question are both in L and its reverse. Because a regular
language and its reverse are both regular and the set of regular languages is closed under
intersection, the intersection of a language with its reverse is regular.
PARSING CONTEXT-FREE LANGUAGES

4.52 Problem: Use the algorithm of Theorem 4.11.2 to construct a parse tree for the string
(a  b + a)  (a + b) generated by the grammar G5 of Example 4.11.2, and give a
leftmost and a rightmost derivation for the string.
...........................................................................
Solution: We exhibit fbi,i+j j 1  i  13, 1  j  7g below. Associated with
each non-terminal in square brackets are the right-hand sides of rules with these nonterminals on the left-hand side.

bi,i+1

bi,i+2

bi,i+3

bi,i+4

bi,i+5

bi,i+6

bi,i+7

(
E , T, F

;
;

E [ TB ], T [ TD]

B , D [ F]

;
;
;

E , T, F[( C]

E [ EA ]

C , G , H[ E)]

E , T, F

E [ EA]

C , G , H[ E )]

C , G , H[ E)]

A [+ T]

E , T, F

C , G , H[ E)


(

10

E , T, F

;
;
;

;
;
;
;
;

;
;
;
;

;
;
;
;
;

;
;
;
;
;
B , D [ F]

;
;
;
;
;
;

E[ EA]

C , G , H[ E )]

11

A [+ T ]

12

E , T, F

C , G , H [ E)]

13

E , F[( C]

All the entries bi,j for 8  j  12 are ;. The value of b1,14 is fE[TB], T[TD]g. Since
this set contains the start symbol E, the string (a  b + a)  (a + b) is in the language.
We can read the derivation by following the path backward from the non-terminal E.

4.1 Solutions

83

E
B

E
A

E
T

4.53 Problem: Let G = (N , T , R, S ) be the context-free grammar with N = S and

T = f(, ), 0g with rules R = fS ! 0, S ! SS, S ! (S)g. Use the algorithm of
Theorem 4.11.2 to generate a parse tree for the string (0)((0)).
...........................................................................
Solution: First, convert the grammar to Chomsky normal form.
S
S
S

!
!
!

SS

CB

!
!
!

AS

(
)

Once the grammar is in Chomsky normal form construct the 9  9 matrices

B (2) , B (3) , B (4) , B (5) , B (6) , B (7) and B (8) .

2;
66;
66
66;
66;
B (1) = 666;
66;
66;
66
4;
;

fAg
;
;
;
;
;
;
;
;

fS g
;
;
;
;
;
;
;

;
;

fB g
;
;
;
;
;
;

;
;
;

fAg
;
;
;
;
;

;
;
;
;

fAg
;
;
;
;

;
;
;
;
;

fS g
;
;
;

;
;
;
;
;
;

fB g
;
;

3
77
77
77
77
77
77
77
77
fB g7
5
;
;
;
;
;
;
;
;

B (1) ,

84

Chapter 4 Finite-State Machines and Pushdown Automata

2;
66;
66
66;
66;
2
) = 6;
(
B
66
66;
66;
66
4;

2;
66;
66
66;
66;
B (3) = 666;
66;
66;
66
4;

2;
66;
66
66;
66;
(
4
)
B = 666;
66;
66;
66
4;
;

2;
66;
66
66;
66;
B (5) = 666;
66;
66;
66
4;
;

;
;
;
;
;
;
;
;
;

fC g
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;

fC g
;
;
;
;

;
;
;
;
;
;
;
;
;

fS g
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

fC g
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;7
77
;7
77
;7
7
;7
77
;7
7
;7
77
;7
5
;

fS g
;
;
;
;

;
;7
77
;7
77
;7
7
;7
77
;7
7
;7
77
;7
5
;

;
;
;
;

;
;
;

;
;7
77
;7
77
;7
7
;7
77
;7
7
;7
77
;7
5
;

3
77
77
77
fS g7
7
; 7
77
; 7
7
; 7
77
; 7
5
;
;
;

4.1 Solutions
2;
66;
66
66;
66;
6
) = 6;
(
B
66
66;
66;
66
4;

2;
66;
66
66;
66;
(
7
)
B = 666;
66;
66;
66
4;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;7
77
;7
77
;7
7
;7
77
;7
7
;7
77
;7
5
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

fS g
; 7
77
; 7
77
; 7
7
; 7
77
; 7
7
; 7
77
; 7
5
;

2;
66;
66
66;
66;
B (8) = 666;
66;
66;
66
4;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

85
;
;
;
;
;
;
;
;
;

;
;
;
;
;
;
;
;
;

(8) so the string (0)((0)) is in the language. Finally, draw the parse tree.
S 2 B1,9

S
B

C
A

C
S

A
(

)
B

CFL ACCEPTANCE WITH PUSHDOWN AUTOMATA

4.54 Problem: Construct PDAs that accept each of the following languages:
a)
b)
c)

fanbn j n  0g
fwwR j w 2 fa, bg g
fw j w 2 fa, bg, w = wR g

;
;
;
;
;
;
;
;
;

;
;7
77
;7
77
;7
7
;7
77
;7
7
;7
77
;7
5
;

86

Chapter 4 Finite-State Machines and Pushdown Automata

...........................................................................
Solution:
a) If in state q0 the blank symbol is read, the PDA accepts the input. If in state q0 the
symbol b is read, the PDA rejects the input by entering qR from it never exits. If
in state q0 a is read, it is pushed onto the stack and state q1 entered. If in state q1
a is read, a is pushed on the stack and state q1 entered. If in this state b is read and
the symbol on the stack is a, state q2 is entered and a is popped. If however, the
stack is empty, state qR is entered.
b) This is an example of a language for which the potential nondeterminism of the
PDA is needed.
This PDA accepts the empty tape. If the tape is not empty, it enters q1 , the push
state, and pushes the input symbol onto the tape, returning either to the push
state or entering the pop state nondeterministically. It remains in the pop state
while the symbol on the top of the stack and the tape match. If both the tape
and the stack contain blank symbols, the accept state is entered. Otherwise, if the
symbol on the tape and the stack do not match, the string is rejected.
c) If a string in this language has even length, it is in the above language. Thus, it
differs only when strings have odd length in which case there is a middle character
around which the string is symmetric. Thus, we need only augment the above
machine to decide nondeterministically whether the string has even or odd length
and, if odd, when it has reached the middle of the string at which point it does not
push or pop on this character, popping after reading the next character.

4.55 Problem: Construct PDAs that accept each of the following languages:
a) fw 2 fa, bg j w has twice as many as as bsg
b) far bs j r  s  2rg
...........................................................................
Solution:
a) A PDA for this problem follows from the CFL grammar given in the solution to
Problem 4.42(c) and the construction of Theorem 4.12.1.
b) A PDA that accepts strings of the form ar bs for s  2r pushes two as onto the
stack for every a seen at the beginning of the string. At each b one character is
popped from the stack. If the blank is not popped, the string is of the desired
form. The strings ar bs for r  s can be found by pushing only one a onto the
stack for each a in the input. It would appear that the desired strings are in the
intersection of these two languages. However, since the set of CFLs is not closed
under intersection, it would appear that the language in question is not CFL, which
we know from Problem 4.43(b) is not the case.
The solution is to design a nondeterministic PDA that pushes either one or two
instances of a for every a in the input and then pops one letter from the stack for
each b that is seen. If we accept only strings for which the number of as placed on
the stack is equal to the number of bs, strings of the proper form can be accepted
nondeterministically. Thus, the language is CFL.
4.56 Problem: Use the algorithm of Theorem 4.12.2 to construct a context-free grammar
that accepts the language accepted by the PDA in Example 4.8.2.

4.1 Solutions

87

...........................................................................
Solution: Space does not permit the enumeration of all the productions in the grammar
in question. It contains the following rules:
1)
S
! < s, , f >
2) < s, , s > ! 
3) < p, , p > ! 

4) < f , , f > ! 
5) < r, , r > ! 
It also contains rules for the form < p, y,  >! x < q, z ,  > for all 
all (p, x, y; q, z ) 2 , where y 6= . These rules are shown below where
6)
< s, a,  > ! b < p, ,  >
7)
< p, a,  > ! b < p, ,  >

4.57

!
!
!
!

b < r, ,  >
< f , ,  >
< f , ,  >
11)
b < r , ,  >
The rules of the form < p, u,  >! x < q, z ,  ><  , u,  > for all ,  2 Q, for all
(p, x, ; q, z ) 2 , and for all u 2 [ fg are shown below
12) < s, u,  > ! a < s, a,  ><  , u,  >
13) < p, u,  > ! a < r, ,  ><  , u,  >
14) < s, u,  > ! < f , ,  ><  , u,  >
15) < f , u,  > !  < f , ,  ><  , u,  >
16) < r, u,  > !  < r, ,  ><  , u,  >
Problem: Construct a context-free grammar for the language fwcwR j w 2 fa, bg g.
8)
9)
10)

< p, ,  >
< p, a,  >
< p, ,  >
< s, ,  >

2 Q and for

Hint: Use the algorithm of Theorem 4.12.2 to construct a context-free grammar that
accepts the language accepted by the PDA in Example 4.8.1.
...........................................................................
Solution: The hint is too complex. Instead we give a direct construction.
S
S
S

! a Sa
! b Sb
! c

PROPERTIES OF CONTEXT-FREE LANGUAGES

4.58 Problem: Show that the intersection of a context-free language and a regular language
is context-free.
Hint: Form machines accepting the two language types, construct a machine accepting
their intersection.
...........................................................................
Solution: Consider a PDA, M , that accepts language L1 and a DFSM, N , that accepts
L2 . Construct a new PDA, M  N that simulates the two machines in parallel. Con-

88

Chapter 4 Finite-State Machines and Pushdown Automata

struct the new PDA so that if M does not read its input, it does not advance the state of
N . Its control unit has states represented as pairs (m, n) where m is a state of M and n
is a state of n. If M has a transition (m, x, y; m0 , z ) and N has a transition (n, x; n0),
M  N is the PDA that has the transition ((m, n), x, y; (m0 , n0 ), z ) if x 6=  and
((m, n), x, y; (m0 , n), z ) otherwise.
An accept state of this new PDA is a pair (m, n) such that m is an accept state of M
and n is an accept state of N . This machine accepts its input if it is accepted by both
machines. Thus, it accepts strings that are in the intersection of the languages accepted
by the two machines.

4.59 Problem: Suppose that L is a context-free language and R is a regular one. Is L R

necessarily context-free? What about R L? Justify your answers.
...........................................................................
Solution: The language L R contains the strings in L that are not in R. The strings
not in R are the strings in the complement of R which is regular. Thus, L R is
the intersection of L with the complement of R. By Problem 4.58 this intersection is
context-free.
In general R L is not context-free because if R =  , where  is the alphabet over
which L is defined, then R L is the complement of L which may not be context-free.
4.60 Problem: Show that, if L is context-free, then so is LR = fwR j w 2 Lg:
...........................................................................
Solution: Construct a context-free grammar for LR using the grammar for L as a base.
(We know L has a context-free grammar since L is context-free.) The grammar for LR
has the same non-terminals and terminals as L. Its rules are the same as Ls except that
the right-hand side is reversed. For example, the rule S ! AB becomes S ! BA.
And, of course, rules that go to a single element are unchanged.
This grammar will generate strings that are the reverse of strings in L. We have successfully constructed a CFG for LR so it is a CFL.

4.61 Problem: Let G = (N , T , R, S ) be context-free. A non-terminal A is self-embedding


if and only if A )G sA u for some s, u 2 T .
a) Give a procedure to determine whether A 2 N is self-embedding.
b) Show that, if G does not have a self-embedding non-terminal, then it is regular.
...........................................................................
Solution:

a) Let m be the number of non-terminals. For each non-terminal A we need only

consider sequences of at most m 1 transitions because we are guaranteed that A
is repeated in this many transitions if it is repeated at all. Let r be the maximum
number of non-terminals on the right-hand side of a rule. It follows that we need
only consider at most 1 + R +    + rm 1 = (rm 1)=(r 1) derivations of
depth m 1 or less before discovering if A is self-embedding. We do this for each
non-terminal to determine if the language itself is self-embedding.
b) Assume that G does not have a self-embedding non-terminal. This implies that
every derivation for every non-terminal A either yields a terminal string or a string


of the form A )G sA (it is right-recursive) or A )G A u (it is left-recursive)

4.1 Solutions

89

for terminal sequences u and v. Because a non-terminal that yields strings of both
types is self-embedding, this does not happen.
Without loss of generality, let A be left-recursive. Let r denote the regular expression that describes the set of terminal strings generated from A without embedding

and let s be the regular expression denoting u. Then, A )G rs which implies
that the strings derivable from A can be described by regular expressions.
CFL PUMPING LEMMA

4.62 Problem: Show that the following languages are not context-free:

a) f02 j i  1g
2
b) fbn j n  1g
c) f0n j n is a primeg
...........................................................................
Solution:
i

a) Assume the language is recognized by a CFL with m non-terminals. Let i be such

i
that 2i  2m 1 + 1. It follows that 02 = rstuv, that is, for positive integers a
i
and b, s = 0a and u = 0b. From the pumping lemma it follows 02 +(n 1)(a+b) .
This is in the language if 2i + (n 1)(a + b) is a power of two. For this to hold,
we require that for each n  0, (n 1)(a + b) = 2k 2i for some k.
We consider two cases, 1 + (a + b) = 2j for some j and 1 + (a + b) 6= 2j for
any j . In the first case, let (n 1) = 2i in which case 2i + (n 1)(a + b) =
2i (1 + 2(a + b)) = 2i (2j +1 1) which is not a power of two. In the second case,
set (n 1) = 2i in which case 2i + (n 1)(a + b) = 2i (1 + (a + b)) which is
also not a power of two.

b) For n sufficiently large, bn = rstuv can be pumped up to rsp tup v for p  0.

Let s and u be of lengths a and b, respectively, where a + b > 0. If the language
is context-free, it follows that for each p  0, n2 + (p 1)(a + b) = q2 for some
integer q. But if we choose p 1 = n, the left-hand side is n(n + a + b) which is
not a square because n + a + b > n.
c) For n sufficiently large, 0n = rstuv can be pumped up to rsp tup v for p  0.
Let s and u be of lengths a and b, respectively, where a + b > 0. If the language
is context-free, it follows that for each p  0, n + (p 1)(a + b) is a prime. But
if p 1 = n, n + (p 1)(a + b) is not prime.
2

4.63 Problem: Show that the following languages are not context-free:
a) f0n1n 0n1n j n  0g
b) fai bj ck j 0  i  j  kg
c) fww j w 2 f0, 1g g
...........................................................................
Solution:
a) The proof is identical with that given for Lemma 4.13.2.
b) Assume that faibj ck j 0  i  j  kg is context-free. Then by the Pumping
Lemma every sufficiently long string rstuv can be pumped up to rsp tup v for

90

p

0. Consider a sufficiently long string in which i = j = k. If s or u falls

completely into the as or bs, let p 1 = i + 1 which insures that the new string
is not in the language. If either string falls completely into the cs, pump u done,
generating a string that violates the condition for membership in the language. If
either s or u overlaps as and bs or overlaps bs and cs, pumping up the string
twice (p = 2), results in bs preceding as or cs preceding bs.
c) If L = fww j w 2 f0, 1gg is context-free, its intersection with the regular
language 0 10 1 is the context-free language f0i1j 0i1j j 0  i, j g. Consider a
string in this language of the form 0n1n0n 1n = rstuv for n = 2m where m is
the number of non-terminals in the language. Then, jstuj  2m . It follows that
stu overlaps with at most two of the four consecutive substrings 0n, 1n , 0n or 1n .
If stu overlaps with one of them, rs2 tu2 v is not of the form 0i 1j 0i 1j for any i
and j . If it overlaps with two of the four substrings, rsp tup v contain too many
alternations of 0s and 1s. Contradiction.

4.64 Problem: Show that the language fww j w 2 fa, bg g is not context-free.
...........................................................................
Solution: This problem is identical with Problem 4.63(c) except for a change of alphabet.
CFL CLOSURE PROPERTIES

4.65 Problem: Let M1 and M2 be pushdown automata accepting the languages L(M1 )
and L(M2 ). Describe PDAs accepting their union L(M1 ) [ L(M2 ), concatenation
L(M1 )  L(M2 ), and Kleene closure L(M1 ) , thereby giving an alternate proof of
Theorem 4.13.1.
...........................................................................
Solution: For the union construct a control unit which from its initial state nondeterministically enters a state reached from the initial state of M1 or M2 . Such a machine
can accept a string accepted by either PDA. It follows that it does accept strings in their
union.
For the concatenation, let the new PDA start in the start state of M1 and execute
transitions in M1 until arriving at an accept state from which it makes a transition to
the start state of M2 . This machine accepts strings in the concatenation of the two
languages.
For the Kleene closure, the new PDA implements M1 except that edges that are transitions from its final state are added to the states reached from its initial state, just as
was done with finite-state machines. This language recognizes the Kleene closure of
L(M1 ).
4.66 Problem: Use closure under concatenation of context-free languages to show that the
language fwwR vR v j w, v 2 fa, bg g is context-free.
...........................................................................
Solution: The language fwwR j w 2 fa, bgg is context-free, as shown in Problem 4.42(a). It follows that fvR v j v 2 fa, bg g is also context-free because we can
let v R = w. Because the concatenation of two context-free languages is context-free,
the resulting language which is formed this way is context-free.

Computability

5.1 Solutions
THE STANDARD TURING MACHINE MODEL

5.1 Problem: Show that the standard Turing machine model of Section 5.1 and the model
of Section 3.7 are equivalent in that one can simulate the other.
...........................................................................
Solution: The next-state function of the Turing machine model of Section 5.1, :
Q  ( [ f g) 7! (Q [ fhg)  ( [ f g [ fL, Rg), maps the current state and
value under the head into a next state and either a value to be written on the tape
or a head movement. However, the next state function for the model of Section 3.7,
: Q  ( [ f g) 7! (Q [ fhg)  ( [ f g)  fL, N, Rg, differs in that it maps the
current state and value under the head into a next state and both a value to be written
on the tape and a head movement.
Let the two models be Model5 and Model3, respectively. Model5 can be simulated
by Model3 as follows: To execute one step by Model5, execute one step by Model3 as
follows: a) if Model5 does not move the head, Model3 writes a potentially new tape
symbol without changing the head position, and b) if Model5 does move the head,
Model3 writes the original value of the symbol in the current cell and performs the
same move as Model5.
To simulate one step by Model3, Model5 performs one or two steps. On the first step
Model5 writes the symbol that Model3 would write. If on the current state and input
Model3 would move the head, Model5 executes a second step on which it moves the

91

92

Chapter 5 Computability
head. The second move, if needed, requires the addition of a new state for each state
and input on which Model3 moves the head.

PROGRAMMING THE TURING MACHINE

5.2 Problem: Describe a Turing machine that generates the binary strings in lexicographical order. The first few strings in this ordering are 0, 1, 00, 01, 10, 11, 000, 001,
:::.
...........................................................................
Solution: Such a Turing machine writes 0 on its otherwise blank work tape. Treating
this string as a binary number, it increments it by adding 1 to the leftmost, least significant position. If the new least significant bit is 1, it checks to see if all more remaining
bits are also 1. If so, it replaces the blank to its right by 0 and replaces all other bits by
0. It then returns to the first step which is to increment the binary number on its work
tape by one.
5.3 Problem: Describe a Turing machine recognizing fxiyj xk

j i, j , k  1 and k = i  j g.

...........................................................................
Solution: We assume without loss of generality that the DTM for this problem has
a work tape. The DTM first verifies that the input string consists of xs followed by
ys and then z s. If it is not of this form, the string is rejected. To verify that the
number of xs, ys, and z s, namely i, j , and k, in xiyj xk satisfy k = i  j , the DTM
uses markers on its input tape to copy xi onto the work tape for each instance of y in
yj . The work tape now contains i  j instances of x. The DTM need only compare
these characters with the characters in the string z k . They have the same number of
characters if k = i  j .
5.4 Problem: Describe a Turing machine that computes the function whose value on input
ai bj is ck , where k = i  j .
...........................................................................
Solution: Given an input, the DTM first checks to see that it contains a string of as
followed by bs. It then invokes the procedure used by the DTM in the solution to
Problem 5.3 to write a string of i  j characters on its work tape which can then be
transferred to its output tape.
5.5 Problem: Describe a Turing machine that accepts the string (u, v) if u is a substring
of v.
...........................................................................
Solution: On the input string (u, v ), the DTM uses markers to determine if v contains u as a contiguous substring by trying to match each character in u starting with
the first position of v. (A marker can be viewed as a character on a second track. A second track can be simulated by using pairs of cells for each of the original cells, the first
contains the tape character, the second contains the marker. A larger control unit can
implement this change.) If that fails, the DTM starts over trying to match u starting
with the second character of v .

5.1 Solutions

93

Control
Unit
Figure S.5.1 A schematic representation of a two-dimensional Turing machine.

5.6 Problem: The element distinctness language, Led , consists of binary strings no
two of which are the same; that is, Led = f2w1 2 : : : 2wk 2 j wi 2 B and wi 6=
wj , for i 6= j g. Describe a Turing machine that accepts this language.
...........................................................................
Solution: The DTM for this problem uses markers to compare w1 with each of the
other strings. If it does not match these other strings, it repeats the process comparing
the next string with all subsequent strings until the next-to-last string has been compared with the last string. If no two strings match, the input is accepted; otherwise it is
rejected.

EXTENSIONS TO THE STANDARD TURING MACHINE MODEL

5.7 Problem: Given a Turing machine with a double-ended tape, show how it can be
simulated by one with a single-ended tape.
...........................................................................
Solution: The double-ended tape is folded in half. The left half is placed on the
bottom track of a single-ended two-track tape. The right half is placed on the top
track. A special character is placed in the two tracks of the leftmost cell so that the TM
can detect that it has reached the left end of the single-ended tape. It then switches
direction and moves to the second cell on the bottom track.
5.8 Problem: Show equivalence between the standard Turing machine and the one-tape
double-headed Turing machine with two heads that can move independently on its
one tape.
...........................................................................
Solution: The one-tape, single-ended, double-headed TM M2 can be simulated by the
one-tape, single-ended, single-headed TM M1 as follows: two special markers are used

94

Chapter 5 Computability

M1 to identify the positions of M2 s two heads. Then for each of the T2 steps of
M2 , M1 sweeps over the non-blank portion of its tape from left to right to pick up the
by

values under the two heads. It then sweeps back to the left end of the tape. When it
reaches a marker for a head, it writes a potentially new symbol under the head and, if
necessary, moves the head marker to an adjacent cell. It then resumes its sweep left until
it finds the next head marker or reaches the left-end of the tape. Thus, M1 will execute
O(T22 ) steps.
5.9 Problem: Show that a pushdown automaton with two pushdown tapes is equivalent to
a Turing machine.
...........................................................................
Solution: With two pushdown stacks, a single, double-ended tape can be simulated.
Since the input tape of a PDA is used as a one-way, read-only tape, the stacks are used
to simulate the two-way, read/write tape of a TM. We first describe how two stacks, S1
and S2 , can be used to simulate a two-way, read/write tape. Let the value under the
tape head be the element at the top of S1 . To read it, it must be popped. If it is to be
changed, a new value is pushed. If the head is to be moved right, the element at the top
of S2 is popped and then pushed onto S1 . If the head is to be moved left, the element
at the top of S1 is popped and pushed onto S2 . A special marker can be pushed onto
S1 to identify the left-most cell of a single-ended tape. The input can then be read from
the read-only input tape and placed on the simulated tape after which the control unit
moves to the leftmost simulated cell and begins its simulation.
5.10 Problem: Figure 5.14 shows a representation of a Turing machine with a two-dimensional
tape whose head can move one step vertically or horizontally. Give a complete definition of a two-dimensional TM and sketch a proof that it can be simulated by a standard
TM.
...........................................................................
Solution: A formal definition of a 2-D Turing machine is given below.

M =
( , , Q, , s, h) where is the tape alphabet not containing the blank symbol ,
Q is the set of states, : Q  ( [ f g) 7! (Q [ fhg)  ( [ f g [ fL, R, U, Dg)
is the next-state function, s is the initial state, and h 62 Q is the accepting halt state.
A TM cannot exit from h. If M is in state q with letter a under the tape head and
(q, a) = (q0 , a0 , C), its control unit enters state q0 and writes a0 if C = a0 2 [ f g
DEFINITION 5.1.1 A two-dimensional Turing machine (TM) is a six-tuple

or moves the head left (if possible), right, up, or down (if possible) if C is L, R, U, or D,
respectively.
The TM M accepts the input string w 2  (it contains no blanks) if when started
in state s with w placed left-adjusted on its otherwise blank first row, the last state entered
by M is h. M accepts the language L(M ) consisting of all strings accepted by M .
Languages accepted by Turing machines are called recursively enumerable. A language L
is decidable or recursive if there exists a TM M that halts on every input string, whether
in L or not, and accepts strings in L.
A function f :  7!  [ ?, where ? is a symbol that is not in , is partial if for some
w 2  , f (w ) = ? (f is not defined on w). Otherwise, f is total.

5.1 Solutions

95

A TM M computes a function f :  7!  [ ? for those w such that f (w) is defined

if when started in state s with w placed left-adjusted in the first row of its otherwise blank
tape, M enters the accepting halt state h with f (w ) written left-adjusted on in the first row
of its otherwise blank tape. If a TM halts on all inputs, it implements an algorithm. A task
defined by a total function f is solvable if f has an algorithm and unsolvable otherwise.
To show that a 2-D TM can be simulated by a 1-D one, we give an ordering to the
cells on the 2-D tape so that we can map each 2-D cell into a cell on the 1-D tape. Let
each cell be indexed by its row and column indices, r and c, where 1  r, c. The cell
in position (r, c) is given index p(r, c) = r + (r + c 2)(r + c 1)=2. This assigns
cell in the lower left corner the value 1, that to its right value 2, and that above it value
3, etc. We simulate the 2-D TM with a 4-tape TM. Its first tape contains the entries
from the 2-D array. On its second and third tapes are kept the value of r and c. On the
fourth tape the value p(r, c) is computed. Since this function involves integer addition
and multiplication and division by 2, which is done by shifting, this computation is
tedious but straightforward. Once the value of p(r, c) is computed, it can be used to
move the tape head on the first tape from the leftmost cell to the cell numbered p(r, c).

The 1-D TM simulates the 2-D TM by determining which or r and c is to be incremented, incrementing it, computing p(r, c) and then advancing the head on the first
tape to the new position.

5.11 Problem: By analogy with the construction given in Section 3.9.7, show that every
deterministic T-step multi-tape Turing machine computation can be simulated on a
two-tape Turing machine in O(T log T ) steps.
...........................................................................
Solution: This solution is due to Hennie and Stearns, Two-tape Simulation of Multitape Turing Machines, JACM, vol. 13, no. 4, (1966), p. 533-546, and follows
the derivation given in the book Introduction to Automata Theory, Languages, and
Computation, by Hopcroft and Ullman, Addison-Wesley, (1979).
Given a k-tape TM Mk that executes T steps, we simulate it by a 2-tape TM M2 that
has k pairs of tracks on its data tape, one pair for each of the k tapes, and two pairs of
tracks on its work tape. The work tape is used only for the temporary storage of data
on the data tape.

M2 simulates the movements of Mk s k heads by moving data on the tracks of its data
tape. After moving the data, k of the 2k tracks under M2 s head contain the data under
Mk s k heads. M2 reads these k values, makes the appropriate state transition, and then
simulates Mk s head movements by again moving on its data tape.
To arrange its data so that so that the data under its head is the same as under each of

Mk s k heads, M2 moves data on one pair of tracks at a time. It moves data left on the
two tracks associated with the j th tape of Mk s tapes if the j th head moves right and
moves data right if j th head being simulated moves left. It is this movement of data
that causes the expansion of the execution time from T to O(T log T ).
Since the simulation of the k heads is done in succession, it suffices to describe M2 s
simulation of Mk s first tape. The procedure employed assumes that the tape is double
ended (which is not essential) and rules the tape into blocks B0 , B1 , B 1 , B2 , B 2 , : : :

96

Chapter 5 Computability

7 x 6 x 5 x 4 x 3 x 2 x 1

x0

x1

1 B0

B1

x2

x3

B2

x4

x5

x6

x7

B3

tape.

in which Bj contains 2j 1 contiguous tape cells each containing 2k tracks. (See

Fig. S.5.2 which shows the two tracks associated with Mk s first tape.)
Before simulating the movement of Mk s k heads, M2 s head is over block B0 which
contains one cell. If Mk s first head moves left (right), the data on the first two tracks
of M2 s data tape moves right (left). As the data is moved we show by induction that
the following conditions apply to the blocks:
1. For each j > 0 one of the following holds: a) both tracks of Bj are full (contain
characters from Mk s first tape, some of which may be the blank ) and both
tracks of B j are empty, b) both tracks of B j are full and both tracks of Bj
are empty, or c) the bottom tracks of both blocks are full and their top tracks are
empty.
2. A non-empty track of block Bj contains values from 2j 1 consecutive cells of
Mk s first tape. For j > 0, if both blocks of Bj are full, the upper track contains
2j 1 entries to the immediate left of those on the lower track whereas if both
blocks of B j are full, the upper track contains 2j 1 entries to the immediate
right of those on the lower track.
3. For i < j the entries in Bi are to the left of those in Bj on Mk s first tape.

B0 has a special marker in its upper track and a tape value on its lower track.
The action of M2 when Mk moves the head on its first tape left is to move a block
of data right. (The action when Mk moves this head right is the mirror image of this
action.) M2 searches right to find the first block, say, Bp , that does not have both tracks
full. (The actions that follow are called a Bp -operation.) Since B1 , B2 , : : : , Bp 1 are
full, by the inductive hypotheses stated above, this implies that B (p 1) , : : : , B 1 are

empty. B0 , B1 , B2 , : : : , Bp 1 contain 1 + 2 1 + 2 +    + 2p 2 = 2p 1 entries.
Move these entries to the work tape and then to the lower track of B1 , B2 , : : : , Bp 1
(erasing the other entries) and either the lower track of Bp if it is empty, or to Bp s
upper track otherwise. There are 1 + 2 + 22 +    + 2p 1 = 2p 1 cells in these tracks,
4.

just enough to hold all the items that are being shifted right. (In the following explana
We now describe the shifting of data on the data tape as M2 simulates the the movement
of a head on Mk s tape. (See Fig. S.5.3 in which cell values are shown as they never
change.)
M2 moves left to B0 , the marked cell, and then uses the 2p 1 non-empty cells on
the work tape to find Bp on the data tape. (Blocks B (p 1) , : : : , B 1 are empty.) If
both tracks of B p are full, M2 copies the upper track to its work tape and then to
the lower tracks of B (p 1) , : : : , B 1 , B0 . If both tracks are not full, M2 copies the

5.1 Solutions
B

1 B0

7 x 6 x 5 x 4 x 3 x 2 x 1

7 x 6 x 5 x 4 x 3 x 2

7 x 6 x 5 x 4

7 x 6 x 5 x 4

B1

x0

x1

B2

97

B3

x2

x3

x4

x5

x6

x7

x2

x3

x4

x5

x6

x7

x0

x1

x2

x3

x4

x5

x6

x7

x0

x1

3 x 1

x2

x3

x4

x5

x6

x7

x0

x1

x2

x3

x4

x5

x6

x7

x0

x1

x2

x3

x4

x5

x6

x7

x0
x

x1

3 x 2 x 1
x
x

7 x 6 x 5 x 4 x 3 x 2 x 1

7 x 6

x
x

5 x 3 x 2 x 1

Figure S.5.3 The movement of blocks as Mk s head moves left five times.
lower track of B p to the lower tracks of these blocks. Since one track of B p contains
2p 1 cells as do the lower tracks of B (p 1) , : : : , B 1 , B0 , there is just enough space
for one track of B p . This movement of data satisfies the conditions of the induction
hypotheses and places the contents of the cell to the left of B0 into the lower track of
B0 .
A Bp -operation, which takes K 2p steps for some K > 0, is performed at most once
every 2p 1 time steps because it takes this many steps to fill B1 , B2 , : : : , Bp 1 , which
are half empty after a Bp -operation. Thus, for at most once every T=2p 1 steps of Mk ,
1  p  (log2 T ) + 1, M2 does a Bp -operation each costing K 2p M2 steps. Thus,
the total number of operations by M2 , T2 , satisfies:

T2 
Thus, T2

logX
2 T +1
p=1

K 2p

2p

= O (T log T ).

PHRASE-STRUCTURE LANGUAGES AND TURING MACHINES

5.12 Problem: Give a detailed design of a Turing machine recognizing fanbn cn j n  1g.
...........................................................................
Solution: The table shown in Fig. S.5.4 describes a TM for this problem. It scans the
input sequence, rejecting it if it is not of the form aibj ck and marking the first instance
of a, b, and c as a, b, and c, respectively. Except for the reject state r, the states are
indexed by integers. The purpose of each state is explained below.

Chapter 5 Computability

98

State
1

a
b
2, a, R r, ,

r,

r,

r,

r,

r,

2, a, R

3, b, R

r,

r,

r,

r,

r,

3, b, R

4, c, R

r,
r,

r,

4, c, R

r,
5, , L r,

5, a, L

5, b, L

5, c, L

r,

6, a, R

5, b, L

5, c, L

7, a, R

r,

r,

r,

r,

10, b, R

r,

7, a, R

8, b, R

r,

7, b, R

r,

8, b, R

8, c, R

r,

r,
r,

r,
r,

r, ,
r,
9, c, R r, ,
r,
9, c, R 5, , L r,

r,

10

r,

r,

r,

10, b, R

, ,

h,

r,

,
,

r,
r,

,
,

r,
r,

,
,

10, c, R

Figure S.5.4 The next-state table for the TM used in the solution of Prob 5.12.

State 1 is the initial state. The DTM M rejects the input unless the first letter is a which
it replaces with a after which it enters state 2. The purpose of this state is to advance
across instances of a to the first instance of b which M replaces with b and enters state
3. The purpose of state 3 is to move across bs to the first c which it converts to c and
enters state 4. In state 4 the DTM advances across cs to the first blank at which point
it enters state 5. If in any of the above states a character is encountered that is out of
place, the input is rejected. Thus, if the DTM enters state 5, the string on the tape is
of the form ai bj ck .
In state 5, the DTM moves back across all characters until it reaches the rightmost
instance of a at which point it enters state 6 and moves right. If in state 6 it sees a, it
replaces it with a and enters state 7. If it sees b it knows that it has checked off all the
instances of a and it enters state 10 to see if it has also checked off all instances of bs and
cs. In state 10 it moves right over instances of b and c, entering the halt state if it finds
the blank without encountering other characters, but rejecting the input otherwise. In
state 7 it moves across as and bs until it finds the first b which it converts to b and
enters state 8. If in state 7 it encounters c, it rejects the input because the input does
not have as many bs as as. In state 8 it moves across bs and bs until it reaches a c. If it
fails to find a c, it rejects the input because it does not have equal numbers of the three
characters. When it finds c in state 8 it converts it to c and enters state 9 during which
it moves right until it finds the first blank upon which it enters state 5.
5.13 Problem: Use the method of Theorem 5.4.1 to construct a phrase-structure grammar
generating fanbn cn j n  1g.

5.1 Solutions

99

...........................................................................
Solution: This is a tedious exercise once the transition table for the solution has been
found, as is done above. As shown in the solution to Problem 4.38, the context-free
grammar of Example 4.9.1 generates this language.
5.14 Problem: Design a Turing machine recognizing the language f02 j i  1g.
...........................................................................
Solution: We scan the input to insure that is consists only of 0s. We then use the
grammar given in the solution to Problem 4.39 to generate strings in this language. If
we generate a string that is shorter than the input, we generate the next string. If we
generate one that is longer, we reject the input. See the solution to Problem 4.39 for a
description of the approach.
i

UNIVERSAL TURING MACHINES

5.15 Problem: Using the description of Section 5.5, give a complete description of a universal Turing machine.
...........................................................................
Solution: The universal TM (UTM) U executes five phases: a) it moves its head right
to find the marked input symbol; b) it searches left for the current state; c) it finds the
value of the new tape symbol and the next state; d) it updates the current state; and e)
it resumes phase a. Two versions of phase a are used, one to simulate the first step in
the simulation of a TM, and the second to simulate later steps. Let M be the TM that
is being simulated.
The leftmost instance of [ is marked, that is, is replaced by b[. This indicates that the
initial state is the first state. The leftmost symbol to the right of the rightmost triple
< z 0 #q0 >< z 00 #q00 >< z 00#q000 > is marked, that is, is either b0, b1, or b. This
indicates the input head of the simulated TM is initially over this cell.
To find the marked input symbol (phase a) the UTM moves right from the leftmost
tape cell and enters state qin. In this state, it moves right until the current symbol a is
b
0, b
1, or b whereapon it enters state qback,a.
States qback,0 , qback,1 , and qback, are used to retain knowledge of the symbol that is
under the input head. While in one of these states the TM searches for the current state
(phase b) by moving left without changing the current state until it finds the symbol b[
which is to the immediate left of the triple < z 0 #q0 >< z 00#q00 >< z 00#q000 > associated with the current state. While in qback,a it replaces b[ by [ and enters qforward,a .
When the UTM enters qforward,a for a 2 f0, 1, g, it is entering phase c. In state
qforward,0 the UTM moves right on input < without changing its current state and
reads its input, z 0 , and enters state qz0 . Its subsequent actions are described below.
In state qforward,1 the UTM moves right without changing its state until it encounters
> at which point it moves right and enters state qagain,1. It remains in this state
moving right until over z 00 at which point it enters state qz00 . Its subsequent actions are
described below.
In state qforward, the UTM moves right without changing its state until it encounters
> at which point it moves right and enters state qagain, . It remains in this state moving

100

Chapter 5 Computability
right until it is over > at which point it moves right and enters qencore, . It remains
in this state moving right until over z 000 on which it enters state qz000 . Its subsequent
actions are described below.
After the TM enters state qz for z 2 f0, 1, , L, Rg it moves to the immediate right of
#. If it finds it finds >, M has entered its halt state. Thus, the UTM U enters its own
halt state, qhalt . If the character to the right of # is 1, U replaces it by b
1 and enters
state qz,left . It is now entering phase d during which U changes the state of M .
The UTM U enters state qz,left so that it can mark the triple associated with the next
state of the simulated TM by changing its [ to b[. In state qz,left the UTM moves to the
leftmost tape cell. It does this by moving left until it finds a special symbol . marking
the left-hand end of the tape and then moves right. It replaces [ by b[ and enters state
qz,search in which it moves right until it finds b1. At this point it replaces b1 by 1 and
enters state qz,first. In this state it moves right and enters state qz,second . If in qz,second
the tape contains >, the next state is properly marked which means that it can return
to phase a. If not, it must move the mark on a [ right at least one more place.
In the first case, namely, when the triple corresponding to the next state has been
marked, the UTM moves right and enters state qz,in in which it continues to move
right until it finds either b
0 or b
1. In this second version of phase a the knowledge of
z is carried forward by the UTM. Its action is determined by z which is 0, 1, , L
or R. In the first three cases it replaces the value in the cell by the appropriate marked
input symbol and enters the appropriate state qback,0, qback,1, and qback, which retains
knowledge of the value under the head. In the last two cases, it enters states in which
it unmarks the current symbol, moves to the appropriate adjacent cell, and marks its
value. It then enters the appropriate state qback,0, qback,1 , and qback, .
However, if in qz,second the tape contains 1, the UTM replaces it by b
1 and enters
qz,third . In this state it moves left and enters state qz,left in which it moves left until
it finds b[ in which case the UTM replaces it by [ and enters qz,fourth . In qz,fourth the
UTM moves right until it finds [ which it replaces with b[ and enters qz,search to resume
phase b. (Note that no instances of b[ exist on the first step of phase b.)

5.16 Problem: Construct a universal TM that has only two non-accepting states.
...........................................................................
Solution: This solution is due to Claude Shannon, A Universal Turing Machine with
Two Internal States, Automata Studies, J. McCarthy and C. E. Shannon, Eds., Princeton University Press, (1965), p. 157-165. This universal machine is designed for a
TM model similar to that of Chapter 3 in which the next-state function maps the
current state and value under the head to a next state, a new value under the head,
and a left or right head movement command. That is, the tape head is not allowed
to stay on the current cell. For this model the next-state function has the following
domain and range: : Q  ( [ f g) 7! (Q [ fhg)  ( [ f g)  fL, Rg.
More than two states may be necessary for the model of this chapter, namely, when
: Q  ( [ f g) 7! (Q [ fhg)  ( [ f g [ fL, Rg).
We design a two-state TM TWO with state set Q(2) and tape alphabet (2) that simulates an arbitrary TM M with state set Q and tape alphabet . TWO uses multiple
steps to simulate one step by M . If M moves from cell i to cell i + 1 (i 1), TWO

5.1 Solutions

101

bounces between cells i and i + 1 (i 1) multiple times to carry M s state from cell i
to cell i + 1 (i 1) as well as record the new value that is printed in cell i by M before
it makes the transition.
(2 ) ( 2 )
Let Q = fq1 , q2 , : : : , qK g and Q(2) = fq0 , q1 g. If = f 1 , 2 , : : : , J g, the
alphabet for TWO is the following.

(2) =

[ f k,j ,t,b j 1  k  K , 1  j  J , t 2 f+, g, b 2 fL, Rgg

The letters k,j , ,b and k,j ,+,b are used to convey information between adjacent cells
when bouncing between them. Here j indicates that j is to remain in the current cell
after the bouncing is done and k is the index of the current state of M . k,j , ,b indicates
that the cell containing this letter is receiving information during the bouncing phase
whereas k,j ,+,b indicates that the cell is supplying information. The value of b specifies
the direction in which the head is to move on the next bounce.
The inductive hypothesis has the following two parts: a) before TWO starts the simulation of the next step of M when M s head is in state qk with its head over cell i which
(2)
contains j , TWO is in state q0 and its head is over cell i which contains k,j , ,m for
m 2 fL, Rg, and b) all cells other than the cell over which TWOs head resides contain
the same letters from M s alphabet as does M .
If the transition by M in state qk on input j is (qk , j ) = (qk0 , j 0 , m) for m 2
fL, Rg, we let TWOs next-state function (2) satisfy

(2) (q0(2), k,j ,

(2)

,x

) = (q, k0 ,j 0 ,+,m , m)

(2 )

where q = q0 if m = L and q = q1 if m = R, independently of the value of

x. Thus, TWO writes k0 ,j 0 ,+,m in cell i, changes to a state associated with the head
movement by M and moves its head to the cell to which M moves its head.

(2 )

After this transition TWO has entered the bouncing phase in state q0 if it has moved
(2)
to the current state from the cell to its right and in state q1 otherwise. The old cell
contains the letter k0 ,j 0 ,+,m whereas the new one contains some letter u .
We let TWO have the following additional next-state transitions:

(2)(q0(2) , u ) = (q0(2) , 1,u,

(2)(q1(2) , u ) = (q0(2) , 1,u,
(2)

R)
,L , L)
,R ,

Thus, if TWO is in state q0 when it enters a cell containing u , it writes 1,u, ,R to

indicate that it has entered this cell from the cell on its right and it moves right to this
(2 )
old cell. If under the same conditions it is in state q1 , it writes 1,u, ,L to indicate
that it has entered this cell from the cell on its left and moves left to this old cell. The
third subscript, , indicates that the cell containing this letter is a receiving cell. It will
receive M s state from the old cell.
Summarizing, after two moves TWOs head is over the old cell which contains k0 ,j 0,+,m
and the new cell contains 1,u, ,b where b is the complement of m (b = R if m = L
and vice versa). TWO now bounces back and forth between the two cells, decrementing
the first subscript of the letter in the old cell by 1 as it increments the first subscript of

102

Chapter 5 Computability
(2)

(2 )

the letter in the new cell. TWO is in state q0 when over the new cell and in state q1
when over the old cell until the first subscript of its letter decreases to 1. At this point
(2 )
TWO changes the letter in the old cell to j 0 and moves to the new cell in state q0 .
This has the effect of restoring the conditions of the inductive hypothesis that apply
before simulating each step of M .
To cause the above actions to occur during bouncing, we add the following next-state
(2 ) ( 2 )
transitions for 1  k  K , 1  j  J , m 2 fL, Rg, and q 2 fq0 , q1 g:

(2)(q1(2) , k,j , ,m ) = (q0(2) , k+1,j , ,m , m)

(2)(q, k,j ,+,m ) = (q1(2) , k 1,j ,+,m , m)
(2)(q, 1,j ,+,m ) = (q0(2) , j , m)
The first type of transition causes k to increment whereas the second causes it to decrement. Thus, as TWO bounces between the old and new cells it decrements the state
index in the old cell and increments it in the new one until the third type of rule is
invoked and the new value is written into the old cell and TWO moves to the new cell.
DECIDABLE PROBLEMS

5.17 Problem: Show that the following languages are decidable:

a) L = f(M ), w j M is a DFSM that accepts the input string wg
b) L = f(M ) j M is a DFSM and L(M ) is infiniteg This problem has changed.
...........................................................................
Solution:
a) (M ), w is in L if the DFSM M accepts input w. To test if M accepts w we
construct a TM M 0 that simulates M on w. This simulation terminates on all
inputs because M is a DFSM. If the simulation ends in an accepting state then M 0
accepts. Otherwise it rejects.
b) From Lemma 4.5.2 the language accepted by M is infinite if and only if it contains
a string of length at least m and at most 2m 1 where m is the number of states
of M . We merely have to simulate M on all strings whose length is in this range
to see if they are accepted.
5.18 Problem: The symmetric difference between sets A and B is defined by (A B ) [
(B A), where A B = A \ B . Use the symmetric difference to show that the
following language is decidable:

LEQ FSM = f(M1 ), (M2 ) j M1 and M2 are FSMs recognizing the same languageg
...........................................................................
Solution: If the languages recognized by the FSMs M1 and M2 , LM1 and LM2 , are
different then either LM1
LM2 or LM2 LM1 is not empty. Thus, they recognize the same languages if and only if L0 = (LM1 LM2 ) [ (LM1 LM2 ) is not
empty. Since LM1 and LM2 are regular and the set of regular languages are closed under complements, intersections, and unions, L0 is regular. Thus, we can use the result
of Problem 5.17 to show that the problem posed in this question is decidable.

5.1 Solutions

103

L = f(G), w j (G) encodes a CFG G that generates wg

Hint: How long is a derivation of w if G is in Chomsky normal form?
...........................................................................
Solution: We assume that G is in Chomsky normal form. We know that some derivation tree for the terminal string w has depth at most log2 jwj. We design a TM M
that systematically produces all derivations of G of this length or less. If it produces w,
the input is accepted. Otherwise, it is rejected.
A procedure that this TM can use to systematically generate derivations is based on the
following tree model for derivations: a) the root of the tree is associated with the start
symbol; b) the root has an edge to one subtree for each derivation that has the start symbol on the left-hand side; c) each of these subtrees has either one descendant, a vertex
associated with a terminal, or two descendants associated with the non-terminals on the
right-hand side of its derivation; d) when there are two descendants, the two vertices
have one subtree for each derivation that has the corresponding non-terminal on the
left-hand side; e) this process is repeated until with each descendant until the appropriate depth is reached. If the rules with a common left-hand side are numbered, they can
be used in this ordering to generate strings systematically in a depth-first fashion until
the appropriate depth is reached.
5.20 Problem: Show that the following language is decidable:

L = f(G) j (G) encodes a CFG G for which L(G) 6= ;g

This problem has changed.
...........................................................................
Solution: We design a TM M 0 that when given as input a description of a CFG G first
marks all the terminals of the grammar and it then scans all the rules of the grammar
and marks non-terminal symbols that can be replaced by some marked symbols (so if
there is a non-terminal A that is not marked and a rule A ! BCD where B , C , D
have already been marked, then it also marks A). We repeat this procedure until no
new non-terminals can be marked. Then if S is marked, we accept; otherwise we reject
because it is not possible to generate a string of terminals from S .
5.21 Problem: Let L1 , L2 2 P where P is the class of polynomial-time problems (see Definition 3.7.2). Show that the following statements hold:
a)
b)
c)

L1 [ L2 2 P
L1 L2 2 P, where L1 L2 is the concatenation of L1 and L2
L1 2 P

Chapter 5 Computability

104

...........................................................................
Solution:
a)

the same input and accepting if at least one of them accepts.


b) Membership in L1 L2 can be decided in time O((n + 1) P1 (n) + P2 (n) ) by
taking an input string w, forming all n + 1 partitions of w (n = jwj) into two
substrings w1 , w2 , and checking for each partition whether w1 belongs to L1 and
w2 belongs to L2 .
c) Just invert the answer of M1 , at a small constant cost. The running time is
O(P1(n)).

5.22 Problem: Let L1 2 P. Show that L1 2 P.

Hint: Try using dynamic programming, the algorithmic concept illustrated by the
parsing algorithm of Theorem 4.11.2.
...........................................................................
Solution: Membership in L1 can be decided in time O(n3 P1 (n)) using the following
dynamic programming approach (the algorithm is very similar to that of CYK). For an
input string w 6= e of length n and 1  i  j  n, let wij be the substring extending
from the ith to the j th character of w; and let xij be a Boolean value indicating whether

wij is a member of L1 or not. The problem is then equivalent to determining the truth
value of x1n. We have the following recursion:

xij = (wij 2 L1 ) _

_
ik<j

(xik ^ xk+1,j ),

which says that wij is a member of L1 either if it is a member of L1 or if it can be


broken into two pieces each belonging to L1 . Proceeding bottom-up from shorter
to longer substrings of w, we can determine the value of each xij by checking wij for
membership in L1 and then looking at the 2(j i) values xik and xk+1,j for i  k < j .
This can be done in O(nP1 (n) + n) steps. Since there are O(n2 ) substrings of w, x1n
and hence the result can be computed in O(n3 (P1 (n) + n)) time. The empty string is
always accepted.
UNSOLVABLE PROBLEMS

5.23 Problem: Show that the problem of determining whether an arbitrary TM starting
with a blank tape will ever halt is unsolvable.
...........................................................................
Solution: We assume there is such a halting TM ME for this problem and show it can
be used to solve the halting problem as follows: Given an encoding (M ) of a TM M
and an input w to this machine, we construct a TM MS that examines its tape. If it
is empty, MS writes w and then invokes a universal TM to simulate M on w which
halts if and only if M on w halts. As a consequence, the halting problem for TM M
on input w can be solved by packaging up M and w as MS and then giving it with
the empty tape to ME . ME halts if and only if M on input w halts. Thus, we have a
solution to the halting problem, which we know does not exist.

5.1 Solutions

105

LEQ = f(M1 ), (M2 ) j L(M1 ) = L(M2 )g

...........................................................................
Solution: We assume there is a TM M that solves this problem and show that a contradiction results. To determine if M1 accepts the empty tape, we construct a simple
TM M2 that does not accept any input. We then pass (M1 ), (M2 ) to M which determines if L(M1 ) is empty, thereby recognizing the language of Problem 5.23 which
we know is not solvable.
5.25 Problem: Determine which of the following problems are solvable and unsolvable.
Defend your conclusions.
a)
b)
c)
d)
e)

f(M ), w, p j M reaches state p on input w from its initial stateg

f(M ), p j there is a configuration [u1 : : : um qv1 : : :vn ] yielding a configuration
containing state pg
f(M ), a j M writes character a when started on the empty tapeg
f(M ) j M writes a non-blank character when started on the empty tapeg
f(M ), w j on input w M moves its head to the leftg

...........................................................................
Solution:
a) Undecidable. Reduction from the halting problem. If this problem were decidable,
given a TM M and a string w we could use it to decide if M halts on w by testing
if M enters state h on input w.
b) Decidable. It suffices to check the transition function of M for a state q 6= p such
that (q, v1 ) = (p, 0 ), for some v1 2  and 0 2  [ fL, Rg.
c) Undecidable. Modify M such that it prints a special symbol \$ just before entering
the halting state. If we could decide whether the modified machine ever prints a \$
sign, we could decide whether M halts on the empty tape, a problem that is easy
to show unsolvable: given a machine M and an input w construct a machine M 0
that first writes w on the tape and then simulates M . M 0 halts on the empty tape
if and only if M halts on w.
d) Decidable. Simulate M until either (1) M prints a nonblank symbol, (2) M halts,
or (3) M enters a state it has reached before (in which case M will loop forever,
since it is deterministic and its input continues to be blank). Answer yes in the
first case and no in the other cases. Note that one of these cases must occur after
at most jQj steps, where Q is the set of states.
e) Decidable. Simulate M until either (1) M moves its head left, (2) M halts, or
(3) M enters a state/current input symbol combination it has reached before (in
which case an infinite loop is present, since the future computation of M depends,
in the absence of left moves, only on the current state and input symbol). The
answer is yes in the first case and no otherwise; and it will be given after at
most jQj  jj steps.

106

Chapter 5 Computability

N N

5.26 Problem: Define the integer division function fdiv : 2 7!

using primitive recursion.
...........................................................................
Solution: The value of fdiv (x, y) is bx=yc. To compute it we first define an auxiliary function fquot rem(x, y, q, d) that computes dx=ye. We then use this function to
compute bx=yc.
The function fquot rem(x, y, q, d) carries not only the value of x and y but also a
quotient q and a difference d. Here x = q  y + d or d = x q  y. Instead of
computing d this way, we let d = x  q  y where  is proper subtraction. Thus,
d = x when q = 0 and d = 0 when q is dx=ye. Hence, we define fquot rem(x, y, q, d)
as follows:

fquot rem(x, y, q, 0) = q

fquot rem(x, y, q, d) = fquot rem(x, y, q + 1, d y)
We let fdx=ye (x, y) = fquot rem(x, y, 0, x). Thus, on each invocation of the second
version of fquot rem(x, y, q, d) the value of d is decreased by y and q is increased by 1
until d is 0 at which point the resulting value is the value of dx=ye.
To compute bx=yc we use the function fif then else (p, a, b) that has value a if p  1
and value b if p = 0. That is, fdiv (x, y) = fif then else (p, a, b) where p = dx=ye 
y x, a = dx=ye, b = P (dx=ye), and P (x) is the predecessor function. The function
fif then else (p, a, b) is defined recursively below.

fif then else(0, a, b) = a

fif then else(1, a, b) = b
fif then else (p, a, b) = fif then else (p

1, a, b)

5.27 Problem: Show that the function fremain : 2 7!

that provides the remainder of
x after division by y is a primitive recursive function.
...........................................................................
Solution: fremain(x, y) = fsub (x, fmult (x, fdiv (x, y)))
5.28 Problem: Show that the factorial function x! is primitive recursive.
...........................................................................
Solution:

ffact (1) = 1
ffact (x) = fmult(x, ffact (x

1))

5.29 Problem: Write a RAM program (see Section 3.4.3) to realize the composition operation.
...........................................................................
Solution: The composition of g : m 7!
of m arguments with m functions
f1 : n 7! , f2 : n 7! , : : : , fm : n 7! , each of n arguments, is defined

5.1 Solutions

107

by the following:

f (x1 , x2, : : : , xn) = g(f1 (x1 , x2, : : : , xn ), : : : , fm (x1 , x2, : : : , xn ))

Let Pg and Pf , Pf , : : : Pfm be RAM programs that compute g and f1 , f2 , : : : , fm ,
respectively, and let R1 , : : : , Rk be the maximal set of registers which any of them uses.
1

Assume that these programs leave the values of their corresponding functions in register
R1 . Let the values of x1 , x2 , : : : , xn be stored in registers R1 , : : : , Rn. A RAM program
for this composition copies the contents of R1 , : : : , Rn into Rk+1 , : : : , Rk+n and then
uses Pf1 to compute the value of f1 (x1 , x2 , : : : , xn ) on R1 , : : : , Rn . The value of
f1 (x1 , x2, : : : , xn) is then stored in register Rk+n+1, the values of R1 , : : : , Rn are
restored and the values of registers Rn+1 , : : : , Rk are cleared (restored to their original
values). This process is repeated for fj (x1 , x2 , : : : , xn ) for j = 2, 3, : : : , m except
that the value of fj (x1 , x2 , : : : , xn) is stored in register Rk+n+j . After computing
fm (x1 , x2, : : : , xn) and storing its value, the contents of fj (x1, x2 , : : : , xn) is stored
in register Rj for 1  j  m and registers Rm+1 , : : : , Rk cleared. The program for
Pg is then invoked to compute g(f1 (x1, x2 , : : : , xn), : : : , fm (x1, x2 , : : : , xn)). The
program for this task is shown in Figure 5.5.
5.30 Problem: Write a RAM program (see Section 3.4.3) to realize the primitive recursion
operation.
...........................................................................
Solution: The program shown in Fig. S.5.6 implements primitive recursion. It is
assumed that the functions g and h use only registers Rj for 1  j  k and that
the n + 1 arguments to f (x1 , x2 , : : : , xn , y) are located initially in registers R1 , R2 ,
: : : , Rn+1, respectively. The program computes f (x1 , x2 , : : : , xn, y) by computing
f (x1 , x2 , : : : , xn, 0), f (x1 , x2, : : : , xn, 1), etc. until reaching f (x1 , x2 , : : : , xn , y).
The program begins by moving the n + 1 arguments to higher indexed registers. The
initial value of y, which appears in Rn+1 , is copied to Rk+n+1 so that it is available to
be decremented until it reaches 0. The program clears registers Rj for n + 1  j  k
and executes the program Pg to compute f (x1 , x2 , : : : , xn , 0). Its value, which is
assumed to reside in R1 , is moved to Rk+n+3 . Register Rk+n+2 contains 0 initially and
is incremented each time that Rk+n+1 is decremented.
The program then tests to see if the iteration number Rk+n+1 has been reduced to
0 (and Rk+n+2 = z has been increased to y). If so, the program terminates; if not,
the arguments x1 , x2 , : : : , xn are restored, the current value for z (Rk+n+2 ) is put
into Rn+1 and the value for f (x1 , x2 , : : : , xn , z ) placed into Rn+2 . The program for
h, Ph , is then executed on these arguments to compute f (x1 , x2 , : : : , xn, z + 1) =
h(x1 , x2, : : : , xn, z , f (x1 , x2 , : : : , xn, z )).
5.31 Problem: Write a RAM program (see Section 3.4.3) to realize the minimalization operation.
...........................................................................
Solution: Minimilization is defined in terms of functions f : n 7!
and g :
n+1 7!
. Here f (x ) be the smallest integer y 2
such that g(x, y) = 0 and
g(x, z ) is defined for all z  y, z 2 .
We assume that only registers Rj , 1  j  k, are used by the program for g and that
its value is left in R1 . The value of f is left in R1 also.

108

Chapter 5 Computability
The program given in Fig. S.5.7 saves the arguments x1 , x2 , : : : , xn and clears the
search variable y which is held in Rk+n+1 . It then restores the arguments and loads the
next larger value of y into Rn+1 . It clears registers Rj , n + 2  j  k, and executes
the program Pg for g. If the result is 0, the program for f terminates after loading the
value of y into R1 . Otherwise, it increments y and repeats the execution. We assume
that the labels N0 and N1 are not used in Pg .

5.1 Solutions

f
N0

R k +1

g(f1 , : : : , fm )

::
:

Rk+n

Pf

R1
Rn

Comments
Save arguments

Rk+n+1
R1
R1
Rk+1

Compute f1 (x1 , x2 , : : : , xn )
Save result
Restore arguments

Rn
Rk+n
CLR Rn+1

Clear working registers

::
:
::
:

CLR Rk

Pf

Rk+n+2
R1
R1
Rk+1

Compute f2 (x1 , x2 , : : : , xn )
Save result
Restore arguments

Rn
Rk+n
CLR Rn+1

Clear working registers

::
:
::
:

109

CLR Rk

::
:

Rk+n+m
R1
R1
Rk+n+1

Compute fm (x1 , x2 , : : : , xn )
Save result
Retrieve values of functions

Rm
Rk+n+m
CLR Rm+1

Pfm
::
:
::
:

CLR Rk

Pg

Compute g(f1 (x1 , x2 , : : : , xn), : : : , fm (x1 , x2 , : : : , xn ))

CONTINUE
Figure S.5.5 A RAM program to implement functional composition.

110

Chapter 5 Computability

Primitive Recursion
R k +1
R1

Comments
Save arguments

Rk+n
Rn
Rk+n+1
Rn+1
CLR Rk+n+2
CLR Rn+1

Save y, number of iterations needed

Initialize z , current number of iterations, to 0
Clear working registers

::
:

::
:

CLR Rk

Pg
N0

Rk+n+3
R1
Rk+n+1 JMP N1
R1
Rk+1

Compute f1 (x1 , x2 , : : : , xn , 0) = g(x1 , x2 , : : : , xn)

Save result
Exit if done
Restore arguments

Rn
Rk+n
Rn+1
Rk+n+2
Rn+2
Rk+n+3
CLR Rn+3

Use current iteration number, z

Use value of f1 (x1 , x2 , : : : , xn , z )
Clear working registers

::
:

::
:

CLR Rk

Ph

N1

Rk+n+3
R1
DEC Rk+n+1
INC Rk+n+2
JMP+ N0
CONTINUE

Compute h(x1 , x2 , : : : , xn , z , f1 (x1 , x2 , : : : , xn , z ))

Save result
Decrement y
Increment z
Jump back to test for another iteration

5.1 Solutions

Minimilization
R k +1
R1

Comments
Save arguments

Rk+n
Rn
CLR Rk+n+1
R1
Rk+1

Initialize the search variable y to 0

Restore arguments

Rn
Rk+n
Rn+1
Rk+n+1
CLR Rn+2

Load current value of y

Clear working registers

::
:

N0

::
:
::
:

CLR Rk

Pg

N1

R1 JMP N1
INC Rk+n+1
JMP+ N0
R1
Rk+n+1
CONTINUE

Compute g(x1 , x2 , : : : , xn , y)
End computation if R1 is 0
Increment y
Redo the computation with next larger value of y
Load value of f (x1 , x2 , : : : , xn)

111