Minimum Spanning Tree: Kruskal's Algorithm Visualized

simulator intermediate ~10 min
Loading simulation...
Kruskal's algorithm builds the MST by greedily adding the cheapest edge that doesn't create a cycle.

A minimum spanning tree connects all V vertices with V-1 edges of minimum total weight. Kruskal's algorithm sorts all edges by weight and adds them greedily, using a union-find structure to detect cycles.

Formula

MST edges = V - 1 for a connected graph with V vertices
T(V, E) = O(E log E) — Kruskal's with union-find

Connecting Everything at Minimum Cost

Imagine you need to connect ten cities with fibre-optic cable and you want to minimize the total length of cable used. You don't need every city connected to every other — just that you can reach any city from any other. This is exactly the minimum spanning tree problem, one of the oldest and most practical problems in combinatorial optimization, with roots going back to Borůvka's 1926 algorithm for electrifying Moravia.

Kruskal's Greedy Strategy

Joseph Kruskal published his elegantly simple algorithm in 1956. Sort all edges by weight. Walk through them in order. If an edge connects two previously disconnected components, include it; if it would create a cycle, skip it. The key data structure is union-find (disjoint set), which answers 'are these two nodes already connected?' in nearly constant time using path compression and union by rank.

Why Greedy Works Here

Not every optimization problem yields to greedy algorithms, but MST does — provably. The cut property guarantees that the lightest edge crossing any cut of the graph must belong to some MST. Since Kruskal's algorithm always adds the globally lightest edge that doesn't form a cycle, every addition respects the cut property. This is one of the cleanest proofs of greedy correctness in all of computer science.

MSTs in the Real World

Beyond network design, MSTs appear in surprising places. In biology, they approximate phylogenetic trees. In computer vision, they segment images by clustering pixels. Single-linkage hierarchical clustering is equivalent to building an MST and cutting its heaviest edges. And the MST gives a quick 2-approximation for the metric travelling salesman problem — a result that connects this elegant structure to one of the hardest problems in mathematics.

FAQ

What is a minimum spanning tree?

A minimum spanning tree (MST) of a weighted, connected graph is a subset of edges that connects all vertices with the minimum possible total edge weight, without forming any cycles. It always contains exactly V-1 edges for V vertices.

How does Kruskal's algorithm work?

Kruskal's algorithm sorts all edges by weight, then iterates through them in order. For each edge, if it connects two different components (checked via union-find), it is added to the MST. Otherwise it is skipped to avoid cycles. The algorithm terminates when V-1 edges have been added.

What is the difference between Kruskal's and Prim's algorithm?

Both find MSTs, but with different strategies. Kruskal's is edge-centric: sort all edges globally and add the cheapest non-cycle edge. Prim's is vertex-centric: grow the tree from a starting node by always adding the cheapest edge leaving the current tree. Prim's is faster on dense graphs, Kruskal's on sparse ones.

What are practical applications of minimum spanning trees?

MSTs are used in network design (laying cable or pipe with minimum cost), cluster analysis (removing the longest MST edges creates clusters), image segmentation, and approximation algorithms for NP-hard problems like the travelling salesman problem.

Sources

Embed

<iframe src="https://homo-deus.com/lab/graph-theory/minimum-spanning-tree/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub