You are on page 1of 2

Introduction to Algorithms Caltech CS 38, Spring 2004

Leonard J. Schulman
Problem set 8 Due Friday May 28 at 5pm in the Intel Lab

1. (10 points)
Let G = (V ; E ) be an undirected graph. Give an O(V + E )-time algorithm to compute a path in G that
traverses each edge in E exactly once in each direction. Describe how you can find your way out of a
maze if you are given a large supply of pennies.
2. (10 points)
Give an example of a directed graph G = (V ; E ), a source vertex s 2 V , and a set of tree edges E  E
such that for each vertex v 2 V , the unique path in E from s to v is a shortest path in G, yet the set
of edges E cannot be produced by running BFS on G, no matter how the vertices are ordered in each
adjacency list.
(The order in the adjacency list simply specifies the order in which the edges out of each vertex are
processed by BFS. I didnt discuss adjacency lists in class but see the discussion of adjacency lists and
adjacency matrices in the notes for lectures 8-9 of the Berkeley course.)
3. (10 points) [No collaboration]
The road map of an ancient kingdom is represented by a weighted directed graph G = (V ; f ) where
f : V  V ! [0; 1 represents the probability that a messenger traveling along that road, will be killed

by highwaymen or wild animals. (The highwaymen might preferentially prey on travelers going
in one directionality or another, since this influences their estimate of the potential loot.) You may
assume that the probability of this sad event is independent for each road. The king, situated at city
s, wishes to know, for each city of the kingdom, the safest route along which a messenger can be sent

to that city. Give an efficient algorithm to solve this problem.


4. (10 points) [No collaboration]
The depth d(v ) of a vertex v in a rooted tree is its distance from the root (number of edges on that
path). For vertex v and 0  `  d(v ) let a` (v ) denote the ancestor of v which is at distance ` from it.
(So a0 (v ) = v .)
The input to this problem is a rooted tree in which a natural number j (v ) is written at each vertex.
Your task is to replace j (v ) by j (aj (v) (v )) at each vertex. (If there is any vertex v at which j (v ) > d(v ),
your algorithm should report this.) Show how to do this task in linear time.
5. (15 points)
(In this exercise its again essential to refer to adjacency lists, this in time in connection with DFS. In
this case the adjacency list of a vertex provides the order in which its out-neighbors are visited in
DFS.)
It is important to study not only the time complexity of algorithms, but also their space complexity: how
much memory they use. In this problem well touch on this matter. Well distinguish between two
types of memory: read-only memory and read-write memory. The input to our problem is an undirected
tree G on n vertices, and this input is provided in adjacency list format, in a read-only memory. Youd
like to visit the vertices of this tree in the same order as they are visited by the usual DFS. (Recall
though that this order is not unique, since it can depend on the order in which vertices are listed in
the adjacency lists. Any order that could have arisen from some way of writing down those lists,
is satisfactory.) However, your computer is equipped with only a very small read-write memory:
the space available is O(log n) bits. (I.e., log n for any constant that you need for your solution.)
Obviously you cant use the DFS algorithm that we discussed in class, because the stack may well
grow to linear size. Nevertheless you need to devise some way of guaranteeing that you visit the

1
vertices in the desired order. But you dont have to be able to compute pre-visit times, parents,
or post-visit times of vertices.
In short: you have O(log n) read-write memory at your disposal. Your algorithm is to traverse the
graph. The algorithm is equipped with a write-only output tape on which, as it works, it prints out
statements of the form I am now at vertex v . The printout should be identical to a printout which
you could have gotten from the usual DFS algorithm (possibly for some other ordering of the vertices
in the adjacency lists.)

You might also like