DSA Notes
Master Prim
How Prim's Works
Prim's algorithm builds the MST by starting at any vertex and repeatedly adding the cheapest edge that connects the current MST to a new vertex not yet in the MST.
The invariant: At every step, we maintain a connected subtree and extend it by the cheapest possible edge that doesn't create a cycle.
Graph
4
A ——— B
| |
2 3
| |
D ——— C
1
Start at A
Step 1: MST={A}. Candidates: {A-D:2, A-B:4}. Add A-D (cheapest).
Step 2: MST={A,D}. Candidates: {A-B:4, D-C:1}. Add D-C (cheapest).
Step 3: MST={A,D,C}. Candidates: {A-B:4, C-B:3}. Add C-B (cheapest).
Step 4: MST={A,D,C,B}. All vertices included. Done!
MST: {A-D, D-C, C-B}, total weight = 2+1+3 = 6
Step-by-Step Trace
| Graph | 5 vertices, edges: 0-1:2, 0-3:6, 1-2:3, 1-3:8, 1-4:5, 2-4:7, 3-4:9 |
| Initial | in_mst=all False, min_edge=[∞,∞,∞,∞,∞], parent=[-1,-1,-1,-1,-1] |
| Start vertex 0 | min_edge[0]=0, pq=[(0,0)] |
| Iteration 1 | Extract (0,0). in_mst[0]=True. mst_weight=0. |
| Neighbors | 1(w=2), 3(w=6) |
| Iteration 2 | Extract (2,1). in_mst[1]=True. mst_weight=2. Add edge (0,1,2). |
| Neighbors | 0(in_mst), 2(w=3), 3(w=8), 4(w=5) |
| 3 < min_edge[3]=6? No | 8>6, skip |
| Iteration 3 | Extract (3,2). in_mst[2]=True. mst_weight=5. Add edge (1,2,3). |
| Neighbor 4(w=7) | 7 > min_edge[4]=5, skip |
| Iteration 4 | Extract (5,4). in_mst[4]=True. mst_weight=10. Add edge (1,4,5). |
| Iteration 5 | Extract (6,3). in_mst[3]=True. mst_weight=16. Add edge (0,3,6). |
| MST | {0-1:2, 1-2:3, 1-4:5, 0-3:6}, total = 16 |
Prim's with Adjacency Matrix (Dense Graphs)
For dense graphs, an array-based implementation avoids the log factor:
Prim's vs Kruskal's
| Feature | Prim's | Kruskal's |
|---|---|---|
| Strategy | Grow MST from one vertex | Sort edges, add if no cycle |
| Data structure | Priority queue | DSU (Union-Find) |
| Time (sparse) | O(E log V) | O(E log E) |
| Time (dense) | O(V²) array-based | O(V² log V) |
| Best for | Dense graphs | Sparse graphs |
| Starting point | Any single vertex | All edges |
| Intermediate result | Always a tree | May be a forest |
| Implementation | Moderate | Simple |
Choosing between them:
- Dense graph (E ≈ V²): Use Prim's with array — O(V²) beats Kruskal's O(V² log V)
- Sparse graph (E ≈ V): Use Kruskal's — O(E log E) with simple code
Summary
Prim's algorithm grows MST one vertex at a time:
- Start at any vertex, mark it as in MST
- Find minimum weight edge from MST to non-MST vertex
- Add that vertex and edge to MST
- Repeat until all vertices are in MST
Time: O(E log V) with priority queue, O(V²) with adjacency matrix Use when: Dense graphs, or when you need the MST as a single connected component from start
*Next Lesson: Kruskal's Algorithm*
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