🔴 Bellman-Ford
Shortest paths with negative weights
Pass 0 / 6
Source: A
Setup
Controls
Stats
Pass / V−1
0 / 6
Status
Ready
Relaxed this pass
0
Total relaxations
0
Distances
Info & Theory

Bellman-Ford finds the shortest distance from a single source vertex to every other vertex in a weighted directed graph, and it works even when some edge weights are negative.

Edge relaxation

Every edge (u, v) with weight w is repeatedly relaxed using the rule dist[v] = min(dist[v], dist[u] + w(u, v)). If going through u is shorter than the current best known distance to v, that distance — and the predecessor of v — is updated.

Why V−1 passes suffice

A shortest path that doesn't repeat a vertex visits at most V−1 edges. Each full pass over all edges guarantees at least one more edge of every shortest path is finalized, so after V−1 passes every shortest path (if the graph has no negative cycle) has fully propagated.

The extra V-th pass

Running one more pass after the required V−1 checks for negative cycles: if any distance can still improve, a negative-weight cycle reachable from the source exists, and no shortest path is well-defined for the vertices it touches.

Complexity

Bellman-Ford runs in O(V·E) time and O(V) space. It is slower than Dijkstra's O((V+E) log V), but unlike Dijkstra it tolerates negative weights and can detect negative cycles — Dijkstra's greedy choice breaks down as soon as a negative edge appears.

About Bellman-Ford Algorithm

Written by MySimulator Team · Reviewed by MySimulator Editorial Review

Last updated: 11 July 2026

The Bellman-Ford algorithm, developed independently by Richard Bellman (1958) and Lester Ford Jr. (1956), computes shortest paths from a single source vertex to every other vertex in a weighted directed graph. Unlike Dijkstra's algorithm, it tolerates negative edge weights, making it essential for applications such as currency arbitrage detection and network routing protocols like RIP. Bellman-Ford is a dynamic programming algorithm: it repeatedly relaxes every edge — replacing dist[v] with dist[u] + w(u,v) whenever that sum is smaller — for exactly V−1 passes, since the longest possible simple shortest path visits at most V−1 edges. A final, extra pass checks whether any distance can still improve; if so, the graph contains a negative-weight cycle reachable from the source, meaning no shortest path is well-defined. This gives Bellman-Ford O(V·E) time and O(V) space complexity, slower than Dijkstra's O((V+E) log V) but strictly more general.

Frequently Asked Questions

Why does Bellman-Ford need V−1 passes?

Because the longest possible shortest path in a graph with V vertices (one that doesn't repeat a vertex) can have at most V−1 edges. Each full pass over every edge guarantees that at least one more edge along every shortest path becomes finalized, so after V−1 passes all shortest distances have fully propagated.

How does Bellman-Ford detect negative cycles?

After the required V−1 passes, the algorithm runs one extra pass over all edges. If any edge can still be relaxed — meaning a distance would still decrease — a negative-weight cycle reachable from the source must exist, since a valid shortest-path tree would already be stable by that point.

How does Bellman-Ford compare to Dijkstra's algorithm?

Both compute single-source shortest paths, but Dijkstra uses a greedy priority queue and requires non-negative weights, running in O((V+E) log V). Bellman-Ford relaxes every edge on every pass, tolerates negative weights, and can detect negative cycles, at the cost of a slower O(V·E) running time.

Can Bellman-Ford handle undirected graphs with negative edges?

No. Any undirected edge with a negative weight is equivalent to a two-vertex negative cycle (u→v and v→u), so Bellman-Ford would immediately flag it as a negative cycle. The algorithm is designed for directed graphs; negative weights only make consistent sense once edges have a direction.