Minimum Spanning Trees
[Minimum Spanning Trees] [Hello. This is Rachel Cardell-Oliver from the University of Western
Australia]
We have already seen spanning trees, and the BFS algorithm for generating a spanning tree of an
undirected, unweighted graph. In this talk, we will consider the problem of finding a minimum
spanning tree for a weighted graph. The graph could be directed or undirected. Recall that:
A spanning tree of a graph is a connected subgraph of G that spans all vertices.
A minimum spanning tree is the spanning tree with minimum total cost.
An MST is a tree because is contains no cycles (we say it is acyclic). It is spanning because it
covers every vertex. And it is minimum because it is the smallest cost tree amongst all the
possible spanning trees.
For a graph with V vertices, the minimum spanning tree must have V-1 edges.
In this talk we will study Prim’s algorithm for generating an MST for a weighted graph. Prim’s
algorithm is a greedy algorithm. A greedy algorithm is an algorithm in which at each stage a
locally optimal choice is made. A greedy algorithm is therefore one in which no overall strategy is
followed, but you simply do whatever looks best at the moment. For example a mountain climber
using the greedy strategy to climb Everest would at every step climb in the steepest direction.
From this analogy we get the computational search technique known as hill-climbing. In general
greedy methods have limited use, but fortunately, the problem of finding a minimum spanning
tree can be solved by a greedy method.
The idea behind Prim’s algorithm is to grow a minimum spanning tree edge-by-edge by always
adding the shortest edge that touches a vertex in the current tree. An alternative approach, used
in Kruskal’s algorithm (which we will not study in this course), always maintains a spanning
subgraph which only becomes a tree at the final stage. On the other hand, Prim’s algorithm always
maintains a tree which only becomes spanning at the final stage.
Prim’s Algorithm
Prim’s algorithm uses a priority queue as its main data structure. Prim’s algorithm is similar to
the BFS algorithm we studied earlier. The main new part is shown in blue. In order to implement
Prim’s algorithm we maintain four data structures:
1. Q, a priority queue of vertices to be processed. The priority of a vertex is given by its key
value, key[v]: with lowest values being given the highest priority (front of the queue).
2. An array colour, with values white (0), grey (1) or black (2) for each vertex.
3. An array key, with the weights of the edges in the tree.
4. An array π, which where π(v) contains the immediate parent of v in the MST. That is the
edges of the spanning tree are given by (v, π(v)).
Prim’s Algorithm
Initial step:
colour[v]=white for all vertices v
key[v] = infinity for all vertices v
π(v) is undefined for all vertices v
Choose an arbitrary vertex s and place it into the priority queue Q
key[s] = 0
colour[s] = grey
Repeated step:
while Q is not empty
dequeue highest priority w from the head of Q
for each vertex v adjacent to w
if colour[x] is white then
colour[v] = grey
key[v] = edgeWeight(w,v)
π(v) = w
enqueue v into priority queue Q using key[v] as priority
else if colour[v] is grey then
if key[v] > edgeWeight(w,v) then
key[v] = edgeWeight(w,v)
π(v) = w
end if
end if
end for
colour[w] = black
end while
At the end of the MST search, every vertex in the graph will have the colour black and the parent
array π will contain the edges of the MST.
Prim’s MST algorithm example
Example: weighted, undirected graph with 8 edges. Edge weights shown in blue.
8
0
3
0
0
0
0
0
0
3
0
1
0
0
0
0
0
0
1
0
2
0
4
0
0
0
0
2
0
0
3
1
0
0
0
0
0
0
2
0
0
0
0
4
3
2
0
2
6
0
0
0
1
0
2
0
1
Prim’s Minimum Spanning Tree algorithm steps:
colours: white = 0, grey = 1, black = 2
Initially: Q={0}
colour = 1, 0, 0, 0, 0, 0, 0, 0,
key = 0, infty, infty, infty, infty, infty, infty, infty,
p = -1, -1, -1, -1, -1, -1, -1, -1,
Step 1: Q = {0}
Dequeue 0
Adjacent vertex to 0 is 1 which is white.
Set key[1]=3 and enqueue 1 with priority 3.
p[1]=3, colour(1)=grey
colour(0) = black
Status after step 1:
colour = 2, 1, 0, 0, 0, 0, 0, 0,
key = 0, 3, infty, infty, infty, infty, infty, infty,
p = -1, 0, -1, -1, -1, -1, -1, -1,
Step 2: Q = {1}
Dequeue 1
Adjacent vertex to 1 is 2 which is white.
Set key[2]=1 and enqueue 2 with priority 1.
p[2]=1, colour(2)=grey
colour(1) = black
Status after step 2:
colour = 2, 2, 1, 0, 0, 0, 0, 0,
key = 0, 3, 1, infty, infty, infty, infty, infty,
p = -1, 0, 1, -1, -1, -1, -1, -1,
Step 3: Q = {2}
Dequeue 2
0
0
0
0
0
6
1
0
Adjacent vertices to 2 are 3,5 which are both white.
Set key[3]=2 and enqueue 3 with priority 2.
p[3]=2, colour(3)=grey
Set key[5]=4 and enqueue 5 with priority 4.
p[5]=4, colour(4)=grey
colour(2) = black
Status after step 3:
colour = 2, 2, 2, 1, 0, 1, 0, 0,
key = 0, 3, 1, 2, infty, 4, infty, infty,
p = -1, 0, 1, 2, -1, 2, -1, -1,
Step 4: Q = { 3,5 }
Dequeue 3
Adjacent vertices to 3 are 2,5,6 of which 6 is white 2,5 grey.
2 is grey, so test key[2] > 2 which is 1 > 2 is false so no change
5 is grey, so test key[5] > 3 which is 4 > 3 is true
So set key[5] = 3 and p[5]=3
6 is white, so set key[6]=1 and enqueue 6 with priority 1
Set p[6]=1, colour(6)=grey
colour(3) = black
Status after step 4:
colour = 2, 2, 2, 2, 0, 1, 1, 0,
key = 0, 3, 1, 2, infty, 3, 1, infty,
p = -1, 0, 1, 2, -1, 3, 3, -1,
Step 5: Q = { 6,5 }
Dequeue 6
Adjacent vertices to 6 are 5,7 of which 7 is white 5 grey.
5 is grey, so test key[5] > 2 which is 3 > 2 is true
So set key[5] = 2 and p[5] = 2
7 is white, so set key[7]=1 and enqueue 7 with priority 1
Set p[7]=1, colour(7)=grey
colour(3) = black
Status after step 5:
colour = 2, 2, 2, 2, 0, 1, 2, 1,
key = 0, 3, 1, 2, infty, 2, 1, 1,
p = -1, 0, 1, 2, -1, 6, 3, 6,
Step 6: Q = { 7,5 }
Dequeue 7
Adjacent vertices to 6 are 5,6 which are both grey
5 is grey, so test key[5] > 6 which is 2 > 6 is false
6 is grey, so test key[6] > 1 which is 1 > 1 is false
colour(7) = black
Status after step 6:
colour = 2, 2, 2, 2, 0, 1, 2, 2,
key = 0, 3, 1, 2, infty, 2, 1, 1,
p = -1, 0, 1, 2, -1, 6, 3, 6,
Step 7: Q = { 5 }
Dequeue 5
Adjacent vertices to 5 are 2,3,4,6,7
2,3,6,7 are black so no action
4 is white, so set key[4]=2 and enqueue 4 with priority 2
Set p[4]=2, colour(4)=grey
colour(5) = black
Status after step 7:
colour = 2, 2, 2, 2, 1, 2, 2, 2,
key = 0, 3, 1, 2, 2, 2, 1, 1,
p = -1, 0, 1, 2, 5, 6, 3, 6,
Step 8: Q = { 4 }
Dequeue 4
Adjacent vertex to 4 is 5
5 is black so no action
colour(4) = black
Status after step 8:
colour = 2, 2, 2, 2, 2, 2, 2, 2,
key = 0, 3, 1, 2, 2, 2, 1, 1,
p = -1, 0, 1, 2, 5, 6, 3, 6,
Prim MST result:
edge=(1,0) weight=3
edge=(2,1) weight=1
edge=(3,2) weight=2
edge=(4,5) weight=2
edge=(5,6) weight=2
edge=(6,3) weight=1
edge=(7,6) weight=1
Java code for Prim’s algorithm is provided in PrimMST.java in your Java code bundle.
[ This talk is by Rachel Cardell-Oliver based on the UWA CITS2200 lecture notes ]
© Copyright 2026 Paperzz