Prim’s Minimum Spanning Tree Algorithm
Consider only edges that cross from the current tree to an outside vertex.
Core Intuition & How It Works
Prim’s algorithm grows a Minimum Spanning Tree outward from a single start vertex, always choosing the lowest-weight edge crossing from the visited tree to an unvisited vertex via a min-heap.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O((V + E) log V) |
| Space Complexity | O(V + E) |
| Topic Chapter | Chapter 12: Shortest Paths & Spanning Trees |
| Difficulty Level | Advanced |
Key Pattern to Recognize
Unlike Dijkstra (which stores total distance from source), Prim’s priority queue stores only the single connecting edge weight.
Common Pitfalls & Edge Cases
Confusing Prim’s edge-weight key with Dijkstra’s cumulative path-distance key.
Real-World & Technical Interview Applications
Dense graph MST construction, maze generation, and approximating metric Traveling Salesperson tours.