Solution:: Quiz 2
Solution:: Quiz 2
Solution: Quiz 2
• Do not open this quiz booklet until directed to do so. Read all the instructions on this page.
• When the quiz begins, write your name on the top of every page of this quiz booklet.
• You have 120 minutes to earn a maximum of 120 points. Do not spend too much time on
any one problem. Skim them all first, and attack them in the order that allows you to make
the most progress.
• You are allowed two double-sided letter-sized sheet with your own notes. No calculators,
cell phones, or other programmable or communication devices are permitted.
• Write your solutions in the space provided. Pages will be scanned and separated for grading.
If you need more space, write “Continued on S1” (or S2, S3, S4, S5) and continue your
solution on the referenced scratch page at the end of the exam.
• Do not waste time and paper rederiving facts that we have studied in lecture, recitation, or
problem sets. Simply cite them.
• When writing an algorithm, a clear description in English will suffice. Pseudo-code is not
required. Be sure to argue that your algorithm is correct, and analyze the asymptotic
running time of your algorithm. Even if your algorithm does not meet a requested bound,
you may receive partial credit for inefficient solutions that are correct.
• Pay close attention to the instructions for each problem. Depending on the problem,
partial credit may be awarded for incomplete answers.
Name:
School Email:
2 6.006 Solution: Quiz 2 Name
(a) [1 point] Write your name and email address on the cover page.
Solution: OK!
a 9 c
1 def relax(Adj, w, d, parent, u, v):
2 # relax edge (u, v)
7 8 6 3 if d[v] > d[u] + w(u, v):
4 d[v] = d[u] + w(u, v)
5 parent[v] = u
s 5 b
For each algorithm below, give the sequence of edges (u, v) on which relax(..., u, v)
gets called, for some valid execution of the algorithm from source vertex s. Note that an al-
gorithm might call relax(..., u, v) on the same edge (u, v) more than once, and a call to
relax(..., u, v) might not decrease the value of d[v].
1
By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.
6.006 Solution: Quiz 2 Name 5
• Using |V | rounds of Bellman-Ford is slower than Johnson’s (which is the point of Johnson’s)
• Johnson’s does create a graph with nonnegative weights, but shortest path weights in the new
graph are different than what you want for radius.
• We cannot assume |E| = O(|V |).
• Absence of negative-weight cycles does not imply DAG.
• Radius isn’t looking for longest paths; it’s looking for the longest shortest path. So we
shouldn’t negate edge weights to find longest paths.
• Using Floyd-Warshall is not wrong, but we haven’t discussed it. We did allow it. It’s never
better that Johnson asymptotically, but it’s good enough for this problem.
6 6.006 Solution: Quiz 2 Name
• Incorrect graph construction, e.g., including outdoor paths so they’re indistinguishable from
indoor ones
• Thinking this was a shortest paths problem.
• Inefficient graph construction, e.g., b-fold graph duplication (one level per outdoor path taken)
• Using o or O as a variable for “outdoor edges”. This is not wrong, but is quite challenging to
distinguish from 0 (zero), especially in handwriting. :(
1
By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.
6.006 Solution: Quiz 2 Name 7
1
By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.
6.006 Solution: Quiz 2 Name 9
• Bellman-Ford to find longest paths (e.g., by negating edge weights) is not wrong, but is slower
than necessary because the graph is a DAG (if set up correctly).
• Dijkstra cannot be used to find longest paths. Negating edge weights would leave negative
weights, which Dijkstra can’t handle.
• Some students ran SSSP from all possible water-break locations, which amounts to solving
APSP. This is more (and slower) than necessary, since there are only two sources we care
about: home and office.
• When running SSSP from the office, you want the graph of uphill edges, not downhill edges,
so you can gain elevation from the office to the water break. The final route will run this path
backwards.
1
By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.
10 6.006 Solution: Quiz 2 Name
Common Mistakes:
1
By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.
6.006 Solution: Quiz 2 Name 11
You can use this paper to write a longer solution if you run out of space, but be sure to write “Con-
tinued on S1” on the problem statement’s page.
Common Mistakes:
• The subproblems “maximum after dealing 2i cards” and “maximum after dealing i cards to
each player” are different! E.g., after dealing 4 cards, Annie might have 3 of them. Many
students meant the latter but wrote the former.
• Many students described the “deck prefix” strategy x(2i) in words but wrote formulas for the
“deck suffix” strategy y(2i), or vice-versa. Make sure your “Subproblem” and “Relate” steps
agree with each other.
• The recurrence above for x(2i) relies on x(2i − 2) and x(2i − 4), so it only applies to x(4)
and higher. This means x(0) and x(2) are both base cases. Setting x(i) = −∞ for i < 0 is
an alternatively acceptable base case.
• Make sure you specify what the final return value is! In this case, it is x(2n).
6.006 Solution: Quiz 2 Name 13
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S2” on the problem statement’s page.
14 6.006 Solution: Quiz 2 Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S3” on the problem statement’s page.
6.006 Solution: Quiz 2 Name 15
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S4” on the problem statement’s page.
16 6.006 Solution: Quiz 2 Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S5” on the problem statement’s page.