algo·visual

Prim's MST

Graphs
avgO(V²)worstO(V²)spaceO(V)
43568782549371A0B∞C∞D∞E∞F∞G∞H∞

Grow the tree from A: its key is 0 and every other key is ∞

1 / 38
Nodes8
Source
Edgesweights 1 to 99

How it works

Prim's algorithm grows a minimum spanning tree from a single node, each round adding the cheapest edge that connects the tree to a node outside it. Every node keeps the weight of its cheapest known link to the tree (its key), so picking the next edge means picking the smallest key. This version scans the keys, the O(V²) form that suits small dense graphs.

Complexity

Best
O(V²)
Average
O(V²)
Worst
O(V²)
Space
O(V)

Search algorithms

Jump to any algorithm or page