Algorithm Exam Help
Algorithm Exam Help
Solution:
Assume all vertices in G are reachable from v so that |V | = O(|E|);
otherwise, run BFS or DFS to solve single source reachability from v, and
replace G with the subgraph reachable from v in O(|E|) time. Construct
a new graph G0 = (V 0 , E0 ) with:
programminghomeworkhelp.com
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.)
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
programminghomeworkhelp.com
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:
• Incorrectly trying to modify Dijkstra to keep track of state
• Failing to identify (or identifying incorrectly) a source from
which to run SSSP
• Weighting or directing edges in a duplicated graph incorrectly
programminghomeworkhelp.com
Solution: Construct a graph G = (V, E) with:
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
programminghomeworkhelp.com
• 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)
Solution:
Construct a new graph G0 by adding a supernode x to G with a zero-
weight directed edge (x, v) for each v ∈ V . Then run SSSP from x in G0
using Bellman-Ford to label each vertex v ∈ V with its shortest path
distance δ(x, v). For each v ∈ V , δ(x, v) = �∞ if and only if v is
reachable from a negative-weight cycle in G (since adding x does not
add or remove any cycles). Further, for any directed edge (u, v), if δ(x,
u) = δ(x, v) = �∞, then both u and v are each reachable from the
same simple negative-weight cycle (since v is reachable from u and
each vertex is reachable from at most one simple negative-weight
cycle).
So, construct a new graph G00 on only the vertices v ∈ V where δ(x, v)
= �∞ in G0 , with an undirected edge between u and v in G00 if they
share a directed edge in G. Graph G00 has the property that the
number of connected components in G00 equals the number of
negative-weight cycles in G, so count and return the number of
connected components in
programminghomeworkhelp.com
G00 using Full-BFS or Full-DFS. This algorithm takes O(|V |+|E|) time to
construct G0 , O(|V ||E|) time to run BellmanFord, O(|V |+|E|) time to
construct G00, and then O(|V |+|E|) time to count connected
components in G00, leading to an O(|V ||E|) = O(|V | 3) running time in
total.
Common Mistakes:
• General lack of precision when describing algorithm
• Trying to enumerate all paths or cycles (may be exponential)
• Repeatedly running Bellman-Ford, generally yielding |V | · O(|V ||E|)
= O(|V | 4) time
• Stating that |E| = O(|V |)
• Confusing connected components with strongly connected
components
programminghomeworkhelp.com
• By the laws of physics, J(x, y) is always strictly greater than the
difference in potential energy between locations x and y, i.e., J(x, y)
> m · g · (h(y) h(x)), where m and g are the mass of the car and the
acceleration due to gravity respectively.
Her car battery has very large energy capacity b > 2nk where k is
the maximum |J(x, y)| of any road. Assuming she departs s at half
capacity, bb/2c, describe an O(n log n)-time algorithm to determine
the maximum amount of energy Bellham can have in her battery
upon reaching t.
Partial credit will be awarded for slower correct algorithms, e.g.,
O(n2).
Solution:
Construct graph G with a vertex for each of the n locations in
Norway and a directed edge for each of the O(n) roads: specifically
for each road from location u to v, add directed edge (u, v)
weighted by J(u, v). Then bb/2c minus the weight of a minimum-
weight path from s to t in G would correspond to the maximum
energy Bellham could have upon reaching t; or at least it would be
if she did not either exceed or exhaust her tank along the way.
programminghomeworkhelp.com
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).
Common Mistakes:
• 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)
programminghomeworkhelp.com