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

Lec-25 Prim's Algorithm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lec-25 Prim's Algorithm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Design and Analysis of

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

Total weight of MST = 4+8+2+4+2+1+7+9


= 37
Minimum Cost Spanning Tree
 Example-2
Differences Between Prim’s and Kruskal’s
Algorithm
Prim’s Algorithm Kruskal’s Algorithm

It generates the minimum spanning tree It generates the minimum spanning tree
starting from the root vertex. starting from the least weighted edge.

It traverses one node more than one time


It traverses one node only once.
to get the minimum distance.

Prim’s algorithm has a time complexity of


Kruskal’s algorithm’s time complexity is
O(V2), V being the number of vertices and
O(E log V), V being the number of
can be improved up to O(E log V) using
vertices.
Fibonacci heaps.

Kruskal’s algorithm can generate


Prim’s algorithm gives connected
forest(disconnected components) at any
component as well as it works only on
instant as well as it can work on
connected graph.
disconnected components

Prim’s algorithm runs faster in dense Kruskal’s algorithm runs faster in sparse
graphs. graphs.

Applications of prim’s algorithm are


Travelling Salesman Problem, Network for Applications of Kruskal’s algorithm are

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy