A Minimum Spanning Tree (MST) is the cheapest set of edges that connects every vertex in a weighted, undirected graph without forming a cycle. The problem shows up in network design, circuit wiring, clustering, image segmentation, and coding interviews.
Kruskal's Algorithm is one of the two standard MST algorithms (the other being Prim's). It works by sorting all edges by weight and greedily picking the cheapest edges that do not form a cycle, using Union-Find to detect cycles efficiently.
A spanning tree of an undirected, connected graph is a subset of edges that:
A minimum spanning tree (MST) is a spanning tree whose total edge weight is as small as possible. In other words, among all possible spanning trees, the MST has the lowest sum of edge weights.
Here are a few important properties of an MST:
Let us look at a concrete example. Consider this weighted graph with 6 vertices:
Here is its MST, highlighted with green edges. It uses exactly 5 edges (V - 1 = 6 - 1 = 5) and connects all 6 vertices:
The total MST weight is 2 + 4 + 3 + 1 + 6 = 16. No other spanning tree of this graph has a smaller total weight.
Kruskal's Algorithm builds the MST with a simple greedy approach:
That is the entire algorithm. By always picking the cheapest available edge that does not form a cycle, you end up with the minimum spanning tree. This is the greedy choice, and it works because of a property called the cut property, which this chapter proves in a later section.
This raises a question: how do you efficiently check whether adding an edge would create a cycle?
The cycle detection problem is solved with Union-Find (also called Disjoint Set Union). Union-Find is a data structure that tracks which elements belong to which group and supports two operations:
Union-Find fits Kruskal's because of how it models connectivity:
Find.Union(u, v).The combination of path compression and union by rank makes both operations nearly O(1) amortized, which keeps the overall algorithm fast.
Let us visualize the full process:
Here is the full implementation of Kruskal's Algorithm. We need two pieces: the Union-Find data structure and the main algorithm that sorts edges and builds the MST.
A few implementation details:
union method returns a boolean that combines the "are they in different sets?" check and the merge into one call.Let us trace Kruskal's Algorithm on the same graph from earlier. The vertices are A(0), B(1), C(2), D(3), E(4), F(5).
All edges sorted by weight:
| Edge | Weight |
|---|---|
| E-D | 1 |
| A-C | 2 |
| C-E | 3 |
| A-B | 4 |
| B-C | 5 |
| E-F | 6 |
| D-F | 7 |
| C-D | 8 |
| B-D | 10 |
Now we process edges one by one:
Step 1: Edge E-D (weight 1)
Step 2: Edge A-C (weight 2)
Step 3: Edge C-E (weight 3)
Step 4: Edge A-B (weight 4)
Step 5: Edge B-C (weight 5)
Step 6: Edge E-F (weight 6)
We have 5 edges (V - 1 = 5), so the MST is complete.
Final MST:
Total MST weight: 16. Edge B-C (weight 5) was rejected because B and C were already connected through A, which is how Union-Find prevents cycles without ever needing to search the graph.
Why does this greedy approach actually produce the optimal result? The answer lies in the cut property of minimum spanning trees.
A cut in a graph is a partition of the vertices into two non-empty groups. The edges that cross this cut (one endpoint in each group) are called crossing edges.
Cut Property: For any cut in the graph, the minimum-weight crossing edge is part of some MST.
This is the fundamental theorem behind both Kruskal's and Prim's algorithms. The intuition: given two groups of vertices with no edge between them, you need at least one crossing edge to connect them. If any spanning tree picks a crossing edge other than the cheapest one, that edge can be swapped for the cheapest one to produce a tree with equal or lower total weight. So the cheapest crossing edge is always a safe choice.
When Kruskal's considers an edge (u, v) and finds that u and v are in different components, that edge is crossing the cut between those two components. Since we process edges in order of increasing weight, this edge is guaranteed to be the lightest crossing edge for that particular cut. By the cut property, it belongs to some MST.
Since every edge we add satisfies the cut property, and we add exactly V - 1 edges without creating cycles, the result must be a minimum spanning tree.
Breaking it down by step:
find and V union operations. With path compression and union by rank, each operation is O(alpha(V)), where alpha is the inverse Ackermann function. For all practical purposes, alpha(V) is less than 5 for any V that fits in the universe. So the total Union-Find cost is O(E * alpha(V)), which is effectively O(E).Since E is at most V^2, we have log E = O(log V), so O(E log E) = O(E log V). Both expressions are equivalent.
For very dense graphs where E approaches V^2, the sorting step becomes O(V^2 log V), which can be slow. In those cases, Prim's with an adjacency matrix runs in O(V^2), which is faster. For sparse graphs where E is close to V, Kruskal's sorting step is only O(V log V), which is very fast.