Prim's Algorithm (Minimum Spanning Tree)
What is a Minimum Spanning Tree?
A spanning tree of a connected, undirected graph is a set of edges that connects every vertex without forming any cycle. A graph with V vertices always needs exactly V − 1 edges for this. The minimum spanning tree (MST) is the spanning tree whose edge weights add up to the smallest total.
Prim's algorithm builds the MST by growing a single tree. It starts from one vertex and, again and again, adds the cheapest edge that connects a vertex inside the tree to a vertex outside it — until every vertex has joined.
Always add the cheapest edge that leaves the tree.
Using the Visualizer
- • Build your graph: press Edit graph to slide out the matrix and type a weight into any cell. Cell [i][j] is the edge between i and j, and it is copied to [j][i] for you because the graph is undirected. 0 means no edge.
- • Vertices / Random / Example: inside the drawer, change the size, get a random connected graph, or load the example back. The graph updates while you type.
- • Start vertex: use the Start menu above the board or simply click a vertex in the graph.
- • The controls above the board: Play / Pause runs the algorithm; Back and Step move one step at a time (or use ← → and space), and the bar under them jumps to any step.
- • The table on the right shows each vertex's key and parent — the same arrays the code uses.
- • The highlighted pseudocode line tells you exactly which part of the algorithm is running.
How Prim's Algorithm Works
1. Start
Put the start vertex in the tree with key 0. Every other vertex gets key ∞ — we don't yet know any edge that reaches it.
2. Pick the smallest key
Among the vertices outside the tree, choose the one with the smallest key. Its key edge is the cheapest edge leaving the tree (drawn thick yellow).
3. Add it to the tree
The vertex and its edge join the tree (green). Edges that now join two tree vertices can never be used — they would close a cycle — so they fade out.
4. Update the neighbours
Check every edge from the new vertex to the outside (blue). If it is cheaper than a neighbour's key, that key drops and its parent changes. Repeat from step 2 until every vertex is in.
Why is it correct? Split the vertices into "in the tree" and "not in the tree". Any spanning tree must use at least one edge across that split, and swapping it for the cheapest crossing edge can never make the tree heavier. This is called the cut property.
Implementation
Properties & Complexity
Time Complexity
- • Adjacency matrix: O(V²)
- • Binary heap + list: O(E log V)
- • Fibonacci heap: O(E + V log V)
- • Per step: pick min O(V), update neighbours O(V)
Space & Properties
- • Space: O(V) for key, parent and inTree
- • Strategy: greedy
- • Grows: one connected tree the whole time
- • Weights: negative weights are fine
Best Use Cases
- • Dense graphs: the O(V²) matrix version beats Kruskal's when E is close to V²
- • Network design: cheapest cables, pipes or roads that connect every site
- • Clustering: build the MST, then cut the heaviest edges
- • Approximations: a building block for the travelling salesman 2-approximation
Limitations
- • Connected graphs only: on a disconnected graph it only spans the start vertex's piece
- • Undirected only: directed graphs need a different algorithm (Edmonds')
- • Sparse graphs: the matrix version wastes time scanning empty cells
- • Not shortest paths: the MST does not give the shortest route between two vertices