Rec C
Rec C
Solution: There are two approaches: one is a reduction; the other is a direct modification of Dijkstra’s
algorithm.
Method I: The idea is to use a reduction: on input (G, l, c, s), we construct a graph G’ = (V ‘, E ’ ) where
G’ only has edge weights (no node weights), so that the shortest path from s to t in G is essentially the same
as that in G ′, with some minor modifications. We can then compute shortest paths in G′ using Dijkstra’s
algorithm.
The reduction works by taking every vertex v of G and splitting it into two vertices vi and v o. All edges
coming into v now come into v i, while all edges going out of v now go out of v o. Finally, we add an edge
from vi to vo of weight c(v).
c(v)
Reduction in 4.19.
Consider now any path in G and notice that it can be converted to an edge-weighted path of the
same weight in G′ by replacing the visit to vertex v with the traversal of edge (vo, vi). Conversely,
consider a path in G ′: every other edge visited is of the form (vi, v o) and corresponds to a vertex v of G.
Replacing these edges with the corresponding vertices we obtain a path in G of the same weight as the path
in G ′. The time required to perform this reduction is O(|V | + |E|). G′ has |V | + |E| edges and 2|V |
vertices, so running Dijkstra takes time O(|V |2 ) and the total running time is O(|V |2).
Method II: We make the following modifications to Dijkstra’s algorithm to take into account node
weights:
• In the initialization phase, dist(s) = w(s).
• In the update phase, we use dist(u) + l(u, v) + w(v) instead of dist(u) + l(u, v).
Analysis of correctness and running time are exactly the same as in Dijkstra’s algorithm.
Problem 2: For the graph below, find the DFS tree starting from A (following the alphabet order
when you have multiple choices). Identify the back edges, forward edges, and cross edges.
Note that the nodes not reachable from A should not be in the DFS tree.
A B C D E
F G H I J
K L M