AlgoMaster Logo

Kruskal's Algorithm

8 min readUpdated May 30, 2026
Listen to this chapter
Unlock Audio

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.

What Is a Minimum Spanning Tree?

A spanning tree of an undirected, connected graph is a subset of edges that:

  1. Connects all vertices (every vertex is reachable from every other vertex)
  2. Contains no cycles (it is a tree)
  3. Uses exactly V - 1 edges (where V is the number of vertices)

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:

  • A graph with V vertices always has exactly V - 1 edges in its MST.
  • If all edge weights are distinct, the MST is unique. If some weights are equal, there may be multiple valid MSTs.
  • An MST is not necessarily the shortest path tree. It minimizes total weight, not individual path lengths.
  • Removing any edge from the MST disconnects the graph. Adding any non-MST edge creates a cycle.

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.

What Is Kruskal's Algorithm?

Kruskal's Algorithm builds the MST with a simple greedy approach:

  1. Sort all edges by weight in ascending order.
  2. Go through edges one by one, from lightest to heaviest.
  3. Add the edge to the MST if it connects two vertices that are not already connected (i.e., it does not create a cycle).
  4. Skip the edge if it would create a cycle.
  5. Stop when you have added V - 1 edges.

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?

How Kruskal's Works

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:

  • Find(x): Which group does vertex x belong to?
  • Union(x, y): Merge the groups containing x and y.

Union-Find fits Kruskal's because of how it models connectivity:

  • Initially, each vertex is in its own group (its own connected component).
  • When we consider an edge (u, v), we check if u and v are in the same group using Find.
  • If they are in different groups, adding this edge is safe (no cycle). We add it and call Union(u, v).
  • If they are in the same group, adding this edge would create a cycle. We skip it.

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:

Implementation

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:

  • The union method returns a boolean that combines the "are they in different sets?" check and the merge into one call.
  • We break early once we have V - 1 edges, avoiding any remaining unnecessary edge processing.

Step-by-Step Walkthrough

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:

EdgeWeight
E-D1
A-C2
C-E3
A-B4
B-C5
E-F6
D-F7
C-D8
B-D10

Now we process edges one by one:

Step 1: Edge E-D (weight 1)

  • Find(E) = 4, Find(D) = 3. Different sets.
  • Add to MST. Union(E, D).
  • MST edges: {E-D}. Total weight: 1.

Step 2: Edge A-C (weight 2)

  • Find(A) = 0, Find(C) = 2. Different sets.
  • Add to MST. Union(A, C).
  • MST edges: {E-D, A-C}. Total weight: 3.

Step 3: Edge C-E (weight 3)

  • Find(C) = 0, Find(E) = 3 (E and D are merged). Different sets.
  • Add to MST. Union(C, E). Now A, C, E, D are all connected.
  • MST edges: {E-D, A-C, C-E}. Total weight: 6.

Step 4: Edge A-B (weight 4)

  • Find(A) = 0, Find(B) = 1. Different sets.
  • Add to MST. Union(A, B). Now A, B, C, E, D are all connected.
  • MST edges: {E-D, A-C, C-E, A-B}. Total weight: 10.

Step 5: Edge B-C (weight 5)

  • Find(B) = 0, Find(C) = 0. Same set!
  • Skip. Adding this would create a cycle (A-B-C-A).

Step 6: Edge E-F (weight 6)

  • Find(E) = 0, Find(F) = 5. Different sets.
  • Add to MST. Union(E, F). Now all vertices are connected.
  • MST edges: {E-D, A-C, C-E, A-B, E-F}. Total weight: 16.

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.

Proof of Correctness

Why does this greedy approach actually produce the optimal result? The answer lies in the cut property of minimum spanning trees.

The Cut Property

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.

Why Kruskal's Satisfies the Cut Property

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.

Complexity Analysis

Time Complexity: O(E log E)

Breaking it down by step:

  1. Sorting edges: O(E log E). This is the dominant cost.
  2. Union-Find operations: We perform at most E 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).
  3. Overall: O(E log E) + O(E alpha(V)) = O(E log 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.

Space Complexity: O(V + E)

  • O(V) for the Union-Find arrays (parent and rank).
  • O(E) for storing the edge list (if not provided as input).
  • The MST itself takes O(V) space (V - 1 edges).

When Does Kruskal's Struggle?

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.

Key Takeaways

  • Kruskal's Algorithm builds a minimum spanning tree by sorting every edge in ascending order of weight, then greedily adding the cheapest edge that connects two currently separate components.
  • A Union-Find data structure answers the cycle question. If the two endpoints of an edge already share a root, adding that edge would form a cycle, so the edge is skipped.
  • The algorithm stops once it has added exactly V - 1 edges, which is the number of edges in any spanning tree of a graph with V vertices.
  • Correctness rests on the cut property. Because edges are processed from lightest to heaviest, each edge that joins two components is the lightest crossing edge for that cut and therefore belongs to some MST.
  • The running time is O(E log E), dominated by sorting the edges, since path compression and union by rank reduce each Union-Find operation to near constant amortized time.
  • Kruskal's is edge-centric and works well on sparse graphs supplied as an edge list, while Prim's with an adjacency matrix can be faster on dense graphs where E approaches V^2.