Professional Documents
Culture Documents
Final Solutions
• Do not open this exam booklet until you are directed to do so. Read all the instructions first.
• The exam contains 10 problems, with multiple parts. You have 180 minutes to earn 180
points.
• This exam booklet contains 20 pages, including this one.
''
• This exam is closed book. You may use three double-sided letter (8 12 × 11'' ) or A4 crib
sheets. No calculators or programmable devices are permitted. Cell phones must be put
away.
• Do not waste time deriving facts that we have studied. Just cite results from class.
• When we ask you to “give an algorithm” in this exam, describe your algorithm in English
or pseudocode, and provide a short argument for correctness and running time. You do not
need to provide a diagram or example unless it helps make your explanation clearer.
• Do not spend too much time on any one problem. Generally, a problem’s point value is an
indication of how many minutes to spend on it.
• Show your work, as partial credit will be given. You will be graded not only on the correct
ness of your answer, but also on the clarity with which you express it. Please be neat.
• Good luck!
Name:
(a) T F [4 points] Suppose algorithm A has two steps, and A succeeds if both the steps
succeed. If the two steps succeed with probability p1 and p2 respectively, then A
succeeds with probability p1 p2 .
(b) T F [4 points] If the divide-and-conquer convex hull algorithm (from Lecture 2) used
a Θ(n2 ) strategy to discover the maximum and minimum tangents, the overall
algorithm would run in Θ(n2 log n) time.
(c) T F [4 points] In order to get an expected Θ(n log n) runtime for “paranoid” quick
sort (from Lecture 3), we require the recursive divide step to split the array into
two subarrays each of at least 14 the size of the original array.
Solution: False. If the graph has a cycle, there are infinitely many paths be
tween some pairs of vertices, so the product ought to be ±∞, yet the matrix-
multiplication algorithm will compute finite values if the original matrix has all
finite values (e.g., a clique).
(g) T F [4 points] Negating all the edge weights in a weighted undirected graph G and
then finding the minimum spanning tree gives us the maximum-weight spanning
tree of the original graph G.
Solution: True.
(h) T F [4 points] In a graph with unique edge weights, the spanning tree of second-
lowest weight is unique.
(k)
Solution: False. duv is the length of the shortest path from vertex u to vertex v
that only uses vertex {1, 2, · · · k} as intermediate nodes.
Solution: True.
Using the algorithm from Problem 10-2, we can construct a BFS tree rooted at
v0 within the given time and message bounds. The root process can broadcast a
signal telling all the processes that the tree is completed. Then the processes can
use the tree for convergecasting their values, computing the max as the messages
move up the tree. The broadcast and convergecast phases do not exceed the
bounds for the BFS construction.
(k) T F [4 points] Suppose a file server stores a hash of every file in addition to the
file contents. When you download a file from the server, you also download
the hash and confirm that it matches the file. This system securely verifies that
the downloaded file has not been modified by an adversary, provided the hash
function has collision resistance.
Solution: False. This scheme is not secure because the adversary can simply
replace the file with any file and the hash of that file, and you cannot tell the
difference.
6.046J/18.410J Final Solutions Name 5
(l) T F [4 points] Suppose Alice, Bob, and Charlie secretly generate a, b and c, respec
tively, and publish g a mod p, g b mod p, and g c mod p, where p is a prime. Then,
Alice, Bob, and Charles can each compute g abc mod p as a shared secret known
only to the three of them.
Solution: False. For example, Alice only knows a, g b and g c , so she can compute
g ab and g ac but not g abc .
(m) T F [4 points] The number of memory transfers used by the best cache-oblivious
algorithm is always at least the number of memory transfers used by the best
external-memory algorithm for the same problem.
¨
Problem 2. Uberstructure [10 points] (1 part)
Design a data structure that maintains a dynamic set S of n elements subject to the following
operations and time bounds:
Describe how the operations are implemented on your data structure and justify their runtime.
Solution: Use a balanced binary search tree and a hash table. Augment the root of the balanced
binary search tree with the value of the minimum element.
I NSERT: Insert the element in both the balanced binary search tree and hash table. If the element
is smaller than the current min, update the root’s stored min value. Insertion into the tree requires
O(log n) worst-case time, and insertion into the hash table requires O(1) expected amortization
time, for a total of O(log n) expected amortized. (In fact, with high probability, insertion into the
hash table requires at most O(log n).)
D ELETE: Find the item in the tree and the hash table, and delete it from both. Rebalance the tree
as necessary and update the root’s min value if the minimum element has been deleted. Deletion,
including rebalancing, costs O(log n), and it takes O(log n) to find the minimum element using
the binary search tree.
F IND -M IN: Return the min value stored at the root node. This takes O(1) worst-case time.
S EARCH: Check whether the element exists in the hash table. This takes O(1) expected time.
6.046J/18.410J Final Solutions Name 7
(a) [10 points] What extra information needs to be stored at each node? Describe how to
Solution: Each node x should store x.size — the size of the subtree rooted at x —
and x.sum — the sum of all the key values in the subtree rooted at x. For a value
x > 0, let Sx be the set of all keys less than or equal to x. Let Ax and Bx be the sum
and the size of Sx .
We can compute Ax as follows. Let u be the leaf with smallest key larger than x.
Finding u from the root only takes O(lg n) time by using S EARCH in a 2-3 tree. Now
consider the path from the root of the tree to u. Clearly, Ax is the sum of all leaves
that are on the left of this path. Therefore, Ax can be computed by summing up all
y.sum’s for every node y that is a left sibling of a node in the path. Since there are
only lg n such nodes y’s, computing Ax only takes O(lg n) time.
Computing Bx is similar: instead of summing up y.sum, we sum up y.size. There
fore, it also takes O(lg n) time to compute Bx .
Ax
Therefore, AVERAGE(x) which is B x
can be answered in O(lg n)) time.
(b) [5 points] Describe how to modify I NSERT to maintain this information. Briefly
justify that the worst-case running time for I NSERT remains O(lg n).
Solution: Maintaining x.size is similar to what was covered in recitation and home
work. Maintaining x.sum is exactly the same: when a node x gets inserted, we simply
increase y.sum for every ancestor y of x by the amount x.key. When a node splits,
we recompute the x.sum attribute for the split nodes and its parent. Hence, I NSERT
still runs in worst-case time O(lg n).
6.046J/18.410J Final Solutions Name 8
(a) [5 points] Suppose we send Forrest k reminders for each of n events. What is the
expected number of appointments Forrest will remember? Give your answer in terms
of k and n.
(b) [5 points] Suppose we send Forrest k reminders for a single event. How should we
set k with respect to n so that Forrest will remember the event with high probability,
i.e., 1 − 1/nα ?
Solution: This problem is equivalent to how many times we must flip a coin to get a
head with high probability. The probability of k tails in a row is 1/2k . Thus exactly
α lg n coin flips suffice.
(c) [5 points] Suppose we send Forrest k reminders for each of n events. How should
we set k with respect to n so that Forrest will remember all the events with high
(a) [6 points] Assume that Prof. Chopin decides to learn exactly k pieces. Prove that he
Solution: Assume there exists a selection of teachers and pieces for learning k pieces.
Let the set of lowest k pieces be Pk . If there is a piece in our selection that is ∈
/ Pk ,
then we must have a piece in Pk not in the final selection. If we swap the one with
the higher cost (∈/ Pk ) with the one with lower cost (∈ Pk ), the new selection thus
made will still be valid, because if the higher time cost was fulfilled in the previous
selection, the lower time cost in the new selection will still be fulfilled. In this way,
we can swap pieces until all of them are ∈ Pk .
Similarly, we can swap the teachers for those of higher value until they are the ones
with the k highest times.
6.046J/18.410J Final Solutions Name 10
(b) [5 points] Assuming part (a), give an efficient greedy algorithm to determine whether
Prof. Chopin can learn exactly k pieces. Argue its correctness.
Solution: Let us sort all the teachers and pieces in increasing order beforehand. Call
the sorted lists P and T . We see that if a solution exists, there is also one in which P1
is paired with Tn−k+1 , P2 is paired with Tn−k+2 and so on.
So for each 1 ≤ i ≤ k, the greedy algorithm checks if Pi ≤ Tn−k+i . If it is, then we
don’t need to use the shared time for this piece. If it is not, we need to use Tn−k+i − Pi
of the shared time. We can add up these values. In the end, if the total shared time we
need is > T , we return false. Otherwise, we return true.
(c) [4 points] Using part (b) as a black box, give an efficient algorithm that finds the
maximum number of pieces Prof. Chopin can learn. Analyze its running time.
Solution: Notice that if kmax is the maximum value of pieces we can learn, we can
also learn k pieces for any k ≤ kmax . This suggests that we binary search over the
value of k.
We try O(log n) values during the binary search, and checking each value takes O(n)
time. This takes O(n log n) time. The sorting also took O(n log n) time, so the algo
rithm takes O(n log n) time overall.
6.046J/18.410J Final Solutions Name 11
Consider the following flow network and initial flow f . We will perform one iteration of the
Edmonds–Karp algorithm.
7:9$
2" 5"
4:6$
1:4$ 0:15$
13:15$ 10:10$
4" 7"
14:30$
Solution:
2#
2" 5"
7#
10# 1# 7#
2# 2# 15#
14# 3#
1# 7# 1#
s" 3" 6" t"
2# 8#
13# 2#
3# 1# 15#
2# 4# 10#
16#
4" 7"
14#
(b) [4 points] List the vertices in the shortest augmenting path, that is, the augmenting
path with the fewest possible edges.
Solution:
s→3→2→5→t
or
s→3→2→6→t
6.046J/18.410J Final Solutions Name 12
(c) [5 points] Perform the augmentation. What is the value of the resulting flow?
(a) [5 points] Give a straightforward algorithm that checks whether any subset of k
projects can be completed to solve the decisional problem. Analyze its time com
plexity in terms of m, n, and k.
Solution: For each nk subsets of k projects, check whether any employee is required
by more than one project. This can be done simply by going each of the k projects p,
marking the employees in Ep as needed, and if any employee is marked twice, then
this subset fails. Output “yes” if any subset of k project can be completed, and “no”
otherwise.
The time complexity is nk ·m because there are nk subsets of size k and we pay O(m)
time per subset (because all but one employee will be marked only once). Asymptoti
cally, this is (n/k)k m.
(b) [5 points] Is your algorithm in part (a) fixed-parameter tractable? Briefly explain.
Solution: No. An FPT algorithms requires a time complexity of nO(1) f (k). By con
trast, in our running time, the exponent on n increases with k.
6.046J/18.410J Final Solutions Name 14
(c) [10 points] Show that the problem is NP-hard via a reduction from 3D matching.
Recall the 3D matching problem: You are given three sets X, Y , Z, each of size m;
a set T ⊆ X × Y × Z of triples; and an integer k. The goal is to determine whether
there is a subset S ⊆ T of (at least) k disjoint triples.
This runs in O(n lg m) time by keeping a min-heap of the machines based on the
Solution: Alternate solution: Sort jobs in non-increasing order. Without loss of gen
erality, let the jobs in order be t1 , . . . , tn . Let Jj = {tk:k≡j mod m }. Variations of
this algorithm also works, with different sorting orders and assignments. This takes
O(n lg n) time to sort the jobs.
6.046J/18.410J Final Solutions Name 16
(b) [10 points] Show that your algorithm from part (a) is a 2-approximation algorithm.
Hint: Determine an ideal bound on the optimal solution OPT. Then consider the ma
chine MT with the longest TT , and the last job i∗ that was added to it.
Solution: A lower bound to the optimal is L = max( m1 1≤i≤n ti , maxi (ti )) since the
m
best you can do is to evenly divide the fractional jobs, and it has to run for at least as
long as the longest job.
Now let MT be the machine that runs for the longest, and let i∗ be the last job that was
assigned to MT using the greedy algorithm. Let Tj∗ be the total run time of all jobs of
Mj immediately before assigning i∗ ; TT∗ = minj Tj∗ . Then we have
m · TT∗ ≤ Tj∗ = ti ≤ ti ≤ m · L,
1≤j≤m 1≤i≤i∗ 1≤i≤n
which implies that TT∗ ≤ L. Putting it together, we have TT = TT∗ + ti∗ ≤ L + ti∗ ≤
2L ≤ 2OP T . Therefore, this is a 2-approximation algorithm.
Solution: Proof for alternate solution: Let L be the lower defined above. Consider the
longest job tn . Let k ≡ n mod m, and let Sk = Tk − tn . It must be that Sk ≤ Tj for
all j: for j > k, we only added elements at least as large as every element of Sk . For
j < k, there are a = i mn
n
ti = tn + Sk + Tj ≥ tn + mSk .
i=1 #
j=k
1. For each edge {u, v} ∈ E, there is an edge between (u, i) and (v, i) in E ' , for all 0 ≤ i ≤ Δ.
(Thus, each index i forms a copy of G.)
2. For each vertex v ∈ V , there is an edge between (v, i) and (v, j) in E ' , for all i = j with
0 ≤ i, j ≤ Δ. (Thus each v forms a (Δ + 1)-clique.)
Figure 1: Graph G.
(a) [8 points] Let S be any maximal independent set of G' (i.e., adding any other vertex to
S would violate independence). Prove that, for each vertex v ∈ V , S contains exactly
one of the Δ + 1 vertices in V ' of the form (v, i). Hint: Use the Pigeonhole Principle.
Solution: It cannot contain more than one, since all of these are connected in G' and
that would violate independence.
Now suppose for contradiction that, for some particular u, S contains no vertices of
the form (u, i). Then by maximality, every vertex of the form (u, i) must have some
G' -neighbor in S. Since that neighbor is not of the form (u, ∗), it must be of the form
(v, i), for some v with (u, v) ∈ E.
Thus, each of the Δ + 1 vertices of the form (u, i) has some neighbor of the form (v, i)
in S, where (u, v) ∈ E. Since u has at most Δ neighbors in G, by the Pigeonhole
Principle, there must be two different values of i, say i1 and i2 , for which there is a
single v such that (u, ii ) is a G' -neighbor of (v, i1 ), (u, i2 ) is a G' -neighbor of (v, i2 ),
and both (v, i1 ) and (v, i2 ) are in S. That is a contradiction because S can contain at
most one vertex of the form (v, ∗).
6.046J/18.410J Final Solutions Name 18
(b) [8 points] Now consider a synchronous network of processes based on the graph G,
where every vertex knows an upper bound Δ on the degree. Give a distributed algo
rithm to find a vertex (Δ + 1)-coloring of G, i.e., a mapping from vertices in V to
colors in {0, 1, . . . , Δ} such that adjacent vertices have distinct colors. The process
associated with each vertex should output its color. Argue correctness.
Hint: Combine part (a) with Luby’s algorithm.
(c) [4 points] Analyze the expected time and communication costs for solving the color
ing problem in this way, including the cost of Luby’s algorithm.
Solution: The costs are just those of solving MIS on G' ; the final decisions are local
and don’t require any extra rounds.
Time (number of rounds): The expected time to solve MIS on G' is O(lg (n · Δ)),
because the number of nodes in G' is n · (Δ + 1). The O(lg (n · Δ)) bound can be
corresponding to O(lg n) rounds and messages on all edges (in both directions) at each
round.
SCRATCH PAPER
SCRATCH PAPER
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.