SlideShare a Scribd company logo
Prim’s Algorithm
Invented By:
Vojtěch jarník
What is Spanning Tree?
Given a connected undirected graph, a
spanning tree of that graph is a sub graph
that is a tree and joined all vertices. A single
graph can have many spanning trees.
2
Minimum Spanning Tree
Minimum Spanning Tree is a Spanning Tree which has minimum
total cost.
If we have a linked undirected graph with a weight (or cost)
combine
with each edge. Then the cost of spanning tree would be the sum
of the
cost of its edges.
3
4
The idea of prim’s algorithm is
Start with any vertex(say A) of the graph
G(V, E), to build a tree T. Add the least cost
edge to the tree T provided one of the
endpoints of that edge must be in T. Add the
edges until all the vertices are covered.
Same here also, the cycle should not be
formed on adding an edge.
5
Select
any node
Connect
it to the
nearest
node
Connect
one node
already
selected to
the nearest
unconnect
ed node.
Repeat 3
until all
nodes are
connecte
d.
Basic Algorithm
EXAMPLE:
Find the minimum
spanning tree of
given graph using
Prim’s Algorithm
6
Solution:
Step 1: Remove all loops and parallel edges
Remove all loops and parallel edges from the given
graph.
In case of parallel edges, keep the one which has the 7
Conti..
Step 2: Choose any arbitrary node as root node
In this case, we choose S node as the root node of Prim's spanning
tree.
This node is arbitrarily chosen, so any node can be the root node.
In the
spanning tree all the nodes of a graph are included and because it
is
connected then there must be at least one edge, which will join it
to the
8
Conti..
Step 3:
Check outgoing edges and select the one with less
cost
After choosing the root node S, we see that S,A and S,C are two
edges with
weight 7 and 8, respectively. We choose the edge S,A as it is
lesser than
the other.
9
Conti..
Now, the tree S-7-A is treated as one node and we
check
for all edges going out from it. We select the one
which
has the lowest cost and include it in the tree.
10
Conti..
Step 4:
After this step, S-7-A-3-C tree is formed. Now we'll again will
check all the
edges again. However, we will choose only the least cost edge. In
this case,
C-3-D is the new edge, which is less than other edges' cost 8, 6, 4,
etc.
11
Conti..
After adding node D to the spanning tree, we now have two
edges going
out of it having the same cost, i.e. D-2-T and D-2-B. Thus, we can
add
either one. But the next step will again yield edge 2 as the least
cost.
Hence, we are showing a spanning tree with both edges
included.
12
Example 2:
Construct a minimum spanning tree of the graph given in the
following
figure by using prim's algorithm.
13
Solution:
Step 1 : Choose a starting vertex B
Step 2: Add the vertices that are adjacent to B. the
edges
that connecting the vertices are shown by dotted
lines.
14
Step 3:
Choose the edge with the minimum weight among
all. i.e. BD
and add it to MST. Add the adjacent vertices of D i.e.
C and E.
15
Step 4:
Choose the edge with the minimum weight among
all. In
this case, the edges DE and CD are such edges. Add
them
To MST and explore the adjacent of C i.e. E and A.
16
Step 5:
Choose the edge with the minimum weight i.e. CA.
We can't choose CE as it would cause cycle in the
graph.
The graph produces in the step 4
is the minimum spanning tree of
the graph shown in the above figure.
The cost of MST will be calculated as;
cost(MST) = 4 + 2 + 1 + 3 = 10 units.
17
18
 Cluster analysis
 Image segmentation
 Handwriting recognition
 Medical Image Processing
 Identify patterns in gene expression
 Approximation to NP hard problems
 Document categorization for web search
 Network design for telephone, electrical, hydraulic, TV cable,
computer, road etc.
 There are many indirect applications also.
Applications:
“
”
THANK YOU
19

More Related Content

PPT
Unit27_MinimumSpanningTree.ppt data structure programming
PPT
Minimum Spanning Tree
PPTX
Grady approach prim's spanning tree
PPT
Graph Theory PPT presentation created by Selvam.
PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
PPTX
Greedy technique - Algorithm design techniques using data structures
PPTX
Minimum Spanning Tree.pptx
PPTX
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx
Unit27_MinimumSpanningTree.ppt data structure programming
Minimum Spanning Tree
Grady approach prim's spanning tree
Graph Theory PPT presentation created by Selvam.
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Greedy technique - Algorithm design techniques using data structures
Minimum Spanning Tree.pptx
11L_2024_DSCS_EN_Trees2_Prim_Kraskal.pptx

Similar to Prim's Algorithm Presentation for prims algorithm (1).pptx (20)

PPTX
7. Spanning trees
PPTX
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
PPT
Minimum spanning tree
PPTX
Presentation.pptx
PPTX
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
PPT
Unit 5 graphs minimum spanning trees
PPTX
Lecture 5.2 - 6.1 MINIMUM SPANNING TREE.pptx
PPTX
Greedy Strategy.pptxbfasjbjfn asnfn anjn
PPTX
Minimum spanning tree
PPT
Spanning trees
PPT
Greedy Approach in Design Analysis and Algorithms
PPTX
DM Min SPan Tree Minimum spanning tree .pptx
PPTX
Data Algorithms And Analysis
PPTX
prim's and kruskal's algorithm
PPTX
Minimum Spanning Tree (Data Structure and Algorithm)
PDF
Topological Sort
PDF
Skiena algorithm 2007 lecture13 minimum spanning trees
PPTX
My presentation minimum spanning tree
7. Spanning trees
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
Minimum spanning tree
Presentation.pptx
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Unit 5 graphs minimum spanning trees
Lecture 5.2 - 6.1 MINIMUM SPANNING TREE.pptx
Greedy Strategy.pptxbfasjbjfn asnfn anjn
Minimum spanning tree
Spanning trees
Greedy Approach in Design Analysis and Algorithms
DM Min SPan Tree Minimum spanning tree .pptx
Data Algorithms And Analysis
prim's and kruskal's algorithm
Minimum Spanning Tree (Data Structure and Algorithm)
Topological Sort
Skiena algorithm 2007 lecture13 minimum spanning trees
My presentation minimum spanning tree
Ad

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
master seminar digital applications in india
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Anesthesia in Laparoscopic Surgery in India
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Module 4: Burden of Disease Tutorial Slides S2 2025
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
GDM (1) (1).pptx small presentation for students
A systematic review of self-coping strategies used by university students to ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
master seminar digital applications in india
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
FourierSeries-QuestionsWithAnswers(Part-A).pdf
RMMM.pdf make it easy to upload and study
Yogi Goddess Pres Conference Studio Updates
Anesthesia in Laparoscopic Surgery in India
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
Final Presentation General Medicine 03-08-2024.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Ad

Prim's Algorithm Presentation for prims algorithm (1).pptx

  • 2. What is Spanning Tree? Given a connected undirected graph, a spanning tree of that graph is a sub graph that is a tree and joined all vertices. A single graph can have many spanning trees. 2
  • 3. Minimum Spanning Tree Minimum Spanning Tree is a Spanning Tree which has minimum total cost. If we have a linked undirected graph with a weight (or cost) combine with each edge. Then the cost of spanning tree would be the sum of the cost of its edges. 3
  • 4. 4 The idea of prim’s algorithm is Start with any vertex(say A) of the graph G(V, E), to build a tree T. Add the least cost edge to the tree T provided one of the endpoints of that edge must be in T. Add the edges until all the vertices are covered. Same here also, the cycle should not be formed on adding an edge.
  • 5. 5 Select any node Connect it to the nearest node Connect one node already selected to the nearest unconnect ed node. Repeat 3 until all nodes are connecte d. Basic Algorithm
  • 6. EXAMPLE: Find the minimum spanning tree of given graph using Prim’s Algorithm 6
  • 7. Solution: Step 1: Remove all loops and parallel edges Remove all loops and parallel edges from the given graph. In case of parallel edges, keep the one which has the 7
  • 8. Conti.. Step 2: Choose any arbitrary node as root node In this case, we choose S node as the root node of Prim's spanning tree. This node is arbitrarily chosen, so any node can be the root node. In the spanning tree all the nodes of a graph are included and because it is connected then there must be at least one edge, which will join it to the 8
  • 9. Conti.. Step 3: Check outgoing edges and select the one with less cost After choosing the root node S, we see that S,A and S,C are two edges with weight 7 and 8, respectively. We choose the edge S,A as it is lesser than the other. 9
  • 10. Conti.. Now, the tree S-7-A is treated as one node and we check for all edges going out from it. We select the one which has the lowest cost and include it in the tree. 10
  • 11. Conti.. Step 4: After this step, S-7-A-3-C tree is formed. Now we'll again will check all the edges again. However, we will choose only the least cost edge. In this case, C-3-D is the new edge, which is less than other edges' cost 8, 6, 4, etc. 11
  • 12. Conti.. After adding node D to the spanning tree, we now have two edges going out of it having the same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the next step will again yield edge 2 as the least cost. Hence, we are showing a spanning tree with both edges included. 12
  • 13. Example 2: Construct a minimum spanning tree of the graph given in the following figure by using prim's algorithm. 13
  • 14. Solution: Step 1 : Choose a starting vertex B Step 2: Add the vertices that are adjacent to B. the edges that connecting the vertices are shown by dotted lines. 14
  • 15. Step 3: Choose the edge with the minimum weight among all. i.e. BD and add it to MST. Add the adjacent vertices of D i.e. C and E. 15
  • 16. Step 4: Choose the edge with the minimum weight among all. In this case, the edges DE and CD are such edges. Add them To MST and explore the adjacent of C i.e. E and A. 16
  • 17. Step 5: Choose the edge with the minimum weight i.e. CA. We can't choose CE as it would cause cycle in the graph. The graph produces in the step 4 is the minimum spanning tree of the graph shown in the above figure. The cost of MST will be calculated as; cost(MST) = 4 + 2 + 1 + 3 = 10 units. 17
  • 18. 18  Cluster analysis  Image segmentation  Handwriting recognition  Medical Image Processing  Identify patterns in gene expression  Approximation to NP hard problems  Document categorization for web search  Network design for telephone, electrical, hydraulic, TV cable, computer, road etc.  There are many indirect applications also. Applications: