Shortest Path Algorithms

What Is the Bellman-Ford Algorithm?

Bellman-Ford finds shortest paths from one source vertex, even when some edges are negative. Learn how it relaxes edges and detects negative cycles.

On this page
  1. The short answer
  2. The problem it solves
  3. Relaxation: the core step
  4. The main loop
  5. A worked example
  6. Detecting a negative cycle
  7. Running time
  8. Why it is called dynamic programming
  9. When to use Bellman-Ford

The short answer

The Bellman-Ford algorithm finds the shortest paths from one source vertex to every other vertex in a weighted graph. Unlike Dijkstra’s algorithm, it still works when some edge weights are negative. It can also detect a negative cycle, which makes shortest paths undefined. It is named after Richard Bellman and Lester Ford Jr., who developed the approach.

The problem it solves

A weighted graph has vertices joined by edges, and each edge has a weight, such as a distance, cost or time. The length of a path is the sum of its edge weights. Given a source vertex, the algorithm finds the path with the smallest total weight to each other vertex.

It stores one distance estimate per vertex. The source starts at 0. Every other vertex starts at infinity, which means no path has been found yet.

Relaxation: the core step

The algorithm improves its estimates with a step called relaxation. For an edge from u to v with weight w, it checks whether the distance to u plus w is less than the current distance to v:

  • If it is, the distance to v is replaced with that smaller value.
  • If it is not, nothing changes.

Each successful relaxation means the algorithm has found a cheaper way to reach a vertex.

The main loop

Bellman-Ford relaxes every edge in the graph, and repeats that whole pass V − 1 times, where V is the number of vertices. The number of passes comes from a simple fact. If the graph has no negative cycles, a shortest path never needs to visit the same vertex twice, so it uses at most V − 1 edges. After each pass, every vertex whose shortest path has that many edges or fewer has its correct distance.

A worked example

Take three vertices, S, A and B, with these edges:

  • S to A, weight 4
  • S to B, weight 5
  • B to A, weight −3

Start with S = 0, A = infinity and B = infinity. Then run the first pass over the edges in the order listed:

  1. S to A: 0 + 4 = 4, which is less than infinity, so A becomes 4.
  2. S to B: 0 + 5 = 5, so B becomes 5.
  3. B to A: 5 + (−3) = 2, which is less than 4, so A becomes 2.

The direct edge from S to A gave 4, but the longer route through B gives 2. After the first pass, S = 0, A = 2 and B = 5. The second pass changes nothing, so the algorithm can stop there. The final answer is A = 2 and B = 5.

Detecting a negative cycle

After the V − 1 passes, Bellman-Ford runs one more pass. If any distance can still be lowered, a negative cycle is reachable from the source. A negative cycle is a loop whose total weight is below zero, so each trip around it lowers the distance again. The extra pass catches that. The algorithm can report that shortest paths are not defined for the graph.

Running time

In the worst case, Bellman-Ford takes O(V × E) time, where E is the number of edges. That is V − 1 passes over E edges. Many implementations stop early when a pass makes no changes, which can save time, but the worst case stays the same. If big O notation is unfamiliar, big o data structures explains how to read these expressions.

For comparison, Dijkstra’s algorithm with a binary heap takes O((V + E) log V) time, which is faster on most graphs, but it requires non-negative edge weights.

Why it is called dynamic programming

Bellman-Ford builds its final distances from the results of earlier passes, reusing those results rather than recalculating them. That is why it is often described as a dynamic programming approach. The broader difference between dynamic programming and divide and conquer is explained in divide and conquer algorithm vs dynamic programming.

When to use Bellman-Ford

Bellman-Ford is the right choice when:

  • some edges have negative weights, for example in models where an edge represents a gain,
  • you need to check whether a negative cycle is reachable from the source, or
  • the graph is small enough that the slower running time does not matter.

When every edge weight is zero or positive, Dijkstra’s algorithm is usually the better choice. For a side-by-side comparison, see dijkstra algorithm vs bellman ford.

The complete guide to shortest path algorithms

Dijkstra Algorithm vs. Bellman-Ford
Read the guide

Keep exploring

Guides from other topics

All topics →