0% found this document useful (0 votes)
3 views

Prims Algorithm

The document presents an overview of Prim's Algorithm, a greedy algorithm used to find a minimum spanning tree for a weighted undirected graph. It covers the algorithm's history, basic concepts, and detailed steps involved in its implementation, along with complexity analysis and applications. The presentation is structured to guide the audience through the algorithm's workings and its significance in computer science.

Uploaded by

umefarwarizvi111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Prims Algorithm

The document presents an overview of Prim's Algorithm, a greedy algorithm used to find a minimum spanning tree for a weighted undirected graph. It covers the algorithm's history, basic concepts, and detailed steps involved in its implementation, along with complexity analysis and applications. The presentation is structured to guide the audience through the algorithm's workings and its significance in computer science.

Uploaded by

umefarwarizvi111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Presentation

On
Prim’s Algorithm

Submitted to: Submitted by:


Mrs. Shano Solanki Pankaj Thakur
Assistant Professor ME(CSE) Modular
Department of CSE RN 171408
NITTTR Chandigarh
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.
b
a
• Every connected graph has a spanning tree.
c • May have multiple spanning tree.
• For example see this graph.
e
d
Graph

b b b
a a a

c c c

e e e
d d d

Spanning Tree 1 Spanning Tree 2 Spanning Tree 3


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:

b
a 7 32
c
10 23
9 e
d
Weighted Graph
Basic Concepts Cont….

Minimum Spanning Tree in an undirected connected weighted graph is


a spanning tree of minimum weight. Example:
b
a 7 32
c
10 23
9 e
d
Weighted Graph

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

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=ϴ •The idea is to start with an


empty graph and try to add
edges one at a time,
2 while A does not form a spanning tree always making sure that
what is built remains
acyclic.
3 find an edge ( u, v ) that is safe for A
•Gives us an idea how to
grow a MST.
4 A = A U { ( u, v ) }
•An edge (u, v) is safe for A
5 return A if and only if A  {(u, v)} is
also a subset of some MST
PRIM’s Algorithm
MST-PRIM( G, w, r )
1 for each u∈V[G] • A special case of generic minimum-
spanning-tree algorithm and operates much
2 do key[u]←∞ like Dijkstra’s algorithm.
• Edges in the set A always form a single tree.
3 Π[u]←NIL
• Greedy algorithm since at each step it adds
4 key[r]←0 to the tree an edge that contributes the
minimum amount possible to the tree’s
5 Q←V[G] weight.
6 while Q is not Empty •Connected graph G and the root r of the
MST to be drawn are inputs.
7 do u←EXTRACT-MIN(Q) •During execution of the algorithm, all vertices
that are not in the MST reside in a min-priority
8 for each v∈Adj[u]
queue Q based on a key attribute.
•For each vertex v, the attribute v.key is the
9 do if v ∈ Q and w( u ,v ) < key[v] minimum weight of any edge connecting v to
a vertex in the tree.
10 then Π[v]←u •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 }.
11 key[v]←w( u, v )
When the algorithm terminates, the min-priority
queue Q is empty.
The MST A for G is thus
A = { ( v, v.Π) : v ∈ V – {r} }.
PRIM’s Algorithm ( Steps 1-5 : Initialization )

MST-PRIM(G,w,r) Example Graph

1 for each u∈V[G]

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

9 do if v∈Q and w(u,v)<key[v] key[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞


10 then Π[v]←u Π[u] NIL NIL NIL NIL NIL NIL NIL NIL NIL

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)

6 while Q is not Empty


8 7
7 do u←EXTRACT-MIN(Q) b c d Example Graph
8 for each v∈Adj[u] 4 2 9
9 do if v∈Q and w(u,v)<key[v]
a 11 i 4 14 e
10 then Π[v]←u
11 key[v]←w(u,v) 7
8 6 10
Status of Q before using u=b h g f
1 2
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 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)

6 while Q is not Empty Example Graph


8 7
7 do u←EXTRACT-MIN(Q) b c d
8 for each v∈Adj[u] 4 2 9
9 do if v∈Q and w(u,v)<key[v]
a 11 i 4 14 e
10 then Π[v]←u
11 key[v]←w(u,v) 7
8 6 10
Status of Q before using u=b h g f
1 2
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 After Step 6-11 (for u=b)

Steps 6-11(for u=b) Q a b c d e f g h i


u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u, key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
Key[v]←w(u,v)
Π[u] NIL a b NIL NIL NIL NIL a NIL
b c YES Π[c]←b, Key[c]←8
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=c)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q) Example Graph
8 for each v∈Adj[u] 8 7
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
Status of Q before using u=c 7
8 6 10
Q a b c d e f g h i h g f
1 2
key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a b NIL NIL NIL NIL a NIL

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

c d YES Π[d]←c, Key[d]←7 Q d e f g h i


c b NO Do Nothing A a b c
PRIM’s Algorithm (Steps 6 to 11, for u=c)
6 while Q is not Empty
7 do u←EXTRACT-MIN(Q) Example Graph
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
Status of Q before using u=c 7
8 6 10
Q a b c d e f g h i h g f
1 2
key[u] 0 4 8 ∞ ∞ ∞ ∞ 8 ∞
Π[u] NIL a b NIL NIL NIL NIL a NIL

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

c d YES Π[d]←c, Key[d]←7 Q d e f g h i


c b NO Do Nothing A a b c
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
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

i g YES Π[g]←i, Key[g]←6 Q d e f g h


A a b c i
i c NO Do Nothing
PRIM’s Algorithm (Steps 6 to 11, for u=f)
6 while Q is not Empty
Example Graph
7 do u←EXTRACT-MIN(Q)
8
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 8 7
11 key[v]←w(u,v) a 11 i 4 14 e
Status of Q before using u=f 7
8 6 10
Q a b c d e f g h i
key[u] 0 4 8 7 ∞ 4 6 7 2 h 1 g 2 f
Π[u] NIL a b c NIL c i i c
Q d e f g h
A a b c i
Steps 6-11(for u=f) After Step 6-11 (for u=f)
v∈Q AND Then Π[v]←u,
u v w( u, v ) < Key[v] Key[v]←w(u,v)
Q a b c d e f g h i
f d NO Do nothing
Π[e]←f, key[u] 0 4 8 7 10 4 2 7 2
f e YES
Key[e]←10 Π[u] NIL a b c f c f i c
f g YES Π[g]←f, Key[g]←2
Q d e g h
f c NO Do nothing A a b c i f
PRIM’s Algorithm (Steps 6 to 11, for u=f)
6 while Q is not Empty
Example Graph
7 do u←EXTRACT-MIN(Q)
8
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 8 7
11 key[v]←w(u,v) a 11 i 4 14 e
7
Status of Q before using u=f 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 6 7 2
Π[u] NIL a b c NIL c i i c
Q d e f g h
A a b c i After Step 6-11 (for u=f)
Steps 6-11(for u=f)
v∈Q AND Then Π[v]←u,
u v
w( u, v ) < Key[v] Key[v]←w(u,v) Q a b c d e f g h i
f d NO Do nothing
Π[e]←f,
key[u] 0 4 8 7 10 4 2 7 2
f e YES
Key[e]←10 Π[u] NIL a b c f c f i c
f g YES Π[g]←f, Key[g]←2
Q d e g h
f c NO Do nothing A a b c i f
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] Example Graph
10 then Π[v]←u 8 7
b c d
11 key[v]←w(u,v) 4 9
2
Status of Q before using u=g
a 11 i 4 14 e
Q a b c d e f g h i
7
key[u] 0 4 8 7 10 4 2 7 2 8 6 10
Π[u] NIL a b c f c f i c h g f
1 2
Q d e g h
A a b c i f
After Step 6-11 (for u=g)

Steps 6-11(for u=g) 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 10 4 2 1 2
g h YES Π[h]←g, Key[h]←1 Π[u] NIL a b c f c f g c
g i NO Do Nothing
Q d e h
g f NO Do Nothing A a b c i f 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] Example Graph
10 then Π[v]←u 8 7
b c d
11 key[v]←w(u,v) 4 9
2
Status of Q before using u=g
a 11 i 4 14 e
Q a b c d e f g h i
7
key[u] 0 4 8 7 10 4 2 7 2 8 6 10
Π[u] NIL a b c f c f i c h g f
1 2
Q d e g h
A a b c i f
After Step 6-11 (for u=g)

Steps 6-11(for u=g) 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 10 4 2 1 2
g h YES Π[h]←g, Key[h]←1 Π[u] NIL a b c f c f g c
g i NO Do Nothing
Q d e h
g f NO Do Nothing A a b c i f g
PRIM’s Algorithm (Steps 6 to 11, for u=h)
6 while Q is not Empty Example Graph
7 do u←EXTRACT-MIN(Q) 8 7
b c d
8 for each v∈Adj[u]
4 2 9
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u a i 4 14 e
11
11 key[v]←w(u,v) 7
8 6 10
Status of Q before using u=h h g f
Q a b c d e f g h i 1 2
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 After Step 6-11 (for u=h)
Steps 6-11(for u=h)
u v
v∈Q AND Then Π[v]←u, Q a b c d e f g h i
w( u, v ) < Key[v] Key[v]←w(u,v)
h a NO Do Nothing key[u] 0 4 8 7 10 4 2 1 2
h b NO Do Nothing Π[u] NIL a b c f c f g c
h i NO Do Nothing
Q d e
h g NO Do Nothing A a b c i f g h
PRIM’s Algorithm (Steps 6 to 11, for u=h)
6 while Q is not Empty Example Graph
7 do u←EXTRACT-MIN(Q) 8 7
b c d
8 for each v∈Adj[u]
4 2 9
9 do if v∈Q and w(u,v)<key[v]
10 then Π[v]←u a i 4 14 e
11
11 key[v]←w(u,v) 7
8 6 10
Status of Q before using u=h h g f
Q a b c d e f g h i 1 2
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 After Step 6-11 (for u=h)
Steps 6-11(for u=h)
u v
v∈Q AND Then Π[v]←u, Q a b c d e f g h i
w( u, v ) < Key[v] Key[v]←w(u,v)
h a NO Do Nothing key[u] 0 4 8 7 10 4 2 1 2
h b NO Do Nothing Π[u] NIL a b c f c f g c
h i NO Do Nothing
Q d e
h g NO Do Nothing A a b c i f g h
PRIM’s Algorithm (Steps 6 to 11, for u=d)
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=d 8 6 10
Q a b c d e f g h i h g f
1 2
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
After Step 6-11 (for u=d)
Steps 6-11(for u=d)
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 9 4 2 1 2
d c NO Do Nothing
Π[u] NIL a b c d c f g c
d f NO Do Nothing
Q e
d e YES Π[e]←d, Key[e]←9 A a b c i f g h d
PRIM’s Algorithm (Steps 6 to 11, for u=d)
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=d 8 6 10
Q a b c d e f g h i h g f
1 2
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
After Step 6-11 (for u=d)
Steps 6-11(for u=d)
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 9 4 2 1 2
d c NO Do Nothing
Π[u] NIL a b c d c f g c
d f NO Do Nothing
Q e
d e YES Π[e]←d, Key[e]←9 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
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)
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)

1 for each u∈V[G] Total time: O(VlgV + ElgV) = O(ElgV)


2 do key[u]←∞

3 Π[u]←NIL O(V), if Q is implemented as min-heap.


4 key[r]←0

5 Q←V[G]

6 while Q is not Empty Body of while loop is


Takes O( V lg V)
executed |V| times.
7 do u←EXTRACT-MIN(Q) times.
Takes O( lg V) times.
8 for each v∈Adj[u] Executed O(E) times total.

9 do if v∈Q and w(u,v)<key[v] Constant


O(E lg V)
10 then Π[v]←u

11 key[v]←w(u,v) Takes O(lg V) times.


Example 2.

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.

• 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
Thanks!
39

You might also like