Guest User
Using app without sign in

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
One pass in topological order, negative weights welcome
Shortest paths in a directed acyclic graph: relax the edges in topological order, negative weights allowed
Select an algorithm and generate steps to begin visualization
In a directed acyclic graph (DAG), shortest paths from a source can be found in linear time by relaxing the edges in topological order. Unlike Dijkstra's algorithm it allows negative edge weights, and unlike Bellman-Ford it needs only one pass over the edges. The same idea with the weights negated gives longest paths, which is how the critical path of a project schedule is computed.
First the vertices are put in a topological order, so that every edge points from an earlier vertex to a later one. All distances start at infinity except the source, which gets 0. Then the vertices are taken in that order, and each one relaxes its outgoing edges: if going through it brings a neighbour closer, the neighbour's distance and predecessor are updated. Every path into a vertex comes only from vertices earlier in the order, so the distance of a vertex is final by the time its turn comes. Vertices before the source in the order stay unreachable. The whole method takes O(V + E) time: one topological sort and one relaxation per edge.
Shortest and longest paths in DAGs schedule projects (the critical path method), order build systems and data pipelines, solve dynamic programming problems whose subproblems form a DAG, such as text segmentation and sequence alignment, and find the cheapest route through layered networks. In algorithms courses it is the standard example of how a topological order turns a problem into a single pass.
In a directed acyclic graph, shortest paths need neither a priority queue nor repeated passes. Relaxing the edges of each vertex once, in topological order, is enough, and negative weights are allowed.
DagShortestPath(graph, s):
order = TopologicalSort(graph)
dist[v] = infinity for every v, dist[s] = 0
for each u in order:
if dist[u] == infinity: continue // not reachable
for each edge (u, v) with weight w:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
parent[v] = u
return dist, parentThe topological order guarantees that every edge into u comes from a vertex earlier in the order. By the time u is processed, all of its predecessors have been finalised and have already relaxed their edges into u, so dist[u] is already the true shortest distance. Each edge is relaxed exactly once, in the one order that makes a single pass sufficient.
Find the shortest paths from S on the example this page loads, the DAG of CLRS figure 24.5, which has two negative edges.
Example graph: Directed edges R-S (5), R-T (3), S-T (2), S-X (6), T-X (7), T-Y (4), T-Z (2), X-Y (-1), X-Z (1) and Y-Z (-2). The topological order is R, S, T, X, Y, Z.
The distances from S are T 2, X 6, Y 5 and Z 3, with R unreachable. The shortest path to Z is S-X-Y-Z with weight 3, even though S-T-Z looks shorter with only two edges and weight 4: the two negative edges make the longer route cheaper. Ten edges were relaxed once each. Dijkstra would have settled Z at 4 too early, and Bellman-Ford would have needed several passes over all ten edges.
Time: O(V + E) · Space: O(V)
The topological sort costs O(V + E) with Kahn's algorithm or DFS. The relaxation pass visits each vertex once and each outgoing edge once, another O(V + E). There is no priority queue, so no log factor, and no repeated passes, so no factor of V. Memory holds the order, the distances and the parent pointers, all O(V). This makes DAG shortest paths the fastest single-source method available, and it applies to every graph without cycles, whatever the signs of the weights.
If the graph is acyclic, this method beats the general ones. If it is not, it cannot be used at all.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Dijkstra's algorithm | The graph may have cycles but all weights are nonnegative. | O((V + E) log V) |
| Bellman-Ford | The graph has cycles and negative weights, and you may need to detect negative cycles. | O(VE) |
| Critical path method | You want the longest path, for example the minimum project duration. Same pass with max instead of min. | O(V + E) |
| BFS | All edges have the same weight, whether or not the graph has cycles. | O(V + E) |
Related algorithms: Topological Sort, Bellman-Ford Algorithm, Dijkstra's Algorithm, Critical Path Method (CPM)