How Prim grows a minimum spanning tree from a cut

Short answer: Prim’s algorithm grows one tree. The lightest edge with one end in the tree and one end outside it is safe to add, by the cut property. A binary heap implements the fringe in O((V+E) log V). A Fibonacci heap improves the proof to O(E + V log V). The graph is undirected and connected, and the weights may be negative. The result is a minimum spanning tree, not a shortest-path tree.


What the problem actually asks

A spanning tree connects every vertex with |V|−1 edges and no cycle. It is minimum when the total weight is as small as possible. The cut property says that for any partition of the vertices into a nonempty proper subset and its complement, a lightest edge across the cut belongs to some minimum spanning tree. Prim maintains one side of the cut as the grown tree and always takes that edge.

The same growth rule is not Dijkstra. Dijkstra minimizes a path length from a source. Prim minimizes the weight of the next attachment edge. A cheap edge into a far vertex is good for Prim and may be a terrible path.


What the algorithm guarantees

On a connected undirected graph the algorithm returns a minimum spanning tree. If several edges share the lightest weight across the cut, any of them is safe, and the tree is not unique. Time is O((V+E) log V) with a binary heap and O(E + V log V) with a Fibonacci heap. Extra memory is Θ(V). A disconnected graph yields a minimum spanning forest, one tree per component, if the outer loop restarts on an unreached vertex.

Negative weights need no special case. The cut property never assumes a sign. Self-loops are useless. Parallel edges keep only the lighter copy.


The working steps

  1. Start the tree as a single vertex. Give every other vertex a key of infinity, meaning the cheapest known edge into the tree.
  2. Put the vertices in a min-heap keyed by that attachment cost.
  3. Pop the vertex u of smallest key. The stored parent edge is the cut edge that added u. Include it in the tree, except for the start vertex.
  4. For each neighbor v not yet in the tree, if the edge u–v is lighter than v’s key, decrease the key and remember u as the parent.
  5. Repeat until the heap has offered every reachable vertex. A leftover infinite key is another component.

The output minimizes the sum of edge weights in a connector, not the path length between a pair. Shortest paths belong to Dijkstra, which looks similar in code because both decrease a key on the fringe. The key in Prim is an edge weight. The key in Dijkstra is a path sum.


Approaches that look simpler and fail

Approach Why it fails when the grown set is not the cut
Add the globally lightest remaining edge, even if both ends are already in the tree That edge closes a cycle. Kruskal will skip it with a union-find test. Prim never generates it, because both ends are inside.
Use path distance as the heap key That is Dijkstra. The tree can omit a light edge that forms a short cut and include a heavy edge that continues a short path.
Assume negative weights require Bellman-Ford Spanning trees do not walk paths repeatedly. A negative edge is simply a very attractive cut edge, and it is safe.
Quote the Fibonacci bound for a binary-heap implementation The binary heap pays O((V+E) log V). The linear-in-E term is the Fibonacci heap’s decrease-key.

A global sort of the edges, plus a disjoint-set test, is Kruskal. A component-wise minimum edge, merged in rounds, is Borůvka. All three return a minimum spanning tree.


A working example

This Python fragment grows one component. The cut is the set of edges with one end inside the tree, and the lightest of those is safe. It prints the same total as Kruskal, 6.

import heapq
adj = {0: [(1, 1), (2, 4)], 1: [(0, 1), (2, 2), (3, 5)], 2: [(0, 4), (1, 2), (3, 3)], 3: [(1, 5), (2, 3)]}
inside = {0}
pq = [(w, 0, v) for v, w in adj[0]]
heapq.heapify(pq)
total = 0
while len(inside) < 4:
    w, u, v = heapq.heappop(pq)
    if v in inside:
        continue
    inside.add(v)
    total += w
    for nxt, nw in adj[v]:
        if nxt not in inside:
            heapq.heappush(pq, (nw, v, nxt))
assert total == 6
print(total)

Where it is used

Prim’s algorithm grows a minimum spanning tree from a start vertex, the way a cable layout grows from a substation. A binary heap makes it the twin of Dijkstra. Kruskal sorts the edges first and is the better fit when the edge list is already nearly sorted.


When you can drop this algorithm

Drop Prim when the graph is so sparse that sorting edges for Kruskal is simpler and fast enough, which is common. Keep Prim when the graph is dense, the heap stays small, or an adjacency matrix makes the O(V²) scan without a heap the tight implementation.


Why this stays published

We publish this so a search for Prim’s algorithm returns the cut property and the difference between an attachment weight and a path distance. Prim’s algorithm adds the lightest edge across the cut around the grown tree, which builds a minimum spanning tree in O((V+E) log V) time with a binary heap.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *