Lec-25 Prim's Algorithm
Lec-25 Prim's Algorithm
Algorithms
Prim’s Algorithm
Akanksha
Assistant Professor, GLA University, Mathura
Minimum Cost Spanning Tree
Prim’s Algorithm :
• It is a greedy algorithm that is used to find the minimum
spanning tree from a graph.
• Finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be
minimized.
• Starts with the source node and explores all the adjacent nodes
with all the connecting edges at every step.
• Edges with the minimal weights causing no cycles in the graph
got selected.
Minimum Cost Spanning Tree
Applications of Prim’s Algorithm :
It can be used in network designing.
It can be used to make network cycles.
It can also be used to lay down electrical wiring
cables.
Minimum Cost Spanning Tree
Prim’s Algorithm :
MST_Prim(G,w,r)
{
Q = V[G] O(V)
For each u ϵ V[G] O(V)
do Key[u] = ∞ O(V)
pi[u] = NIL O(V)
Key[r] = 0O(1)
while(Q ǂ φ) O(V)
u = Extract_Min (Q) O(VlogV)
for each v ϵ adj (u) O(V*V) if v ϵ Q and w(u,v) < Key[v] O(V*V)
pi[v] = u O(V*V)
Key[v] = w(u,v) O(V*V)
}
Time Complexity of Prim’s Algorithm = O(V2)
Minimum Cost Spanning Tree
Example-1: Vertex Key Pi
(v) (v) (v)
Step-1: Initialization a NIL
∞
b ∞ NIL
c ∞ NIL
d ∞ NIL
e ∞ NIL
f ∞ NIL
g ∞ NIL
h ∞ NIL
i ∞ NIL
Minimum Cost Spanning Tree
Step-2: Initialize source vertex a with Vertex Key Pi
(v) (v) (v)
value 0 a NIL
0
b ∞ NIL
c ∞ NIL
d ∞ NIL
e ∞ NIL
f ∞ NIL
g ∞ NIL
h ∞ NIL
i ∞ NIL
Minimum Cost Spanning Tree
Step-3: Select node a as it have minimum Vertex Key Pi
(v) (v) (v)
key value and Update key value of each a NIL
0
adjacent node of node a. b 4 a
c ∞ NIL
d ∞ NIL
e ∞ NIL
f ∞ NIL
g ∞ NIL
h 8 a
i ∞ NIL
Minimum Cost Spanning Tree
Step-4: Select node b as it have minimum Vertex Key Pi
(v) (v) (v)
key value and update key value of each a NIL
0
adjacent node of node b. b 4 a
c 8 b
d ∞ NIL
e ∞ NIL
f ∞ NIL
g ∞ NIL
h 8 a
i ∞ NIL
Minimum Cost Spanning Tree
Step-5: Select node c as it have minimum Vertex Key Pi
(v) (v) (v)
key value and update key value of each a NIL
0
adjacent node of node c. b 4 a
c 8 b
d 7 c
e ∞ NIL
f 4 c
g ∞ NIL
h 8 a
i 2 c
Minimum Cost Spanning Tree
Step-6: Select node i as it have minimum Vertex Key Pi
(v) (v) (v)
key value and update key value of each a NIL
0
adjacent node of node i. b 4 a
c 8 b
d 7 c
e ∞ NIL
f 4 c
g 6 i
h 7 i
i 2 c
Minimum Cost Spanning Tree
Step-7: Select node f as it have minimum Vertex Key Pi
(v) (v) (v)
key value and update key value of each a NIL
0
adjacent node of node f. b 4 a
c 8 b
d 7 c
e 10 f
f 4 c
g 2 f
h 7 i
i 2 c
Minimum Cost Spanning Tree
Step-8: Select node g as it have minimum Vertex Key Pi
(v) (v) (v)
key value and update key value of each a NIL
0
adjacent node of node g. b 4 a
c 8 b
d 7 c
e 10 f
f 4 c
g 2 f
h 1 g
i 2 c
Minimum Cost Spanning Tree
Step-9: Select node h as it have minimum Vertex Key Pi
(v) (v) (v)
key value and update key value of each a NIL
0
adjacent node of node h. b 4 a
c 8 b
d 7 c
e 10 f
f 4 c
g 2 f
h 1 g
i 2 c
Minimum Cost Spanning Tree
Step-10: Select node d as it have minimum Vertex Key Pi
(v) (v) (v)
key value and update key value of each a NIL
0
adjacent node of node d. b 4 a
c 8 b
d 7 c
e 10 f
f 4 c
g 2 f
h 1 g
i 2 c
Minimum Cost Spanning Tree
Step-11: Now select node e as it is the Vertex Key Pi
(v) (v) (v)
Last node, and we will get the final MST. a NIL
0
b 4 a
c 8 b
d 7 c
e 10 f
f 4 c
g 2 f
h 1 g
i 2 c
It generates the minimum spanning tree It generates the minimum spanning tree
starting from the root vertex. starting from the least weighted edge.
Prim’s algorithm runs faster in dense Kruskal’s algorithm runs faster in sparse
graphs. graphs.