Hi peers,
Prim’s Algorithm is for finding the minimum spanning tree (MST) of a graph. It is simple, straightforward, and powerful. It always gives us the correct MST for an input graph every time. However, you cannot conclude that this algorithm is correct simply because it is correct for only several cases that you tested.
We have to prove its correctness with some level of formality and rigor. In this short essay, I hope you could derive as great joy as I did by appreciating the subtlety and beauty in this proof. To prove Prim’s, we have to introduce several new concepts.
Concepts that help in proof:
Concept1: Cut
A cut of a graph is a partition of the graph into two non-empty sets.
Notation Cut(A, B) denotes a cut that partitions the original graph into subgraph A and subgraph B.
Concept2: An important Property of Cut
Consider an undirected and weighted graph G(V, E). Cut(A, B) is an arbitrary cut of a graph and Edge e is an edge that crosses subgraph A & B. If e has the minimum cost among all edges that cross A & B, then e belongs to the MST of G.
Well, if you stare at Concept2, you might find it obvious that: if Concept2 is true, Prim’s Algorithm must be true. Why? Because the only thing that Prim’s algorithm is doing is: separating the graph into two subgraphs and figuring out the minimum cost edge which crosses two subgraphs. Now, if you tell me that the minimum cost edge must belong to MST of G, then Prim’s Algorithm must be always correct at finding MST.
Now, let’s prove the correctness of Concept2, because we know the correctness of Prim’s algorithm is established on Concept2. In the following, we prove by contradiction.
Setup:
Given an arbitrary Cut(A, B) in an undirected and weighted graph G(V, E), we have an minimum cost edge e* that crosses subgraph A&B. Let µ* denote the minimum spanning tree of G.
Assume the proposition “If an edge e = e*, then e belongs to MST” (proposition1) is false.
Then, the proposition “If edge e = e*, then e does NOT belong to the MST, µ*”(proposition2) must be true.
Proof:
Because µ* is the MST of G, µ* must have one edge (denoted as e’) that crosses the subgraph A&B. Notice that e’ is not equal to e*, because proposition2 is true. Also notice that e’ and e* are in a cycle, because the two end vertices of e’ are in µ* (MST includes all vertices in G).
If we now delete e’ from µ* and add e’ into µ*, we get another spanning tree (denoted as µ) that spans the whole graph. This spanning tree µ has a lower total cost than µ*, because the cost of e* < the cost of e’ (remember e* is the minimum cost edge). This fact contradicts that µ* is the MST of G.
We conclude that the proposition2 leads this the contradiction. Thus, proposition2 must not be true, which means the proposition1 must be true. By this, we complete the proof for Concept2.
We have discussed that, if the Concept2 is true, Prim’s Algorithm must be correct. Thus, we also complete the proof of correctness for Prim’s Algorithm. Q.E.D.
All the above proof are written by myself with a reference to this website: https://www.commonlounge.com/discussion/614bbd3cc5fb41639b6c8c907ebec7d5
Best,
Ben