Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Minimum spanning tree calculator
Finds minimum spanning tree by growing from a single vertex
Select an algorithm and generate steps to begin visualization
Prim's algorithm builds a minimum spanning tree (MST) of a weighted undirected graph, the subset of edges that connects every vertex with the smallest possible total weight. It grows a single tree outward from an arbitrary start vertex, always attaching the cheapest edge that reaches a new vertex.
The algorithm keeps a priority queue of edges that cross from the tree to the rest of the graph. At each step it extracts the minimum-weight crossing edge, adds its new endpoint to the tree, and inserts that vertex's edges into the queue. The cut property of MSTs guarantees each chosen edge belongs to some minimum spanning tree. With a binary heap the running time is O(E log V).
Prim's algorithm designs low-cost networks: electrical grids, fiber and telecom layouts, water pipelines, and chip wiring. It also supports clustering and image segmentation. Interviews often pair it with Kruskal's algorithm to test understanding of greedy correctness arguments.
Prim grows one tree outward from an arbitrary start. At every step it takes the cheapest edge with exactly one endpoint already in the tree.
Prim(graph, start):
inTree = {start}
pq = priority queue of edges leaving start
mst = []
while inTree does not contain all vertices:
(u, v, w) = pq.extractMin()
if v in inTree: continue // stale, both ends inside
mst.append((u, v, w))
inTree.add(v)
for each edge (v, x, w2):
if x not in inTree: pq.insert((v, x, w2))Correctness rests on the cut property: for any split of the vertices into two sides, the cheapest edge crossing that split belongs to some minimum spanning tree. Prim applies it with the split being "already in the tree" against "not yet", which is why taking the cheapest crossing edge is always safe and no backtracking is ever needed.
Grow a minimum spanning tree from A on a small weighted graph where the greedy choice deliberately rejects a cheaper-looking direct edge.
Example graph: Undirected edges A-B (2), A-C (3), B-C (1), C-D (4) and B-D (7).
The minimum spanning tree is A-B, B-C and C-D with total weight 2 + 1 + 4 = 7. Note that it has exactly three edges, one fewer than the four vertices, as every spanning tree must. Note also that the tree is a path here, which is a reminder that a minimum spanning tree is not a shortest-path tree: the tree distance from A to D is 7, while the graph shortest path A to C to D would be 3 + 4 = 7 and A to B to D is 9. The two problems optimise different things.
Time: O(E log V) · Space: O(V + E)
With a binary heap, each edge can be inserted once and extracted once at O(log E), and since E is at most V squared, log E is within a constant factor of log V, giving O(E log V). Each vertex is added to the tree exactly once. A Fibonacci heap with decrease-key instead of lazy insertion improves the bound to O(E + V log V), which is asymptotically better on dense graphs though rarely worth the constants. On a very dense graph, the simplest option wins: keep an array of the cheapest known edge to each outside vertex and scan it each round for O(V squared), which beats O(E log V) once E approaches V squared.
Prim and Kruskal both produce a minimum spanning tree. The right one depends on density and on how the edges arrive.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Kruskal's algorithm | Sparse graphs, or edges already sorted by weight. Grows a forest instead of one tree, using Union-Find. | O(E log E) |
| Boruvka's algorithm | You want parallelism. Every component picks its cheapest outgoing edge simultaneously each round. | O(E log V) |
| Prim with an array scan | Dense graphs where E approaches V squared. Avoids heap overhead entirely. | O(V^2) |
| Dijkstra's algorithm | You actually want shortest paths from a source, not a minimum-weight spanning structure. Similar shape, different objective. | O((V + E) log V) |
Read the full article: Minimum Spanning Trees: Prim, Kruskal and Boruvka
Related algorithms: Kruskal's MST Algorithm, Borůvka's Algorithm, Dijkstra's Algorithm