Graph algorithms

O(E log 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 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 7565558723788657AABBCCDDEEFFGGHHIIJJSorted edges, cheapest firstSorted edgescheapest firstDisjoint sets, one per groupDisjoint setsone per group
  • Being taken

The graph has 10 nodes and 15 edges, and nothing has been explored yet. Kruskal starts with every node in a group of its own.

The graph has 10 nodes and 15 edges, and nothing has been explored yet. Kruskal starts with every node in a group of its own.
  1. 1put every node in a group of its own
  2. 2sort the edges by weight, cheapest first
  3. 3take the next cheapest edge and ask whether its ends are already in one group
  4. 4they are, so this edge would close a cycle: reject it
  5. 5they are not, so take the edge and merge the two groups
  6. 6the edges have run out: one group left is a tree, more than one is a forest
Edges taken
0
Rejected for a cycle
0
Edges considered
0

Complexity

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

Kruskal

Kruskal sorts the edges cheapest first and then considers each in turn. An edge whose two ends are already in the same group would close a cycle, so it is rejected; an edge joining two different groups is taken, and the two groups merge. When one group is left, its edges are a minimum spanning tree. The groups are kept in a union-find, which answers "are these two in the same group?" in almost constant time.

Its frontier

A sorted edge list, and the disjoint sets themselves.

When it is the right choice

Whenever the cheapest set of links that connects everything is what is wanted: laying cable, clustering by nearest neighbour, building a maze. It is the natural choice when the edges are already sorted or easy to sort, and when the graph is sparse. On a disconnected graph it produces a spanning forest rather than failing, which is usually the right behaviour.

How it works

Worth noticing

Cost

Best
O(E log E)
Average
O(E log E)
Worst
O(E log E)
Extra space
O(V)
Runs on
a weighted undirected graph
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