Prim’s Algorithm
We consider a weighted connected graph G with n vertices. Prim’s algorithm finds a
minimum spanning tree of G.
procedure Prim(G: weighted connected graph with n vertices)
T := a minimum-weight edge
for i = 1 to n − 2
begin
e := an edge of minimum weight incident to a vertex in T and not forming a circuit
in T if added to T
T := T with e added
end
return(T)
Example: Use Prim’s algorithm to find a minimum spanning tree in the following
weighted graph. Use alphabetical order to break ties.
r.
....................................................................................................................................................................................................
r
.
....................................................................................................................................................................................................
r. ............................................................................................................................................................................................................................................................ r
. ............................................................................................................................................................................................................................................................ r.
....................................................................................................................................................................................................
r
.
.....................................................................................................................................................................................................
........................................................................................................................................................................
.
...................................................................................................................................................................................................................................................................................................................
.
........................................................................................................................................................................
a z
c e
b d
3 4
3
2
2 2
5
16
Solution: Prim’s algorithm will proceed as follows. First we add edge {d, e} of weight
1. Next, we add edge {c, e} of weight 2. Next, we add edge {d, z} of weight 2. Next, we
add edge {b, e} of weight 3. And finally, we add edge {a, b} of weight 2. This produces
a minimum spanning tree of weight 10. A minimum spanning tree is the following.
r.
....................................................................................................................................................................................................
r
r. ............................................................................................................................................................................................................................................................ r
r.
....................................................................................................................................................................................................
r
.
........................................................................................................................................................................
.
...................................................................................................................................................................................................................................................................................................................
a z
c e
b d
3
2
2 2
1
Gilles Cazelais. Typeset with LATEX on December 6, 2006.

Prim algorithm

  • 1.
    Prim’s Algorithm We considera weighted connected graph G with n vertices. Prim’s algorithm finds a minimum spanning tree of G. procedure Prim(G: weighted connected graph with n vertices) T := a minimum-weight edge for i = 1 to n − 2 begin e := an edge of minimum weight incident to a vertex in T and not forming a circuit in T if added to T T := T with e added end return(T) Example: Use Prim’s algorithm to find a minimum spanning tree in the following weighted graph. Use alphabetical order to break ties. r. .................................................................................................................................................................................................... r . .................................................................................................................................................................................................... r. ............................................................................................................................................................................................................................................................ r . ............................................................................................................................................................................................................................................................ r. .................................................................................................................................................................................................... r . ..................................................................................................................................................................................................... ........................................................................................................................................................................ . ................................................................................................................................................................................................................................................................................................................... . ........................................................................................................................................................................ a z c e b d 3 4 3 2 2 2 5 16 Solution: Prim’s algorithm will proceed as follows. First we add edge {d, e} of weight 1. Next, we add edge {c, e} of weight 2. Next, we add edge {d, z} of weight 2. Next, we add edge {b, e} of weight 3. And finally, we add edge {a, b} of weight 2. This produces a minimum spanning tree of weight 10. A minimum spanning tree is the following. r. .................................................................................................................................................................................................... r r. ............................................................................................................................................................................................................................................................ r r. .................................................................................................................................................................................................... r . ........................................................................................................................................................................ . ................................................................................................................................................................................................................................................................................................................... a z c e b d 3 2 2 2 1 Gilles Cazelais. Typeset with LATEX on December 6, 2006.