Shortest Path Algorithms

Dijkstra Algorithm vs. Bellman-Ford

Dijkstra and Bellman-Ford both find shortest paths from one start vertex. Compare their speed, edge weight limits, negative cycle handling and best uses.

On this page
  1. Dijkstra vs. Bellman-Ford at a glance
  2. The problem both algorithms solve
  3. How Dijkstra’s algorithm works
  4. How the Bellman-Ford algorithm works
  5. Running time compared
  6. Negative edge weights: where Dijkstra breaks
  7. Negative cycles
  8. Which one to use
  9. Where each one appears in networks
  10. Johnson’s algorithm: using both
  11. Bottom line

Dijkstra vs. Bellman-Ford at a glance

Dijkstra’s algorithm and the Bellman-Ford algorithm both solve the same problem: starting from one vertex in a weighted graph, they find the cheapest path to every other vertex that can be reached. The main difference is which edge weights each one accepts. Dijkstra’s algorithm requires every edge weight to be zero or positive, and in return it is faster. Bellman-Ford accepts negative edge weights and can report negative cycles, but it does more work to do so.

Dijkstra’s algorithm Bellman-Ford algorithm
Problem solved Single-source shortest paths Single-source shortest paths
Edge weights Must be zero or positive Negative weights allowed
Negative cycles Not designed for them; results can be wrong Detected and reported
Core method Greedy, using a priority queue Repeatedly relaxes every edge
Running time O((V + E) log V) with a binary heap O(V × E)
Typical use Route planning, OSPF routing RIP routing, finding negative cycles

The problem both algorithms solve

A weighted graph is made of vertices, which are points, joined by edges. Each edge has a weight, which can stand for a distance, a cost or a travel time. The length of a path is the sum of the weights of its edges. The single-source shortest path problem asks for the path with the smallest total weight from a chosen source vertex to each other vertex.

Both algorithms keep a distance estimate for every vertex. The source starts at 0, and every other vertex starts at infinity, meaning “not reached yet.” Whenever an algorithm finds a cheaper way to reach a vertex, it lowers that vertex’s estimate. This step is called relaxation. Relaxing an edge from u to v means checking whether the distance to u plus the weight of that edge is less than the current distance to v. If it is, the distance to v is replaced.

The two algorithms differ in how they choose which edges to relax and when they stop.

How Dijkstra’s algorithm works

Dijkstra’s algorithm is a greedy method. It keeps the unvisited vertices in a priority queue ordered by their current distance estimate. At each step it removes the vertex with the smallest estimate, treats that distance as final, and relaxes the edges leaving it. A vertex that has been removed is never updated again.

This is what makes it fast. It works because, when every edge weight is zero or positive, extending a path never makes it shorter. Once a vertex has the smallest estimate left in the queue, nothing found later can beat it. Negative weights break this assumption, as shown below.

How the Bellman-Ford algorithm works

Bellman-Ford does not choose vertices in order. Instead, it relaxes every edge in the graph, then repeats the whole pass. If the graph has no negative cycles, the cheapest path to any vertex uses at most V − 1 edges, where V is the number of vertices. After V − 1 passes, every distance has had enough rounds to settle.

Bellman-Ford then makes one more pass as a check. If any distance can still be lowered, the graph contains a negative cycle that the source can reach. Many implementations stop early when a pass changes nothing, which can save time in practice.

Because it builds its answers from the results of earlier rounds, Bellman-Ford is often described as a dynamic programming approach. For a broader look at how that method differs from divide and conquer, see divide and conquer algorithm vs dynamic programming.

Running time compared

Dijkstra’s running time depends on the data structure used for its priority queue. With a binary heap it takes O((V + E) log V) time, where V is the number of vertices and E is the number of edges. With a plain array instead of a heap, it takes O(V²) time. If big O notation is new to you, big o data structures gives a short refresher on reading these expressions.

Bellman-Ford takes O(V × E) time in the worst case, because it may make V − 1 passes over all E edges. On a graph with many edges, that is far more work than Dijkstra’s approach. Early stopping can help when the distances settle quickly, but the worst case stays the same.

Negative edge weights: where Dijkstra breaks

Negative edge weights are the main reason to choose Bellman-Ford. Consider three vertices, A, B and C. The edge from A to B has weight 2, the edge from A to C has weight 5, and the edge from C to B has weight −4.

The cheapest route from A to B is A to C to B, with a total of 5 + (−4) = 1. Dijkstra’s algorithm does not find it. It first sees the direct edge to B with a cost of 2. Since 2 is the smallest estimate in the queue, it finalizes B at 2. When it later reaches C and looks at the edge to B, B is already final, so the cheaper route is never applied. Dijkstra’s algorithm reports 2, which is wrong.

Bellman-Ford gets this right. On its first pass it sets B to 2 from the direct edge and C to 5. Then the edge from C to B gives 5 + (−4) = 1, which is lower than 2, so B is updated to 1.

Negative cycles

A negative cycle is a loop whose edge weights add up to less than zero. Going around it again makes the total smaller, so the shortest path can keep getting cheaper and no shortest path exists. Bellman-Ford detects this with its extra check pass. Dijkstra’s algorithm has no such check, so with a negative cycle its numbers may not mean anything.

In an undirected graph, any negative edge is already a negative cycle, because you can travel back and forth along it. Bellman-Ford will therefore always report a negative cycle in that case, and shortest paths are not well defined. Undirected graphs with negative weights call for a different approach.

Which one to use

Use Dijkstra’s algorithm when:

  • every edge weight is zero or positive, and
  • you need speed on large graphs, such as route finding.

Use Bellman-Ford when:

  • some edge weights can be negative, for example in models where an edge represents a gain,
  • you need to check for negative cycles, or
  • the graph is small enough that the extra running time is acceptable.

Where each one appears in networks

Routing protocols show both approaches in use. RIP is a distance-vector protocol based on Bellman-Ford: each router shares its distance estimates with its neighbors and updates its own table from what they report. OSPF is a link-state protocol: each router learns a map of the whole network and runs Dijkstra’s algorithm on that map to build its shortest-path tree.

Other graph problems need different methods. Finding the cheapest set of edges that connects every vertex is a separate problem, covered in prim’s algorithm vs kruskal.

Johnson’s algorithm: using both

Johnson’s algorithm finds shortest paths between every pair of vertices in a graph that has negative edges but no negative cycles. It first runs Bellman-Ford from an extra helper vertex to compute adjustments that make every edge weight non-negative, without changing which paths are shortest. Then it runs Dijkstra’s algorithm from each vertex on the adjusted graph.

Bottom line

Choose Dijkstra’s algorithm for fast shortest paths when no edge weight is negative. Choose Bellman-Ford when negative weights are possible or when you need to check for negative cycles, and accept its slower running time.

Keep exploring

Guides from other topics

All topics →

AVL Trees in Data Structures

An AVL tree is a self-balancing binary search tree. Learn how balance factors and rotations keep search, insert, and delete at O(log n) in the worst case.

AVL Trees 4 min read