Exercise 1695Combinatorics › Graph theoryL1Added 3 Nov 2026
Prim's algorithm
#Prim#MST#greedy
Statement
Describe Prim's algorithm for finding an MST.
Hint
Grow a tree from a starting vertex, always adding the cheapest edge connecting the tree to a new vertex.
Show solutionHide solution
Solution
Algorithm.
1. Start at any vertex . Set , priority queue of edges incident to with weights.
2. Repeatedly: pick the minimum-weight edge from to , add its other endpoint to and the edge to the MST.
3. Stop when .
Complexity. with a binary heap.
Conclusion. Prim computes an MST.