Prim's Algorithm: Analysis
- Compare various approaches to finding the next min edge and the resulting time/space tradeoffs between them for Prim's algorithm.
Exercise Based on your understanding of Prim's algorithm, how can we efficiently implement the step which involves finding min-weight edge with one endpoint in ?
Solution
-
Naive approach: Try all edges .
-
Better approach: Keep all the edges that have one endpoint in in a (min-heap) Priority Queue and remove the best (min) at each iteration:
Exercise Based on your answer to the previous question, analyze the asymptotic complexity of Prim's algorithm.
Solution
Runtime: – with auxiliary space.
Operation | Frequency | Cost per operation |
---|---|---|
pq.remove() | ||
pq.insert() |
Note: you might have to remove multiple edges until you find one with only one endpoint in . That's why remove's frequency is , not .
Considering , we have for simple graphs.