Skip to content

Resource · Guide or article

Bellman–Ford Algorithm

Introduces Bellman-Ford, which computes single-source shortest paths in a weighted directed graph. Unlike Dijkstra's algorithm it accepts negative edge weights, at the cost of running slower.

Reviewed

Open the original: Bellman–Ford Algorithm (opens the original site in a new tab)

Practice this topicOpens the practice interviewer with a question on Graphs.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. Worst-case performance O(|V||E|) Best-case performance O(|E|) Worst-case space complexity O(|V|)

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Reference

What it covers

  • The tradeoff against Dijkstra: slower, but able to handle negative weights
  • An animation contrasting Dijkstra and Bellman-Ford on the same graph
  • Worst-case time O(|V||E|), best-case O(|E|) and worst-case space O(|V|)

How to use it

PrepKit study advice, not a claim made by the source.

Implement the relax-every-edge loop, add a final pass that reports a negative cycle, and test on a graph with one negative edge where Dijkstra returns the wrong distance.

Scope and limitations

Only a few lines long. Relaxation steps, negative-cycle detection and a worked example are left to a Wikipedia article and a YouTube video.

Access and format

Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/graph/bellman-ford/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email