Skip to content
Part IA Lent Term

Prim's Algorithm

Prim’s algorithm builds an MST by growing a single tree from an arbitrary root, repeatedly adding the lightest edge connecting the tree to a vertex outside. It is structurally similar to Dijkstra’s algorithm but uses direct edge weights rather than cumulative path distances.

Algorithm

PRIM(G, w, r):
    for each v in G.V:
        v.key = INFINITY
        v.parent = NIL
    r.key = 0
    Q = priority queue containing all vertices, keyed by v.key
    while Q is not empty:
        u = EXTRACT-MIN(Q)
        for each v in G.adj[u]:
            if v in Q and w(u, v) < v.key:
                v.parent = u
                v.key = w(u, v)
                DECREASE-KEY(Q, v, v.key)

At termination, the edge set {(v.parent,v)vr,v.parentNIL}\{(v.\text{parent}, v) \mid v \neq r, v.\text{parent} \neq \text{NIL}\} forms the MST.

Key Insight

The key of a vertex is the minimum weight of any edge connecting it to the current tree. This differs from Dijkstra, where key is the cumulative distance from the source. In Prim, the key is just the single edge weight, not a path sum.

Correctness

The algorithm maintains the invariant: at the start of each loop iteration, for every vertex not yet extracted, its key is the minimum-weight edge connecting it to the tree (vertices already extracted). When EXTRACT-MIN selects uu, the edge (u.parent,u)(u.\text{parent}, u) is the lightest edge crossing the cut (current tree, rest of graph), so it is a safe edge. The correctness follows from the safe-edge theorem.

Formally, at each step the set SS of extracted vertices forms a tree, and the cut (S,VS)(S, V \setminus S) is respected by the tree edges. The extracted vertex uu is connected via the lightest edge crossing this cut.

Running Time

Using a binary heap:

  • Initialisation: O(V)O(|V|), or O(V)O(|V|) for BUILD-HEAP
  • V|V| EXTRACT-MIN operations: O(VlogV)O(|V| \log |V|)
  • Up to E|E| DECREASE-KEY operations, each O(logV)O(\log |V|): O(ElogV)O(|E| \log |V|)
  • Queue membership test via bit array: O(1)O(1) per check

Total: O(VlogV+ElogV)=O(ElogV)O(|V| \log |V| + |E| \log |V|) = O(|E| \log |V|) for connected graphs.

Using a Fibonacci heap:

  • EXTRACT-MIN: amortised O(logV)O(\log |V|)
  • DECREASE-KEY: amortised O(1)O(1)

Total: O(VlogV+E)O(|V| \log |V| + |E|), which is better for dense graphs where E=Θ(V2)|E| = \Theta(|V|^2). The O(E)O(|E|) term dominates.

Worked Example

Same graph as the Kruskal example: vertices {A,B,C,D,E,F}\{A, B, C, D, E, F\}, root AA.

Edges: AB:4A-B:4, AC:1A-C:1, BC:3B-C:3, BD:2B-D:2, CD:5C-D:5, CE:6C-E:6, DE:7D-E:7, DF:4D-F:4, EF:3E-F:3.

Initialise: key(AA) = 0, all others \infty. Queue: all vertices.

  1. Extract AA (key 0). Update neighbours: key(BB) = 4, key(CC) = 1. Parents: BAB \leftarrow A, CAC \leftarrow A.
  2. Extract CC (key 1). Update neighbours: key(BB) \leftarrow min(4, 3) = 3 (parent CC), key(DD) = 5 (parent CC), key(EE) = 6 (parent CC).
  3. Extract BB (key 3). Update neighbours: key(DD) \leftarrow min(5, 2) = 2 (parent BB).
  4. Extract DD (key 2). Update neighbours: key(EE) \leftarrow min(6, 7) = 6 (no change), key(FF) = 4 (parent DD).
  5. Extract FF (key 4). Update neighbours: key(EE) \leftarrow min(6, 3) = 3 (parent FF).
  6. Extract EE (key 3). No unextracted neighbours.
  7. Queue empty.

MST edges: (A,C)(A,C), (C,B)(C,B), (B,D)(B,D), (D,F)(D,F), (F,E)(F,E). Total weight: 1+3+2+4+3=131 + 3 + 2 + 4 + 3 = 13 (same total as Kruskal via different tree shape).

Prim example step by step

Prim vs. Dijkstra

AspectPrimDijkstra
Key meaningWeight of connecting edgeCumulative distance from source
Initialisationroot.key = 0s.d = 0
Relax conditionw(u, v) < v.keyu.d + w(u, v) < v.d
Updatev.key = w(u, v)v.d = u.d + w(u, v)
HandlesConnected undirected graphsDirected graphs, non-negative weights
OutputMST (tree of minimum total weight)Shortest path tree

Summary

AspectBinary heapFibonacci heap
EXTRACT-MINO(logV)O(\log \lvert V \rvert)O(logV)O(\log \lvert V \rvert) amortised
DECREASE-KEYO(logV)O(\log \lvert V \rvert)O(1)O(1) amortised
TotalO(ElogV)O(\lvert E \rvert \log \lvert V \rvert)O(VlogV+E)O(\lvert V \rvert \log \lvert V \rvert + \lvert E \rvert)
Best forSparse graphsDense graphs (E=Θ(V2)\lvert E \rvert = \Theta(\lvert V \rvert^2))

Past Tripos: y2024p2q7, y2023p1q9.