Minimum Spanning Trees

Week 14, Tuesday — Kruskal’s Algorithm

April 7, 2026

Announcements

MST (review)

8 2 4 7 2 1 3 9 5 A B C D E F

Input: connected, undirected graph \(G\) with edge weights

Output: subgraph \(G'\) such that:

  • \(G'\) spans \(G\) (contains all vertices)
  • \(G'\) is connected and acyclic (a tree)
  • \(G'\) has minimum total weight among all spanning trees

 

What is the MST of this graph?

Total weight = ____

Kruskal’s Algorithm

5 2 16 15 5 16 10 11 13 17 12 8 2 4 9 12 a b c d e f g h
(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)

Kruskal’s Algorithm (1956)

5 2 16 15 5 16 10 11 13 17 12 8 2 4 9 12 a b c d e f g h
a b c d e f g h
(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)
  1. Initialize graph \(T = (V, \emptyset)\) — all \(n\) vertices, no edges.

  2. Initialize a disjoint sets structure where each vertex is its own set.

  3. 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\).

Kruskal’s Algorithm: Pseudocode

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

Input \(G = (V,E)\), \(|V| = n\), and \(|E| = m\).

 

Priority Queue: Heap Sorted Array
To build
Each removeMin

Kruskal’s Algorithm: Runtime Analysis

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