Skip to main content

Kruskal's Algorithm

Kruskal's algorithm builds a minimum spanning tree (MST) of a connected, undirected, weighted graph by sweeping the edges in nondecreasing order of weight and accepting each edge that does not form a cycle with the edges already chosen. "Does it form a cycle" is answered in near-constant amortized time by a disjoint-set union (union-find) structure: two endpoints are already connected if and only if they share a root.

The algorithm is a textbook greedy: the global optimum is assembled from locally cheapest non-cycling choices. Its correctness rests on the cut property of MSTs, proved below.

The solve function takes [n, edges] where edges is a list of [u, v, w] triples on vertices 0,1,,n10, 1, \dots, n - 1, and returns the total weight of the MST. If the graph is disconnected, it returns -1.

Codes

def solve(xs):
"""xs = [n, edges]. Return total MST weight, or -1 if disconnected."""
n, edges = xs
parent = list(range(n))
rank = [0] * n

def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]] # path compression (halving)
x = parent[x]
return x

def union(a, b):
ra, rb = find(a), find(b)
if ra == rb:
return False
if rank[ra] < rank[rb]:
ra, rb = rb, ra
parent[rb] = ra
if rank[ra] == rank[rb]:
rank[ra] += 1
return True

total = 0
taken = 0
for u, v, w in sorted(edges, key=lambda e: e[2]):
if union(u, v):
total += w
taken += 1
if taken == n - 1:
break
return total if taken == n - 1 else -1

Example

Loading Python runner...

Description

Run time analysis

O(ElogE)O(E \log E), which is O(ElogV)O(E \log V) since EV2E \le V^2. Sorting the edges dominates; the EE union-find operations together take O(Eα(V))O(E\, \alpha(V)), where α\alpha is the inverse Ackermann function — essentially constant for any input we will ever run.

Space analysis

O(V+E)O(V + E). The disjoint-set arrays are O(V)O(V) and the (possibly modified) edge list is O(E)O(E). No heap or adjacency structure is required.

Proof of correctness

We prove the cut property: for any cut (S,VS)(S, V \setminus S) of the graph, if ee is a minimum-weight edge crossing the cut, then some MST contains ee.

Exchange argument. Let TT be any MST. If eTe \in T we are done. Else T{e}T \cup \{e\} contains a unique cycle CC, and since ee crosses the cut, CC must contain at least one other crossing edge ff. Because ee is minimum across the cut, w(e)w(f)w(e) \le w(f). Then T=(T{f}){e}T' = (T \setminus \{f\}) \cup \{e\} is still a spanning tree (we removed one edge from a cycle), and its total weight is w(T)w(f)+w(e)w(T)w(T) - w(f) + w(e) \le w(T). So TT' is also an MST and it contains ee.

Kruskal as repeated cut-property application. When Kruskal considers edge e=(u,v)e = (u, v) of weight ww, either uu and vv are already connected by the partial forest FF — in which case accepting ee would create a cycle and we correctly reject it — or they are in different components AA and BB. In the latter case, no earlier-processed edge (i.e. no edge of strictly smaller weight) crosses the cut (A,VA)(A, V \setminus A), because any such edge would already have merged AA with something. So ee is a minimum-weight edge across that cut, and the cut property guarantees that extending FF with ee is consistent with some MST. By induction on the number of accepted edges, the final forest is an MST. If fewer than n1n - 1 edges are accepted, the graph is disconnected and no spanning tree exists.

Extensions

Applications

References

  • Cormen, Leiserson, Rivest, Stein. Introduction to Algorithms, 3rd ed., Chapter 23 (Minimum Spanning Trees).
  • Kruskal, J. B. (1956). "On the shortest spanning subtree of a graph and the traveling salesman problem." Proceedings of the American Mathematical Society, 7(1), 48-50.
  • cp-algorithms — Minimum spanning tree (Kruskal)