SlideShare a Scribd company logo
1 of 39
Presentation
On
Prim’s Algorithm
Submitted to:
Mrs. Shano Solanki
Assistant Professor
Department of CSE
NITTTR Chandigarh
Submitted by:
Pankaj Thakur
ME(CSE) Modular
RN 171408
Sub Topics to be covered:
• 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
•Prim's algorithm is a greedy algorithm that finds a minimum spanning
tree for a weighted undirected graph.
•Developed in 1930 by Czech mathematician Vojtěch Jarník and later
rediscovered and republished by computer scientists Robert C. Prim in
1957 and E. W. Dijkstra in 1959.
•Therefore, it is also sometimes called the DJP algorithm, Jarník's
algorithm, the Prim–Jarník algorithm,or the Prim–Dijkstra algorithm.
Basic Concepts
Spanning Trees: A subgraph T of a undirected graph G = ( V, E ) is a
spanning tree of G if it is a tree and contains every vertex of G.
a
b
c
d
e
a
b
c
d
e
a
b
c
d
e
a
b
c
d
e
Graph
Spanning Tree 1 Spanning Tree 2 Spanning Tree 3
• Every connected graph has a spanning tree.
• May have multiple spanning tree.
• For example see this graph.
Spanning Tree Cont…
Basic Concepts Cont…..
Weighted Graph: A weighted graph is a graph, in which each edge has
a weight (some real number ) Example:
a
b
c
d
e
Weighted Graph
10
9
7 32
23
Basic Concepts Cont….
Minimum Spanning Tree in an undirected connected weighted graph is
a spanning tree of minimum weight. Example:
a
b
c
d
e
Weighted Graph
10
9
7 32
23
a
c
d
e
Spanning Tree 1,
w=74
10
9
32
23
a
c
d
e
Spanning Tree 2,
w=71
9
7 32
23
b b a
c
d
e
Spanning Tree 3,
w=72
10
7 32
23
b
(Minimum Spanning Tree)
Minimum Spanning Tree Problem
MST Problem : Given a connected weighted undirected graph G, design
an algorithm that outputs a minimum spanning tree (MST) of graph G.
• How to find Minimum Spanning Tree ?
• Generic solution to MST
Two
Algorithms
Kruskal’s
algorithm
Prim’s
algorithm
GENERIC-MST Algorithm
GENERIC-MST ( G, w )
1 A = ϴ
2 while A does not form a spanning tree
3 find an edge ( u, v ) that is safe for A
4 A = A U { ( u, v ) }
5 return A
•The idea is to start with an
empty graph and try to add
edges one at a time,
always making sure that
what is built remains
acyclic.
•Gives us an idea how to
grow a MST.
•An edge (u, v) is safe for A
if and only if A  {(u, v)} is
also a subset of some MST
PRIM’s Algorithm
• A special case of generic minimum-
spanning-tree algorithm and operates much
like Dijkstra’s algorithm.
• Edges in the set A always form a single tree.
• Greedy algorithm since at each step it adds
to the tree an edge that contributes the
minimum amount possible to the tree’s
weight.
•Connected graph G and the root r of the
MST to be drawn are inputs.
•During execution of the algorithm, all vertices
that are not in the MST reside in a min-priority
queue Q based on a key attribute.
•For each vertex v, the attribute v.key is the
minimum weight of any edge connecting v to
a vertex in the tree.
•The attribute v.Π names parent of v in the tree.
•Maintains the set A from GENERIC-MST as
A = { ( v, v.Π) : v ∈ V – {r} – Q }.
When the algorithm terminates, the min-priority
queue Q is empty.
The MST A for G is thus
A = { ( v, v.Π) : v ∈ V – {r} }.
MST-PRIM( G, w, r )
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL
4 key[r]←0
5 Q←V[G]
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v ∈ Q and w( u ,v ) < key[v]
10 then Π[v]←u
11 key[v]←w( u, v )
PRIM’s Algorithm ( Steps 1-5 : Initialization )
MST-PRIM(G,w,r)
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL
4 key[r]←0
5 Q←V[G]
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u, v)
u a b c d e f g h i
key[u] ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL
u a b c d e f G H i
key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL
Q A b c d e f g h i
A EMPTY
After Steps 1-3
After Step 4
After Step 5
Initialization
Example Graph
Q a b c d e f g h i
key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL
Q a b c d E f g h i
A
EM
PTY
PRIM’s Algorithm (Steps 6 to 11) ……
Before Step 6 Steps 6-11 (for u=a) After Step 6-11 (for u=a)
MST-PRIM(G,w,r)
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL
4 key[r]←0
5 Q←V[G]
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
u v
v∈Q AND
w(u,v) < Key[v]
Π[v]←u,
Key[v]←w(u,v)
a b YES
Π[b]←a,
Key[b]←4
a h YES
Π[h]←a,
Key[h]←8
Q a b c d e f g h i
key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a NIL NIL NIL NIL NIL a NIL
Q b c d e f g h i
A a
PRIM’s Algorithm (Steps 6 to 11) ……
MST-PRIM(G,w,r)
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL
4 key[r]←0
5 Q←V[G]
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Q a b c d e f g h i
key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL
Q a b c d e f g h i
A
Q a b c d e f g h i
key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a NIL NIL NIL NIL NIL a NIL
Q b c d e f g h i
A a
Before Step 6 Steps 6-11 (for u=a) After Step 6-11 (for u=a)
u v
v∈Q AND
w(u,v) < Key[v]
Π[v]←u,
Key[v]←w(u,v)
a b YES
Π[b]←a,
Key[b]←4
a h YES
Π[h]←a,
Key[h]←8
PRIM’s Algorithm (Steps 6 to 11, for u=b)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=b)
After Step 6-11 (for u=b)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
b c YES Π[c]←b, Key[c]←8
b h NO do nothing
b a NO do nothing
Q a b c d e f g h i
key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a b NIL NIL NIL NIL a NIL
Q c d e f g h i
A a b
Status of Q before using u=b
Q a b c d e f g h i
key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a NIL NIL NIL NIL NIL a NIL
Q b c d e f g h i
A a
PRIM’s Algorithm (Steps 6 to 11, for u=b)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=b)
After Step 6-11 (for u=b)
Status of Q before using u=b
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
b c YES Π[c]←b, Key[c]←8
b h NO do nothing
b a NO do nothing
Q a b c d e f g h i
key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a b NIL NIL NIL NIL a NIL
Q c d e f g h i
A a b
Q a b c d e f g h i
key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a NIL NIL NIL NIL NIL a NIL
Q b c d e f g h i
A a
PRIM’s Algorithm (Steps 6 to 11, for u=c)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
2
7
c
8
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=c)
After Step 6-11 (for u=c)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
c i YES Π[i]←c, Key[i]←2
c f YES Π[f]←c, Key[f]←4
c d YES Π[d]←c, Key[d]←7
c b NO Do Nothing
Q a b c d e f g h i
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
Status of Q before using u=c
Q a b c d e f g h i
key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a b NIL NIL NIL NIL a NIL
Q c d e f g h i
A a b
b
PRIM’s Algorithm (Steps 6 to 11, for u=c)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
2
7
c
8
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=c)
After Step 6-11 (for u=c)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
c i YES Π[i]←c, Key[i]←2
c f YES Π[f]←c, Key[f]←4
c d YES Π[d]←c, Key[d]←7
c b NO Do Nothing
Q a b c d e f g h i
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
Status of Q before using u=c
Q a b c d e f g h i
key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a b NIL NIL NIL NIL a NIL
Q c d e f g h i
A a b
b
PRIM’s Algorithm (Steps 6 to 11, for u=i)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=i) After Step 6-11 (for u=i)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
i h YES Π[h]←i, Key[h]←7
i g YES Π[g]←i, Key[g]←6
i c NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 ∞ 4 6 7 2
Π[u] NIL a b c NIL c i i c
Q d e f g h
A a b c i
Status of Q before using u=i
Q a b c d e f g h i
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
PRIM’s Algorithm (Steps 6 to 11, for u=i)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=i)
After Step 6-11 (for u=i)
u v
v∈Q AND
w( u, v ) < Key[v] Then Π[v]←u,
Key[v]←w(u,v)
i h YES Π[h]←i, Key[h]←7
i g YES Π[g]←i, Key[g]←6
i c NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 ∞ 4 6 7 2
Π[u] NIL a b c NIL c i i c
Q d e f g h
A a b c i
Status of Q before using u=i
Q a b c d e f g h i
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
8
PRIM’s Algorithm (Steps 6 to 11, for u=f)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
e
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=f) After Step 6-11 (for u=f)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
f d NO Do nothing
f e YES
Π[e]←f,
Key[e]←10
f g YES Π[g]←f, Key[g]←2
f c NO Do nothing
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e g h
A a b c i f
Status of Q before using u=f
Q a b c d e f g h i
key[u] 0 4 8 7 ∞ 4 6 7 2
Π[u] NIL a b c NIL c i i c
Q d e f g h
A a b c i
i
8
PRIM’s Algorithm (Steps 6 to 11, for u=f)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
e
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=f)
After Step 6-11 (for u=f)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
f d NO Do nothing
f e YES
Π[e]←f,
Key[e]←10
f g YES Π[g]←f, Key[g]←2
f c NO Do nothing
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e g h
A a b c i f
Status of Q before using u=f
Q a b c d e f g h i
key[u] 0 4 8 7 ∞ 4 6 7 2
Π[u] NIL a b c NIL c i i c
Q d e f g h
A a b c i
i
PRIM’s Algorithm (Steps 6 to 11, for u=g)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=g)
After Step 6-11 (for u=g)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
g h YES Π[h]←g, Key[h]←1
g i NO Do Nothing
g f NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 1 2
Π[u] NIL a b c f c f g c
Q d e h
A a b c i f g
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e g h
A a b c i f
Status of Q before using u=g
PRIM’s Algorithm (Steps 6 to 11, for u=g)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=g)
After Step 6-11 (for u=g)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
g h YES Π[h]←g, Key[h]←1
g i NO Do Nothing
g f NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 1 2
Π[u] NIL a b c f c f g c
Q d e h
A a b c i f g
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e g h
A a b c i f
Status of Q before using u=g
PRIM’s Algorithm (Steps 6 to 11, for u=h)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=h)
After Step 6-11 (for u=h)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
h a NO Do Nothing
h b NO Do Nothing
h i NO Do Nothing
h g NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 1 2
Π[u] NIL a b c f c f g c
Q d e
A a b c i f g h
Status of Q before using u=h
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e h
A a b c i f g
PRIM’s Algorithm (Steps 6 to 11, for u=h)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=h)
After Step 6-11 (for u=h)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
h a NO Do Nothing
h b NO Do Nothing
h i NO Do Nothing
h g NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 1 2
Π[u] NIL a b c f c f g c
Q d e
A a b c i f g h
Status of Q before using u=h
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e g h
A a b c i f
PRIM’s Algorithm (Steps 6 to 11, for u=d)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=d)
After Step 6-11 (for u=d)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
d c NO Do Nothing
d f NO Do Nothing
d e YES Π[e]←d, Key[e]←9
Q a b c d e f g h i
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q e
A a b c i f g h d
Status of Q before using u=d
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e
A a b c i f h
PRIM’s Algorithm (Steps 6 to 11, for u=d)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=d)
After Step 6-11 (for u=d)
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
d c NO Do Nothing
d f NO Do Nothing
d e YES Π[e]←d, Key[e]←9
Q a b c d e f g h i
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q e
A a b c i f g h d
Status of Q before using u=d
Q a b c d e f g h i
key[u] 0 4 8 7 10 4 2 7 2
Π[u] NIL a b c f c f i c
Q d e
A a b c i f h
PRIM’s Algorithm (Steps 6 to 11, for u=e)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=e)
After Step 6-11 (for u=e)
u v
v∈Q AND
w( u, v ) < Key[v] Then Π[v]←u,
Key[v]←w(u,v)
e d NO Do Nothing
e f NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q
A a b c i f g h d e
Status of Q before using u=e
Q a b c d e f g h i
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q e
A a b c i f g h d
PRIM’s Algorithm (Steps 6 to 11, for u=e)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Steps 6-11(for u=e)
After Step 6-11 (for u=e)
u v
v∈Q AND
w( u, v ) < Key[v] Then Π[v]←u,
Key[v]←w(u,v)
e d NO Do Nothing
e f NO Do Nothing
Q a b c d e f g h i
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q
A a b c i f g h d e
Status of Q before using u=e
Q a b c d e f g h i
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q e
A a b c i f g h d
PRIM’s Algorithm (Steps 6 to 11, for u=e)
d
f
ei
4
8
11a
8 7
cb
2
7
6
1 2gh
4 14
10
9
Example Graph
Q a b c d e f g h i
key[u] 0 4 8 7 9 4 2 1 2
Π[u] NIL a b c d c f g c
Q
A a b c i f g h d e
MST
MST-PRIM( G, w, r )
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL
4 key[r]←0
5 Q←V[G]
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v ∈ Q and w( u ,v ) < key[v]
10 then Π[v]←u
11 key[v]←w( u, v )
Complexity Analysis of Prim’s algorithm
MST-PRIM(G,w,r)
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL
4 key[r]←0
5 Q←V[G]
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q)
8 for each v∈Adj[u]
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u
11 key[v]←w(u,v)
O(V), if Q is implemented as min-heap.
Body of while loop is
executed |V| times.
Takes O( lg V) times.
Takes O( V lg V)
times.
Executed O(E) times total.
Constant
Takes O(lg V) times.
O(E lg V)
Total time: O(VlgV + ElgV) = O(ElgV)
32
Example 2.
1
2
6
3
7
5
4
10
28
14
16
25
24
18
12
Example 2 Continue…..
22
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.
• Finding airline routes.
• To create high quality mazes
• Routing algorithms
• Study of molecular bonds in Chemistry
• Cartography
• Geometry
• Clustering
•Tour/Travel Management
Latest research papers based on Prim’s Algorithm
Exploring the parallel implementations of the three classical MST algorithms
( N. R. Latha; G. Shyamala; G. R. Prasad 2017 International Conference on Inventive Communication and
Computational Technologies (ICICCT))
Joint reconfiguration of feeders and allocation of capacitor banks in distribution systems using a multi-start
strategy
( Márcio M. Montsutsumi; Jose N. Melchor; Leonardo H. Macedo; Rubén Romero, 2017 IEEE PES Innovative Smart
Grid Technologies Conference - Latin America (ISGT Latin America) )
Distributed minimum spanning tree based information exchange policy for distributed systems
( Taj Alam; Zahid Raza, 2016 Fourth International Conference on Parallel, Distributed and Grid Computing (PDGC) )
Generating spanning tree of non-regular graphic sequences through a variant of Prim's algorithm
( Prantik Biswas; Abhisek Paul; Paritosh Bhattacharya, 2015 International Conference on Circuits, Power and Computing
Technologies [ICCPCT-2015] )
The transmission time analysis of IPTV multicast service in SDN/OpenFlow environments
( Pornnipa Rattanawadee; Natchaphon Ruengsakulrach; Chaiyachet Saivichit, 2015 12th International Conference on
Electrical Engineering/Electronics, Computer, Telecommunications and Information Technology (ECTI-CON) )
Optimization of the Connection Topology of an Offshore Wind Farm Network
( Ouahid Dahmani; Salvy Bourguet; Mohamed Machmoum; Patrick Guérin; Pauline Rhein; Lionel Jossé, IEEE Systems
Journal )
Multiagent-Based Distribution Automation Solution for Self-Healing Grids
( Markus Eriksson; Mikel Armendariz; Oleg O. Vasilenko; Arshad Saleem; Lars Nordström, IEEE Transactions on
Industrial Electronics )
Cost-minimum network planning in large wind farm using revised prim's algorithm
Ichiro Kousaka; Daisuke Eguchi; Daiki Yamashita; Yosuke Nakanishi; Ruichi Yokoyama; Kenji Iba ISGT 2014
Prime Object Proposals with Randomized Prim's Algorithm
Santiago Manen; Matthieu Guillaumin; Luc Van Gool, 2013 IEEE International Conference on Computer Vision
Prim's algorithm based P2MP energy-saving routing design for MiDORi
Akiko Hirao; Yuki Nomura; Haruka Yonezu; Hidetoshi Takeshita; Daisuke Ishii; Satoru Okamoto; Naoaki Yamanaka, The
10th International Conference on Optical Internet (COIN2012)
References
• Coremen, Leiserson, Rivest and Stein: Introduction to algorthms, PHI
• Horowitz, Sahni and Rajsekaran: Fundamentals of Computer Algorithms,
Galgotia.
• www.mathworld.wolfram.com
• IEEE xplore
Summary
Prim's algorithm is a greedy algorithm, and is a special
case of generic minimum-spanning-tree algorithm and
operates much like Dijkstra’s algorithm, that finds a
minimum spanning tree for a weighted undirected
graph and is mainly used for a dense graph i.e. a graph
with lots of edges.
Question?
38
39
Thanks!

More Related Content

What's hot

What's hot (20)

Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 

Similar to Prim's algorithm

Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
lecture 20
lecture 20lecture 20
lecture 20sajinsc
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptprithivr1
 
Maximum clique detection algorithm
Maximum clique detection algorithmMaximum clique detection algorithm
Maximum clique detection algorithmAbhishek Kona
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfShiwani Gupta
 
lecture 22
lecture 22lecture 22
lecture 22sajinsc
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
module3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfShiwani Gupta
 
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmBellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmFulvio Corno
 
TDD Boot Camp 東京 for C++ 進行
TDD Boot Camp 東京 for C++ 進行TDD Boot Camp 東京 for C++ 進行
TDD Boot Camp 東京 for C++ 進行Takashi Imagire
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
 
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習Masahiro Suzuki
 
Bellmanfordwith negative cycle js
Bellmanfordwith negative cycle jsBellmanfordwith negative cycle js
Bellmanfordwith negative cycle jsJeeSa Sultana
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explainedPIYUSH Dubey
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithmsAyesha Tahir
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdfDKTaxation
 

Similar to Prim's algorithm (20)

Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
 
lecture 20
lecture 20lecture 20
lecture 20
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
Maximum clique detection algorithm
Maximum clique detection algorithmMaximum clique detection algorithm
Maximum clique detection algorithm
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
lecture 22
lecture 22lecture 22
lecture 22
 
lecture 21
lecture 21lecture 21
lecture 21
 
module3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdf
 
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s AlgorithmBellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
 
Aaex2 group2
Aaex2 group2Aaex2 group2
Aaex2 group2
 
TDD Boot Camp 東京 for C++ 進行
TDD Boot Camp 東京 for C++ 進行TDD Boot Camp 東京 for C++ 進行
TDD Boot Camp 東京 for C++ 進行
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
 
lec6.pptx
lec6.pptxlec6.pptx
lec6.pptx
 
Bellmanfordwith negative cycle js
Bellmanfordwith negative cycle jsBellmanfordwith negative cycle js
Bellmanfordwith negative cycle js
 
Graphs
GraphsGraphs
Graphs
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explained
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
 

Recently uploaded

Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 

Recently uploaded (20)

Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 

Prim's algorithm

  • 1. Presentation On Prim’s Algorithm Submitted to: Mrs. Shano Solanki Assistant Professor Department of CSE NITTTR Chandigarh Submitted by: Pankaj Thakur ME(CSE) Modular RN 171408
  • 2. Sub Topics to be covered: • 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.
  • 3. Introduction •Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. •Developed in 1930 by Czech mathematician Vojtěch Jarník and later rediscovered and republished by computer scientists Robert C. Prim in 1957 and E. W. Dijkstra in 1959. •Therefore, it is also sometimes called the DJP algorithm, Jarník's algorithm, the Prim–Jarník algorithm,or the Prim–Dijkstra algorithm.
  • 4. Basic Concepts Spanning Trees: A subgraph T of a undirected graph G = ( V, E ) is a spanning tree of G if it is a tree and contains every vertex of G. a b c d e a b c d e a b c d e a b c d e Graph Spanning Tree 1 Spanning Tree 2 Spanning Tree 3 • Every connected graph has a spanning tree. • May have multiple spanning tree. • For example see this graph.
  • 6. Basic Concepts Cont….. Weighted Graph: A weighted graph is a graph, in which each edge has a weight (some real number ) Example: a b c d e Weighted Graph 10 9 7 32 23
  • 7. Basic Concepts Cont…. Minimum Spanning Tree in an undirected connected weighted graph is a spanning tree of minimum weight. Example: a b c d e Weighted Graph 10 9 7 32 23 a c d e Spanning Tree 1, w=74 10 9 32 23 a c d e Spanning Tree 2, w=71 9 7 32 23 b b a c d e Spanning Tree 3, w=72 10 7 32 23 b (Minimum Spanning Tree)
  • 8. Minimum Spanning Tree Problem MST Problem : Given a connected weighted undirected graph G, design an algorithm that outputs a minimum spanning tree (MST) of graph G. • How to find Minimum Spanning Tree ? • Generic solution to MST Two Algorithms Kruskal’s algorithm Prim’s algorithm
  • 9. GENERIC-MST Algorithm GENERIC-MST ( G, w ) 1 A = ϴ 2 while A does not form a spanning tree 3 find an edge ( u, v ) that is safe for A 4 A = A U { ( u, v ) } 5 return A •The idea is to start with an empty graph and try to add edges one at a time, always making sure that what is built remains acyclic. •Gives us an idea how to grow a MST. •An edge (u, v) is safe for A if and only if A  {(u, v)} is also a subset of some MST
  • 10. PRIM’s Algorithm • A special case of generic minimum- spanning-tree algorithm and operates much like Dijkstra’s algorithm. • Edges in the set A always form a single tree. • Greedy algorithm since at each step it adds to the tree an edge that contributes the minimum amount possible to the tree’s weight. •Connected graph G and the root r of the MST to be drawn are inputs. •During execution of the algorithm, all vertices that are not in the MST reside in a min-priority queue Q based on a key attribute. •For each vertex v, the attribute v.key is the minimum weight of any edge connecting v to a vertex in the tree. •The attribute v.Π names parent of v in the tree. •Maintains the set A from GENERIC-MST as A = { ( v, v.Π) : v ∈ V – {r} – Q }. When the algorithm terminates, the min-priority queue Q is empty. The MST A for G is thus A = { ( v, v.Π) : v ∈ V – {r} }. MST-PRIM( G, w, r ) 1 for each u∈V[G] 2 do key[u]←∞ 3 Π[u]←NIL 4 key[r]←0 5 Q←V[G] 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v ∈ Q and w( u ,v ) < key[v] 10 then Π[v]←u 11 key[v]←w( u, v )
  • 11. PRIM’s Algorithm ( Steps 1-5 : Initialization ) MST-PRIM(G,w,r) 1 for each u∈V[G] 2 do key[u]←∞ 3 Π[u]←NIL 4 key[r]←0 5 Q←V[G] 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u, v) u a b c d e f g h i key[u] ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL u a b c d e f G H i key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL Q A b c d e f g h i A EMPTY After Steps 1-3 After Step 4 After Step 5 Initialization Example Graph
  • 12. Q a b c d e f g h i key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL Q a b c d E f g h i A EM PTY PRIM’s Algorithm (Steps 6 to 11) …… Before Step 6 Steps 6-11 (for u=a) After Step 6-11 (for u=a) MST-PRIM(G,w,r) 1 for each u∈V[G] 2 do key[u]←∞ 3 Π[u]←NIL 4 key[r]←0 5 Q←V[G] 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph u v v∈Q AND w(u,v) < Key[v] Π[v]←u, Key[v]←w(u,v) a b YES Π[b]←a, Key[b]←4 a h YES Π[h]←a, Key[h]←8 Q a b c d e f g h i key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a NIL NIL NIL NIL NIL a NIL Q b c d e f g h i A a
  • 13. PRIM’s Algorithm (Steps 6 to 11) …… MST-PRIM(G,w,r) 1 for each u∈V[G] 2 do key[u]←∞ 3 Π[u]←NIL 4 key[r]←0 5 Q←V[G] 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Q a b c d e f g h i key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL Q a b c d e f g h i A Q a b c d e f g h i key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a NIL NIL NIL NIL NIL a NIL Q b c d e f g h i A a Before Step 6 Steps 6-11 (for u=a) After Step 6-11 (for u=a) u v v∈Q AND w(u,v) < Key[v] Π[v]←u, Key[v]←w(u,v) a b YES Π[b]←a, Key[b]←4 a h YES Π[h]←a, Key[h]←8
  • 14. PRIM’s Algorithm (Steps 6 to 11, for u=b) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=b) After Step 6-11 (for u=b) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) b c YES Π[c]←b, Key[c]←8 b h NO do nothing b a NO do nothing Q a b c d e f g h i key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a b NIL NIL NIL NIL a NIL Q c d e f g h i A a b Status of Q before using u=b Q a b c d e f g h i key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a NIL NIL NIL NIL NIL a NIL Q b c d e f g h i A a
  • 15. PRIM’s Algorithm (Steps 6 to 11, for u=b) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=b) After Step 6-11 (for u=b) Status of Q before using u=b u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) b c YES Π[c]←b, Key[c]←8 b h NO do nothing b a NO do nothing Q a b c d e f g h i key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a b NIL NIL NIL NIL a NIL Q c d e f g h i A a b Q a b c d e f g h i key[u] 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a NIL NIL NIL NIL NIL a NIL Q b c d e f g h i A a
  • 16. PRIM’s Algorithm (Steps 6 to 11, for u=c) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 2 7 c 8 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=c) After Step 6-11 (for u=c) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) c i YES Π[i]←c, Key[i]←2 c f YES Π[f]←c, Key[f]←4 c d YES Π[d]←c, Key[d]←7 c b NO Do Nothing Q a b c d e f g h i 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 Status of Q before using u=c Q a b c d e f g h i key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a b NIL NIL NIL NIL a NIL Q c d e f g h i A a b b
  • 17. PRIM’s Algorithm (Steps 6 to 11, for u=c) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 2 7 c 8 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=c) After Step 6-11 (for u=c) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) c i YES Π[i]←c, Key[i]←2 c f YES Π[f]←c, Key[f]←4 c d YES Π[d]←c, Key[d]←7 c b NO Do Nothing Q a b c d e f g h i 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 Status of Q before using u=c Q a b c d e f g h i key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞ Π[u] NIL a b NIL NIL NIL NIL a NIL Q c d e f g h i A a b b
  • 18. PRIM’s Algorithm (Steps 6 to 11, for u=i) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=i) After Step 6-11 (for u=i) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) i h YES Π[h]←i, Key[h]←7 i g YES Π[g]←i, Key[g]←6 i c NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 ∞ 4 6 7 2 Π[u] NIL a b c NIL c i i c Q d e f g h A a b c i Status of Q before using u=i Q a b c d e f g h i 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
  • 19. PRIM’s Algorithm (Steps 6 to 11, for u=i) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=i) After Step 6-11 (for u=i) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) i h YES Π[h]←i, Key[h]←7 i g YES Π[g]←i, Key[g]←6 i c NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 ∞ 4 6 7 2 Π[u] NIL a b c NIL c i i c Q d e f g h A a b c i Status of Q before using u=i Q a b c d e f g h i 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
  • 20. 8 PRIM’s Algorithm (Steps 6 to 11, for u=f) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f e 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=f) After Step 6-11 (for u=f) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) f d NO Do nothing f e YES Π[e]←f, Key[e]←10 f g YES Π[g]←f, Key[g]←2 f c NO Do nothing Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e g h A a b c i f Status of Q before using u=f Q a b c d e f g h i key[u] 0 4 8 7 ∞ 4 6 7 2 Π[u] NIL a b c NIL c i i c Q d e f g h A a b c i i
  • 21. 8 PRIM’s Algorithm (Steps 6 to 11, for u=f) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f e 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=f) After Step 6-11 (for u=f) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) f d NO Do nothing f e YES Π[e]←f, Key[e]←10 f g YES Π[g]←f, Key[g]←2 f c NO Do nothing Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e g h A a b c i f Status of Q before using u=f Q a b c d e f g h i key[u] 0 4 8 7 ∞ 4 6 7 2 Π[u] NIL a b c NIL c i i c Q d e f g h A a b c i i
  • 22. PRIM’s Algorithm (Steps 6 to 11, for u=g) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=g) After Step 6-11 (for u=g) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) g h YES Π[h]←g, Key[h]←1 g i NO Do Nothing g f NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 1 2 Π[u] NIL a b c f c f g c Q d e h A a b c i f g Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e g h A a b c i f Status of Q before using u=g
  • 23. PRIM’s Algorithm (Steps 6 to 11, for u=g) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=g) After Step 6-11 (for u=g) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) g h YES Π[h]←g, Key[h]←1 g i NO Do Nothing g f NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 1 2 Π[u] NIL a b c f c f g c Q d e h A a b c i f g Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e g h A a b c i f Status of Q before using u=g
  • 24. PRIM’s Algorithm (Steps 6 to 11, for u=h) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=h) After Step 6-11 (for u=h) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) h a NO Do Nothing h b NO Do Nothing h i NO Do Nothing h g NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 1 2 Π[u] NIL a b c f c f g c Q d e A a b c i f g h Status of Q before using u=h Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e h A a b c i f g
  • 25. PRIM’s Algorithm (Steps 6 to 11, for u=h) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=h) After Step 6-11 (for u=h) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) h a NO Do Nothing h b NO Do Nothing h i NO Do Nothing h g NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 1 2 Π[u] NIL a b c f c f g c Q d e A a b c i f g h Status of Q before using u=h Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e g h A a b c i f
  • 26. PRIM’s Algorithm (Steps 6 to 11, for u=d) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=d) After Step 6-11 (for u=d) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) d c NO Do Nothing d f NO Do Nothing d e YES Π[e]←d, Key[e]←9 Q a b c d e f g h i key[u] 0 4 8 7 9 4 2 1 2 Π[u] NIL a b c d c f g c Q e A a b c i f g h d Status of Q before using u=d Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e A a b c i f h
  • 27. PRIM’s Algorithm (Steps 6 to 11, for u=d) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=d) After Step 6-11 (for u=d) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) d c NO Do Nothing d f NO Do Nothing d e YES Π[e]←d, Key[e]←9 Q a b c d e f g h i key[u] 0 4 8 7 9 4 2 1 2 Π[u] NIL a b c d c f g c Q e A a b c i f g h d Status of Q before using u=d Q a b c d e f g h i key[u] 0 4 8 7 10 4 2 7 2 Π[u] NIL a b c f c f i c Q d e A a b c i f h
  • 28. PRIM’s Algorithm (Steps 6 to 11, for u=e) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=e) After Step 6-11 (for u=e) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) e d NO Do Nothing e f NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 9 4 2 1 2 Π[u] NIL a b c d c f g c Q A a b c i f g h d e Status of Q before using u=e Q a b c d e f g h i key[u] 0 4 8 7 9 4 2 1 2 Π[u] NIL a b c d c f g c Q e A a b c i f g h d
  • 29. PRIM’s Algorithm (Steps 6 to 11, for u=e) 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Steps 6-11(for u=e) After Step 6-11 (for u=e) u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) e d NO Do Nothing e f NO Do Nothing Q a b c d e f g h i key[u] 0 4 8 7 9 4 2 1 2 Π[u] NIL a b c d c f g c Q A a b c i f g h d e Status of Q before using u=e Q a b c d e f g h i key[u] 0 4 8 7 9 4 2 1 2 Π[u] NIL a b c d c f g c Q e A a b c i f g h d
  • 30. PRIM’s Algorithm (Steps 6 to 11, for u=e) d f ei 4 8 11a 8 7 cb 2 7 6 1 2gh 4 14 10 9 Example Graph Q a b c d e f g h i key[u] 0 4 8 7 9 4 2 1 2 Π[u] NIL a b c d c f g c Q A a b c i f g h d e MST MST-PRIM( G, w, r ) 1 for each u∈V[G] 2 do key[u]←∞ 3 Π[u]←NIL 4 key[r]←0 5 Q←V[G] 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v ∈ Q and w( u ,v ) < key[v] 10 then Π[v]←u 11 key[v]←w( u, v )
  • 31. Complexity Analysis of Prim’s algorithm MST-PRIM(G,w,r) 1 for each u∈V[G] 2 do key[u]←∞ 3 Π[u]←NIL 4 key[r]←0 5 Q←V[G] 6 while Q is not Empty 7 do u←EXTRACT-MIN(Q) 8 for each v∈Adj[u] 9 do if v∈Q and w(u,v)<key[v] 10 then Π[v]←u 11 key[v]←w(u,v) O(V), if Q is implemented as min-heap. Body of while loop is executed |V| times. Takes O( lg V) times. Takes O( V lg V) times. Executed O(E) times total. Constant Takes O(lg V) times. O(E lg V) Total time: O(VlgV + ElgV) = O(ElgV)
  • 34. 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. • Finding airline routes. • To create high quality mazes • Routing algorithms • Study of molecular bonds in Chemistry • Cartography • Geometry • Clustering •Tour/Travel Management
  • 35. Latest research papers based on Prim’s Algorithm Exploring the parallel implementations of the three classical MST algorithms ( N. R. Latha; G. Shyamala; G. R. Prasad 2017 International Conference on Inventive Communication and Computational Technologies (ICICCT)) Joint reconfiguration of feeders and allocation of capacitor banks in distribution systems using a multi-start strategy ( Márcio M. Montsutsumi; Jose N. Melchor; Leonardo H. Macedo; Rubén Romero, 2017 IEEE PES Innovative Smart Grid Technologies Conference - Latin America (ISGT Latin America) ) Distributed minimum spanning tree based information exchange policy for distributed systems ( Taj Alam; Zahid Raza, 2016 Fourth International Conference on Parallel, Distributed and Grid Computing (PDGC) ) Generating spanning tree of non-regular graphic sequences through a variant of Prim's algorithm ( Prantik Biswas; Abhisek Paul; Paritosh Bhattacharya, 2015 International Conference on Circuits, Power and Computing Technologies [ICCPCT-2015] ) The transmission time analysis of IPTV multicast service in SDN/OpenFlow environments ( Pornnipa Rattanawadee; Natchaphon Ruengsakulrach; Chaiyachet Saivichit, 2015 12th International Conference on Electrical Engineering/Electronics, Computer, Telecommunications and Information Technology (ECTI-CON) ) Optimization of the Connection Topology of an Offshore Wind Farm Network ( Ouahid Dahmani; Salvy Bourguet; Mohamed Machmoum; Patrick Guérin; Pauline Rhein; Lionel Jossé, IEEE Systems Journal ) Multiagent-Based Distribution Automation Solution for Self-Healing Grids ( Markus Eriksson; Mikel Armendariz; Oleg O. Vasilenko; Arshad Saleem; Lars Nordström, IEEE Transactions on Industrial Electronics ) Cost-minimum network planning in large wind farm using revised prim's algorithm Ichiro Kousaka; Daisuke Eguchi; Daiki Yamashita; Yosuke Nakanishi; Ruichi Yokoyama; Kenji Iba ISGT 2014 Prime Object Proposals with Randomized Prim's Algorithm Santiago Manen; Matthieu Guillaumin; Luc Van Gool, 2013 IEEE International Conference on Computer Vision Prim's algorithm based P2MP energy-saving routing design for MiDORi Akiko Hirao; Yuki Nomura; Haruka Yonezu; Hidetoshi Takeshita; Daisuke Ishii; Satoru Okamoto; Naoaki Yamanaka, The 10th International Conference on Optical Internet (COIN2012)
  • 36. References • Coremen, Leiserson, Rivest and Stein: Introduction to algorthms, PHI • Horowitz, Sahni and Rajsekaran: Fundamentals of Computer Algorithms, Galgotia. • www.mathworld.wolfram.com • IEEE xplore
  • 37. Summary Prim's algorithm is a greedy algorithm, and is a special case of generic minimum-spanning-tree algorithm and operates much like Dijkstra’s algorithm, that finds a minimum spanning tree for a weighted undirected graph and is mainly used for a dense graph i.e. a graph with lots of edges.