Some graphs have negative edge weights, for example a route that earns a refund, or a currency exchange that gains money. Dijkstra’s algorithm fails on such graphs. This page explains what can go wrong, gives the Bellman–Ford algorithm that handles negative edges, and shows a faster method for graphs without cycles.
Negative edges and negative cycles
Two situations can occur when a graph has negative edges:
Negative edges, but every cycle has non-negative total weight: shortest paths exist, but Dijkstra may give wrong answers.
A cycle with negative total weight reachable from the source: going round the cycle again and again makes paths cheaper without limit, so shortest paths are not defined for the vertices reachable from the cycle.
In Figure 8.6 (S→A 5, A→B 5, B→A −3) the cycle A→B→A weighs 5−3=2: case 1. In Figure 8.7 (B→A −8) it weighs 5−8=−3: case 2, since each extra trip round the cycle saves 3.
The Bellman–Ford algorithm
Set d[v] = ∞ for all v, d[s] = 0.
Repeat n−1 times: relax every edge (u,v).
For every edge (u,v): if d[u]+w(u,v)<d[v], report “negative-weight cycle”.
Running time: O(n⋅e), slower than Dijkstra but correct with negative edges.
Shortest paths in a directed acyclic graph
A DAG (directed acyclic graph) has no cycles at all, so negative edges cannot create negative cycles. Its vertices can be put in topological order: an order in which every edge goes from an earlier vertex to a later one. Relaxing the edges of each vertex in topological order gives correct shortest paths in one pass, in O(n+e) time, even with negative weights.
A topological order can be found with a queue (Kahn’s method): repeatedly output a vertex with in-degree 0 and remove its outgoing edges (or by listing vertices in decreasing DFS finishing time).
DAGs model tasks with dependencies (a project plan, a course prerequisite chart); the longest path in such a DAG gives the critical path of a project.
Key takeaways
A reachable negative-weight cycle makes shortest paths undefined.
Bellman–Ford: relax all edges n−1 times, then one check round for negative cycles: O(ne).
A DAG has a topological order; relaxing in that order solves shortest paths in O(n+e).
Dijkstra needs non-negative weights.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain why Dijkstra’s algorithm can fail with negative edges, using the graph s→a 2, s→b 3, b→a −2.
Show worked answer
Dijkstra settles s (d[a] = 2, d[b] = 3), then settles a because 2 < 3, fixing d[a] = 2. Then it settles b (3) and relaxes b→a: 3 − 2 = 1 < 2, but a is already settled, so its distance is not corrected (or, if it is, vertices settled using the old value are not updated). The true shortest distance to a is 1 via s→b→a. Dijkstra’s greedy rule assumes that a path cannot become cheaper by adding edges, which is false with negative weights. Bellman–Ford, which relaxes all edges repeatedly, finds d[a] = 1.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/graphs/negative-edge-weights-and-acyclic-graphs/ to read this page.