Graph algorithms

O(V · E)·O(V) space
Whole graph

Drag to pan; pinch or Ctrl+scroll to zoom, which stops at the whole graph; arrow keys pan once the picture has focus.

A to B, weight -2A to C, weight 4B to D, weight 3A to E, weight 1D to F, weight 9C to G, weight 1G to H, weight 7C to I, weight 1A to D, weight 3A to F, weight 6B to H, weight -4C to D, weight 4D to G, weight 5E to H, weight 3-2431917136-4453A, the startABBCCDDEEFFGGHHIIEdge list, swept in orderEdge listswept in order
  • Being taken

The graph has 9 nodes and 14 edges, and nothing has been explored yet. Bellman-Ford keeps no frontier at all: it sweeps every edge in the graph, over and over.

The graph has 9 nodes and 14 edges, and nothing has been explored yet. Bellman-Ford keeps no frontier at all: it sweeps every edge in the graph, over and over.
  1. 1set the source to zero and every other node to infinity
  2. 2begin a sweep of the whole edge list
  3. 3going through this edge is cheaper, so record the new cost
  4. 4this edge offers nothing better, so move on
  5. 5that sweep changed nothing, so nothing later will either
  6. 6every cost is final, so report them
  7. 7a sweep still improves something, so a negative cycle exists and no shortest path does
Sweeps made
0
Edges relaxed
0
Costs improved
0

Complexity

BestO(E)
AverageO(V · E)
WorstO(V · E)
Extra spaceO(V)
V · E at n=9 predicts 126; this run took 0.

Bellman-Ford

Bellman-Ford makes no attempt to be clever about order. It sweeps the entire edge list and relaxes every edge; then it does it again, up to V−1 times in all. A shortest path uses at most V−1 edges, and each sweep is guaranteed to get at least one more edge of every shortest path right, so V−1 sweeps are enough. One more sweep that still improves something proves there is a negative cycle.

Its frontier

No frontier at all: the whole edge list, swept in order.

When it is the right choice

When an edge can be negative and Dijkstra is therefore not available: currency arbitrage, any cost that can be a refund, distance-vector routing. It is also the one to use when detecting a negative cycle is itself the goal. On a graph with no negative edges it is simply slower than Dijkstra for the same answer, so it is not a general-purpose replacement.

How it works

Worth noticing

Cost

Best
O(E)
Average
O(V · E)
Worst
O(V · E)
Extra space
O(V)
Runs on
a weighted undirected graph; a directed acyclic graph; a directed graph with a cycle
Largest grid
11 by 7 cells
Largest graph
14 nodes, 28 edges

The limits are pedagogical rather than technical: this tool keeps a cost and a predecessor for every node in every recorded step, so the size of the graph sets the size of the trace directly, and a run has to stay short enough to sit and watch. A graph that would take more than three thousand steps on this algorithm is refused before a single step is generated, from a formula in the number of nodes and edges rather than by running it and finding out.

Graphs are generated from a seed, so the link in your address bar draws exactly the graph on your screen — no part of it is random in a way a reload could change.

Worth comparing

All graph algorithms