Algorithm Search⌘K

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 G=(V,E)G = (V, E) be a finite undirected graph with a real weight w(e)w(e) on each edge. Weights may tie and may be negative, GG may have parallel edges and self-loops, and GG need not be connected. Then PRIM(G,w)(G, w) stops, and the set FF that it returns is a minimum spanning forest of GG: a forest with the same connected components as GG, of least total weight among all such forests.

Setup. A vertex is in a tree when inTree[v]=trueinTree[v] = \text{true}. Each run of lines L4 to L12 for one root rr is a phase. During a phase, SS is the set of vertices that joined in this phase, and DD is the set of vertices that joined in earlier phases. U=V∖(S∪D)U = V \setminus (S \cup D) is the set of outside vertices. Let Fnow={π[x]:x∈S∪D,π[x]≠nil}F_{\text{now}} = \{\pi[x] : x \in S \cup D, \pi[x] \ne \text{nil}\}. Each spanning forest of GG has n−cn - c edges, where cc is the number of components (spanning forest), and a minimum spanning forest exists because EE is finite.

Lemma 1 (the cut step). Let A⊆F∗A \subseteq F^* for a minimum spanning forest F∗F^*. Let X⊆VX \subseteq V be a set such that no edge of AA has exactly one end in XX. Let ee be an edge with exactly one end in XX and w(e)≤w(f)w(e) \le w(f) for each edge ff with exactly one end in XX. Then A∪{e}A \cup \{e\} is a subset of some minimum spanning forest.

Proof. If e∈F∗e \in F^*, take F∗F^*. Otherwise let e=uve = uv with u∈Xu \in X and v∉Xv \notin X. The edge ee joins uu and vv, so they are in one component of GG and so in one tree of F∗F^*. The path PP from uu to vv in that tree starts in XX and ends outside XX, so it has an edge ff with exactly one end in XX, and f∉Af \notin A. Let F′=F∗∖{f}∪{e}F' = F^* \setminus \{f\} \cup \{e\}. Removing ff splits its tree into a tree with uu and a tree with vv, since ff is on the only path between them, and ee joins them again. So F′F' is a forest with the components of F∗F^*, which are those of GG. Its weight is w(F∗)−w(f)+w(e)≤w(F∗)w(F^*) - w(f) + w(e) \le w(F^*), so F′F' is a minimum spanning forest. It contains AA, since f∉Af \notin A, and it contains ee. □\square

Invariant. Before each run of line L6, and at the end of each phase:

  • (I1) For each v∈Uv \in U: if some edge joins a vertex of SS to vv, then π[v]\pi[v] is such an edge and key[v]=w(π[v])key[v] = w(\pi[v]) is the least weight of such an edge. Otherwise key[v]=∞key[v] = \infty and π[v]=nil\pi[v] = \text{nil}.
  • (I2) For each v∈Uv \in U with key[v]<∞key[v] < \infty, the pair (key[v],v)(key[v], v) is in QQ. Each pair (k,v)(k, v) in QQ with v∈Uv \in U has k≥key[v]k \ge key[v].
  • (I3) No edge of GG joins a vertex of DD to a vertex of S∪US \cup U. The vertices of SS, with the edges π[x]\pi[x] for x∈S∖{r}x \in S \setminus \{r\}, form a tree, and π[r]=nil\pi[r] = \text{nil}.
  • (I4) There is a minimum spanning forest F∗F^* with Fnow⊆F∗F_{\text{now}} \subseteq F^*.

Initialization of a phase. At the start of the phase for rr, by (I3) of the earlier phases (or trivially for the first phase), no edge joins DD to U∪{r}U \cup \{r\}. Line L1 set key[v]=∞key[v] = \infty for every vertex, and a key changes only in line L11, for a vertex outside all trees. A vertex v∈Uv \in U could have a finite key only from an edge scanned from a vertex of DD, and no such edge reaches UU. So before line L4, every v∈U∪{r}v \in U \cup \{r\} has key[v]=∞key[v] = \infty and π[v]=nil\pi[v] = \text{nil}. Line L4 sets key[r]=0key[r] = 0 and Q={(0,r)}Q = \{(0, r)\}, and at this moment S=∅S = \emptyset. (I1) holds since SS is empty and all keys of U∖{r}U \setminus \{r\} are infinite; we treat rr as a member of UU with key 00, and (I2) holds for it. (I3) holds for S=∅S = \emptyset, and (I4) holds by the earlier phases (for the first phase, Fnow=∅F_{\text{now}} = \emptyset is in every minimum spanning forest).

Maintenance. Consider a run of lines L6 to L12 that takes the pair (k,u)(k, u).

  1. A stale pair. If uu is in a tree, line L7 skips it. Removing a pair whose vertex is not in UU changes no statement.

  2. The first pair of the phase. Then u=ru = r, S=∅S = \emptyset, and line L8 makes S={r}S = \{r\}. Lines L9 to L12 scan each edge e=rve = rv. For each v∈Uv \in U, after the scan, key[v]key[v] is the least weight of an edge from rr to vv and π[v]\pi[v] 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: S={r}S = \{r\} is a tree with no edge. (I4) holds since FnowF_{\text{now}} did not change.

  3. A later pair with u∈Uu \in U. By (I2), every pair in QQ with a vertex in UU has k′≥key[v′]k' \ge key[v'], and the pair (key[v],v)(key[v], v) is in QQ for each finite key. The pair taken is the least in QQ, so k≤key[v]k \le key[v] for each v∈Uv \in U with a finite key, and k≥key[u]k \ge key[u]. So key[u]≤k≤key[u]key[u] \le k \le key[u], and key[u]=min⁡v∈Ukey[v]key[u] = \min_{v \in U} key[v]. This minimum is finite, because kk is finite.

    By (I1), π[u]\pi[u] is an edge from SS to uu, with weight key[u]key[u]. Let ff be any edge with exactly one end in SS. By (I3), its other end is not in DD, so it is some v∈Uv \in U, and by (I1) w(f)≥key[v]≥key[u]=w(π[u])w(f) \ge key[v] \ge key[u] = w(\pi[u]). So π[u]\pi[u] is a lightest edge with exactly one end in SS. No edge of FnowF_{\text{now}} has exactly one end in SS: the edges π[x]\pi[x] with x∈Sx \in S lie inside SS by (I3), and the edges π[x]\pi[x] with x∈Dx \in D lie inside DD because they were edges of earlier trees. Lemma 1 with X=SX = S, A=FnowA = F_{\text{now}} and e=π[u]e = \pi[u] gives a minimum spanning forest that holds Fnow∪{π[u]}F_{\text{now}} \cup \{\pi[u]\}. Line L8 adds uu to SS, and this is (I4) after the step.

    The new SS is a tree, since π[u]\pi[u] joins the tree SS to the new vertex uu, so (I3) holds. For each v∈Uv \in U after the step, the edges from the new SS to vv are the old ones and the edges from uu to vv. Lines L9 to L11 scan every edge uvuv and keep the lightest, so (I1) holds. Line L12 pushes each new key, and a key only drops, so each old pair has k≥key[v]k \ge key[v], and (I2) holds.

End of a phase. The loop of line L5 stops when QQ is empty. By (I2), every v∈Uv \in U then has key[v]=∞key[v] = \infty, and by (I1) no edge joins SS to UU. By (I3), no edge joins SS to DD. So SS is a union of components of GG. It is connected by its tree edges, so SS is exactly the component of rr. The phase moves SS into DD, and (I3) holds for the next phase: no edge joins the new DD to the new UU.

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 nn times, since each vertex joins once; each such run pushes at most deg⁡(u)\deg(u) pairs. So at most n+2mn + 2m pairs are ever pushed, and each run of line L6 removes one. Each phase stops, there are at most nn 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 GG, and each tree is spanned by its edges π[x]\pi[x], one for each non-root vertex. So FF is a forest with the components of GG, with n−cn - c edges. By (I4), F⊆F∗F \subseteq F^* for a minimum spanning forest F∗F^*, which also has n−cn - c edges, so F=F∗F = F^*. Therefore FF is a minimum spanning forest, and a minimum spanning tree when GG is connected. ■\blacksquare

Back to Prim's algorithm