PRIM’S MINIMUM SPANNING TREE
Informatics Department
Parahyangan Catholic University
PRIM’S MST
Used to solve minimum spanning tree problem
Has the property that edges in the set A always forms a single tree
The tree starts from an arbitrary vertex r,
and grows until the tree spans all the vertices
in V
PRIM’S MST
At each step, a light edge is added to the tree A that connects A to an isolated vertex
A
b
d a
c 14
8 5
22
Light edge ≡ edge with smallest weight that connects A
to an isolated vertex
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
A
EXAMPLE :: INITIALIZATION
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
a
0b
c
d
e
f
g
h
i
Priority queue Q :
EXAMPLE :: ITERATION #1
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
a
0b
4h
8c
d
e
f
g
i
Priority queue Q :
w(a,b) = 4 w(a,h) = 8
EXAMPLE :: ITERATION #2
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
b
4h
8c
8d
e
f
g
i
Priority queue Q :
w(b,a) = 4 w(b,h) = 11
w(b,c) = 8
a not in Q 11 > key[h]
EXAMPLE :: ITERATION #3
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
h
8g
1c
8i
7d
e
f
Priority queue Q :
w(h,a) = 8 w(h,b) = 11
w(h,i) = 7 w(h,g) = 1
a not in Q b not in Q
EXAMPLE :: ITERATION #4
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
g
1f
2i
6c
8d
e
Priority queue Q :
w(g,h) = 1 w(g,i) = 6 w(g,f) = 2
h not in Q
EXAMPLE :: ITERATION #5
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
f
2c
4i
6e
10d
14Priority queue Q :
w(f,g) = 2 w(f,c) = 4 w(f,d) = 14 w(f,e) = 10
g not in Q
EXAMPLE :: ITERATION #6
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
c
4i
2d
7e
10Priority queue Q :
w(c,b) = 8 w(c,i) = 2 w(c,f) = 4 w(c,d) = 7
b not in Q f not in Q
EXAMPLE :: ITERATION #7
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
i
2d
7e
10Priority queue Q :
w(i,h) = 7 w(i,g) = 6 w(i,c) = 2
h not in Q g not in Q c not in Q
EXAMPLE :: ITERATION #8
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
d
7e
9Priority queue Q :
w(d,c) = 7 w(d,f) = 14
w(d,e) = 9
c not in Q f not in Q
EXAMPLE :: ITERATION #9
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14
9
10
e
9Priority queue Q :
w(e,d) = 9 w(e,f) = 10
d not in Q f not in Q
RESULT #1 VS RESULT #2
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14 9
10
a
b c
i
h g f
d
e 8
8
11 4
7
4 2
7 6
1 2
14 9
10
PSEUDOCODE
1.
MST-Prim(G, w, r)
2.
for each u V do
3.
key[u] =
4.
parent[u] = NIL
5.
key[r] = 0
6.
Q = all vertices of G
7.
While Q ≠ do
8.
u = EXTRACT-MIN(Q)
9.
for each v adjacent to u do
10.
if v Q and w(u,v) < key[v] then
11.
parent[v] = u
12.
key[v] = w(u,v)
Initialization :
•Set all vertices’ key to
except the root, so that it will be the first vertex processed.
•Parent of each vertex is set to NIL.
•Min-priority queue Q contains all vertices.
IMPLEMENTATION
The performance of Prim’s MST depends on how we implement the min-priority queue Q
Suppose we implement Q using a min-heap:
Store vertices’ id on the heap’s array
Store vertices’ key on a separate array
Store vertices’ location on a separate array
Additionally, we need an array parent to
store the resulting tree
IMPLEMENTATION
Example:
vertex a b c d e f g h i
key 0 4 8 8
parent NIL a b NIL NIL NIL NIL a NIL locatio
n -1 -1 2 3 4 5 6 1 7
h
8c
8d
e
f
g
i
idx 1 2 3 4 5 6 7 8 9
heap h c d e f g i
h
c d
e f g i
-1 means the vertex is not in
the heap
TIME COMPLEXITY
The performance of Prim’s MST depends on how we implement the min-priority queue Q
If Q is implemented using min-heap :
(V = #vertices , E = #edges)
Initialization phase :
The main loop has V iterations :
While Q ≠ u = EXTRACT-MIN(Q) do for each v adjacent to u doif v Q and w(u,v) < key[v]
then parent[v] = u
key[v] = w(u,v) V
V.lg V E
E E
= DECREASE-KEY E lg(V) for each u V do
key[u] =
parent[u] = NIL key[r] = 0
Q = all vertices of G
V V V 1 V