Prim’s Algorithm

Context: FIT2004_MOC · grow one tree from a root — Dijkstra with the relaxation formula changed from distance from the source to distance from the tree Parent Framework: Minimum Spanning Tree

Quick Revision

  • 🎯 Objective: repeatedly absorb the vertex nearest the current tree ➔ after absorptions the tree is a MST.
  • 📦 Core Components: min-Heap keyed on v.distance ➔ serve/update | v.distance ➔ weight of the single cheapest edge into the tree | v.previous ➔ the MST edge itself.
  • ⚡ Key Constraint: v.distance = w, never u.distance + w — writing Dijkstra’s formula here produces a shortest-path tree and silently answers a different question.

📝 How It Works

1. Growing, Not Merging

  • One component throughout ➔ the tree starts as and every step attaches exactly one new vertex; it is never fragmented, unlike Kruskal’s forest.
  • The greedy choice ➔ of all edges with one endpoint in the tree and one outside, take the lightest. That edge and its outside endpoint join together.
  • Root is arbitrary ➔ any start vertex produces an MST of the same weight; the order of selection changes, which is why exam questions fix the root (“use ”).
  • Acyclicity is free ➔ each added edge lands on a vertex not yet in the tree, so no cycle can form; Prim needs no cycle test and therefore no union-find.

2. The Diff From Dijkstra — one line

  • Same loop, same ADT ➔ init all except the root at , serve the minimum, relax out-edges, finalise on serve. Nothing else changes.
  • Relaxation formula ➔ Dijkstra v.distance = u.distance + w ➔ Prim v.distance = w.
  • Guard is unchanged ➔ relax only if the new value is smaller: if not v.visited and w < v.distance.
  • What the key means ➔ in Dijkstra v.distance is a path cost from the source; in Prim it is the cost of one edge — the cheapest known link from to the tree.
  • Reading the answer ➔ the MST is ; its weight is at finalisation, not the final key of any one vertex.
  • Nickname ➔ the lecture calls it Prim-Dijkstra for exactly this reason.

3. Correctness — the Cut Argument

  • Invariant ➔ the selected edge set is always a subset of some MST.
  • Maintenance (exchange) ➔ let be the lightest edge crossing the cut (tree, rest). If some MST omits , then has a cycle which must re-cross the cut at some with ; is a spanning tree of weight , so it is also an MST and it contains .
  • Termination ➔ each iteration finalises one vertex from a finite ⟹ exactly serves, edges.
  • No sign condition ➔ the argument compares with and never sums, so negative weights are legal ➔ Minimum Spanning Tree §3.

⚙️ Core Implementation

🔹 Prim with a min-heap

⚖️ Complexity

Binary Heap with an index map, adjacency list, connected so .

CaseTimeAuxiliary spaceTrigger
Bestno early exit exists — every vertex must join the tree
Averagesame work regardless of weights
Worstsame again; Prim has no bad-input case, only a bad graph size
  • Derivation ➔ serves at relaxations each costing an heap update once — identical to Dijkstra, as the lecture states.
  • Dense instantiation ➔ ⟹ ; replacing the heap with a linear array scan gives , which beats the heap on a dense graph.
  • Space ➔ auxiliary (heap three vertex fields) on top of the adjacency list, which is input space ➔ Algorithmic Complexity §6.
  • Update needs an index map ➔ vertex ➔ heap slot, maintained by every rise/sink; scanning the heap for makes each update and the algorithm .

📊 Exam Execution Trace & Applied Exercises

Lecture graph, undirected: , , , , , , . Root .

Manual Execution Trace

StepServed (joins tree)Edge addedRelaxations (only if current)
0——init
—,
: ✅ · ·
: ✗ no change
: ✅
none left

Final Extracted Output: MST , weight · selection order — note it is NOT nondecreasing in key (), unlike Dijkstra’s finalisation order.

  • Read the trap ➔ step tests and rejects; a strict < is what keeps the earlier parent. Step then overwrites from to — the parent pointer, not just the number, must be rewritten.

Applied Exercise — prep P2, root

Problem: , , , , , , , , , .

Final Extracted Output: edges in selection order , weight . Kruskal returns the same edge set in a different order — quote the order, not just the set.

⚠️ Common Mistakes

  • 💡 Serving in key order and calling it the answer ➔ Prim’s keys are edge weights, so the finalisation order is not sorted. Do not sanity-check it the way you check Dijkstra’s.
  • 💡 Forgetting the strict inequality ➔ w <= v.distance still yields a valid MST but rewrites previous on ties, so your trace stops matching the model answer. Use < and state your tie-break rule.
  • 💡 Claiming Prim needs non-negative weights ➔ that precondition is Dijkstra’s. Prim compares single edges and never accumulates ➔ Minimum Spanning Tree §3.

🧠 Active Recall