DSA Notes
Master Prim\
The Approach
While Kruskal's processes edges globally, Prim's grows the MST one vertex at a time. Starting from any vertex, it repeatedly adds the cheapest edge that connects a vertex IN the tree to a vertex NOT in the tree. Think of it like growing a crystal — at each step, the closest atom attaches to the existing structure.
Algorithm Steps
Step-by-Step Example
Graph
0 --2-- 1 --3-- 2
| | |
6 8 5
| | |
3 --9-- 4 --7-- 5
Start from vertex 0
MST = {0}, PQ = [(2,0,1), (6,0,3)]
Step 1: Pop (2,0,1) → add vertex 1
MST = {0,1}, add edges from 1: (3,1,2), (8,1,4)
PQ = [(3,1,2), (6,0,3), (8,1,4)]
Step 2: Pop (3,1,2) → add vertex 2
MST = {0,1,2}, add edges from 2: (5,2,5)
PQ = [(5,2,5), (6,0,3), (8,1,4)]
Step 3: Pop (5,2,5) → add vertex 5
MST = {0,1,2,5}, add edges from 5: (7,5,4)
PQ = [(6,0,3), (7,5,4), (8,1,4)]
Step 4: Pop (6,0,3) → add vertex 3
MST = {0,1,2,5,3}, add edges from 3: (9,3,4)
PQ = [(7,5,4), (8,1,4), (9,3,4)]
Step 5: Pop (7,5,4) → add vertex 4
MST = {0,1,2,5,3,4} — all vertices included!
MST edges: (0,1,2), (1,2,3), (2,5,5), (0,3,6), (5,4,7)
Total weight: 2+3+5+6+7 = 23
Python Implementation
C++ Implementation
Time and Space Complexity
- With binary heap: O(E log V) — each edge is pushed/popped from heap at most once
- With Fibonacci heap: O(E + V log V) — decrease-key is O(1) amortized
- Space: O(V + E) — for visited set and priority queue
Prim's vs Kruskal's: When to Use Which
Choose Prim's when:
- Graph is dense (E close to V²)
- Graph is given as adjacency list/matrix
- You want to grow MST from a specific starting point
Choose Kruskal's when:
- Graph is sparse (E close to V)
- Edges are given as a list (easy to sort)
- You want parallelizable computation
In competitive programming, both work well. Prim's with adjacency list is slightly more natural for dense graphs. Kruskal's is easier to implement correctly (sort + Union-Find).
Correctness: Why Prim's Works
At each step, Prim's considers the cut between MST vertices and non-MST vertices. The minimum-weight edge crossing this cut is added. By the cut property, this edge must be in the MST. Since Prim's always picks the minimum crossing edge (via priority queue), it correctly builds the MST.
Practical Tips
- Lazy vs Eager Prim's: The implementation above is "lazy" — it pushes duplicate edges and skips stale ones. An "eager" version tracks the best edge to each non-MST vertex and updates it, avoiding duplicates but requiring a decrease-key operation.
- Dense graphs with matrix: For very dense graphs, a simple O(V²) version without a heap works well — just scan all non-MST vertices for the minimum key at each step.
- Starting vertex doesn't matter: The MST is the same regardless of which vertex you start from (assuming unique edge weights).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Prim\.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, prims, algorithm
Related Data Structures & Algorithms Topics