Sub topic 14
Kruskal's and Prim's Algorithms
Prerequisites
Assume you know vertices, weighted edges, connected graphs, minimum spanning trees, sorting, priority queues, and the union-find data structure. A minimum spanning tree (MST) is a set of edges that connects every vertex, contains no cycle, and has minimum total weight.
The cut property
A cut divides the vertices into two nonempty groups. An edge crosses the cut when its endpoints belong to different groups.
The cut property says that a lightest edge crossing a cut is safe for some MST. Both Kruskal's and Prim's algorithms repeatedly choose safe edges, but they create the tree in different ways.
Kruskal's algorithm
Kruskal's algorithm considers edges globally, from smallest weight to largest. It accepts an edge only when that edge joins two different components. Union-find tests this condition efficiently.
The algorithm stops after accepting V - 1 edges, where V is the number of vertices.
Walkthrough
For edges (weight, u, v):
- Sort all edges by weight.
- Start with every vertex in its own component.
- For each edge, use
findon both endpoints. - If their representatives differ, add the edge and
unionthe components. - Skip edges whose endpoints already share a component.
def kruskal(n, edges):
parent = list(range(n))
rank = [0] * n
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[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
tree = []
for weight, u, v in sorted(edges):
if union(u, v):
tree.append((u, v, weight))
if len(tree) == n - 1:
break
return tree
edges = [(1, 0, 1), (4, 0, 2), (3, 1, 2),
(2, 1, 3), (5, 2, 3)]
print(kruskal(4, edges))Sorting costs O(E log E). Union-find operations add almost linear total cost, written O(E α(V)), where α grows extremely slowly. Thus the total is O(E log E).
Prim's algorithm
Prim's algorithm grows one connected tree. Begin with any vertex. At each step, select the lightest edge from the current tree to an unvisited vertex. A binary heap gives O((V + E) log V) time with adjacency lists.
| Algorithm | Main choice | Typical complexity | Useful structure |
|---|---|---|---|
| Kruskal | Lightest remaining edge | `O(E log E)` | Union-find |
| Prim | Lightest edge leaving the tree | `O((V + E) log V)` | Min-heap |
Common errors
- Kruskal may inspect an edge that creates a cycle; it must skip that edge.
- Prim chooses an edge crossing the current tree boundary, not the globally lightest unused edge.
- Equal weights can produce different MSTs with the same total weight.
- The algorithms assume an undirected graph. A disconnected graph produces a minimum spanning forest, not one spanning tree.
Exercise
For edges (1, A, B), (2, B, C), (3, A, C), and (4, C, D), which edges does Kruskal's algorithm accept?
Show answer
It accepts (1, A, B), (2, B, C), and (4, C, D). It skips (3, A, C) because A and C are already connected.