Prim's algorithm
Prim's algorithm is a greedy algorithm used to find the Minimum Spanning Tree in a graph.
It works well for dense graphs.
![Animation of Prim's algorithm on a graph](../assets/prim.gif 'Prim's algorithm - Wikipedia')
Algorithm
- Initialize the tree: chose the root node, which is a single vertex chosen arbitrarily from the graph.
- Grow the tree by one edge, by choosing one with the minimum weight (e.g. distance).
- Repeat step 2, until all vertices are in the tree.
Complexity
Prim algorithm complexity can change depending of the data structure used.
Operation | Array | List | Tree | Heap | Fibonacci heap |
---|---|---|---|---|---|
Insert | |||||
Access minimum | |||||
Delete minimum | |||||
Decreases key |
Resources
- Prim's algorithm visualization – David Galles