Skip to content
StackDecode
GraphsAnimated

Minimum Spanning Tree

The cheapest set of edges that connects every node. Built by Prim or Kruskal.

Time

O(E log V)

Extra space

O(V)

Use it for

Network design, clustering.

How it works

  1. A spanning tree connects every node using no cycles; a MINIMUM spanning tree has the smallest possible total edge weight.
  2. Kruskal builds it by adding the cheapest edges that do not form a cycle; Prim grows it from one node. Both give the same total weight.
  3. This animation uses Kruskal: sort the edges, then accept each edge that joins two different groups.

Watch it run

Loading animation…