Prims Algorithm
Prims Algorithm
On
Prim’s Algorithm
• Introduction
• Basics concepts
• Minimum Spanning Tree Problem
• Solution to Minimum Spanning Tree Problem
• Generic-MST
• Prim’s algorithm details
• Complexity Analysis of Prim’s Algorithm.
• Applications and latest research work based on Prim’s Algorithm
• Summary
• Queries.
Introduction
b b b
a a a
c c c
e e e
d d d
b
a 7 32
c
10 23
9 e
d
Weighted Graph
Basic Concepts Cont….
a 32 b a 7 32 b a 7 32 b
c c c
10 23 23 10 23
9 e 9 e e
d d d
Spanning Tree 1, Spanning Tree 2, Spanning Tree 3,
w=74 w=71 w=72
(Minimum Spanning Tree)
Minimum Spanning Tree Problem
Kruskal’s
algorithm
Prim’s
algorithm
GENERIC-MST Algorithm
GENERIC-MST ( G, w )
2 do key[u]←∞
3 Π[u]←NIL Initialization
4 key[r]←0
u a b c d e f g h i
5 Q←V[G]
key[u] ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
6 while Q is not Empty
Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL
7 do u←EXTRACT-MIN(Q)
After Steps 1-3
8 for each v∈Adj[u] u a b c d e f G H i
After Step 4
11 key[v]←w(u, v)
Q A b c d e f g h i
A EMPTY
After Step 5
PRIM’s Algorithm (Steps 6 to 11) ……
MST-PRIM(G,w,r)
1 for each u∈V[G]
2 do key[u]←∞ Example Graph
3 Π[u]←NIL 8 7
b c d
4 key[r]←0 4 9
2
5 Q←V[G]
6 while Q is not Empty a 11 i 4 14 e
7 do u←EXTRACT-MIN(Q) 7
8 6 10
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v] h 1 g 2 f
10 then Π[v]←u
11 key[v]←w(u,v)
Before Step 6 Steps 6-11 (for u=a) After Step 6-11 (for u=a)
v∈Q AND
Π[v]←u,
Q a b c d e f g h i u v w(u,v) < Key[v]
Key[v]←w(u,v) Q a b c d e f g h i
Π[b]←a,
key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ a b YES
Key[b]←4
key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
Π[h]←a,
Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL a h YES Π[u] NIL a NIL NIL NIL NIL NIL a NIL
Key[h]←8
Q a b c d E f g h i
EM
Q b c d e f g h i
A
PTY A a
PRIM’s Algorithm (Steps 6 to 11) ……
MST-PRIM(G,w,r)
1 for each u∈V[G]
2 do key[u]←∞ Example Graph
3 Π[u]←NIL 8 7
b c d
4 key[r]←0 4 9
2
5 Q←V[G]
6 while Q is not Empty a 11 i 4 14 e
7 do u←EXTRACT-MIN(Q) 7
8 6 10
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v] h 1 g 2 f
10 then Π[v]←u
11 key[v]←w(u,v)
Before Step 6 Steps 6-11 (for u=a) After Step 6-11 (for u=a)
v∈Q AND
Π[v]←u,
Q a b c d e f g h i u v w(u,v) < Key[v]
Key[v]←w(u,v) Q a b c d e f g h i
Π[b]←a,
key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ a b YES
Key[b]←4
key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
Π[h]←a,
Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL a h YES Π[u] NIL a NIL NIL NIL NIL NIL a NIL
Key[h]←8
Q a b c d e f g h i
A
Q b c d e f g h i
A a
PRIM’s Algorithm (Steps 6 to 11, for u=b)
Q b c d e f g h i
A a After Step 6-11 (for u=b)
Steps 6-11(for u=b)
Q a b c d e f g h i
v∈Q AND
Then Π[v]←u,
u v w( u, v ) < Key[v]
Key[v]←w(u,v) key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
b c YES Π[c]←b, Key[c]←8 Π[u] NIL a b NIL NIL NIL NIL a NIL
b h NO do nothing
Q c d e f g h i
b a NO do nothing A a b
PRIM’s Algorithm (Steps 6 to 11, for u=b)
Q b c d e f g h i
A a After Step 6-11 (for u=b)
Q c d e f g h i
After Step 6-11 (for u=c)
A a b
Steps 6-11(for u=c)
v∈Q AND
Q a b c d e f g h i
Then Π[v]←u,
u v w( u, v ) < Key[v] Key[v]←w(u,v)
key[u] 0 4 8 7 ∞ 4 ∞ 8 2
c i YES Π[i]←c, Key[i]←2
c f YES Π[f]←c, Key[f]←4
Π[u] NIL a b c NIL c NIL a c
Q c d e f g h i
After Step 6-11 (for u=c)
A a b
Steps 6-11(for u=c)
v∈Q AND
Q a b c d e f g h i
Then Π[v]←u,
u v w( u, v ) < Key[v] Key[v]←w(u,v)
key[u] 0 4 8 7 ∞ 4 ∞ 8 2
c i YES Π[i]←c, Key[i]←2
c f YES Π[f]←c, Key[f]←4
Π[u] NIL a b c NIL c NIL a c
Q d e f g h i
A a b c
Steps 6-11(for u=i) After Step 6-11 (for u=i)
v∈Q AND Then Π[v]←u, Q a b c d e f g h i
u v w( u, v ) < Key[v] Key[v]←w(u,v)
key[u] 0 4 8 7 ∞ 4 6 7 2
i h YES Π[h]←i, Key[h]←7
Π[u] NIL a b c NIL c i i c
i g YES Π[g]←i, Key[g]←6
Q d e f g h
i c NO Do Nothing A a b c i
PRIM’s Algorithm (Steps 6 to 11, for u=i)
6 while Q is not Empty Example Graph
7 do u←EXTRACT-MIN(Q)
8 7
8 for each v∈Adj[u] b c d
9 do if v∈Q and w(u,v)<key[v] 4 2 9
10 then Π[v]←u
11 key[v]←w(u,v) a 11 i 4 14 e
7
Status of Q before using u=i 8 6 10
Q a b c d e f g h i
h 1 g 2 f
key[u] 0 4 8 7 ∞ 4 ∞ 8 2
Π[u] NIL a b c NIL c NIL a c
Q d e f g h i
A a b c After Step 6-11 (for u=i)
Steps 6-11(for u=i)
Q a b c d e f g h i
v∈Q AND
w( u, v ) < Key[v] Then Π[v]←u,
u v
Key[v]←w(u,v) key[u] 0 4 8 7 ∞ 4 6 7 2
i h YES Π[h]←i, Key[h]←7 Π[u] NIL a b c NIL c i i c
Q e
After Step 6-11 (for u=e)
A a b c i f g h d
Steps 6-11(for u=e) Q a b c d e f g h i
v∈Q AND
Then Π[v]←u,
u v w( u, v ) < Key[v]
Key[v]←w(u,v)
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
e d NO Do Nothing
Q
e f NO Do Nothing
A a b c i f g h d e
PRIM’s Algorithm (Steps 6 to 11, for u=e)
6 while Q is not Empty
Example Graph
7 do u←EXTRACT-MIN(Q)
8 7
8 for each v∈Adj[u] b c d
9 do if v∈Q and w(u,v)<key[v] 4 2 9
10 then Π[v]←u
11 key[v]←w(u,v) a 11 i 4 14 e
7
8 6 10
Status of Q before using u=e
Q a b c d e f g h i h 1 g 2 f
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q e
After Step 6-11 (for u=e)
A a b c i f g h d
Steps 6-11(for u=e) Q a b c d e f g h i
v∈Q AND
Then Π[v]←u,
u v w( u, v ) < Key[v]
Key[v]←w(u,v)
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
e d NO Do Nothing
Q
e f NO Do Nothing
A a b c i f g h d e
PRIM’s Algorithm (Steps 6 to 11, for u=e)
MST-PRIM( G, w, r ) Example Graph
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL
4 key[r]←0
5 Q←V[G] 8 7 MST
b c d
6 while Q is not Empty 4 2 9
7 do u←EXTRACT-MIN(Q) a 11 i 4 14 e
7
8 for each v∈Adj[u] 8 6 10
h 1 g 2 f
9 do if v ∈ Q and w( u ,v ) < key[v]
Q a b c d e f g h i
10 then Π[v]←u
key[u] 0 4 8 7 9 4 2 1 2
11 key[v]←w( u, v )
Π[u] NIL a b c d c f g c
A a b c i f g h d e
Complexity Analysis of Prim’s algorithm
MST-PRIM(G,w,r)
5 Q←V[G]
32
Example 2 Continue…..
1
28
10 2
16
14
3
6 7
24
25
12
5 18
22
4
Applications
• Design of a network
(telephone network, computer network, electronic circuitry, electrical wiring
network, water distribution network, cable TV network)
• A less obvious application is that the minimum spanning tree can be used
to approximately solve the travelling salesman problem.
• Routing algorithms
• Cartography
• Geometry
• Clustering
•Tour/Travel Management
Latest research papers based on Prim’s Algorithm
• www.mathworld.wolfram.com
• IEEE xplore
Summary
38
Thanks!
39