Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Problem Set 3
Lecturer: Daniel A. Spielman
Homework Policy
Collaboration is not allowed on these problems: you are expected to solve them on your
own.
You should cite any sources that you use on the problem sets other than the textbook,
TA and instructor. You must not search the web for solutions to similar problems given
out in other classes. If you think this policy needs any clarification, please let me know.
Do 3 of 4
While you should think about all 4 problems assigned, you should only turn in 3 of them.
Each problem is worth 10 points.
Corrections
I have made one significant correction to the problem set. In problem 4, I now ask you
to assume that S only contains two elements.
comparison
(1,2)
(1,3)
(2,3)
cost of i cost of j
log 3
log 3
0
log 3
log 2
0
I will define the memory cost of an loop to be the sum of all of these logarithmic terms.
You can see that the memory cost of the naive loop is some constant times n2 log n. You
may assume that n is a power of 2.
a. (5 points) Give a loop that reduces the memory cost to O(n2 ).
b. (5 points) Prove that it does this.
If you cant get all the way down to cost O(n2 ), you will receive half credit if you can
get it down to O(n2 log(log n)) (and if you can explain why).
Hint: You can actually be rather wasteful in this problem, as long as the memory cost
is O(n2 ). For example, you could execute both comp(i, j) and comp(j, i) for all i and j.
You will loose a factor of 2, but can produce cleaner code. In my analysis, I found the
following inequality to be useful. You can use it to
X i
= 2.
i
2
i1
{1}
{3, 4}
{1, 2, 3, 4}
So, I will think of the elements of the vector as being indexed by the subsets of {1, . . . , k}.
This way, if I write xS , I mean xi where
X
i=
2j1 .
jS
xS xT .
S{1,...,k} T {1,...,k}:ST =
That is, we compute the product of xS and xT over all pairs of sets S and T that are
disjoint.
Design an algorithm that computes this product in time O(n log2 n). Explain why
your algorithm is correct and why it runs in the stated time.
You should definitely assume that n is a power of 2.
Hint: The most natural way to divide the sets in half is to separately consider those
that do and do not contain element k.
The naive way of computing the meet of a and b is to just walk from each of a and b to
the root, and see where the paths first meet. This is fast if the tree has low depth. But,
not all trees have low depth (for example, consider a long path).
One way to speed up computations in trees is to create a list of pointers at each node
that point to nodes that are closer to the root. For example, for every node a, we define
parent(a) to be the node adjacent to a that is closer to the root. Imagine that we have
computed the parent of a for every node a. We can then compute the grandparent of a
by the following loop:
For every node a, grandparent(a) = parent(parent(a)).
So that this process is well-behaved, we define the parent of the root to be the root.
Equivalent, we say that the root is its own parent. We can then compute grandparents
of grandparents, and so on. I will call these hyperparents.
Given a tree of depth d (the depth is the longest distance of a node from the root), we
compute all of these, as follows.
1. For every node a, set hyperparent(a, 1) = parent(a).
2. For i from 2 to dlog2 de,
a. For every node a, set
hyperparent(a, i) = hyperparent(hyperparent(a, i 1), i 1).
We can compute all these hyperparents in time O(n log2 d).
Your job is to figure out how to use them. Given access to the tree and all these hyperparents, your job is to design an algorithm that on input nodes a and b will compute the
meet of a and b.
Design an algorithm that computes the meet of a and b in time O(log2 d). Explain
why your algorithm is correct and runs in the stated time.
You will get half credit for an algorithm that runs in time O((log2 d)2 ).
Note: You do not need to look at the whole input. Really, your algorithm should just
query the data structure. So, it should just ask for hyperparents. You may assume that
your algorithm knows d.
Grid graphs arise in many (mostly physical) applications. However, they usually have
some extra information that makes them more interesting. In our case, we will assume
that every edge can have a different (positive) length. That is, we are given as input a
grid graph G = (V, E, l), where l assigns a length to every edge.
As in problem 3 from the last problem set, we are only interested in the distances between
pairs of vertices in a set S V . So, we are going to remove all of the vertices that are
not in S. You should assume that S only has two elements. In the last problem
set, we proved that the set of edges in the resulting graph does not depend on the order
in which we remove vertices. However, the time required by the removal process does
depend on this order.
The time required to remove a vertex v is proportional to the square of its degree at the
time it is removed. Note that this degree can change during the course of the algorithm.
As the graph has n2 nodes, no node will ever have degree more than n2 . So, the time
required for all the removals will be at most O(n6 ). I would like you to design a faster
algorithm.
Describe an ordering in which to remove vertices so that the total time to perform the
removals is at most O(n3 ). Prove that your answer is correct. You may assume that
n is a power of 2 if you prefer. In fact, you are free to give an algorithm that makes
any assumption you like about n, as long as it works for an infinite sequence of n. My
solution considers n = 2k 1, for integers k.