Prim's Algorithm & Proof
The Algorithm
Notice that this algorithm only applies to connected graphs.
-
Start with a blank list v i s i t e d : = { } visited:=\{\} visited:={}, which is used to record visted vertices. We also need to keep track of the edges we pick.
-
The first iteration starts here: Pick some arbitrary vertex v 1 v_1 v1 to start, update the list to { v 1 } \{v_1\} {v1}. This iteration, the 0 0 0th iteration, ends.
-
If v 1 v_1 v1 is not all the vertex we have in the graph, continue, or the algorithm ends.
-
The second iteration starts. Look for the edge incident on v 1 v_1 v1 such that
- (a) the edge is incident on v 1 v_1 v1 and an unvisited vertex
- (b) the edge has the smallest weight
-
Say the edge we pick, e 1 e_1 e1, is incident on v 1 v_1 v1 and v 2 v_2 v2.
- Update the list to v i s i t e d = { v 1 , v 2 } visited=\{v_1,v_2\} visited={v1,v2} and record e 1 e_1 e1. This iteration is finished. If v 1 , v 2 v_1,v_2 v1,v2 are not all the vertices we have in the graph, continue, or the algorithm ends. -
The next iteration will be to choose the edge with the smallest weight among all the edges incident on v 1 v_1 v1 OR v 2 v_2 v2 (but not both, or we will be choosing a duplicate)and some unvisited vertex. Say we choose an edge, e 2 e_2 e2, that is incident on v 3 v_3 v3.
-
Update the list to v i s i t i e d = { v 1 , v 2 , v 3 } visitied = \{v_1,v_2,v_3\} visitied={v1,v2,v3} and record e 2 e_2 e2. This iteration is finished.
-
Repeat this process again, but this time we will be choosing the edge with the smallest weight among all the edges incident on v 1 v_1 v1 OR v 2 v_2 v2 OR v 3 v_3 v3 and some univisited vertex.
-
When the last vertex in the graph is added to v i s i t e d visited visited, we are done.
Prim’s Algorithm is Greedy
- Prim’s Algorithm is a greedy algorithm, which means in each iteration, it always makes the locally best choice (i.e. the best choice available in each iteration). However, we might argue that a collection of local optimum may not always give the global optima, yet the algorithm does always give the minimum spanning tree. We want to prove this.
Prim’s Algorithm Always Works
- Claim: Prim’s Algorithm is correct. That is, the tree obtained at the termination of the algorithm is a minimum spanning tree.
Proof
- Assume T i T_i Ti is the (sub)graph obtained after the i i ith iteration. We will be showing, by induction, that for each i , 0 ≤ i ≤ n − 1 i,0≤i≤n-1 i,0≤i≤n−1 (assuming n n n vertices), T i T_i Ti is in a minimum spanning tree.
- Let T 0 T_0 T0 be the graph obtained after ther 0 0 0th iteration. Notice that T 0 T_0 T0 must be contained in every minimum spanning tree because a spanning tree contains all vertices, and T 0 T_0 T0 is just a single vertex.
- Assume for i = k , k ≥ 0 i=k,k≥0 i=k,k≥0, T i T_i Ti is contained in some minimum spanning tree.
- Consider i = k + 1 i=k+1 i=k+1. The algorithm will pick an edge incident on some vertex v ∈ T k v\in T_{k} v∈Tk and another vertex w ∉ T k w\not\in T_k w∈Tk, say e e e, with minimum weight, to add to T k T_k Tk (along with w w w) to form T k + 1 T_{k+1} Tk+1. Assume the minimum spanninbg tree that T k T_k Tk is in is T 1 T_1 T1.
- If T 1 T_1 T1 contains e e e, then by construction the only way it contains e e e is by having T k + 1 T_{k+1} Tk+1 as its subgraph, then we are done.
- If T 1 T_1 T1 does not contain e e e, then T 2 : = T 1 ∪ { e } T_2:=T_1\cup \{e\} T2:=T1∪{e} contains a cycle (as T 1 T_1 T1 is already a spanning tree). Assume we remove from T 2 T_2 T2 an edge that is not e e e, say e ′ e' e′ incident on x ∈ T k x\in T_{k} x∈Tk and y ∉ T k y\not\in T_{k} y∈Tk, from the cycle, and obtain T 3 T_3 T3 i.e. T 3 = T 1 ∪ { e } − { e ′ } T_3=T_1\cup \{e\}-\{e'\} T3=T1∪{e}−{e′}.
- Notice we only removed this single edge, so other edges and vertices in T 1 T_1 T1 are intact. Therefore, By the important exercise, we know T 3 T_3 T3 is still connected, and it is acyclic.
- Moreover, as T 3 T_3 T3 contains all the vertices, it is a spanning tree. Since T 1 T_1 T1 is already a minimum spanning tree, w ( T 1 ) ≤ w ( T 3 ) w(T_1)≤w(T_3) w(T1)≤w(T3).
- However, because e e e is the edge chosen by the algorithm at the ( k + 1 ) (k+1) (k+1)th iteration, w ( e ) ≤ w ( e ′ ) w(e)≤w(e') w(e)≤w(e′).
- Thus, w ( T 3 ) = w ( T 1 ) + w ( e ) − w ( e ′ ) ≤ w ( T 1 ) w(T_3)=w(T_1)+w(e)-w(e')≤w(T_1) w(T3)=w(T1)+w(e)−w(e′)≤w(T1).
- Thus, w ( T 3 ) = w ( T 1 ) w(T_3)=w(T_1) w(T3)=w(T1), which means T k + 1 T_{k+1} Tk+1 is in T 3 T_3 T3, another minimum spanning tree.
- Thus,
T
k
+
1
T_{k+1}
Tk+1 is always in some minimum spanning tree, which verifies the claim.
Notice that in the proof we used an important technique regarding spanning tree: if you add any edge that was not originally in the spanning tree to a spanning tree, you will end up with a cycle in the resulting graph. Then, you may want to use the important exercise to show something about connectivity. Below is another application of this idea.
The Edge with Minimum Weight
- Claim: The edge with the minimum weight in a connected graph is in every minimum spanning tree of the graph.
Proof
- Suppose e e e in a connected graph G G G is the edge with the minimun weight.
- Suppose T T T is a minimum spanning tree of G G G. Suppose it does not contain e e e. Then we add e e e to T T T, and we obtain a cycle containing e e e. Suppose we remove a single edge in that cycle that is not e e e. Say we remove e ′ e' e′. By the important exercise, we will have another spanning tree. However, since w ( e ) ≤ w ( e ′ ) w(e)≤w(e') w(e)≤w(e′), the resulting spanning tree’s weight is less than or equal to T T T. If w ( e ) = w ( e ′ ) w(e)=w(e') w(e)=w(e′), then both e e e and e ′ e' e′ are the edges with the minimum weight, and we are done; if w ( e ) < w ( e ′ ) w(e)<w(e') w(e)<w(e′), we get a contradiction, so T T T must contain e e e in the first place.