CMPS 144
Demonstration of Prim's Algorithm

It is assumed that you have already examined Prim's Algorithm at least up to the section titled "What if G is not Connected".

You will recall from there that

Here we show the state of affairs, before and after each iteration of the main loop, when Prim's algorithm is applied to the connected graph shown below, whose vertices are named A through I. Arbitrarily, vertex A was chosen to be the first one placed into S. Vertices in S are shown as boxes; others are shown as circles. Edges in E' are shown in green. For each vertex in Z, the minimum-cost edge connecting it to a vertex in S is shown using a thick line.

Initially
S = {A}
Z = {B,D,G}
E' = ∅
closest[D] = A (cost 3)
closest[B] = A (cost 7)
closest[G] = A (cost 9)

After choosing edge {A,D}
S = {A,D}
Z = {B,G}
E' = {{A,D}}
closest[B] = D (cost 2)
closest[G] = D (cost 5)

After choosing edge {B,D}
S = {A,D,B}
Z = {C,F,G}
E' = {{A,D},{B,D}}
closest[C] = B (cost 3)
closest[F] = B (cost 5)
closest[G] = D (cost 5)

After choosing edge {B,C}
S = {A,D,B,C}
Z = {E,F,G,H}
E' = {{A,D},{B,D},{B,C}}
closest[H] = C (cost 4)
closest[F] = B (cost 5)
closest[G] = D (cost 5)
closest[E] = C (cost 6)

After choosing edge {C,H}
S = {A,D,B,C,H}
Z = {E,F,G,I}
E' = {{A,D},{B,D},{B,C},{C,H}}
closest[E] = H (cost 2)
closest[I] = H (cost 4)
closest[F] = B (cost 5)
closest[G] = D (cost 5)

After choosing edge {E,H}
S = {A,D,B,C,H,E}
Z = {F,G,I}
E' = {{A,D},{B,D},{B,C},{C,H},{E,H}}
closest[I] = H (cost 4)
closest[F] = B (cost 5)
closest[G] = D (cost 5)

After choosing edge {H,I}
S = {A,D,B,C,H,E,I}
Z = {F,G}
E' = {{A,D},{B,D},{B,C},{C,H},{E,H},{I,H}}
closest[G] = I (cost 3)
closest[F] = B (cost 5)

After choosing edge {G,I}
S = {A,D,B,C,H,E,I,G}
Z = {F}
E' = {{A,D},{B,D},{B,C},{C,H},{E,H},{I,H},{G,I}}
closest[F] = B (cost 5)

After choosing edge {B,F}
S = {A,D,B,C,H,E,I,G,F}
Z = ∅
E' = {{A,D},{B,D},{B,C},{C,H},{E,H},{I,H},{G,I},{B,F}}