The problem
Six places joined by one-way arrows. Each arrow has a cost, its weight: S to A costs 2. B to C costs −2, a refund. No loop of arrows, so this is a DAG. Goal: the cheapest total cost from S to every place.
First, a topological sort lists the places so every arrow points forward: S, A, B, C, D, T. Here every arrow points left to right. So when we reach a place, every arrow into it starts at a place we have already passed.
Each place keeps dist: the cheapest cost found so far. S costs 0 to reach from itself. Every other place starts at ∞ (infinity), meaning no path found yet. These numbers only ever go down.
One pass in order
Visit S. For each arrow out, ask: is dist[S] plus the weight cheaper than the far end’s dist? 0 + 2 = 2 beats ∞, so A becomes 2. 0 + 6 = 6 beats ∞: B becomes 6. This check is relaxing an arrow.
Visit A, whose dist is 2. Going on to B costs 2 + 3 = 5, cheaper than the 6 from the direct arrow, so B drops to 5 and remembers A as its parent. C gets 2 + 7 = 9 for now.
Visit B, now 5. Its only arrow costs −2: 5 + (−2) = 3, cheaper than 9. C drops to 3. The longer path S, A, B, C is cheaper than the short one through A alone. The order makes the negative weight safe.
Visit C, now 3. Both arrows into C were already relaxed, so 3 is final. C sends 3 + 1 = 4 to D and 3 + 2 = 5 to T. Both beat ∞.
Visit D, now 4. Its arrow to T gives 4 + 3 = 7, but T already has 5. 7 is not cheaper, so T keeps 5. Visiting T relaxes nothing: it has no arrows out. One pass, and we are done.
Why it works
The cheapest path to any place v arrives from some place u before v in the order. By the time we visit v, u was final and its arrow to v was relaxed. So dist[v] is final too. The parent arrows trace each best path.
The topological sort is O(V + E). Then each place is visited once and each arrow relaxed once: 6 visits, 8 relaxations. Total O(V + E). Bellman-Ford needs O(V·E) for any graph; Dijkstra needs no negative weights.
The whole function in Python
import math
from graphlib import TopologicalSorter
def dag_shortest(graph, source):
preds = {u: [] for u in graph}
for u in graph:
for v, w in graph[u]:
preds[v].append(u)
order = list(TopologicalSorter(preds).static_order())
dist = {u: math.inf for u in graph}
parent = {u: None for u in graph}
dist[source] = 0
for u in order:
for v, w in graph[u]:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
parent[v] = u
return dist, parent
graph = {
"S": [("A", 2), ("B", 6)],
"A": [("B", 3), ("C", 7)],
"B": [("C", -2)],
"C": [("D", 1), ("T", 2)],
"D": [("T", 3)],
"T": [],
}
print(dag_shortest(graph, "S"))
What happens
# step 1/10 · Arrows with costs
a weighted DAG: one-way arrows, no directed cycle
the cost of a path = the sum of its arrow weights
S -> A 2
S -> B 6
A -> B 3
A -> C 7
B -> C −2
C -> D 1
C -> T 2
D -> T 3
in Python, each node lists (next node, weight) pairs:
graph = {
"S": [("A", 2), ("B", 6)],
"A": [("B", 3), ("C", 7)],
"B": [("C", -2)],
"C": [("D", 1), ("T", 2)],
"D": [("T", 3)],
"T": [],
}
This is DAG relaxation from MIT 6.006 Lecture 11, AcyclicSP in Sedgewick and Wayne §4.4, and DAG-SHORTEST-PATHS in CLRS §22.2 (4th ed.; §24.2 in the 3rd). All three relax the arrows of each node in topological order and run in O(V + E) time. Negative weights are fine here because a DAG has no cycle: no path can loop back to make itself cheaper again and again. Bellman-Ford handles negative weights on any graph, at O(V·E) cost.