BITS Pilani
CS F364: Design and Analysis of Algorithms

MST Algorithms

Lecture 16 |2026-06-09

Prim’s Algorithm

  • Prim’s Algorithm
  • Correctness of Prim’s Algorithm
  • Boruvka’s Algorithm
  • Exchange Arguments
  • Data Structure Comparison
  • Summary

Prim’s Algorithm — Overview

Idea: Grow a tree from a starting node, always adding the cheapest edge that connects the tree to a new vertex.

  1. Pick an arbitrary start node u. Initialize T = \{u\}.
  2. Maintain a priority queue of vertices not yet in T, keyed by the minimum-weight edge connecting them to T.
  3. Repeatedly:
    • Extract the vertex v with the smallest key from the PQ.
    • Add v and the corresponding edge to T.
    • Update keys of v’s neighbors (if they can now reach T via a cheaper edge).
  4. Stop when T spans all V vertices.

Prim’s Algorithm — Interactive Example

Algorithm:

  1. Start with any vertex a in tree T.
  2. Pick cheapest crossing edge.
  3. Update keys of neighbors.
  4. Repeat until all vertices in T.

Step 0 / 9 — click ▶ to start

in T   crossing edge   not visited

Priority Queue Implementation

PrimMST(G, w, r):                    // r = arbitrary root
    for each v in V:
        key[v] = ∞                   // cheapest edge-weight from T to v
        parent[v] = NIL
        insert v into PQ with key[v]

    key[r] = 0
    while PQ is not empty:
        u = extract-min(PQ)          // vertex with smallest key = cheapest to connect to T
        for each v in adj[u]:        // T just grew by u; update u's neighbours
            if v in PQ and w(u,v) < key[v]:
                parent[v] = u
                key[v] = w(u,v)
                decrease-key(PQ, v, key[v])
  • extract-min: O(\log V) each (binary heap)
  • decrease-key: O(\log V) each

Time Complexity

Implementation extract-min decrease-key Total
Binary heap O(\log V) O(\log V) O((V+E)\log V) = O(E \log V)
Fibonacci heap O(\log V) amortized O(1) amortized O(E + V \log V)
  • Binary heap is simpler and preferred for typical graphs.
  • Fibonacci heap gives better worst-case bound but higher constants.
  • For dense graphs (E \approx V^2), O(E + V \log V) from Fibonacci heap is essentially O(V^2).

Correctness of Prim’s Algorithm

  • Prim’s Algorithm
  • Correctness of Prim’s Algorithm
  • Boruvka’s Algorithm
  • Exchange Arguments
  • Data Structure Comparison
  • Summary

Correctness — Invariant

Prim’s algorithm correctly computes an MST.

Invariant: After each iteration, the tree T is a subgraph of some minimum spanning tree M.

  • Base case: T = \{r\} (single node, no edges) — trivially a subgraph of any MST.

  • Inductive step: Suppose T \subseteq M for some MST M, and Prim’s adds edge e = (u, v) where u \in T, v \notin T.

Correctness — Exchange

We need to show T \cup \{e\} is a subgraph of some MST.

Case 1: e \in M. Then T \cup \{e\} \subseteq M. Done.

Case 2: e \notin M. Consider M \cup \{e\} — it contains a cycle C.

  • Let S be the set of vertices already in T when e is added.
  • The cut (S, V-S) has e as its cheapest crossing edge (by Prim’s — it was extracted from the PQ with minimum key).
  • Cycle C must cross (S, V-S) at some other edge f \neq e.
  • By the cut property, c_e \le c_f.

Construct M' = M \cup \{e\} - \{f\}:

  • M' is a spanning tree.
  • \text{cost}(M') \le \text{cost}(M) (since c_e \le c_f).
  • So M' is an MST and T \cup \{e\} \subseteq M'. \square

Boruvka’s Algorithm

  • Prim’s Algorithm
  • Correctness of Prim’s Algorithm
  • Boruvka’s Algorithm
  • Exchange Arguments
  • Data Structure Comparison
  • Summary

Boruvka’s Algorithm — History

  • Invented in 1926 by Czech mathematician Otakar Boruvka (1899–1995).
  • Designed to find the minimum-cost electrical grid for Moravia.
  • Rediscovered multiple times (also called Sollin’s algorithm).
  • Key advantage: Naturally suited for distributed and parallel computation — each component independently finds its cheapest outgoing edge.
  • Plays a pivotal role in Karger, Klein, and Tarjan’s randomized linear-time MST algorithm (1995).

Boruvka’s Algorithm

  1. Start with each vertex as its own component S(v) = \{v\}, and T = \emptyset.
  2. In each round:
    1. For each component S, find the minimum-weight edge e_S that has exactly one endpoint in S (the cheapest outgoing edge).
    2. Add all such edges e_S to T.
    3. Merge components connected by the added edges.
  3. Repeat rounds until only one component remains.
  4. Return T.

Boruvka’s Algorithm — Correctness

  • Every edge added in a round is the minimum-weight edge crossing some cut (the component boundary).
  • By the cut property, each such edge belongs to some MST.
  • Cycle concern: Could edges added in the same round form a cycle? No — because if edges (a,b), (b,c), (c,a) were all cheapest-out for their respective components, then the weights would have to satisfy w(a,b) < w(b,c) < w(c,a) < w(a,b), an impossibility.
  • Thus T remains acyclic throughout.

Result: Boruvka’s algorithm correctly computes an MST.

Boruvka’s Algorithm — Time Complexity

  • Each round takes O(E) time (scan all edges once from each endpoint).
  • In each round, every component merges with at least one other component.
  • The number of components at least halves each round.
  • After at most \log_2 V rounds, only one component remains.

T(V,E) = O(E \log V)

  • Parallel version: Each component finds its cheapest edge independently in O(1) parallel rounds — excellent for distributed computing (MapReduce, Pregel).

Exchange Arguments

  • Prim’s Algorithm
  • Correctness of Prim’s Algorithm
  • Boruvka’s Algorithm
  • Exchange Arguments
  • Data Structure Comparison
  • Summary

Exchange Arguments

A general technique for proving greedy algorithms are optimal:

  1. Let A be the greedy solution and OPT be any optimal solution.
  2. Find the first “difference” between A and OPT.
  3. Exchange: Modify OPT by replacing the differing element with the corresponding greedy choice, without making OPT worse.
  4. Repeat — the greedy solution is at least as good as any optimal solution.
Algorithm What is exchanged Why it works
Kruskal’s Cheaper edge e replaces heavier e' in cycle Cut/cycle property
Prim’s Cheapest crossing edge e replaces heavier f Cut property
Interval Scheduling Earlier-finishing job replaces later one Exchange preserves feasibility
Huffman Low-frequency characters get deeper codes Optimal prefix code property

Data Structure Comparison

  • Prim’s Algorithm
  • Correctness of Prim’s Algorithm
  • Boruvka’s Algorithm
  • Exchange Arguments
  • Data Structure Comparison
  • Summary

Three Algorithms, Three Strategies

Kruskal’s Prim’s Boruvka’s
Strategy Global — sort all edges Local — grow from a root Distributed — each component acts independently
Data Structure Union-Find (DSU) Priority Queue (min-heap) Component edge lists
Core Operation Cycle detection via find Extract cheapest crossing vertex Find cheapest outgoing edge per component
Time Complexity O(E \log V) O(E \log V) O(E \log V)
Best For Sparse graphs Dense graphs (with Fibonacci heap) Parallel / distributed systems
Parallelism Difficult (sequential edge processing) Difficult (sequential growth) Natural (independent components)

Choosing the Right Algorithm

  • Sparse graphs (E \approx V): All three run in O(V \log V). Kruskal’s is simplest to implement.

  • Dense graphs (E \approx V^2): Prim’s with Fibonacci heap gives O(V^2), which beats O(V^2 \log V).

  • Parallel/distributed environment: Boruvka’s is the natural choice — each round is embarrassingly parallel.

  • Educational value:

    • Kruskal’s teaches Union-Find and the exchange argument.
    • Prim’s teaches priority queues and the cut property.
    • Boruvka’s teaches parallel thinking and the oldest MST algorithm.

Summary

  • Prim’s Algorithm
  • Correctness of Prim’s Algorithm
  • Boruvka’s Algorithm
  • Exchange Arguments
  • Data Structure Comparison
  • Summary

What We Learned Today

  • Prim’s Algorithm — grow a tree from a root using a priority queue, O(E \log V)
  • Prim’s Correctness — invariant + exchange argument via cut property
  • Boruvka’s Algorithm — parallel component-based approach, O(E \log V)
  • Exchange Arguments — a general technique for proving greedy optimality
  • Data Structure Comparison — Union-Find vs. Priority Queue vs. component lists
Algorithm DS Complexity Key Idea
Kruskal’s Union-Find O(E \log V) Sort edges, Union-Find for cycles
Prim’s Priority Queue O(E \log V) Grow from root, extract-min
Boruvka’s Component lists O(E \log V) Parallel component merging