Bellman–Ford: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Relax all edges repeatedly to compute shortest distances.

Repeatedly relax all edges so that paths with progressively more edges can improve tentative distances.

Open the 3D visualization

Bellman–Ford example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Bellman–Ford

Learning relaxation across multiple passes.

Choosing an alternative

Dijkstra is faster under its nonnegative-weight assumptions.

Worked example: input and state changes

Example input

0,1,4; 0,2,2; 2,1,1; 1,3,5; 2,3,8; 3,4,2

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial graph.

    State at this checkpoint
    {
      "values": [
        "∞",
        "∞",
        "∞",
        "∞",
        "∞"
      ],
      "active": [],
      "kind": "graph",
      "visited": [],
      "chosen": []
    }
  2. Step 2: Relax 0 → 2; distance = 2.

    State at this checkpoint
    {
      "values": [
        0,
        4,
        2,
        "∞",
        "∞"
      ],
      "active": [
        0,
        2
      ],
      "kind": "graph",
      "visited": [],
      "chosen": []
    }
  3. Step 3: Relax 2 → 1; distance = 3.

    State at this checkpoint
    {
      "values": [
        0,
        3,
        2,
        "∞",
        "∞"
      ],
      "active": [
        2,
        1
      ],
      "kind": "graph",
      "visited": [],
      "chosen": []
    }
  4. Step 5: Relax 3 → 4; distance = 10.

    State at this checkpoint
    {
      "values": [
        0,
        3,
        2,
        8,
        10
      ],
      "active": [
        3,
        4
      ],
      "kind": "graph",
      "visited": [],
      "chosen": []
    }
  5. Step 6: Shortest distances from node 0; ∞ means unreachable.

    State at this checkpoint
    {
      "values": [
        0,
        3,
        2,
        8,
        10
      ],
      "active": [],
      "kind": "graph",
      "visited": [],
      "chosen": []
    }

Expected final state

Shortest distances from node 0; ∞ means unreachable.

Complexity and input bounds

Family: Graphs. Time complexity: O(VE).

Space complexity: O(V) distance storage.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

Directed edges as source,target,weight; separated by semicolons. Nodes 0–7, positive weights 1–99. Start node is 0.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

This bounded positive-weight reference does not detect negative cycles.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Directed edge arrays from, to, weight; nodes 0..vertices-1; source 0. Up to 8 vertices, 16 edges. Allocate output with at least 256 integers. Positive weights in this playground. Writes distances; 1000000 means unreachable. This variant does not check negative cycles. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(from, to, weight, edges, vertices, output) {
    for (let i = 0; i < vertices; i++) { output[i] = 1000000; }
    output[0] = 0;
    for (let pass = 1; pass < vertices; pass++) {
        let changed = 0;
        for (let i = 0; i < edges; i++) {
            let u = from[i]; let v = to[i];
            if (output[u] != 1000000 && output[u] + weight[i] < output[v]) {
                output[v] = output[u] + weight[i]; changed = 1;
            }
        }
        if (changed == 0) { break; }
    }
    return 0;
}

Source

Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.