Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Shortest-path calculator with negative edges
Finds shortest paths and detects negative weight cycles
Select an algorithm and generate steps to begin visualization
The Bellman-Ford algorithm solves the single-source shortest path problem in graphs that may contain negative edge weights, something Dijkstra cannot handle. It also detects negative cycles, cycles whose total weight is below zero, which make shortest paths undefined.
Bellman-Ford relaxes every edge of the graph V - 1 times, where V is the number of vertices. Each pass propagates correct shortest distances one hop further, so after V - 1 passes all shortest paths of at most V - 1 edges are final. A final extra pass checks whether any edge can still be relaxed; if so, the graph contains a negative cycle reachable from the source. The running time is O(VE), slower than Dijkstra but far more general.
Bellman-Ford is used in distance-vector routing protocols such as RIP, in currency arbitrage detection where exchange rates become negative log weights, and in any planning problem where costs can be negative. Interview questions often test whether candidates know when Dijkstra fails and Bellman-Ford is required.
Bellman-Ford is the shortest-path algorithm that gives up cleverness in exchange for generality. There is no priority queue and no ordering decision, just repeated relaxation of every edge.
BellmanFord(graph, source):
for each vertex v: dist[v] = infinity
dist[source] = 0
repeat V - 1 times:
changed = false
for each edge (u, v, w):
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
parent[v] = u
changed = true
if not changed: break // early exit
// One extra pass detects negative cycles
for each edge (u, v, w):
if dist[u] + w < dist[v]:
report negative cycle reachable from sourceThe invariant is that after pass i, every shortest path using at most i edges is correct. Since a shortest path in a graph with no negative cycle uses at most V - 1 edges, V - 1 passes settle everything. If a V-th pass still improves something, a path is getting shorter without bound, which is exactly what a negative cycle means.
Run Bellman-Ford from A on a graph with a negative edge that Dijkstra would get wrong. Edges are relaxed in the fixed order listed below.
Example graph: Directed edges A to B (4), A to C (5), B to C (-3), C to D (2).
Correct distances are A 0, B 4, C 1, D 3. The single negative edge is enough to break Dijkstra, and it is the reason Bellman-Ford exists.
Time: O(VE) · Space: O(V)
The algorithm performs V - 1 passes, and each pass relaxes all E edges once, giving O(VE). Space is one distance and one parent entry per vertex, so O(V), notably independent of E. On a dense graph where E approaches V squared, the running time approaches O(V cubed), which is why Bellman-Ford is reserved for cases where negative weights genuinely occur. The early-exit check, stopping as soon as a pass changes nothing, often finishes in a handful of passes on real graphs even though the worst case remains V - 1.
Bellman-Ford is strictly more general than Dijkstra and strictly slower. Pick it only when you actually need what it buys.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Dijkstra's algorithm | All edge weights are nonnegative. Substantially faster and the correct default. | O((V + E) log V) |
| BFS | The graph is unweighted, so hop count is distance. | O(V + E) |
| Floyd-Warshall | You need all-pairs distances rather than single-source, and the graph is small or dense. | O(V^3) |
| SPFA (queue-based Bellman-Ford) | Negative weights on a sparse graph. Much faster in practice, though the worst case is still O(VE). | O(VE) worst case |
| Johnson’s algorithm | All-pairs shortest paths with negative weights on a sparse graph. Uses Bellman-Ford once to reweight, then Dijkstra from each node. | O(V·E + V^2·log V) |
Read the full article: Shortest Path Algorithms Explained
Related algorithms: Dijkstra's Algorithm, Floyd-Warshall Algorithm, Cycle Detection