Week 14, Tuesday — Kruskal’s Algorithm
April 7, 2026
Input: connected, undirected graph \(G\) with edge weights
Output: subgraph \(G'\) such that:
What is the MST of this graph?
Total weight = ____
| (a,d) |
| (e,h) |
| (f,g) |
| (a,b) |
| (b,d) |
| (g,e) |
| (g,h) |
| (e,c) |
| (c,h) |
| (e,f) |
| (f,c) |
| (d,e) |
| (b,c) |
| (c,d) |
| (a,f) |
| (d,f) |
| (a,d) |
| (e,h) |
| (f,g) |
| (a,b) |
| (b,d) |
| (g,e) |
| (g,h) |
| (e,c) |
| (c,h) |
| (e,f) |
| (f,c) |
| (d,e) |
| (b,c) |
| (c,d) |
| (a,f) |
| (d,f) |
Initialize graph \(T = (V, \emptyset)\) — all \(n\) vertices, no edges.
Initialize a disjoint sets structure where each vertex is its own set.
RemoveMin from PQ. If that edge connects two vertices from different sets, add the edge to \(T\) and take the Union of their sets; otherwise skip. Repeat until ____ edges are added to \(T\).
def KruskalMST(G):
forest = DisjointSets()
for v in G.vertices:
forest.makeSet(v)
Q = PriorityQueue(G.edges, key=weight)
T = [] # MST edges
while len(T) < len(G.vertices) - 1:
e = Q.removeMin()
u, v = e.endpoints
if forest.find(u) != forest.find(v):
T.append(e)
forest.smartUnion(
forest.find(u), forest.find(v))
return TInput \(G = (V,E)\), \(|V| = n\), and \(|E| = m\).
| Priority Queue: | Heap | Sorted Array |
|---|---|---|
| To build | ||
| Each removeMin |
def KruskalMST(G):
forest = DisjointSets()
for v in G.vertices:
forest.makeSet(v)
Q = PriorityQueue(G.edges, key=weight)
T = [] # MST edges
while len(T) < len(G.vertices) - 1:
e = Q.removeMin()
u, v = e.endpoints
if forest.find(u) != forest.find(v):
T.append(e)
forest.smartUnion(
forest.find(u), forest.find(v))
return T| Priority Queue: | Total runtime: build + execute |
|---|---|
| Heap | |
| Sorted Array |