Greedy optimization · Algorithm 11 of 15
Kruskal’s Algorithm
Connect all vertices as cheaply as possible without forming cycles. Citation needed
The whole, before the parts
Kruskal’s algorithm connects all the points in a network with the least total connection cost, without making loops. Connecting a set of sites is a different task from finding the shortest route out of one site. Citation needed
Terminology and core concepts
A spanning tree connects every vertex of an undirected graph without a cycle. A minimum spanning tree minimizes the sum of its edge weights, rather than distances from one start vertex. Kruskal’s algorithm considers edges from lightest to heaviest and accepts an edge only when it joins different components. A disjoint-set structure tracks which vertices are already connected. The cut property justifies the greedy choice: a lightest edge crossing a separation of components is safe to add to some minimum spanning tree. Citation needed
Learning path
Trace an example
undirected edges by weight: AB:1 BC:2 AC:3 CD:4 BD:5
AB:1 take components {A,B} {C} {D}
BC:2 take components {A,B,C} {D}
AC:3 skip would close a cycle
CD:4 take components {A,B,C,D}
BD:5 skip already connected
chosen weight: 1 + 2 + 4 = 7
Implement it in Python
Read the loop alongside the trace, then run this self-contained example with Python 3. The assertions show expected results. Inputs use ordinary Python lists and dictionaries; comparisons and numeric operations are assumed to behave normally. Citation needed
def kruskal(vertices, edges):
# Undirected edges are (u, v, weight); list every vertex explicitly.
parent = {v: v for v in vertices}
size = {v: 1 for v in vertices}
def find(v):
while v != parent[v]:
parent[v] = parent[parent[v]]
v = parent[v]
return v
chosen = []
for u, v, weight in sorted(edges, key=lambda edge: edge[2]):
a, b = find(u), find(v)
if a == b:
continue
if size[a] < size[b]:
a, b = b, a
parent[b] = a
size[a] += size[b]
chosen.append((u, v, weight))
return chosen
edges = [('A', 'B', 1), ('B', 'C', 2), ('A', 'C', 3),
('C', 'D', 4), ('B', 'D', 5)]
assert sum(w for _, _, w in kruskal(['A', 'B', 'C', 'D'], edges)) == 7
assert kruskal(['A', 'B'], []) == []Citation needed
Analyze the cost
Sorting costs O(E log E). Union by size plus path compression makes disjoint-set operations nearly constant amortized time, O(α(V)), where α is the inverse Ackermann function. Including initialization, the usual bound is O(V + E log(E + 1)); this implementation uses O(V + E) extra space including the sorted copy. Citation needed
A mistake to watch for
A disconnected graph produces a minimum spanning forest, not one tree. For a nonempty connected graph, exactly V - 1 edges are selected. Negative weights are allowed; shortest-path reasoning does not apply here. Citation needed
Practice and recall
-
Why does the trace reject AC even though its weight is less than CD?
Work it out before opening the solution. Citation needed
Show the reasoning
- After AB and BC, A and C are already in the same component.
- Adding AC would make the cycle A–B–C–A.
- CD connects a new component, so it is accepted instead.
Citation needed
- When does Kruskal reject an edge?
- When both endpoints are already in the same component. Citation needed
To check your understanding, reproduce the implementation without looking, explain why its main step is safe, and test an edge case before moving to the next algorithm. Citation needed
Sources
- Princeton: Algorithms — graphs — further study of the underlying algorithm and analysis.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms — correctness arguments and complexity analysis.