Kruskal’s algorithm is a greedy algorithm that finds a minimum spanning tree (MST) of a connected, weighted, undirected graph by repeatedly adding the lightest remaining edge that does not create a cycle. Among graph algorithms in programming, it is a standard example of how a simple local rule can produce a provably optimal global result.
Published by Joseph Kruskal in 1956, the method runs in O(E log E) time, dominated by sorting the edges. Its efficiency in practice depends on a disjoint-set (union-find) structure that answers the question “are these two vertices already connected?” in nearly constant time.
How Kruskal’s algorithm builds a spanning tree
The algorithm treats every vertex as its own one-vertex tree and gradually merges these trees into a single tree. It sorts all edges by weight in ascending order and examines them one by one, accepting an edge only when its two endpoints belong to different components.
- Create a separate set for each of the V vertices.
- Sort the E edges by non-decreasing weight.
- For each edge (u, v), check whether u and v are in the same set.
- If they are not, add the edge to the tree and merge the two sets.
- Stop once the tree contains V − 1 edges or all edges have been examined.
Because the accepted edges always connect separate components, the growing edge set is a forest that never contains a cycle. When the input graph is disconnected, the same procedure returns a minimum spanning forest, one tree per connected component, instead of failing.
The following table shows how each step of the procedure maps to its role and cost.
| Step | Purpose | Typical cost |
|---|---|---|
| Initialization | One set per vertex | O(V) |
| Edge sorting | Process edges from lightest to heaviest | O(E log E) |
| Find operations | Detect whether an edge would close a cycle | O(E α(V)) |
| Union operations | Merge two components | O(V α(V)) |
| Termination check | Stop after V − 1 accepted edges | O(1) per edge |
Why the greedy choice is correct
The correctness of the method rests on the cut property of minimum spanning trees. For any partition of the vertices into two groups, the lightest edge crossing the cut belongs to some minimum spanning tree. Each edge that the algorithm accepts is the lightest edge connecting its component to the rest of the graph at that moment, so it is always a safe choice.
A complementary argument is the cycle property: the heaviest edge on any cycle cannot be required in an MST when weights are distinct. Kruskal’s algorithm rejects exactly those edges that would close a cycle, and since edges arrive in sorted order, a rejected edge is always the heaviest edge on that cycle.
When several edges share the same weight, the algorithm still returns a minimum total weight, but the tree itself may not be unique. Only when all edge weights are distinct is the minimum spanning tree guaranteed to be unique.
The union-find structure behind the speed
A naive cycle check, such as running a breadth-first search for every edge, would cost O(V) per edge and make the algorithm O(E · V) overall. The disjoint-set data structure removes this bottleneck by storing each component as a rooted tree and identifying it by its root.
Two optimizations make the structure fast. Union by rank (or by size) attaches the shorter tree under the taller one, and path compression points every visited node directly at the root during a find. Together they give an amortized cost of α(n) per operation, where α is the inverse Ackermann function, which stays below 5 for any practical input size.
This table compares union-find variants and the resulting cost of the cycle checks.
| Implementation | Cost per operation | Practical effect |
|---|---|---|
| Naive linked sets | O(V) worst case | Slow on large graphs |
| Union by rank only | O(log V) | Acceptable for moderate inputs |
| Path compression only | O(log V) amortized | Good in most cases |
| Rank plus path compression | O(α(V)) amortized | Effectively constant time |
How Kruskal’s algorithm compares with other MST methods
Kruskal’s algorithm is one of three classic MST methods, and the choice among them depends mainly on graph density and the available data representation. Prim’s algorithm grows a single tree outward from a start vertex using a priority queue, much like Dijkstra’s algorithm, while Borůvka’s algorithm lets every component pick its cheapest outgoing edge in parallel rounds.
The comparison below summarizes where each method performs best.
| Algorithm | Strategy | Time complexity | Best suited for |
|---|---|---|---|
| Kruskal’s | Global sorted edge scan | O(E log E) | Sparse graphs, edge lists |
| Prim’s (binary heap) | Grow one tree from a vertex | O(E log V) | Dense graphs, adjacency lists |
| Prim’s (array) | Scan all vertices each step | O(V²) | Very dense graphs |
| Borůvka’s | Parallel cheapest-edge rounds | O(E log V) | Parallel and distributed systems |
A frequent misconception is that Kruskal’s algorithm finds shortest paths. It does not: an MST minimizes the total weight of the whole tree, while Dijkstra’s algorithm and the Bellman–Ford algorithm minimize the distance from one source to each vertex. The path between two vertices inside an MST can be much longer than their shortest path.
Practical use, variants and pitfalls
The algorithm is a natural fit when the graph arrives as an edge list, when edges are already sorted, or when the edge count is small relative to V². Stopping early after V − 1 accepted edges often saves work, and with integer weights in a small range, a counting or radix sort brings the sorting cost close to linear.
Variants extend the idea well beyond network design. Stopping the algorithm at k components instead of one produces single-linkage clustering, and sorting in descending order yields a maximum spanning tree. Typical errors and their remedies are listed below.
| Mistake | Symptom | Fix |
|---|---|---|
| Cycle check by graph search | Quadratic running time | Use union-find with both optimizations |
| Applying it to directed graphs | Invalid or non-minimal result | Use a minimum arborescence algorithm instead |
| Assuming a connected input | Fewer than V − 1 edges returned | Treat the output as a spanning forest |
| Union without find on roots | Corrupted components | Always merge the two root nodes |
Negative edge weights are not a problem for Kruskal’s algorithm, unlike for Dijkstra’s algorithm, because the procedure only compares weights and never sums path lengths. The main limitation is that it requires undirected edges; directed versions of the problem need the Chu–Liu/Edmonds algorithm.
The short version
Imagine several towns that need to be linked by roads, and every possible road has a price. Kruskal’s algorithm looks at all roads from cheapest to most expensive and builds each one unless the two towns are already reachable through roads built earlier.
Following that one rule, the method ends with every town connected and no money wasted on redundant roads. The result is the cheapest possible network that still reaches everyone.
Where it makes a difference
A regional utility plans a fiber backbone for 40 municipal buildings. Engineers estimate the trenching cost of 300 possible links, each between 12,000 and 90,000 dollars depending on distance and terrain, and load them into a small script as an edge list.
The script sorts the 300 links, runs Kruskal’s algorithm with a union-find structure and returns 39 links in well under a second. Compared with an initial hand-drawn plan, the result connects every building while cutting the estimated trenching budget by about 18 percent, and the team then adds two extra links for redundancy as a deliberate engineering choice rather than an accident of the layout.