SlideShare a Scribd company logo
Greedy Algorithm
Prim’s Algorithm for Computing MSTs
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
e
d
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
10
9
e
d
Weighted
Graph
7 32
23
Basic Concepts Cont….
Minimum Spanning Tree in an undirected connected weighted graph
is a spanning tree of minimum weight. Example:
b
c
10
9
e
d
Weighted
Graph
a 7 32
23
a
c
d
Spanning Tree 1,
w=74
10
9 e
32
23
a
c
d
w=71
9 e
7 32
23
b b a
c
e
d
Spanning Tree
3, w=72
7 32
10 23
b
Spanning Tree 2,
(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
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
GENERIC-MST ( G, w )
•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} }.
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 )
MST-PRIM( G, w, r )
PRIM’s Algorithm ( Steps 1-5 :
Initialization )
MST-PRIM(G,w,r)
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
1 for each u∈V[G]
2 do key[u]←∞
3 Π[u]←NIL Initialization
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)
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)
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)
MST-PRIM(G,w,r)
d
f
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
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)
……
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)
MST-PRIM(G,w,r)
d
f
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
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
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
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
e
i
4
8
11
a
7
c
8
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=c)
After Step 6-11 (for u=c)
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
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
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
e
i
4
8
11
a
2
7
c
8
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=c)
After Step 6-11 (for u=c)
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
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
Q d e g h
A a b c i f
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
11
a
8 7
c
b
2
7
6
1 2
g
h
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
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
Q d e g h
A a b c i f
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
11
a
8 7
c
b
2
7
6
1 2
g
h
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
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
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
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=g)
After Step 6-11 (for u=g)
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
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
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=g)
After Step 6-11 (for u=g)
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
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
h a NODo Nothing
h b NODo Nothing
h i NODo Nothing
h g NODo Nothing
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=h)
After Step 6-11 (for u=h)
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
u v
v∈Q AND
w( u, v ) < Key[v]
Then Π[v]←u,
Key[v]←w(u,v)
h a NODo Nothing
h b NODo Nothing
h i NODo Nothing
h g NODo Nothing
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=h)
After Step 6-11 (for u=h)
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
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
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=d)
After Step 6-11 (for u=d)
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
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
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=d)
After Step 6-11 (for u=d)
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
u v
v∈Q AND
w( u, v ) < Key[v] Then Π[v]←u,
Key[v]←w(u,v)
e d NODo Nothing
e f NODo Nothing
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=e)
After Step 6-11 (for 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
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
u v
v∈Q AND
w( u, v ) < Key[v] Then Π[v]←u,
Key[v]←w(u,v)
e d NODo Nothing
e f NODo Nothing
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
4 14
10
9
Example Graph
Steps 6-11(for u=e)
After Step 6-11 (for 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
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
e
i
4
8
11
a
8 7
c
b
2
7
6
1 2
g
h
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
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 )
MST-PRIM( G, w, r )
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.
2
6
3
7
5
10
1
28
14
16
25
24
18
12
Example 2 Continue…..
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

More Related Content

PPTX
Prim's algorithm
PPTX
Prims and kruskal algorithms
PPT
Lec-35Graph - Copy Graph therory in Data strucure
PPT
lecture 20
PPT
Graphs > Discrete structures , Data Structures & Algorithums
PDF
algoritm prim kruskal review time complexity .pdf
PDF
Minimum spanning tree
PPT
algorthm analysis from computer scince.ppt
Prim's algorithm
Prims and kruskal algorithms
Lec-35Graph - Copy Graph therory in Data strucure
lecture 20
Graphs > Discrete structures , Data Structures & Algorithums
algoritm prim kruskal review time complexity .pdf
Minimum spanning tree
algorthm analysis from computer scince.ppt

Similar to prims algorithm for algorithms for cs st (20)

PPT
Unit 5 session 2 MinimumSpanningTrees.ppt
PDF
Shortest Path in Graph
PPT
Maximum clique detection algorithm
PDF
module4_dynamic programming_2022.pdf
PPTX
Bellman ford Algorithm
PPTX
Bellmanfordwith negative cycle js
KEY
TDD Boot Camp 東京 for C++ 進行
PPT
lecture 22
PPTX
Lecture 16 data structures and algorithms
PPT
lecture 21
PPT
Lec-35Graph - Graph - Copy in Data Structure
PPT
Graphs
PDF
Shortest path algorithms
PDF
How many vertices does a random walk miss in a network with moderately increa...
PPTX
lec6.pptx
PPTX
Aaex2 group2
PDF
The Ring programming language version 1.6 book - Part 62 of 189
PPTX
Secure Domination in graphs
PDF
Greedy Algorithms in Algorithms and Design
PPT
Prim's Algorithm on minimum spanning tree
Unit 5 session 2 MinimumSpanningTrees.ppt
Shortest Path in Graph
Maximum clique detection algorithm
module4_dynamic programming_2022.pdf
Bellman ford Algorithm
Bellmanfordwith negative cycle js
TDD Boot Camp 東京 for C++ 進行
lecture 22
Lecture 16 data structures and algorithms
lecture 21
Lec-35Graph - Graph - Copy in Data Structure
Graphs
Shortest path algorithms
How many vertices does a random walk miss in a network with moderately increa...
lec6.pptx
Aaex2 group2
The Ring programming language version 1.6 book - Part 62 of 189
Secure Domination in graphs
Greedy Algorithms in Algorithms and Design
Prim's Algorithm on minimum spanning tree
Ad

Recently uploaded (20)

PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PPTX
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
PDF
Biophysics 2.pdffffffffffffffffffffffffff
PPTX
Introduction to Cardiovascular system_structure and functions-1
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PDF
Phytochemical Investigation of Miliusa longipes.pdf
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
2Systematics of Living Organisms t-.pptx
PPTX
2. Earth - The Living Planet earth and life
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPTX
microscope-Lecturecjchchchchcuvuvhc.pptx
PDF
The scientific heritage No 166 (166) (2025)
PPTX
Introduction to Fisheries Biotechnology_Lesson 1.pptx
PPTX
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
Derivatives of integument scales, beaks, horns,.pptx
PPTX
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
PPT
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
PDF
An interstellar mission to test astrophysical black holes
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
Biophysics 2.pdffffffffffffffffffffffffff
Introduction to Cardiovascular system_structure and functions-1
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
Phytochemical Investigation of Miliusa longipes.pdf
7. General Toxicologyfor clinical phrmacy.pptx
2Systematics of Living Organisms t-.pptx
2. Earth - The Living Planet earth and life
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
microscope-Lecturecjchchchchcuvuvhc.pptx
The scientific heritage No 166 (166) (2025)
Introduction to Fisheries Biotechnology_Lesson 1.pptx
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
Derivatives of integument scales, beaks, horns,.pptx
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
An interstellar mission to test astrophysical black holes
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
Ad

prims algorithm for algorithms for cs st

  • 2. 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.
  • 3. 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 e d 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.
  • 5. 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 10 9 e d Weighted Graph 7 32 23
  • 6. Basic Concepts Cont…. Minimum Spanning Tree in an undirected connected weighted graph is a spanning tree of minimum weight. Example: b c 10 9 e d Weighted Graph a 7 32 23 a c d Spanning Tree 1, w=74 10 9 e 32 23 a c d w=71 9 e 7 32 23 b b a c e d Spanning Tree 3, w=72 7 32 10 23 b Spanning Tree 2, (Minimum Spanning Tree)
  • 7. 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
  • 8. GENERIC-MST Algorithm 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 GENERIC-MST ( G, w ) •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
  • 9. 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} }. 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 ) MST-PRIM( G, w, r )
  • 10. PRIM’s Algorithm ( Steps 1-5 : Initialization ) MST-PRIM(G,w,r) 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 1 for each u∈V[G] 2 do key[u]←∞ 3 Π[u]←NIL Initialization 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) Example Graph
  • 11. 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) 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) MST-PRIM(G,w,r) d f e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 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
  • 12. PRIM’s Algorithm (Steps 6 to 11) …… 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) MST-PRIM(G,w,r) d f e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 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
  • 13. 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 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
  • 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 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
  • 15. 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 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 e i 4 8 11 a 7 c 8 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=c) After Step 6-11 (for u=c) 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
  • 16. 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 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 e i 4 8 11 a 2 7 c 8 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=c) After Step 6-11 (for u=c) 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=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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 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
  • 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 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. Q d e g h A a b c i f 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 11 a 8 7 c b 2 7 6 1 2 g h 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 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
  • 20. Q d e g h A a b c i f 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 11 a 8 7 c b 2 7 6 1 2 g h 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 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. 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 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=g) After Step 6-11 (for u=g) 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
  • 22. 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 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=g) After Step 6-11 (for u=g) 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. u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) h a NODo Nothing h b NODo Nothing h i NODo Nothing h g NODo Nothing 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=h) After Step 6-11 (for u=h) 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
  • 24. u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) h a NODo Nothing h b NODo Nothing h i NODo Nothing h g NODo Nothing 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=h) After Step 6-11 (for u=h) 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
  • 25. 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 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=d) After Step 6-11 (for u=d) 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
  • 26. 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 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=d) After Step 6-11 (for u=d) 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. u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) e d NODo Nothing e f NODo Nothing 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=e) After Step 6-11 (for 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 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
  • 28. u v v∈Q AND w( u, v ) < Key[v] Then Π[v]←u, Key[v]←w(u,v) e d NODo Nothing e f NODo Nothing 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 e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 4 14 10 9 Example Graph Steps 6-11(for u=e) After Step 6-11 (for 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 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) d f e i 4 8 11 a 8 7 c b 2 7 6 1 2 g h 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 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 ) MST-PRIM( G, w, r )
  • 30. 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)
  • 33. 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