Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Minimum spanning tree calculator
Finds minimum spanning tree by sorting edges and using Union-Find
Select an algorithm and generate steps to begin visualization
Kruskal's algorithm finds a minimum spanning tree by considering edges in increasing order of weight and adding each edge that does not create a cycle. Unlike Prim's tree-growing approach, Kruskal grows a forest of components that gradually merge into one tree.
After sorting all edges by weight, the algorithm walks through them smallest first. For each edge it uses a Union-Find (disjoint set) structure to check in near-constant time whether the two endpoints are already in the same component. If they are not, the edge is accepted and the components are merged; otherwise it is skipped as a cycle edge. Sorting dominates the cost, giving O(E log E) time.
Kruskal's algorithm is preferred for sparse graphs and for problems where edges arrive pre-sorted, such as single-linkage clustering, image segmentation, and network design with cost tiers. The embedded Union-Find structure is itself a top interview topic, covering path compression and union by rank.
Sort every edge by weight, then walk the list adding any edge that joins two different components. Union-Find makes the "different components" test almost free.
Kruskal(graph):
sort all edges by weight, ascending
makeSet(v) for every vertex v
mst = []
for each edge (u, v, w) in sorted order:
if find(u) != find(v): // different components
union(u, v)
mst.append((u, v, w))
if mst has V - 1 edges: break
return mstKruskal grows a forest, not a tree. Several disconnected fragments develop independently and merge as cheap edges join them, which is the structural difference from Prim and the reason Kruskal handles disconnected graphs for free: it simply returns a minimum spanning forest. Correctness follows from the cut property applied to the component boundaries, exactly as with Prim.
Build the minimum spanning tree on the same graph used for the Prim example, so the two orders of discovery can be compared.
Example graph: Undirected edges A-B (2), A-C (3), B-C (1), C-D (4) and B-D (7).
The minimum spanning tree is B-C, A-B and C-D with total weight 1 + 2 + 4 = 7, identical to what Prim produced from A. The trees match, as they must when edge weights are distinct, but the discovery order differs: Prim went A-B, B-C, C-D growing outward from A, while Kruskal went B-C, A-B, C-D in pure weight order and had to explicitly reject a cycle-closing edge along the way.
Time: O(E log E) · Space: O(V + E)
Sorting the edges dominates everything else at O(E log E), which is the same as O(E log V) since E is at most V squared and so log E is within a constant factor of log V. After sorting, the loop performs at most 2E find operations and V - 1 unions. With both path compression and union by rank, each of those costs the inverse Ackermann function of V, which is below 5 for any input that fits in memory and is treated as constant. So the Union-Find part is effectively O(E) and the sort is the whole cost. When edges arrive already sorted, or can be bucketed because weights are small integers, Kruskal drops to near-linear and clearly beats Prim.
Kruskal and Prim solve the same problem. Density, edge ordering and connectivity decide which is better.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Prim's algorithm | Dense graphs, where E approaches V squared and sorting all edges is wasteful. | O(E log V) or O(V^2) |
| Boruvka's algorithm | You want to parallelise. Each component picks its cheapest outgoing edge simultaneously. | O(E log V) |
| Kruskal with bucket sort | Weights are small integers, so the sort becomes linear and Kruskal becomes near-linear overall. | O(E·α(V)) |
| Minimum spanning forest | The graph is disconnected. Kruskal already does this with no modification at all. | O(E log E) |
Read the full article: Minimum Spanning Trees: Prim, Kruskal and Boruvka
Related algorithms: Prim's MST Algorithm, Borůvka's Algorithm, Cycle Detection