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 7A to C, weight 6A to D, weight 6C to E, weight 5E to F, weight 3E to G, weight 7E to H, weight 1D to I, weight 3I to J, weight 3A to E, weight 8A to H, weight 5B to C, weight 8C to I, weight 6D to H, weight 9F to H, weight 4G to I, weight 17665371338586941AABBCCDDEEFFGGHHIIJJQueue, nothing left beforeQueuenothing left beforeOrder so far, earliest firstOrder so farearliest first
  • Being taken

The graph has 10 nodes and 16 edges, and nothing has been explored yet. A topological sort starts by counting what points into each node.

The graph has 10 nodes and 16 edges, and nothing has been explored yet. A topological sort starts by counting what points into each node.
  1. 1count how many edges point into each node
  2. 2every node with nothing pointing into it is free, so it joins the queue
  3. 3take a free node and add it to the order
  4. 4one fewer edge points into this neighbour now
  5. 5nothing points into it any more, so it is free and joins the queue
  6. 6every node is in the order, so the order is valid
  7. 7the queue is empty but nodes remain, so a cycle means no order exists
Placed in the order
0
Nodes taken
0
Edges examined
0
Added to the queue
0
Most free at once
0

Complexity

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

Topological sort

This is Kahn’s algorithm. It counts, for every node, how many edges point *into* it. Any node with a count of zero has nothing that must come before it, so it can be emitted next; removing it decreases the count of everything it points at, which may free more nodes. The queue holds exactly the nodes that are currently free.

Its frontier

A queue of nodes with nothing left before them.

When it is the right choice

Any time dependencies have to be turned into an order: build systems, module loading, course prerequisites, spreadsheet recalculation, task scheduling. It is also the cheapest reliable way to find out whether a directed graph has a cycle at all — which is what the next paragraph is about.

How it works

Worth noticing

Cost

Best
O(V + E)
Average
O(V + E)
Worst
O(V + E)
Extra space
O(V)
Runs on
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