Unit - Ii MST Prim's Algorithms
Unit - Ii MST Prim's Algorithms
1. The new graph is constructed - with one node from the old graph.
2. While new graph has fewer than n nodes i.e n-1 edges for n nodes,
1. Find the node from the old graph with the smallest connecting
edge to the new graph,
2. Add it to the new graph
Note: each time selecting the node whose connecting edge has the
smallest weight out of the available nodes’ connecting edges.
❖ Every step will have joined one node, so that at the end we will have
one graph with all the nodes and it will be a minimum spanning tree
of the original graph.
Prim’s Algorithm Example
• Find out the minimum cost spanning tree for the
connected graph shown below using Prim’s
algorithm.
8 7
b c d
4 9
2
11 4 14
a e
i
7 6
8 10
h 1
g 2
f
Prim’s Algorithm Example
• Solution
• For the given graph the vertex set V [G] = {a, b,
c, d, e, f, g, h, i}
• The Edge set E = {(a, b), (b, c), (b, h), (c, d), (c,
f), (c, i), (d, e), (d, f), (e, f), (f, g), (g, h), (g, i), (h,
i)}
Q = {a, b, c, d, e, f, g, h, i}
key [a] 0 π [a] NIL
key [b] ∞ π [b] NIL
key [c] ∞ π [c] NIL
key [d] ∞ π [d] NIL
key [e] ∞ π [e] NIL
key [f] ∞ π [f] NIL
key [g] ∞ π [g] NIL
key [h] ∞ π [h] NIL
key [i] ∞ π [i] NIL
Prim’s Algorithm Example π [a]
π [b]
NIL
a
π [c] NIL
while (Q ≠ NULL) True
π [d] NIL
u ← Extract_Min(Q) u=a
π [e] NIL
for each v ϵ ADJ[u] {b, h} ← ADJ[a]
π [f] NIL
v=b
π [g] NIL
if (b ϵ Q and w(a, b) < key [b]) If (b ϵ Q and 4 < ∞ ) True
π [h] a
then π[b] ← u π[b] ← a
π [i] NIL
key [b] ← w(u, v) key [b] ← 4
key [a] 0
v=h
key [b] 4
if (h ϵ Q and w(a, h) < key [h]) If (h ϵ Q and 8 < ∞ ) True
key [c] ∞
then π[h] ← u π[h] ← a
key [d] ∞
key [h] ← w(u, v) key [h] ← 8
key [e] ∞
B 4 C
4
2 1
A 4 E
1 F
D 2 3
10
G 5
5 6 3
4
I
H
2 3
J
Old Graph New Graph
Choose {A} to start
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,D}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,D}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,C,D}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,C,D,F}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,C,D,E,F}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,C,D,E,F,G}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,C,D,E,F,G,I}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,C,D,E,F,G,I,J}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Old Graph New Graph
{A,B,C,D,E,F,G,H,I,J}
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A 4 E F
1
D 2 3
10 D 2 3
G 5 10
G 5
5 6 3
5 6 3
4
I 4
H I
3 H
2 J
2 3
J
Complete Graph Minimum Spanning Tree
Cost=1+4+4+2+1+2+3+3+2=22
B 4 C
4 B 4 C
2 1 4
2 1
A 4 E
1 F A E F
1
D 2 3
10 D 2
G 5
G
5 6 3
3
4
I
H I
3 H
2 J
2 3
J
Prim’s Algorithm
Uses a priority queue Q to find a light edge quickly.
Each object in Q is a vertex in V - VA.
Min-heap as a binary
tree. 1 11
2 14 3 13
4 18 5 17 6 19 7 20
8 18 9 24 26 10
Analysis of Prim's Algorithm
Unlike Kruskal’s, it doesn’t need to see all of the graph at once. It can
deal with it one piece at a time. It also doesn’t need to worry if adding
an edge will create a cycle since this algorithm deals primarily with the
nodes, and not the edges.
Prim’s Algorithm Text Book Example
Construct the minimum spanning tree (MST) for the given graph using
Prim’s Algorithm
Prim’s Algorithm Text Book Example
Step-01: Step-04:
Step-03:
Step-06: