Loading...
Tree-expanding greedy MST algorithm leveraging key[] frontier weights and a priority queue to choose the minimum crossing edge each step.
Prim's algorithm grows a minimum spanning tree from an arbitrary start vertex by repeatedly taking the minimum-weight edge that connects the current tree to a new vertex (the cut frontier). It parallels Dijkstra's style but tracks edge weights instead of distances.
Choice of priority queue structure changes time bounds similarly to Dijkstra. For dense graphs, a simple array (O(n^2)) is competitive.
Algorithm | Approach | Best For | Time |
---|---|---|---|
Prim (Heap) | Grow tree | Dense / general | O(m log n) |
Prim (Matrix) | Scan keys | Very dense | O(n^2) |
Kruskal | Merge forests | Sparse / sorted reuse | O(m log n) |
Study the priority queue updates and then watch the dynamic frontier evolve.