MST — Prim's Algorithm
Build a minimum spanning tree by growing one tree outward from a start vertex.
Open on GeeksforGeeks ↗Intuition
Keep one connected tree and repeatedly absorb whichever outside vertex is cheapest to attach. Each vertex remembers key[v] — the cost of the cheapest single edge joining it to the current tree. The cut property guarantees this is safe: the lightest edge crossing the boundary between tree and non-tree always belongs to some MST.
Grow one tree outward, always absorbing the cheapest edge that leaves it. Kruskal sorts all edges globally and needs union-find; Prim keeps a frontier heap and needs only a visited array. On dense graphs Prim wins; on sparse edge lists Kruskal is simpler.
Approach
Track the cheapest way in
key[v] starts ∞ (0 for the seed). Repeatedly pick the unabsorbed vertex with the smallest key, add that cost to the total, and mark it in-tree.
Update the frontier
After absorbing u, each neighbour v outside the tree may now have a cheaper entry: key[v] = min(key[v], w(u, v)). Only edges crossing the cut matter.
Why greedy is optimal
The cut property: for any split of the vertices, the lightest crossing edge is in some MST. Every step takes exactly such an edge, so the final tree is minimal.
Solution & live demo
Common pitfalls
Not skipping vertices already in the tree
w, u = heapq.heappop(pq) in_tree[u] = True total += w
w, u = heapq.heappop(pq)
if in_tree[u]:
continueThe heap accumulates several stale entries per vertex, one for each frontier edge reaching it. Without the skip, a vertex is absorbed multiple times and the total is inflated by edges that form cycles.
Adding only one direction for undirected edges
adj[u].append((w, v))
adj[u].append((w, v)) adj[v].append((w, u))
An undirected edge must be traversable from both endpoints. Storing one direction makes parts of the graph unreachable from the start vertex and produces a spanning forest fragment rather than a full MST.
Pushing the vertex before the weight
heapq.heappush(pq, (v, w2))
heapq.heappush(pq, (w2, v))
A heap of tuples orders by the first element, so this pops the lowest-numbered vertex instead of the cheapest edge — turning Prim into an arbitrary traversal that returns a spanning tree of no particular weight.
Edge cases
No MST exists; the loop stops early with fewer than V−1 edges.
Ties can be broken arbitrarily — multiple MSTs of identical total weight.
Prim's with a heap is usually preferred over Kruskal's when E ≈ V².