MIT6_006S20_q2_sol
MIT6_006S20_q2_sol
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 90 minutes to earn a maximum of 90 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) 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!
w1 w2 w3
a b c d
Given a 3-color labeling c : E → {red, green, blue} of a connected, weighted, undirected graph
G = (V, E, w) containing only positive edge weights, and given two vertices s, t ∈ V , describe
an efficient algorithm to return a path from s to t having minimum color cost.
(By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.)
Solution: Construct a new graph G0 = (V 0 , E 0 ) with:
• 3 vertices for each vertex v ∈ V : specifically vi for i ∈ {red, green, blue} corresponding to
arriving at vertex v via an edge with color i;
• (vertex-edges) 3 undirected edges for each vertex v ∈ V : specifically {vred , vblue }, {vgreen , vred },
and {vblue , vgreen } of weight wc ; and
• (edge-edges) 1 undirected edge for each undirected edge {u, v} ∈ E of weight w and color
c(u, v): specifically undirected edge {uc(u,v) , vc(u,v) } with weight w.
Graph G0 has 3|V | vertices and 3|V | + |E| edges, and has the property that the minimum weight
of any path in G0 from any vertex si to any vertex tj for i, j ∈ {red, green, blue} is equal to the
minimum color cost of any 3-color labeled path in G from s to t, as switching colors at a vertex
requires traversing an edge of weight wc . So solve SSSP three times, once from each vertex si and
find the minimum weight of any path to any tj , and then return a minimum path by constructing
parent pointers as shown in lecture. Since this graph only has positive edge weights, we can solve
SSSP using Dijkstra in O(|V | + |E| + |V | log |V |) = O(|E| + |V | log |V |) time.
Note that you can avoid running Dijkstra three times via a supernode, but this only reduces work
by a constant factor. Also, one can also avoid adding vertex-edges by adding three edges for each
edge connected and weighted appropriately, but these edges will need to be directed toward a
vertex labeled with the same color as the corresponding edge.
Common Mistakes:
• Not directing edges with vertex weight, or otherwise not clearly defining a graph
• Expanding weights on all edges, leading to an O(nk) expansion
• (e.g., a town with Θ(k) Orks may connect to Θ(n) roads)
• Using Dijkstra without modification to achieve an O(k log k)-time algorithm
• Expanding vertex weights incorrectly (path length ri instead of ri − 1)
• Finding shortest paths in a BFS or DFS tree (may not contain shortest paths)
6 6.006 Solution: Quiz 2 Name
1
Recall a cycle is simple if visits any vertex at most once.
6.006 Solution: Quiz 2 Name 7
Any simple path in G traverses at most n − 1 edges, so the magnitude of its weight is at most
(n − 1)k < b/2. Thus bb/2c minus the weight of any simple path in G will always be > 0 and < b
(so Bellham cannot exhaust or exceed her tank by driving on a simple path from s to t).
Lastly, we find the weight of a minimum-weight path from s to t by solving SSSP. Unfortunately
using Bellman-Ford takes O(n2 ) time which is too slow. However, we can re-weight edges in G to
be positive while preserving shortest paths by exploiting the provided vertex potentials, similar to
Johnson’s algorithm. Specifically, create new graph G0 , identical to G, except change the weight
of each edge (u, v) to J(u, v) − mg(h(v) − h(u)) > 0. This transformation preserves shortest
paths since the weight of each path from, e.g., a to b changes by the same amount, namely by
mg(h(b) − h(a)). So run Dijkstra from s to find the minimum weight D of any path to t in G0 , and
return bb/2c − (D − mg(h(b) − h(a))).
Constructing G takes O(n) time, reweighting to G0 also takes O(n) time, and then running Dijkstra
from s in G0 takes O(n log n) time, leading to O(n log n) time in total.
Common mistakes continued on S1.
8 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 S1” on the problem statement’s page.
• Using Bellman-Ford directly yielding O(n2 )-time algorithm (eligible for half the points)
• Running Bellman-Ford to find new weights, still leading to O(n2 )-time
• Using −J on weights instead of J
• b-times graph duplication (inefficient, b may be much larger than n)
6.006 Solution: Quiz 2 Name 9
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.
10 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.
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms