Prim’s MST Algorithm
• Begin with the following subsets:
• A = any one of the vertices
• B = all of the other vertices
• Repeatedly do the following:
• select the lowest-cost edge (v
a
, v
b
)
connecting a vertex in A to a vertex in B
• add (v
a
, v
b
) to the spanning tree
• move vertex
v
b
from set B to set A
• Continue until set A contains all of the vertices.
Example: Prim’s Starting from Concord
• Tracing the algorithm:
edge added set A set B
{Con} {Alb, Bos, NY, Ptl, Pts, Pro, Wor}
(Con, Wor) {Con, Wor} {Alb, Bos, NY, Ptl, Pts, Pro}
(Wor, Pro) {Con, Wor, Pro} {Alb, Bos, NY, Ptl, Pts}
(Wor, Bos) {Con, Wor, Pro, Bos} {Alb, NY, Ptl, Pts}
(Bos, Pts) {Con, Wor, Pro, Bos, Pts} {Alb, NY, Ptl}
(Pts, Ptl) {Con, Wor, Pro, Bos, Pts, Ptl} {Alb, NY}
(Wor, Alb) {Con, Wor, Pro, Bos, Pts, Ptl, Alb} {NY}
(Pro, NY) {Con,Wor,Pro,Bos,Pts,Ptl,Alb,NY} {}
Portland (Ptl)
Portsmouth(Pts)
Boston (Bos)
Concord (Con)
Albany (Alb) Worcester(Wor)
Providence(Pro)
New York (NY)
39
54
44
83
49
42
185
134
63
84
74