Graph algorithms

O(E + V log V)·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 5B to C, weight 6C to D, weight 5A to E, weight 5D to F, weight 5C to G, weight 8C to H, weight 7F to I, weight 2B to J, weight 3B to G, weight 7C to I, weight 8D to G, weight 8E to H, weight 6E to I, weight 5G to H, weight 7565558723788657A, the startABBCCDDEEFFGGHHIIJ, the targetJPriority queue, cheapest firstPriority queuecheapest first
  • Being taken

The graph has 10 nodes and 15 edges, and nothing has been explored yet. Dijkstra orders its queue by the cost from A alone.

The graph has 10 nodes and 15 edges, and nothing has been explored yet. Dijkstra orders its queue by the cost from A alone.
  1. 1set the source to zero and every other node to infinity
  2. 2take the cheapest node in the priority queue
  3. 3a cheaper copy of this node was already settled, so drop it
  4. 4settle it: nothing left in the queue can reach it more cheaply
  5. 5offer each neighbour a route through it, and record the offer if it is cheaper
  6. 6the neighbour already has a route at least as cheap, so leave it alone
  7. 7stop once the target is settled, or once the queue empties
Nodes settled
0
Edges relaxed
0
Added to the queue
0
Largest the queue got
0

Complexity

BestO(E + V log V)
AverageO(E + V log V)
WorstO(E + V log V)
Extra spaceO(V)
V + E at n=10 predicts 25; this run took 0.

Dijkstra

Dijkstra keeps a tentative cost for every node and a priority queue ordered by that cost. It repeatedly takes the cheapest node in the queue, declares its cost final, and then relaxes every edge leaving it — offering each neighbour a cheaper route through the node just settled. The order of settling is by cost, not by arrival, which is the whole difference from breadth-first search.

Its frontier

A priority queue, smallest cost first.

When it is the right choice

Any shortest-path question where the edges cost different non-negative amounts: road distances, network latency, a weighted grid. It is the default for a reason — it settles each node once and touches each edge once. It cannot be used when an edge can be negative, and that is not a limitation to work around but a consequence of the argument that makes it correct.

How it works

Worth noticing

Cost

Best
O(E + V log V)
Average
O(E + V log V)
Worst
O(E + V log V)
Extra space
O(V)
Runs on
a grid with walls; 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