Proofs: Prim's algorithm
These are the human proofs of Prim's algorithm, at full length.
Prim's algorithm returns a minimum spanning forest full
This proof shows: correctness, termination.
Claim. Let be a finite undirected graph with a real weight on each edge. Weights may tie and may be negative, may have parallel edges and self-loops, and need not be connected. Then PRIM stops, and the set that it returns is a minimum spanning forest of : a forest with the same connected components as , of least total weight among all such forests.
Setup. A vertex is in a tree when . Each run of lines L4 to L12 for one root is a phase. During a phase, is the set of vertices that joined in this phase, and is the set of vertices that joined in earlier phases. is the set of outside vertices. Let . Each spanning forest of has edges, where is the number of components (spanning forest), and a minimum spanning forest exists because is finite.
Lemma 1 (the cut step). Let for a minimum spanning forest . Let be a set such that no edge of has exactly one end in . Let be an edge with exactly one end in and for each edge with exactly one end in . Then is a subset of some minimum spanning forest.
Proof. If , take . Otherwise let with and . The edge joins and , so they are in one component of and so in one tree of . The path from to in that tree starts in and ends outside , so it has an edge with exactly one end in , and . Let . Removing splits its tree into a tree with and a tree with , since is on the only path between them, and joins them again. So is a forest with the components of , which are those of . Its weight is , so is a minimum spanning forest. It contains , since , and it contains .
Invariant. Before each run of line L6, and at the end of each phase:
- (I1) For each : if some edge joins a vertex of to , then is such an edge and is the least weight of such an edge. Otherwise and .
- (I2) For each with , the pair is in . Each pair in with has .
- (I3) No edge of joins a vertex of to a vertex of . The vertices of , with the edges for , form a tree, and .
- (I4) There is a minimum spanning forest with .
Initialization of a phase. At the start of the phase for , by (I3) of the earlier phases (or trivially for the first phase), no edge joins to . Line L1 set for every vertex, and a key changes only in line L11, for a vertex outside all trees. A vertex could have a finite key only from an edge scanned from a vertex of , and no such edge reaches . So before line L4, every has and . Line L4 sets and , and at this moment . (I1) holds since is empty and all keys of are infinite; we treat as a member of with key , and (I2) holds for it. (I3) holds for , and (I4) holds by the earlier phases (for the first phase, is in every minimum spanning forest).
Maintenance. Consider a run of lines L6 to L12 that takes the pair .
-
A stale pair. If is in a tree, line L7 skips it. Removing a pair whose vertex is not in changes no statement.
-
The first pair of the phase. Then , , and line L8 makes . Lines L9 to L12 scan each edge . For each , after the scan, is the least weight of an edge from to and is that edge, because line L10 replaces the key exactly when the new edge is strictly lighter. Each new key is pushed in line L12. So (I1) and (I2) hold. (I3) holds: is a tree with no edge. (I4) holds since did not change.
-
A later pair with . By (I2), every pair in with a vertex in has , and the pair is in for each finite key. The pair taken is the least in , so for each with a finite key, and . So , and . This minimum is finite, because is finite.
By (I1), is an edge from to , with weight . Let be any edge with exactly one end in . By (I3), its other end is not in , so it is some , and by (I1) . So is a lightest edge with exactly one end in . No edge of has exactly one end in : the edges with lie inside by (I3), and the edges with lie inside because they were edges of earlier trees. Lemma 1 with , and gives a minimum spanning forest that holds . Line L8 adds to , and this is (I4) after the step.
The new is a tree, since joins the tree to the new vertex , so (I3) holds. For each after the step, the edges from the new to are the old ones and the edges from to . Lines L9 to L11 scan every edge and keep the lightest, so (I1) holds. Line L12 pushes each new key, and a key only drops, so each old pair has , and (I2) holds.
End of a phase. The loop of line L5 stops when is empty. By (I2), every then has , and by (I1) no edge joins to . By (I3), no edge joins to . So is a union of components of . It is connected by its tree edges, so is exactly the component of . The phase moves into , and (I3) holds for the next phase: no edge joins the new to the new .
Termination. Each pair is pushed at most once per run of line L4 or line L12. Line L12 runs only when line L8 has just put a vertex in a tree, and line L8 runs at most times, since each vertex joins once; each such run pushes at most pairs. So at most pairs are ever pushed, and each run of line L6 removes one. Each phase stops, there are at most phases, and the loop of line L2 stops.
Conclusion. Each vertex is in a tree at the end, because line L3 starts a phase at each vertex outside all trees. By the end-of-phase step, the trees are exactly the components of , and each tree is spanned by its edges , one for each non-root vertex. So is a forest with the components of , with edges. By (I4), for a minimum spanning forest , which also has edges, so . Therefore is a minimum spanning forest, and a minimum spanning tree when is connected.