Programming Homework Help
Programming Homework Help
Given a weighted, directed graph G = (V, E,w) with positive and negative
edge weights, and given a particular vertex v ∈ V , describe an O(k|E|)-
time algorithm to return the minimum weight of any cycle containing
vertex v that also has exactly k edges, or return that no such cycle exists.
Recall that a cycle may repeat vertices/edges.
A 3-color labeling of a graph maps each edge to either red, green, or blue.
The color cost of a 3-color labeled path is its path weight plus a positive
integer wc every time the path changes color.
For example, in the graph below (having four vertices {a, b, c, d}, a blue
edge (a, b) with weight w1, a red edge (b, c) with weight w2, and another
blue edge (c, d) with weight w3) the path (a, b, c, d) has color cost w1 +
w2 + w3 + 2wc, because the path changes color twice.
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.
Problem 3.
• two unweighted directed edges (uru , v1) and (vrv , u1) for each road
between towns u and v.
Graph G has v rv = k vertices and 2(3n) + v(rv - 1) = 5n + k edges. Since
there is at least one Ork in each town, k ≥ n, so G has size O(k). Let s and t
correspond to the towns of Tina’s Mirth and Riverdell respectively. Graph
G has the property that any path from s1 to trt corresponds to a path from
Tina’s Mirth to Riverdell crossing edges equal to the number of Orks
encounters in towns along the way, since for any road connecting towns u
and v, going from u1 to v1 requires traversing rv edges in G. So solve
unweighted SSSP from s1 to trt using BFS in O(k) time, and return the
sequence of towns visited along the found shortest path by following parent
pointers.
Common Mistakes:
• Not directing edges with vertex weight, or otherwise not clearly defining
a graph
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.