1992 Bookmatter TheDesignAndAnalysisOfAlgorith
1992 Bookmatter TheDesignAndAnalysisOfAlgorith
HOMEWORK 1 219
Homework 1
1. (a) Let (S,I) be a matroid and let A ~ S. Prove that all maximal
independent subsets of A have the same cardinality. (This number is
called the mnk of A. The rank of the matroid is the rank of the set
s.)
(b) Let I be a maximal independent set and let x rf- I. Prove that there
is a unique cycle contained in I U {x}. (This is called the fundamental
cycle of x and I.)
(c) Let G = (V, E) be an undirected graph, not necessarily connected.
Consider the system (E,I), where I consists of all subsets E' ~ E
such that the subgraph (V, E') has no cycles. Show that (E, I) is a
matroid. What is its rank? What are its maximal independent sets?
(a) Prove that G has an Euler circuit iff G is connected and the degree of
every vertex is even.
(b) Give an D(IED algorithm to find an Euler circuit if one exists. Give
a proof of correctness and detailed complexity analysis.
220 HOMEWORK 2
Homework 2
1. Give a linear-time algorithm for topological sort based on depth-first search.
/ s t i ng
tail
Give an algorithm that makes only O(n) probes of the adjacency matrix of
G and determines whether G is a scorpion. (This is a counterexample to an
earlier version of the famous Andema-Rosenberg conjecture, which stated
that any nontrivial graph property that is invariant under graph isomor-
phism requires O(n 2 ) probes of the adjacency matrix to test. Anderaa
disproved this version in 1975 using a different counterexample (see [91]),
but conjectured that it held for monotone properties (those that cannot
change from false to true when edges are deleted). This was later verified
by Rivest and Vuillemin [91].)
HOMEWORK 3 221
Homework 3
1. Verify that the family RegE ofregular expressions over an alphabet E with
the operations defined as in Lecture 6, Example 6.2 is a Kleene algebra.
{J(ax"() Ix E R({3)}
exists and is equal to J(a{3"(). (Hint. Try induction on {3, using the axioms
of Kleene algebra.) Note that Lemma 7.1 of Lecture 7 is a special case with
a="(=1.
Homework 4
1. The following algorithm, known as Prim's algorithm, produces a minimum
spanning tree T in a connected undirected graph with edge weights. Ini-
tially, we choose an arbitrary vertex and let T be the tree consisting of that
vertex and no edges. We then repeat the following step n - 1 times: find
an edge of minimum weight with exactly one endpoint in T and include
that edge in T.
2. The Planar Separator Theorem gives ~-~ separators of size O( y'n) for ar-
bitrary planar graphs. Show that this is the best you can do in general;
i.e., give a family of planar graphs whose smallest ~-~ separators are of size
O(y'n).
HOMEWORK 5 223
Homework 5
1. (a) Given a flow f on a directed graph G with positive edge capacities,
show how to construct the residual graph G, in Oem) time.
(b) Using (a), show how to calculate efficiently an augmenting path of
maximum bottleneck capacity. (Hint. Modify Dijkstra's algorithm.)
3. Give an efficient algorithm for the min cut problem: given an undirected
graph G = (V, E), elements s, t E V, s =1= t, and edge weights w : E -+ 'R,+,
find an s, t-cut of minimum weight; i.e., find a partition A, B of V with
sEA, t E B minimizing
L w(u,v) .
(u,v)EEn(AxB)
(Several minor variants of this problem are NP-complete. For example, the
max cut problem is NP-complete.)
224 HOMEWORK 6
Homework 6
1. (The stable marriage problem.) In a group of n boys and n girls, each girl
ranks the n boys according to her preference, and each boy ranks the n girls
according to his preference. A marriage is a perfect matching between the
boys and the girls. A marriage is unstable if there is a pair who are not
married to each other but who like each other more than they like their
respective spouses, otherwise it is stable. Prove that a stable marriage
always exists, and give an efficient algorithm to find one.
N (8) = {v E V I 3u E 8 (u, v) E E} .
4. An undirected graph is regular if all vertices have the same degree. Prove
that any nontrivial regular bipartite graph has a perfect matching. (Hint.
Use 2 or 3.)
HOMEWORK 7 225
Homework 7
1. Consider a restricted version of CNFSat in which formulas may contain at
most k occurrences of any variable, either negated or unnegated, where k
is fixed.
Homework 8
1. Let Zp denote the field of integers modulo a fixed prime p. Consider the
problem of determining whether a given expression involving +, -,·,0,1,
and variables ranging over Zp vanishes for all possible values for the vari-
ables. Show that this problem is coNP-complete. (Hint. You may want to
use Fermat's Theorem: aP- 1 = 1 for all nonzero a E Zp.)
2. Consider a restricted version of the TSP such that distances are symmetric
and satisfy the triangle inequality:
d(u,v) d(v,u)
d(u,w) < d(u,v) + d(v,w) .
Homework 9
1. Give an NC algorithm for obtaining a topological sort of a given directed
acyclic graph. (Hint. Use Miscellaneous Exercise 27.)
Xi Ci, 0 ~ i ~ k - 1
Xn alXn-l + a2Xn -2 + ... + akXn-k + c, n;::: k
where the Ci, ai, and c are constants. For example, the Fibonacci sequence
is of order 2 with Co = Cl = al = a2 = 1 and C = O. Show that the nth term
of a linear recurrence of order k can be computed in time
(a) O(k 2 Iogn) with a single processor. (Hint. Use Miscellaneous Exercise
22.)
(b) o (log k logn) with O(kO!) processors, where a = 2.81. .. is the con-
stant in Strassen's matrix multiplication algorithm.
(c) O(log k + log n) with O(kn) processors, assuming that the ring we
are working in supports an FFT. (Hint. Work with the generating
function
00
x(y) = LXiyi
i=O
Homework 10
1. In Luby's algorithm, we need to show that if we expect to delete at least
a fixed fraction of the remaining edges in each stage, then the expected
number of stages is logarithmic in the number of edges. We can formalize
this as follows.
Proposition Let m ~ 0 and 0 < f < 1. Let Xl, X 2 , ••• and So, Sl,
S2, . .. be nonnegative integer-valued random variables such that
Sn = Xl + ... + Xn ~ m
£(Xn+1 I Sn) ~ f· (m - Sn) .
(Hint. Using the fact £(£(Xn+l I Sn)) = £Xn+1 shown in class, give
a recurrence for £Sn.)
(b) Using the definition of expectation, show also that
£Sn ~ m - 1 + Pr(Sn = m)
and therefore
Pr(Sn = m) ~ 1 - m(l - ft .
(c) Conclude that the expected least n such that Sn = m is O(logm).
(Hint. Define the function
I, ifx<m
{
f(x) = 0, otherwise
(1 , u, 2 ..• , U d-l)
U,
H := {( u, t( u)) I u E V} .
(b) If there are two or more edges (u, t(u)) in H with t(u) = v, then v
chooses one of them arbitrarily and deletes the rest from H.
(c) Let U be the set of vertices with at least one incident edge in H. Each
vertex in the graph (U, H) has degree 1 or 2. If 2, it randomly selects
one of its two incident edges as its favorite. If 1, it selects its one
incident edge as its favorite.
(d) For each edge e E H, e is included in M if it is the favorite of both
its endpoints.
Miscellaneous Exercises
1. Let (S,I,w) be a weighted matroid. Let M be the family of all maximal
independent sets of minimum weight, and let Imax be the family of all
subsets of elements of M. Show that (S,Imax ) is a matroid. (Hint. Use the
blue rule to give a procedure for finding an x E J - I such that IU{ x} E Imax
whenever I, J E Imax and III < IJI.)
2. Let T = (V, E) be a connected undirected tree such that each vertex has
degree at most 3. Let n = IVI. Show that T has an edge whose removal
disconnects T into two disjoint subtrees with no more than 2nt
vertices
each. Give a linear-time algorithm to find such an edge.
3. Show how to solve the all-pairs shortest path problem on directed graphs
when negative edge weights are allowed. (If there is no lower bound to the
weights of paths from s to t, then we define the distance from s to t to be
-00.)
4. Suppose that we wish to schedule n unit-time jobs on a single processor,
starting at time O. Associated with each job j is a deadline dj 2:: 1 and a
penalty Pj 2:: O. Job j must be completed by time dj or the penalty Pj is
incurred.
6. (The Carpenter's Rule Problem) Prove that the following problem is NP-
complete: given a sequence of rigid rods of various integral lengths con-
nected end-to-end by hinges, can it be folded so that its overall length is
as most k?
MISCELLANEOUS EXERCISES 231
. /c=::+~+~+~~._
hmge
.
5:.k
12. The following problem arose recently in Keith Marzullo's META project.
Given an undirected graph with black and red vertices, does there exist
a maximal clique with no red vertices? Show that this problem is NP-
complete.
13. Show that the partition problem can be solved in polynomial time if the
weights are restricted to be integers and bounded in absolute value by a
fixed polynomial in the number of items.
14. Given n > 0, prove that there exists a number k such that when the
binary-to-Gray operation is applied k times in succession starting with any
bit string of length n, we get back the original bit string. What is the
smallest such k as a function of n?
15. (a) In the proof given in Lecture 27 of the #P-completeness of the perma-
nent, show that the four-node widget can be replaced by a three-node
widget.
(b) Show that no two-node widget exists.
16. Consider the following scheduling problem. You are given positive integers
m and t and an undirected graph G = (V, E) whose vertices specify unit-
time jobs and whose edges specify that the two jobs cannot be scheduled
simultaneously. Can the jobs be scheduled on m identical processors so
that all jobs complete within t time units?
(a) Give a polynomial time algorithm for t = 2. (Hint. Use Miscellaneous
Exercise 13.)
(b) Show that the problem is NP-complete for t ~ 3.
17. Let G be a directed graph with positive and negative edge weights and let
sand t be vertices of G. Recall that a path or cycle of G is simple if it has
no repeated vertices.
(a) Give a polynomial-time algorithm to determine whether G contains a
simple cycle of negative weight.
(b) Show that the problem of determining whether G contains a simple
path from s to t of negative weight is NP-complete.
18. Show how matching can be used to give efficient algorithms for the following
two problems.
(a) Given an undirected graph with no isolated vertices, find an edge cover
of minimum cardinality. (An edge cover is a subset of the edges such
that every vertex is an endpoint of some edge in the subset.)
(b) Find a vertex cover in a given undirected graph that is at most twice
the cardinality of the smallest vertex cover.
MISCELLANEOUS EXERCISES 233
19. Give a fast algorithm to determine whether a given directed graph has a
cycle cover.
20. Given a bipartite graph G = (U, V, E), say that D ~ U is deficient if
IDI > IN(D)I, where N(D) denotes the set of neighbors of D; i.e.,
N(D) = {vEVI3uED(u,v)EE}.
24. The following problem arose recently in Tim Teitelbaum's synthesizer gen-
erator project. Let f be a binary function symbol, a and b constant sym-
bols, and X = {x, y, ... } a set of variables. A term is a well-formed expres-
sion over f, a, b, and X; for example, the following are terms:
a (1)
234 MISCELLANEOUS EXERCISES
b (2)
x (3)
f(a,b) (4)
f(a,x) (5)
f(b,x) (6)
f(f(x,y),z) (7)
A term is a ground term if it contains no variables; for example, (1), (2)
and (4) above are ground terms. A ground term t is a substitution instance
of a term s if t can be obtained from s by substituting ground terms for the
variables of s; for example, (4) is a substitution instance of (5) obtained by
substituting b for x. A set of terms T is a cover if every ground term is a
substitution instance of some term in T; for example, (1), (2), (5), (6), (7)
form a cover.
25. Give an NC algorithm for finding the preorder numbering of a directed tree.
That is, the algorithm should label each node of the tree with number of
vertices visited before it in a preorder traversal.
26. An outerplanar is a graph that can be embedded in the plane so that every
vertex is on the outer face. An outerplane graph is an outerplanar graph
along with such an embedding.
o
An outerplane graph.
(b) Find the best s(n) possible such that any outerplanar graph has a ~-~
separator of size s(n); i.e., such that there exists a partition A, S, B of
the vertices with IAI, IBI ~ 2;,
lSI ~ s(n), and there are no edges be-
tween A and B. Give a linear time algorithm for finding the separator.
(Hint. Use Miscellaneous Exercise 2.)
27. Assuming that comparisons between data elements take one unit of time
on one processor, give parallel sorting algorithms that run on a CREW
PRAM in
(a) time O(logn) with O(n 2) processors
(b) time O((logn)2) with O(n) processors
where n is the number of inputs. Your algorithm should produce an array
of length n containing the input data in sorted order.
28. An n x n matrix is called a circulant matrix if the ith row is obtained from
the first row by a right rotation of i positions, 0 ~ i ~ n - 1. For example,
[ ~ ! ~ ~l
c dab
bed a
is a 4 x 4 circulant matrix.
(a) Find an algorithm to multiply two n x n circulant matrices in time
O(logn) with O(n 2) processors.
(b) Assuming that the field contains all nth roots of unity and a multi-
plicative inverse of n, show that the processor bound in part (a) can
be reduced to O(n). Represent circulant matrices by their first row.
(Hint. See [3, pp. 256-257].)
(c) Under the assumptions of (b), find an algorithm to invert a nonsingu-
lar circulant matrix in O(logn) time with O(n) processors.
29. In Lecture 40 we gave an RNC algorithm to test for the existence of a
perfect matching in a given bipartite graph. In this exercise we extend this
technique to arbitrary undirected graphs.
The Thtte matrix of an undirected graph G = (V, E) is an n x n matrix T
with rows and columns indexed by V such that
Xuv, if (u,v) E E and u < v
Tuv = { -X vu ,if(u,v)EEandu>v
0, if(u,v)¢E
236 MISCELLANEOUS EXERCISES
Homework 1 Solutions
1. (a) Suppose I and J are maximal independent subsets of A, but III < IJI.
By property (ii) of matroids, we can find an x E J - I such that
I U {x} E I. But then I was not a maximal independent subset of A;
this is a contradiction.
Incidentally, it can be shown that for systems (S,I) satisfying axiom
(i) of matroids that the property we have just proved is equivalent to
axiom (ii) of matroids.
(b) The set I U {x} is dependent, since I is a maximal independent set.
It therefore contains a minimal dependent set D. We show that D is
unique. First, xED, since any subset of I is independent. Suppose
there were two such cycles D and D'; assume without loss of generality
that y E D - D'. The set (D U D') - {x} is a subset of I and is
therefore independent. The set D - {y} is also independent, since D
is minimal, and its cardinality is no more than that of (D U D') - {x}.
By property (ii) of matroids, elements of (DUD') - {x} can be added to
D-{y} until the cardinality is the same as (DUD')-{x}, maintaining
independence. Then y is not in the resulting set, since then D would
be a subset, and D is dependent. Therefore the resulting set must
be (D U D') - {y}. But this set contains D' as a subset, which is
dependent; this is a contradiction.
(c) Property (i) holds trivially, since removing an edge from a graph can-
not add any cycles. To show (ii), let E', E" ~ E such that (V, E') and
(V, E") have no cycles. Let d and d' be the number of connected com-
ponentsof (V,E') and (V, E"), respectively, m' = IE'I, mil = IE"I, and
n = IVI. Suppose that m' < mil. By the equation m+c = n proved in
class, d > d'. Then there must be an edge of E" between two distinct
connected components of (V, E'), otherwise all components of (V, E")
would be contained in components of (V, E'), implying that d' ~ d.
Adding that edge to E' cannot give a cycle.
The rank is n - c and the maximal independent sets are the spanning
forests.
Two good references about matroids in optimization problems are [85, 70].
An excellent reference on matroid theory itself is [107].
2. (a) Since the graph is acyclic, all directed paths are of length at most
n -1. Define
For any (x, y) E E H , the only E-path from x to y is the edge (x, y)
itself; if there were a longer path, then (x,y) would never lie on any
maximal length path and would not be in E H. Thus
E* I V E V E2 V ... V E n- 1 V En V ...
I V E V E2 V ... V E n - 1 (why?)
(I V E)n-l .
(a) Suppose the graph is connected and each vertex is of even degree.
Starting from an arbitrary vertex v, trace an arbitrary path, traversing
edges at most once (mark each edge as traversed as we encounter it),
until we return to v. This must happen eventually: because each
vertex is of even degree, is impossible to get stuck. Now delete the
cycle we have found. Deleting a cycle maintains the invariant that all
vertices are of even degree, but the graph may no longer be connected.
HOMEWORK 1 SOLUTIONS 241
Homework 2 Solutions
1. As shown in Theorem 4.8, a directed graph is a dag iff the DFS tree has no
back edges. It was also shown that if the DFS tree is numbered in postorder,
then all edges go from higher numbered vertices to lower numbered vertices.
Then this numbering gives a topological sort.
2. (a) The graph G has an embedding consistent with (J iff this is true for
every connected component of G, so we can assume without loss of
generality that G is connected.
Assume the adjacency lists are ordered according to (J. Then a depth-
first search corresponds to traversing the edges clockwise around each
face. All back edges can be drawn going up the right side of the path
from the root down to the source of the back edge. The very first back
edge encountered can be taken to be on the outer face. We need only
maintain a stack of back edges that we are "inside", and make sure
that any new back edge does not go up higher than the destination
vertex of the innermost back edge that we are inside. The back edges
that we are inside form a chain.
In Lecture 14, Theorem 14.6 and Miscellaneous Exercise 11 we will
see another approach using Euler's Theorem, which states that n +
n* - m = 2 for a connected plane graph G, where n, n*, m are the
number of vertices, faces, and edges of G, respectively. We will use the
theorem in the opposite direction: given (J, we calculate the number
n* of "faces". We will show that n + n* - m = 2 iff (J does indeed
correspond to a plane embedding with n* faces.
(b) If G is not planar then it has no planar embeddings. Otherwise, G
has exactly one embedding on the sphere consistent with (J. We can
choose the North Pole to be in anyone of the faces of the embedding
and then project the graph onto the plane from the North Pole. Each
choice of face for the pole gives a different embedding in the plane.
Thus there are as many different embeddings as faces of G.
Suppose now that G is not connected. Number the connected com-
ponents 0,1, ... , c -1 and let Ii be the number of faces of component
i. There are Ii ways to choose the outer face of the component i, so
there are loft·· . Ic-l ways to choose all the outer faces. To get the
number of embeddings, we must multiply this by the number of ways
to place the components inside the inner faces of other components.
For example, when c = 2, there are ft - 1 ways to place component 0
inside an inner face of component 1, 10 - 1 ways to place component
1 inside an inner face of component 0, and one way to place the
components so that neither occurs inside the other. This gives
(fo - 1) + (ft - 1) + 1 .
HOMEWORK 2 SOLUTIONS 243
interesting vertices (the body, tail, or sting); once we have done that, we
can locate all the other interesting vertices with 3n probes and check that
the graph is a scorpion. For example, if we have found a vertex v with
d( v) = n - 2, then that vertex must be the body if the graph is a scorpion.
By scanning the v th row of the matrix, we can check that d( v) = n - 2 and
determine its unique non-neighbor u, which must be the sting if the graph
is a scorpion. Then by scanning the uth row, we can verify that d(u) = 1
and find its unique neighbor w, which must be the tail; and with n more
probes we can verify that d( w) = 2.
We start with an arbitrary vertex v, and scan the vth row. If d(v) = 0 or
n -1, the graph is not a scorpion. If d(v) = 1, 2, or n - 2, then either v is
interesting itself or one of its 1 or 2 neighbors is, and we can determine all
the interesting vertices as above and check whether the graph is a scorpion
with at most 4n additional probes.
Otherwise, 3 :5 d( v) :5 n - 3, and v is boring. Let B be the set of neighbors
of v and let S = V - (B U {v}). The body must be in B and the sting
and tail must be in S. Choose arbitrary x E Band yES and repeat
the following: if x and y are connected, then delete y from S (y cannot
be the sting) and choose a new yES. If x and y are not connected, then
delete x from B (x is not the body unless y is the sting) and choose a new
x E B. If the graph is indeed a scorpion, then when this process ends, B
will be empty and y will be the sting. To see this, observe that B cannot
be emptied without encountering the sting, because the body cannot be
deleted from B by any vertex in S except the sting; and once the sting is
encountered, all remaining elements of B will be deleted.
Whether or not the graph is a scorpion, the loop terminates after at most
n probes of the adjacency matrix, since after each probe some vertex is
discarded.
If a property is such that we have to look at every entry in the adjacency
matrix, then that property is said to be evasive. Many monotone graph
properties have been conjectured to be evasive. Yao [110] has shown that all
monotone bipartite graph properties are evasive if we are given a bipartite
adjacency matrix representation. The question for general graphs remains
open. BolloMs discusses this issue in his book [13]. He gives a 6n-probe
solution to the scorpion problem there.
HOMEWORK 3 SOLUTIONS 245
Homework 3 Solutions
1. The structure (RegE, U,·, *,0, {t:}) is a Kleene algebra if it satisfies the
axioms of Kleene algebra. We therefore need to show, for any regular sets
A,B and G,
Au (BUG) (AUB) uG
AUB BuA
AuA A
Au0 0uA = A
A· (B·G) (A· B)· G
A· {t:} {t:}·A = A
0·A A·0 = 0
A· (BUG) A·BuA·G
(BUG) ·A B·AuG·A
A·B*·G supA· Bn·G. (3)
n~O
Bn = {YIY2··· Yn I Yi E B, 1 ~ i ~ n} .
To be in the regular set on the left side of equation (3), a string must be
of the form XYIY2 ... YnZ for some n ~ 0, where x E A, Z E G, and Yi E B,
1 ~ i ~ n. Here we allow the possibility n = 0, in which case the string
would be of the form xz. Thus the left side of (3) is equal to the set
This is the least upper bound of the sets A· Bn. G, n ~ 0, with respect to
set inclusion ~: it is an upper bound, since it includes all the A . Bn . G
as subsets; and it is the least upper bound, since any set that includes all
the A . Bn . G must include their union.
246 HOMEWORK 3 SOLUTIONS
The case of 1 is similar, since R(l) = {fl. Finally, since R(O) = 0 and
since the element OK: is the least element in K and therefore the supremum
of the empty set,
sup I(ax,) sup 0
XER(O)
There are three cases to the inductive step, one for each of the operators
+, ., *. We give a step-by-step argument for the case +, followed by a
justification of each step.
I(a)· (I((31) + I((32))' I(r) (4)
(I(a) . I((31)' I{-y)) + (I(a) . I((32) . I(r)) (5)
I(a(3n) + I(a(32,) (6)
sup I(ax,) + sup I(an) (7)
XER(th) YER(~2)
Equation (4) follows from the properties of the map I; (5) follows from the
distributive laws of Kleene algebra satisfied by K; (6) again follows from
the properties of the map I; (7) follows from the induction hypothesis on
(31 and (32; (8) follows from the general property of Kleene algebras that
if A and B are two sets whose suprema sup A and sup B exist, then the
supremum of Au B exists and is equal to sup A + supB (this requires
proof-see beloW); finally, equation (9) follows from the definition of the
map R interpreting regular expressions as regular sets.
The general property used in equation (8) states that if A and B are two
subsets of a Kleene algebra whose suprema sup A and sup B exist, then the
supremum sup A U B of A U B exists and
supAUB = supA+supB.
HOMEWORK 3 SOLUTIONS 247
(i) supA+supB is an upper bound for AUB; that is, for any x E AUB,
x :s; sup A + sup B; and
(ii) sup A + sup B is the least such upper bound; that is, for any other
upper bound y of the set A U B, sup A + sup B :s; y.
To show (i),
x E Au B -+ x E A or x E B
-+ x:S; sup A or x :s; supB
-+ x:S; sup A + sup B .
We give a similar chain of equalities for the case of the operator ., but omit
the justifications.
sup I(ax,)
n::O:O, xER({3n)
248 HOMEWORK 3 SOLUTIONS
sup I(ax'Y)
XEU,,~O R(P")
SUp I(ax'Y).
XER(P*)
X:= {s}j
P(s) := nilj
for each v E V - {s} do
if (s, v) E E then
P(v):= Sj
D(v):= l(s,v)
else
P(v) := 1..j
D(v) := 00
end if
end forj
while X =J V do
let u E V - X such that D(u) is a minimumj
X:=XU{u}j
for each edge (u, v) with v E V - X do
if D(u) +l(u,v) < D(v) then
P(v) := Uj
D(v):= D(u) +l(u,v)
end if
end for
end while
250 HOMEWORK 4 SOLUTIONS
Homework 4 Solutions
1. (a) The algorithm is correct because each step is an instance of the blue
rule in the spanning tree matroid.
(b) Prim's algorithm is a variant of Dijkstra's shortest-path algorithm.
The implementation is very similar to the solution to Exercise 3 of
Homework 3. We maintain a Fibonacci heap containing the set V -T,
where V is the set of all vertices and T is the set of vertices in the
portion of the spanning tree chosen up to that point. The value D(v)
for the purposes of findmin, deletemin and decrement is the weight
of the minimum-weight edge connecting v to a vertex in T, or 00 if no
such edge exists. In addition, we maintain with each vertex v in the
heap a pointer P( v) to a vertex in T closest to v; i. e., a u E T such
that d(u,v) = D(v).
We initialize the values D(v) and P( v) after the first vertex u is chosen
by setting D(v) := d(u,v) and P(v) := u for each vertex v adjacent
to u and D(v) := 00 and P(v) := nil for each vertex v not adjacent
to u.
At each step, we find the element w of the heap such that D(w) is
minimum, delete it from the heap, and add it to T. We also add the
edge (w, P( w)) to the spanning tree. Then we update distances as
follows: for each edge (w,v), v rt T, if d(w,v) is less than D(v), we
set D(v) := dew, v) and P(v) := w.
It takes constant time for each edge or Oem) time in all to do the
updates. Using the Fibonacci heap, it takes O(nlogn) time amor-
tized over the entire sequence of operations to maintain the heap, find
the minima and delete them, and decrement the values D(v) when
necessary.
010
and 181 must be at least as big as the positive root of this quadratic:
-1+~
181 > 2
-1+/¥
> 2
k 1
y'3 - 2
252 HOMEWORK 5 SOLUTIONS
Homework 5 Solutions
1. (a) For every edge (u,v) in G, compute the residual capacity from u to v
using the formula r( u, v) = c(u, v) - 1(u, v), where c is the capacity
function. Also compute the residual capacity r( v, u) in the opposite
direction. If r(u, v) is zero, then do not put the edge (u, v) into G f .
Otherwise, put the edge (u, v) into G f with capacity r( u, v). Do the
same for (v, u). All the relevant computations can be done in O(m)
time since we spend at most constant time for each edge.
(b) A minor modification of the algorithm of Exercise 3, Homework 3 does
it. The only difference is that we are seeking paths of maximum bot-
tleneck capacity instead of minimum length. Here is the algorithm:
X:= {s};
P(s) := nil;
for each x E V - {s} do
D(x) := r(s,x);
P(x):= s
end for;
while X i=- V do
let x E V - X such that D(x) is a maximum;
X:=XU{x};
for each edge (x, y) E E f such that y E V - X do
ifmin(D(x),r(x,y)) > D(y) then
P(y):= x;
D(y) := min(D(x), r(x, y))
end if
end for
end while
2. Let every edge in G have unit capacity and find a maximum flow 1* in G.
Then there exist k edge-disjoint paths from s to t if and only if 11* 12': k:
certainly if there exist k edge-disjoint paths, then 11* 1 2': k by pushing
one unit of flow along each path. Conversely, if 11*1 2': k, then we can
repeatedly find a path flow from s to t with unit flow as in the proof of
Lemma 17.4, then remove the edges along this path and repeat. With each
iteration the flow value decreases by one, so at least k paths are found.
3. Make G a directed graph by replacing each undirected edge {u, v} with two
directed edges (u, v) and (v, u). The capacities on these directed edges will
be the same as those on the corresponding original edge. Find a maximum
flow from s to t in this network and compute the residual graph of this
flow. Take A to be the set of vertices reachable from s in the residual
graph, and let B be the rest. By the Max Flow-Min Cut Theorem, this
HOMEWORK 5 SOLUTIONS 253
cut has minimum weight in the network. It is also a minimum cut for the
original undirected graph because any edge between A and B has the same
weight in both graphs.
254 HOMEWORK 6 SOLUTIONS
Homework 6 Solutions
1. Gale and Shapley were the first to investigate the stable marriage problem
and gave this solution in 1962 [36].
Let rg(b) be g's ranking of b, rb(g) be b's ranking of g. Let B be the set of
boys and G be the set of girls, n = IBI = IGI. If M is a partial matching,
let BM = {b 13g (b,g) E M} and G M = {g 13b (b,g) EM}. Let M(b) = 9
if (b,g) E M, undefined if no such 9 exists. Let M(g) = b if (b,g) E M,
undefined if no such b exists. Execute the following program:
M:=0j
whileIMI < n do
9 := arbitrary element of G - GM ;
b:= element of B with rg(b) maximum and either
(i) b ¢ BM
(ii) bE BM and rb(g) > rb(M(b));
if (i), set M:= M U {(b,g)};
if (ii), set M := (M - {(b, M(b)}) U {(b, g)}
end while
• M is stable;
• if rg(b) ~ rg(M(g)) then b E B M .
The algorithm halts in O(n 2 ) iterations of the while loop because the
quantity
{(s, u) I u E U} U E U {(v, t) I v E V} .
(TnU) u (snv) ,
so this set forms a vertex cover of G. Moreover, the capacity of the cut
S, T is the size of this set, which is also the maximum flow value in H by
the Max Flow-Min Cut Theorem. In Lecture 18 we argued that the size of
the maximum matching in G is the value of a maximum flow in H.
IN(S)I < IV n CI
ICI-IU n CI since U and V are disjoint
< IUI-iunci
lSI since S is defined to be U - C.
d· lUI = d· IVI ,
thus lUI = IVI; therefore any matching that uses all the vertices in U will
be a perfect matching in G. By Hall's Theorem, it suffices to show that
lSI:::; IN(S)I for any S ~ U. For an arbitrary subset S of U, consider the
subgraph of G induced by S U N(S). There are exactly d ·ISI edges in this
256 HOMEWORK 6 SOLUTIONS
Homework 7 Solutions
1. (a) We reduce the general CNFSat problem to the restricted problem in
which variables are allowed at most three occurrences to show that
the restricted problem is NP-hard. Given the CNF formula B, let x
be a variable with exactly m occurrences in B. Let xI, X2, ••. ,Xm be
m new variables. Replace the ith occurrence of x with Xi and append
the CNF formula
This will force all the Xi to have the same truth value in any satisfying
assignment, and there are exactly three occurrences of each Xi. Do
this for all variables in the original formula.
The restricted problem is in NP, since it is a special case of the unre-
stricted CNFSat problem. Therefore it is NP-complete.
(b) If a variable X appears only positively (only negatively), then we can
satisfy the clauses containing x by assigning x the value true (false),
thus we might as well eliminate those clauses. The new formula is
satisfiable iff the original one is.
Suppose then that the variable x appears both positively and nega-
tively. Since there are only two occurrences of x, there is one of each.
If the occurrences are in the same clause, then that clause is true un-
der any truth assignment, and we can eliminate it. If not, and if the
two clauses contain no other variables, then we have reached a con-
tradiction x 1\ oX and there is no satisfying assignment. Otherwise,
we apply the resolution rule of propositional logic: we combine the
two clauses containing x, but throw out x itself. For example, if one
clause is x V y V z and the other is oX V u V v, then the new clause is
y V z V u V v. Again, the new formula is satisfiable iff the original one
was.
We continue applying these rules until we see a contradiction x 1\ oX
or we eliminate all of the variables. In the latter case, the formula is
satisfiable.
The resolution rule is a widely used proof procedure for propositional
logic. It is known to require exponential time in the worst case when
there are no restrictions on the number of occurrences of variables
[47,101]. Resolution, suitably extended to handle first-order formulas,
forms the basis for many PROLOG implementations.
258 HOMEWORK 7 SOLUTIONS
v v
We give each ofthe edges in Dk weight w(e)/2. Any tour in the new graph
gives rise to a tour in the old graph of the same weight. Conversely, any
tour in the old graph that traverses e at least k times gives rise to a tour
in the new graph of the same weight. Thus the weight of an optimal tour
in the new graph is at least w*, and strictly greater than w* if all optimal
tours in the old graph traverse e fewer than k times. Since no optimal tour
of the old graph traverses e more than n times, if k > n then the weight of
the optimal tour in the new graph must be strictly greater than w*.
Now, for each edge e in turn, we replace e with D k , where k is as large as
possible such that there still exists a tour of weight w*. We discover k by
binary search in the interval 0 ~ k ~ n. When we are done, there is a tour
of weight w* in the resulting graph that traverses each edge exactly once (if
not, some Dk could have been replaced by Dk+l). We find an Euler circuit
in this graph, which will give rise to a TSP tour in the original graph of
optimum weight w*.
Let 0 be the set of clauses, let X be the set of variables, and let L be the
set of occurrences of literals in B. Let the set of vertices be
and take k = 101 + IXI. For CEO, connect all occurrences of literals in C
to Se and te' For x EX, connect all the positive occurrences of x in B in
a single path, connect the negative occurrences of x in a single path, and
connect one endpoint of each of these two paths to s'" and the other to t",.
For example, consider the formula
B = (x V fj V z) 1\ (x V y V z) 1\ (x V y V z)
'----v----' '----v----' '----v----'
C d e
Sy.t ~
.t ~
'-.,.-1
"...... "......
If B has a satisfying assignment, then for each CEO, take the path from
Se to te of length two through some true literal of c, and for each x EX,
take the path from s'" to t", through the false literals. These k paths are
vertex-disjoint. Conversely, suppose there are k vertex-disjoint paths. The
path from s'" to t"" x E X, must go through either all and only the positive
occurrences of x or all and only the negative occurrences of x; any deviation
would necessarily go through some Se or te' Assign x true if the path from
s'" to t", goes through the negative occurrences of x and false if it goes
through the positive occurrences of x. Since the paths are vertex-disjoint,
the paths from Se to t e , CEO, go through only true literals. Thus the
truth assignment satisfies all clauses.
The problem is in NP since the disjoint paths can be guessed and verified
in polynomial time.
260 HOMEWORK 8 SOLUTIONS
Homework 8 Solutions
1. This problem is in coNP because its complement is in NP: we can guess
a value for each variable and verify in polynomial time that the given
expression does not vanish. We show coNP-hardness by showing that its
complement is NP-hard using a reduction from CNFSat. Given a Boolean
formula, we transform it into an algebraic expression that vanishes mod
p for all variable assignments iff the original formula is unsatisfiable. We
simulate the Boolean values true and false with 1 and 0 in Zp, respectively.
If x is a variable of the Boolean formula, replace it with xp - 1 . Replace each
expression -,A by 1 - A, A /\ B by AB, and A V B by A + B - AB.
2. (a) The restricted problem remains in NP, so the difficult part is to show
NP-hardness. We do this by reducing the undirected Hamiltonian
circuit problem to TSP with the triangle inequality. Given an undi-
rected graph G = (V, E), we will construct a symmetric distance
function d : V x V --t N satisfying the triangle inequality such that
G has a Hamiltonian circuit iff it has a TSP tour of length n. Let
d(u v)
,
= {I2 E
if (u,v) E,
if(u,v)¢E.
Then d obeys the triangle inequality trivially. A TSP tour in G that
has length n must contain only edges of length 1. Thus it corresponds
directly to a Hamiltonian tour in G.
(b) Actually, it is possible to find in polynomial time a tour that is no
worse than 3/2 times the optimal tour by using an algorithm known
as Christofides 'heuristic. Our solution will only be guaranteed to be
no more than twice the optimal solution, but it is important to un-
derstand how our solution works before looking at Christofides' algo-
rithm, which uses matching. Papadimitriou and Steiglitz [85] discuss
Christofides' heuristic for those interested in finding out more about
it.
We will assume that distances are symmetric. First, we find a mini-
mum spanning tree T. Next, we create a directed graph G by using
two copies of each edge of T, one in each direction. Finally, we find
an Euler circuit in G, which gives a TSP tour of weight twice that of
the minimum spanning tree. Since every TSP tour contains a span-
ning tree of the original graph, the length of any TSP tour is at least
as large as the weight of the minimum spanning tree. Therefore, the
length of our Euler circuit is no more than twice the length of the
optimal TSP tour.
Since the triangle inequality holds, we can convert the Euler circuit
to a TSP tour of no greater weight in which vertices are visited only
once: we merely skip over vertices previously visited.
HOMEWORK 8 SOLUTIONS 261
Homework 9 Solutions
1. We will use the concurrent-read exclusive-write (CREW) PRAM model
with unit cost for integer operations and comparisons.
Represent the dag G as an adjacency matrix A. Compute its reflexive
transitive closure G* either by computing A* = (I V A)n in NC using
parallel prefix, or more efficiently, using the relationship between matrix
multiplication and transitive closure discussed in Lecture 5. Then sort the
vertices by indegree in G* using the algorithm of Miscellaneous Exercise
27. This gives a topological sort, since if there is a path from u to v in G,
then vertex u has smaller indegree than v in G*.
2. If G has an odd cycle, then it must have an odd simple cycle (one with no
repeated vertices), because any odd cycle that is not simple is composed
of two smaller cycles, one of which must be odd. Therefore, to check for
an odd cycle, we need only check for a path of odd length at most n from
a vertex back to itself. The kth power of the adjacency matrix A of G tells
us the paths of length k in G. Using parallel prefix, we can compute all
the odd powers of A up to n in NC and see if any of them contain a 1 on
the main diagonal.
This algorithm is NC, but it is not very efficient in its use of processors.
We can save a factor of n processors by observing that we only have to
check the diagonal of Ak for some odd k greater than n - 1. If there is an
odd cycle of shorter length, we can extend it to one of length k by retracing
an edge backwards and forwards. Thus we can use matrix powering instead
of parallel prefix.
Another approach would compute the * of a matrix A over a Kleene algebra
consisting of four elements 1., 0, 1, and T, where the operations + and·
are given by
I + Ill. I 0 I 1 I T I I " 1. I 0 I 1 I T I
1. 1. 01 T 1. 1. 1. 1. 1.
0 0 0 T T 0 1. 0 1 T
1 1 T 1 T 1 1. 1 0 T
T T T T T T 1. T T T
We set
if i = j
if (i,j) E E
otherwise.
both (then the graph is not bipartite); and 1- means i and j are in different
connected components.
Here is a method due to Shiloach and Vishkin [93] that is much more
efficient in its use of processors. First, find a spanning tree of G. This can
be done in O(logn) time using O(n + m) processors on a CRCW PRAM.
Assign a parity 0 or 1 to each vertex according to its distance from the
root; this too can be done using O(n + m) processors in parallel, using
the technique of pointer doubling (see Miscellaneous Exercise 25). Finally,
check every edge of G to make sure that an edge does not join two vertices
of the same color.
3. (a) The system is described by the following matrix-vector equation, il-
lustrated here for the case k = 5.
0 1 0 0 0 0 Xn-5 Xn -4
0 0 1 0 0 0 Xn -4 Xn -3
0 0 0 1 0 0 Xn-3 Xn -2
0 0 0 0 1 0 Xn-2
= Xn-l
(10)
a5 a4 a3 a2 al 1 Xn-l Xn
0 0 0 0 0 1 C C
Xo Co
Xl CI
X2 C2
(11)
X3 C3
X4 C4
C C
where
81 (b-d)·(g+h)
82 (a + d) . (e + h)
83 (a - c) . (e + f)
84 h·(a+b)
85 a· (f - h)
86 d·(g-e)
87 e· (c + d) .
We can first compute the quantities b - d, g + h, a + d, e + h, a - c,
e + f, a + b, f - h, g - e, and c + d in parallel, then compute 81, ... , 87
in parallel from these, and finally the four entries of the product from
the 8i in parallel.
Now we apply this technique inductively. Given a pair of k x k ma-
trices, we wish to build an NC circuit to compute their product. We
break each matrix up into four submatrices of size roughly ~ x ~, and
assuming that we have already constructed circuits to compute the
sum and product of ~ x ~ matrices, we can use those circuits in the
calculation of the k x k product exactly as in the 2 x 2 case described
above.
Let P(k) and T(k) be, respectively, the number of processors (size
of the circuit) and the time (depth of the circuit) necessary to to
multiply two k x k matrices by this method. These quantities satisfy
the recurrences
x(y) = LXiyi
i=O
Homework 10 Solutions
1. (a) As shown in Lecture 36, the expected value of the random variable
£(Xn+l I Sn) is
£SO 0
£Sn+l £(Sn + Xn+l)
£Sn +£Xn+1
£Sn + £(£(Xn+l I Sn))
> £Sn + £(E(m - Sn))
Em + (1 - E)£Sn
whose solution gives
L £f(Sn)
00
£R
n=O
i-I
L 1 + L m(l -
00
< E)n
n=O n=i
L (l- Et
00
l+m(l-E)i
n=O
m i
l+ -(1- E) .
10
Taking
m - log 1
rlOg
-log(l - E)
10
2. Let au = IAul. It will suffice to show that for any subset B of Zp of size
k '5: d,
Ax = z
where A is a k x d submatrix of a d x d Vandermonde consisting of all rows
(1 , u, 2
U, •.. , U
d-l)
, uEB.
E
pd-k
1
pel zuEAu1 uEB
1
kIIau .
p uEB
3. The solution to this problem is very similar to the analysis of Luby's algo-
rithm given in class. Recall from there that a vertex is good if
1 1
E -
UEN(v) d(u)
>-.
- 3
~< E_l_<~
3 - UEM(v) d(u) - 3
Then
Pr(v E U)
> Pr(3u E M(v) v = t(u))
> E
Pr(v = t(u)) - E Pr(v = t(u) 1\ v = t(w))
UEM(v) u,w E M(v)
U~W
(by inclusion-exclusion)
> E Pr(v = t(u)) - E Pr(v = t(u)) . Pr(v = t(w))
UEM(v) u,w E M(v)
U~W
HOMEWORK 10 SOLUTIONS 271
• u• v• w• •
There are eight possibilities for the choices of favorites of u, v, w, all equally
likely. Of these, four give matchings for v. Thus
1
Pr(v is matched I v E U) ;::: "2'
D
E = (J U (8 - J)) - I .
2. Determine for each e E E the sizes of the two connected components ob-
tained by deleting e. This can be done in linear time by depth-first search,
computing the values recursively. Orient the edge e in the direction of the
smaller component. The resulting directed graph is a directed tree, since
no vertex has indegree greater than 1: if (8, u) and (t, u) were both oriented
toward u, and if 181, ITI, and lUI were the subtrees pictured,
then
3. There are several reasonable approaches to this problem. Here is one in-
volving Kleene algebra. Consider the structure
and define * by
a* = {O,-00,
~f a ~
If
0,
a < O.
A routine check of the axioms verifies that this structure is a Kleene algebra.
Let G be a directed graph with n vertices and real edge weights w, possibly
negative. Form the n x n matrix A with Auv = w(u, v). If there is no edge
(u, v), set Auv = 00. As shown in Lecture 7, the family ofnxn matrices over
K is again a Kleene algebra, and we can calculate A * in time proportional
to the time needed to multiply two n x n matrices over K.
We claim that A~v is the weight of the minimum-weight path from u to v,
or -00 if there exists a path from u to v but no path is of minimum weight.
As with the min, + algebra discussed in Example 6.6, it can be argued by
274 SOLUTIONS TO MISCELLANEOUS EXERCISES
induction that A:v gives the weight of the minimum-weight path of length
k from u to v. Since A * = infk A k, if there exists a minimum-weight path
from u to v, and that path is of length k, then A:v = A:v.
It remains to argue that if there exists a path from u to v but no minimum-
weight path, then the weights of paths between u and v are unbounded be-
low, so that A:v = -00. This argument is necessary, since it is conceivable
that the weights of the paths from u to v approach some real lower limit
without ever achieving it. We show that this cannot happen: if there is a
path from u to v but no minimum-weight path, then there is a path from
u to v that traverses some cycle of strictly negative weight, which can be
traversed arbitrarily many times.
Under our assumption, there exists an infinite sequence Po, PI. ... of paths
from u ~to v such that Pi is a shortest (in terms of number of edges) path
of weight strictly less than that of PO,Pl, ... ,Pi-l. The number of edges in
these paths is unbounded, since for each k there are only finitely many paths
with k edges. Let Pi be the first path in the list with at least n edges; then
some vertex x must be repeated on Pi. The cycle that is traversed between
the two occurrences of x on Pi must be of strictly negative weight, otherwise
that cycle could be cut out of Pi to give a path of fewer edges and weight
strictly less than that of Po, Pl, ... ,Pi-l, contradicting the minimality of Pi.
Another approach to this problem is to identify the vertices contained in
negative-weight cycles and treat them separately. The solution to Miscella-
neous Exercise 17(a) would presumably be useful in this regard. See [100]
for more details about this approach.
4. (a) Call a schedule for a set of jobs A safe if no deadlines are violated. If
A has a safe schedule, then so does any subset: simply delete the jobs
not in the subset.
Let A and B be two independent sets with IAI < IBI. Consider sep-
arate safe schedules for A and B. Assume without loss of generality
that jobs are scheduled as early as possible with no gaps in the sched-
ules. Let j be the job occurring latest in the schedule for B that is
not in A. Let C be the set of jobs occurring after j in the schedule for
B. Then C ~ A. Consider the following schedule for AU {j}: first
schedule all the jobs in A - C in the same order as in the schedule
for A, then schedule j, then schedule C in the same order as in the
schedule for B. This schedule is safe, since all elements of A - Care
scheduled no later than they were in A, and all elements in {j} U C
are scheduled no later than they were in B.
(b) We use the greedy algorithm with the jobs sorted by penalty in de-
creasing order. The greedy algorithm produces a maximal indepen-
dent set of maximum weight; these jobs can be scheduled safely. The
remaining jobs are all scheduled after their deadlines and incur a
SOLUTIONS TO MISCELLANEOUS EXERCISES 275
o (16)
J.LA(d + 1) (17)
< J.LA(d) + 1 . (18)
J.L7/(k) = {d I J.LA(d) = k}
are intervals (contiguous sequences of natural numbers), and if the set
J.L7/(k) is nonempty, then J.Li(i) is nonempty for any 0:::; i :::; k.
We will use the union-find data structure to maintain the disjoint sets
J.LA1(k). Consider a new job j ¢ A with deadline dj • Then Au {j}
is independent iff J.LA(dj ) > 0 iff dj ¢ J.LA1(O). Also, if Au {j} is
independent, then
thus
6. The problem is in NP, because we can guess a folding and compute its
length in polynomial time. To show NP-hardness, we reduce the partition
problem to it. Given an instance of the partition problem consisting of
the weight function w : {l, 2, ... , n} ~ N, construct a ruler with n + 4
segments of length (in order)
N N
N, 2' w(l), w(2), ... , w(n), 2' N
where N is very large (actually N ;::: Ei=l w(i) suffices), and let k = N.
In order to fit, the endpoints of the two end segments of length N must
line up vertically, and the two segments next to them of length ~ must be
folded back in. Thus we will get a fit if and only if the remainder of the
ruler can be folded so that the inner endpoints of the ~ segments line up
vertically.
0
: ooJ :
0
•
N
.
SOLUTIONS TO MISCELLANEOUS EXERCISES 277
This can occur if and only if there exists a subset S C {l, 2, ... ,n} such
that
Lw(i) = LW(i);
iES i~S
the sets S and {I, 2, ... ,n} - S correspond to the segments in the ruler
pointing left and right, respectively.
(19)
with
(20)
where y is a new variable. The two formulas are equisatisfiable, and the
number of satisfying truth assignments is the same, because the truth as-
signment to y is forced by the truth assignment to Xl and X2. The rightmost
clause of (20) has one fewer literal than (19).
Next, replace the new clause
This procedure gives a formula with at most three literals per clause.
It is possible to get exactly three distinct literals per clause as follows. Let
X, y, and z be three new variables. Replace each deficient clause (u V v)
with (u V v V x) and each deficient clause (u) with (u V x V y), and add
the clauses (x), (y) and (z). The number of satisfying assignments is the
same, since the new variables have to be true in any satisfying assignment.
All clauses have exactly three literals except the three new ones. It now
suffices to show how to express the conjunction X y z in 3CNF. But
~~y~ - xy~VxyzVxy~VxyzVxy~VxyzVxy~,
list adj(u) of edges e such that t(e) = u. By "Given e, ... " we will mean,
"Given the list element for e on the list adj(t(e)), ... " The order on the
circular list adj(u) will give the order (). The edge e will point to ()(e),
t(e) and e; that is to say, the list element for e on adj(t(e)) will contain
pointers to the list element for () (e) on adj (t ( e)), the list element for t ( e )
on V, and the list element for e on adj(t(e)) = adj(h(e)).
Given e, in constant time we can compute ()(e), e or t(e) by following a
single pointer; and we can compute h( e) by following a pointer to e and
then following the pointer there to t(e) = h(e). We can delete a given
edge e in constant time by unlinking its list element from adj(t(e)) and
unlinking the list element of e from adj (h( e)). We can delete a vertex u by
first deleting all the edges on adj (u) as above and then unlinking u from
V.
We now show how to obtain the new representation from the old one in
linear time. The old representation consists essentially of the list V, for
each u on V a pointer to an adjacency list adj(u) containing all edges e
with t(e) = u, and for each e on adj(t(e)) a pointer to h(e) on V. To
calculate the pointers from e to t(e) in linear time, for each u scan the list
adj (u) and append to each list element a pointer back to u.
It remains to calculate the pointers from e to e. First produce for each
vertex u an auxiliary adjacency list aux(u) with a list element for each e
with t(e) = u and a pointer to e. The lists aux(u) will contain the desired
pointers but will not be in the correct order. The lists aux(u) are produced
in linear time by initializing all aux(u) to the empty list, then scanning
through all the lists adj (u) and for each e encountered appending a pointer
to e to the front of the list aux(h( e)).
At this point each u points to two lists, adj(u) and aux(u), each with one
entry for each edge e with t(e) = u. The former list is in the correct order
() and the latter contains the - pointers. We wish to consolidate them into
a single list with both properties. For each u, execute the following two
steps:
(a) Scan the list aux(u). For each e on the list, save the pointer to e in
the list element for h(e) on V. The pointer to h(e) is available as t(e).
(b) Scan the list adj(u). For each e on the list, pick up the pointer to e
from the list element for h(e) on V.
This procedure takes linear time, since each vertex and edge is visited a
constant number of times.
9. Since - and () can be computed in constant time, the permutation ()* = ()o-
can be computed in constant time. Construct a doubly linked list L of
pointers to the list elements of all (directed) edges in the adjacency list
SOLUTIONS TO MISCELLANEOUS EXERCISES 279
10. As in Lecture 14, let nand n* be the number of vertices and faces of
G, respectively; let m = IEI/2, the number of undirected edges; let c
be the number of connected components (orbits of E under the subgroup
generated by () and -); and let ()* = () 0 - . Note that the subgroup
generated by () and - is the same as the subgroup generated by () and ()*,
since - = (}-l 0 ()*.
Write () as a product of disjoint cycles. Multiplying () on the left by an
involution (a b) acts on () as follows: if a and b are in different cycles of
(), then those two cycles are merged, and if a and b are in the same cycle,
then that cycle is split. All cycles of () not containing a or b are left intact.
For example,
(1 5) 0 (1 2 3 4) 0 (5 6 7 8) (1 2 3 4 5 6 7 8)
(1 5) 0 (1 2 3 4 5 6 7 8) (1 2 3 4) 0 (5 6 7 8) .
Here (ao al ... an-l) is the permutation that maps ai to a(i+1)modn' 0 :::;
i < n, and fixes all other elements of E.
Let ()' = (a b) 0 (). In terms of the graphs
G (E, (), -)
G' (E, ()', -) ,
if the edges a and b have a common tail in G (i. e., if a and b are in the
same cycle of ()), then that tail is split into two vertices in G' as shown.
If a and b have different tails (i. e., if a and b are contained in different
cycles of ()), then the tails are merged in G'.
280 SOLUTIONS TO MISCELLANEOUS EXERCISES
Note that
o = 00 0 01 0 ••• OOp_l •
(a b) oO'i(d)
O'i(d) ,
so in this case we can take 1/ = O'i+1. Suppose now that O'i(d) = aj the case
O'i(d) = b is symmetric. Since a and b are in the same cycle of O'i+l, there
exists a k such that O'f+1(b) = a. Let 1/ = O'f-tr Then
The reverse inclusion, which implies that c does not decrease, follows from
a dual argument. (It is the dual argument that uses assumption (iii).)
11. We prove the harder direction (-+) j the direction (+-) follows from a
straightforward induction on the number of faces.
Let O'i and G i be as in the solution to Miscellaneous Exercise 10. In that
solution it was argued that x( Gi+l) ;::: X( Gi ) ;::: 0 for all i. Since G2m - p = G
and X( G) = 0, we have X( Gi ) = 0 for all i.
We proceed by induction on i. For the basis, 0'0 is the identity map and
Go is the graph consisting of m connected components, each consisting of
two vertices and an undirected edge. This certainly has a plane embedding
consistent with 0'0.
Now suppose G i has a plane embedding consistent with O'i. Let (a b) be
the ith transposition, so that O'i+1 = (a b) OO'i. The tails of a and b in G i
are merged in Gi +1. Either
In case (i), the tails of a and b can be merged across the common face
of a and b. It is easily checked that the resulting embedding of Gi+l is
consistent with O'i+1.
In case (ii), both n and n* decrease by one in passing from G i to Gi+l> so
c must also decrease by one to maintain X(Gi+l) = o. Thus a and b are
in different connected components of Gi . By embedding on the sphere and
changing the position of the North Pole, we can arrange the embedding of
Gi so that a and b occur on the outer faces of their components, then merge
the tails of a and b to obtain a plane embedding consistent with O'i+l.
282 SOLUTIONS TO MISCELLANEOUS EXERCISES
12. Instead of maximal cliques, take the complement graph and consider the
maximal independent sets. Given a Boolean formula, construct a graph
with a black vertex for each literal and a red vertex for each clause. Con-
nect complementary literals and connect each clause to the literals it con-
tains. Any maximal independent set M containing no red vertex contains
exactly one of each pair of complementary literals and thus gives a truth
assignment; moreover, it is a satisfying assignment, since every red ver-
tex is connected to a vertex in M (otherwise M would not be maximal).
Conversely, any satisfying assignment gives a black maximal independent
set.
13. Given a partition problem (S, w), where S is the set of items, n = lSI, and
W : S - t Z the weight function, let f be an upper bound on the absolute
values of weights of elements of S. Then for any subset R ~ S,
Thus, although there are 21RI subsets of R, there are only 2iIRI + 1 weights
these subsets can take on.
The following algorithm is quite reasonable if f is sufficiently small. We
calculate recursively the set of all possible weights of subsets of R ~ S.
If R = {a}, the possible weights are 0 and w(a). If IRI ~ 2, partition R
into two disjoint sets Rl, R2 of roughly equal size and recursively produce
the data for RI and R2 . For each pair of weights WI and W2 in the sets
computed for RI and R 2 , respectively, calculate WI + W2 and record it in
the set for R. Since WI is the weight of some subset of RI and W2 is the
weight of some subset of R 2 , WI + W2 is the weight of their union, which is
a subset of R; moreover, every subset of R is obtained as such a union.
Since the weights of subsets of ~ lie between -fl~1 and fl~l, the calcula-
tion of all the weights WI + W2 requires at most O( fIRII· flR 2 1) operations.
This gives rise to the recurrence
with solution
14. Try k = 2 [log nl , the smallest power of 2 greater than or equal to n. Re-
call that in the representation over Z2[X]/Xn , the binary-to-Gray operation
amounts to multiplication by x + 1. We wish to show that (x + l)k = 1
SOLUTIONS TO MISCELLANEOUS EXERCISES 283
and for all 0 $; f < k, (x+ 1)l =f 1; in other words, the order of x+ 1 in the
group of invertible elements of Z2[X]/X n is k. This will suffice to prove our
result, since for any p, applying the binary-to-Gray operation k times gives
(x + 1)kp = p, and for any 0 < f < k, (x + 1)l(x + 1)k-l = 1 =f (x + 1)k-l.
To show that (x + 1)k = 1, note that in Z2[Xl/X n , squaring is a linear
operation:
(x + 1?r10gn l
(... ((x + 1)2)2 ... ?
x2rlOgnl + 1
1,
since 2rlognl 2: n and therefore x2POgnl = 0 mod xn. To show that no
smaller power works, we use the fact that the order of x + 1 must divide
k, therefore it must be a power of 2. But for m < flog n1 we have 2m < n,
therefore
15. (a) Consider the weighted 4-node widget A we used in Lecture 27 with
the following adjacency matrix:
01
[o~1 -1
~
0
-1
o
o
12 0
1
0
The input nodes of the widget are 1 and 3 and the output nodes are 2
and 4. Note that A34 = 1 and everything else in the row and column
containing A34 is O. This says that every cycle cover of nonzero weight
must contain the edge from 3 to 4 of weight 1. We might just as well
collapse 3 and 4 into one node. The resulting widget is
Tl
11
[ 1 1
(21)
2 2
1 -1
with input nodes 1 and 3 and output nodes 2 and 3. The desired be-
havior is summarized by the following equations, analogous to those
presented in the lecture for the four-node widget. As before, A(i;j)
284 SOLUTIONS TO MISCELLANEOUS EXERCISES
Proof.
(-+) If the negative-weight cycle containing u is simple, we are done.
Otherwise there is a repeated vertex x on the cycle, and x is in the
strong component of u. The cycle can be decomposed into two strictly
smaller cycles containing x, at least one of which must have negative
weight. By induction, there is a simple negative-weight cycle contain-
ing an element of the strong component of x, which is also the strong
component of u.
(-) Let x be a vertex in the strong component of u contained in a
simple negative-weight cycle. Combining the cycle through u and x
with sufficiently many trips around the negative-weight cycle through
x, we obtain a negative-weight cycle through u. 0
(i) G is k-connected;
(ii) any pair of vertices is connected by at least k vertex-disjoint paths;
(iii) for any vertex s E S and any vertex v E V, s and v are connected by
at least k vertex-disjoint paths.
Proof. The implication (i) ---* (ii) is given by Menger's Theorem and (ii) ---*
(iii) is trivial.
To show (iii) ---* (i), let U be any set of k -1 vertices. At least one element
s E S is not in U, and for any other vertex v not in U, by (iii) there is a
path from s to v avoiding U. Thus the removal of U does not disconnect
G. 0
before after
The value of an integral max flow from s to t in this graph will be the
maximum number of vertex-disjoint paths from s to t. This follows from
the Max Flow-Min Cut Theorem and the splitting of vertices that prevents
more than one unit of flow to pass through any interior vertex.
However, we do not need to find a max flow, but only to check whether it
is at least k. This can be done by performing k augmenting steps of the
Edmonds-Karp algorithm, calculating new level graphs as necessary. The
time to calculate all the level graphs is at most O(km) and the time to do
all the augmenting steps is at most O(km).
288 SOLUTIONS TO MISCELLANEOUS EXERCISES
22. Let ei be the row vector with a 1 in position i and 0 elsewhere. For
1 :5 i :5 k, the ith row of Cp is the first row of C;; that is,
eiCp = eIC;.
Then for any m ;::: 1, the ith row of C; is the first row of C;+i-l:
eipem eip
c epm - l
e I eie
p P
m- l
eI em +
p
i- l
.
For any row vector x = (Xl, ... ,Xk), xCp can be computed in time O(k):
elc;m = (eIC;)C;.
This takes time O(k2) and gives the first row of C'j,m. Compute the remain-
ing rows of C'j,m by successively multiplying by Cp on the right. This takes
time O(k2) by (22). We can compute C; with at most logn operations of
squaring and multiplying by Cp ; the sequence of operations is determined
by the binary representation of n.
23. Let r = rank A and let A' be an n x r submatrix of A of full rank r.
Let B be an n x (n - r) matrix spanning ker A. As argued in Lecture
40, rank (RA)2 = rank RA = rank A iff the matrix [RA'IB] formed by
juxtaposing RA' and B is nonsingular. Thus
where the sum is over the set S of all n x r matrices of full rank r such
that [SIB] is nonsingular.
We claim that RA' is a random l n x r matrix. To see this, let D be any
nonsingular n x n matrix agreeing with A on the columns A'. Then
RA' = (RD)',
II (qn _ qn-i)
r
i=l
ways of choosing S, and this number is lSI. Combining all the above
equations, we have
24. (a) To show that the problem is coNP-hard, we give a reduction from
the pattern covering problem of Miscellaneous Exercise 5. Let P be
a given set of patterns p E {O, 1, *In. We show how to construct in
polynomial time a set of terms T such that T is a cover of the ground
terms over {f,a,b} iff P is a cover of {O, l}n.
Let
tja«(3) = t(a(3).
Now let T be the given set of terms. Let
At {a I t(a) i: .1}
A UAt
tET
A Au {aL, aR I a E A} .
X (thus 0.,(3 E A) and tla #- tl(3. Then there exists a string "I such
that
25. This problem can be solved with n processors in O(logn) time. We asso-
ciate a processor with each vertex. The processor associated with vertex
v will calculate a pointer next(v) to the successor of v in the preorder
traversal. If v is not a leaf, then next (v) is its leftmost child. If v is a leaf,
then next(v) is the leftmost right sibling of d(v), where d(v) is the lowest
ancestor of v that is not the rightmost child of its parent. If no such d( v)
exists, then v is the last vertex in preorder and has no successor.
To compute the next pointers of leaves in logarithmic time, we use the
technique of pointer doubling. Initially, each vertex v sets
parent (v) if v is the rightmost child of its parent;
d( v):= { v if v is not the rightmost child of its par-
ent or v is the root.
SOLUTIONS TO MISCELLANEOUS EXERCISES 293
Each vertex v then iterates the operation d( v) := d( d( v)) log n times. This
must be done synchronously in parallel. At this point d(v) = d(v) if d(v)
exists, or the root if not. If so, the leaf v sets next (v) to the leftmost right
sibling of d(v).
We now have a list next of all vertices in the correct order. It remains
to compute the preorder number of v for each v. This is the number
of vertices appearing on the list next before v. This can be done in log n
stages with parallel prefix addition, using pointer doubling (i. e., next (v) :=
next(next(v))) to calculate the address to send the next message in each
stage.
26. (a) To test whether G is outerplanar and to find an outerplane embed-
ding if one exists, we add a new vertex v and an edge from v to all
other vertices, then use the Hopcroft-Tarjan planarity test [52] to test
whether the resulting graph G' is planar and find a plane embedding
if so. This can be done in linear time. If G is outerplanar, then G can
be embedded with all vertices on the outer face, so embedding v in
the outer face allows v to be connected to all other vertices without
crossing; thus G' is planar. Conversely, if G' is planar, then it can be
embedded with v adjacent to the outer face; then deleting v and its
incident edges gives an outerplane embedding of G.
(b) There always exists a l-~ separator of size 2. To find it, we first find
an outerplane embedding as in part (a).
o
We then triangulate the interior faces. This can be done by traversing
each interior face, adding an edge from the first vertex on the face
to every other vertex on the face it is not already connected to. (We
know from part (a) which face is the exterior face: it is the one that
contained v.)
in one step. Note that each 8 i is at most half the size of 8 and each
1'; is at most half the size of T. For each 0 ~ i ~ 2 in parallel,
merge 8i and Ti recursively with the 18i l + 11';1 processors assigned
to 8i and 1';. Let Ui be the array obtained by merging 8i and Ti ,
o ~ i ~ 2. Now store Uo, Ul , and U2 in an array end-to-end. The
processor associated with the ith element of Uo stores its element in
position i of the output array; the processor associated with the ith
element of Ul stores its element in position IUoI + i of the output
array; and the processor associated with the ith element of U2 stores
its element in position IUol + lUll + i of the output array. This takes
constant time in parallel. We obtain the recurrence
T(n) = O(logn)
28. (a) We first show that the product of two circulant matrices is again a
circulant matrix. A matrix C is circulant iff, when the columns are
rotated one position to the left and then the rows are rotated up, we
get C back; in terms of permutation matrices,
p-1CP = C,
where
0 0 0 1
1 0 0 0
p 0 1 0 0
0 0 1 0
While we are at it, let us show that the inverse of a circulant matrix,
if it exists, is circulant:
p-l A-Ip = (p-l AP)-l = A-I.
We can easily compute the first row of AB in O(logn) time with n 2
processors, since each element of the first row is an inner product
and can be computed in O(log n) time with n processors. Since AB
is circulant, we need only rotate the- first row to get the other rows.
This takes constant time with n 2 processors.
(b) Let a = (ao, al, ... , an-I) and b = (b o, bI, ... , bn- l ). Let c(a) denote
the unique circulant matrix with first row a; thus c(a)ij = aj-i. In [3,
pp. 256-257], it is stated that the vector F;;I(Fna·Fnb) is the positive
wmpped convolution of a and b:
F;;I(Fna . Fnb) (23)
n-l n-l n-l n-l
= (L aib-i, L aibl-i, L ai~-i' ... , L aibn-I-i). (24)
;=0 ;=0 i=O i=O
L L aibjxi+j
n-In-l
fg mod xn-l
i=O j=O
n-In-l
L L aibk_i xk
i=O k=O
n-l n-l
L(L aibk_i)Xk .
k=O i=O
SOLUTIONS TO MISCELLANEOUS EXERCISES 297
Lw
n-l
j=O
ij = {'f' == °
n, 1 Z mo d n
0, otherwise
where w is a primitive nth root of unity: the i, gth element of F;:lc(a)Fn
is
-1 L L w-·(k-m)amwkl
n-l n-l .
n m=Ok=O
1 n-l
L L
n-l . .
amw·m ( - w(h)k)
m=O n k=O
{ E;;;:}o amw
im if i = e
° if i i e
which is also the i, gth element of d(Fna).
(c) The first row of the inverse of c( a) can be calculated by taking the
Fourier transform of a, inverting all the elements of the resulting vec-
tor, and transforming back. In other words,
c(a)-l = c(F;:l((Fna)')) ,
where b' is the vector obtained from b by inverting all the elements.
This follows immediately from the isomorphism c(a) f----' d(Fna) dis-
cussed in part (b) above, but in case you did not have the patience
to wade through all that, here is a more direct argument. If (Fna)'
exists, then by (25),
c(a) . c(F;:l((Fna)')) c(F;:l(Fna. Fn(F;:l((Fna)'))))
c(F;:l(Fna· (Fn a)'))
C(F;:l(l, 1, 1, ... ,1))
c(l,O,O, ... ,0))
I,
298 SOLUTIONS TO MISCELLANEOUS EXERCISES
by (25). Therefore
so (Fna)' exists and is equal to Fnb. The entire operation can be done
in time O(log n) with n processors using the fast Fourier transform.
Circulant matrices have numerous applications in geometry, differential
equations, and mechanics. To find out more about them, see Davis' book
[27].
29. For permutation a E Sn, define
n
t(a) II Ti,O"(i) .
i=l
Then
Lemma
-t(Tp) ,
SOLUTIONS TO MISCELLANEOUS EXERCISES 299
since rp-l changes the signs of an odd number of factors of t(rp). For
example, if p = (13746) and r = (25), then p-l = (64731), and
t(rp)
t(rp-l)
-t(rp) .
which is even, thus rp and rp-l are either both even or both odd. Thus
the permutations containing odd cycles p and p-l can be paired up so that
their contributions (-1 )sign(u)t( 0') to det T cancel. This assignment can be
repeated for other permutations 0' not containing p but containing another
odd cycle. Thus we are left with the permutations containing even cycles
only. 0
[6] N. Alon, L. Babai, and A. Itai. A fast and simple randomized parallel
algorithm for the maximal independent set problem. J. Algorithms,
7:567-583, 1986.
301
302 BIBLIOGRAPHY
[20] R. Cole. Parallel merge sort. In Proc. 27th Symp. Foundations of Com-
puter Science, pages 511-516. IEEE, October 1986.
[23] S. A. Cook. The classification of problems which have fast parallel algo-
rithms. In Karpinski, editor, Proc. 1983 Symp. Foundations of Compu-
tation Theory, volume 158 of Lect. Notes in Comput. Sci., pages 78-93.
Springer-Verlag, 1983.
[24] J. M. Cooley and J. W. Tukey. An algorithm for the machine calculation
of complex Fourier series. Mathematics of Computation, 19:297-301,
1965.
[59] R. M. Karp and A. Wigderson. A fast parallel algorithm for the maximal
independent set problem. In Proc. 16th Symp. Theory of Computing,
pages 266-272. ACM, May 1984.
[60] L. G. Khachian. Polynomial algorithms in linear programming. Zhumal
Vychislitelnoi Matematiki i Matematicheskoi Fiziki, 20:53-72, 1980.
[61] S. C. Kleene. Representation of events in nerve nets and finite au-
tomata. In Shannon and McCarthy, editors, Automata Studies, pages
3-41. Princeton U. Press, 1956.
[76J M. Luby. A simple parallel algorithm for the maximal independent set
problem. In Proc. 17th Symp. Theory of Computing, pages 1-10. ACM,
May 1985.
[83J V. Va. Pan. Strassen's algorithm is not optimal. In Proc. 19th Symp.
Foundations of Computer Science, pages 166-176. IEEE, 1978.
[84J V. Va. Pan. How to multiply matrices faster, volume 179 of Lect. Notes
in Comput. Sci. Springer-Verlag, 1984.
[85J C. H. Papadimitriou and K. Steiglitz. Combinatorial Optimization: AL-
gorithms and Complexity. Prentice-Hall, 1982.
BIBLIOGRAPHY 307
309
310 INDEX
Hall's theorem, 224, 233, 255, 256, maximal, 13, 15, 191, 194, 219,
286 230, 239, 272, 274, 282
Hamiltonian induced subgraph, 191
circuit, 131, 133, 144, 155, 158, infimum, 38
260, 285 inner product, 166, 178
directed, 261 inorder, 58, 59, 65, 66
Hamming distance, 157 insert, 40, 58, 65-67
Hasse diagram, see transitive reduc- integer
tion addition, 160
heap arithmetic, 160
binomial, 40-44 division, 163
Fibonacci, 25, 40, 44-47, 61, 99 multiplication, 161
order, 41, 44, 65, 66 programming, 112, 116, 125, 130,
height, 53 133
Hermitian, 177 integral, 69
heuristic, 48, 49, 51, 52, 92-94, 104 maximum flow, 90, 287
homeomorphism, 72 interior
homomorphism point method, 130
group, 208 vertex, 84
Hopcroft, J. E., 78, 102, 107, 178, interpolation, 187
234, 286, 293 interpretation, 34
Huang, M.-D. A., 202 standard, 35
Hungarian tree, 108,233,286 invariant, 19, 26, 174, 248, 254
hypercube, 151, 154, 156 credit, 42, 61
invertible elements
Ibarra, 0., 171, 176 group of, 203, 283
idempotence, 29, 30 involution, 279
identity, 29 irreducible, 212
im, see image isolated vertex, 71, 232
image, 174, 215 isomorphism
inclusion-exclusion principle, 194, 196, algebra, 190, 297
270 field, 179
incremental weight, 103, 106 graph, 72, 75, 231
independence, 13, 152 group, 204, 209
3-wise,271 ring, 149, 204, 207-209
algebraic, 14 tree, 214
d-wise, 200, 229, 269 vector space, 175, 215
data, 151 Itai, A., 191
linear, 4, 14, 176
pairwise, 193, 196, 199, 271 Johnson, D. S., 112, 122, 133
statistical, 66, 192, 193, 201 join, 59
independent set, 13, 117, 118, 125, Jordan canonical form, 174, 175
230,274
k-clique, 113
INDEX 315
uniform
circuit, 5, 152
distribution, 66, 200, 215
Texts and Monographs in Computer Science
(continued from page ii)
John V. Guttag and James J. Horning, Larch: Languages and Tools for Formal
Specification
Brian Randell, Ed., The Origins of Digital Computers: Selected Papers, 3rd Edition
Thomas W. Reps and Tim Teitelbaum, The Synthesizer Generator: A System for
Constructing Language-Based Editors
Thomas W. Reps and Tim Teitelbaum, The SyntheSizer Generator Reference Manual,
3rd Edition
J.T. Schwartz, R.B.K. Dewar, E. Dubinsky, and E. Schonberg, Programming with Sets:
An Introduction to SETL
Study Edition