SlideShare a Scribd company logo
NAME- SHREYA KHANNA
ROLL NO- 202210101110196
SUBJECT-
DESIGN AND ANALYSIS OF ALGORITHM
COURSE- B.TECH (CS)-57/58
TOPIC-MINIMUM SPANNING TREE
TEACHER- Mr. Anil Kumar
COURSE CODE- BCS5001
• A Spanning Tree is a subgraph that includes all vertices
of the original graph, is connected, and contains no
cycles.
1.Properties:
• Includes all vertices of the graph.
• Minimizes the number of edges (V-1 edges for V
vertices).
• Does not contain cycles.
Spanning Tree
Introduction to Minimum Spanning
Tree
A Minimum Spanning Tree (MST) is a
spanning tree with the smallest possible
total edge weight.
• Example: Imagine connecting cities
with minimum road length—MST
helps find this optimized connection.
MST only exists if the graph is connected and undirected.
An MST will have V-1 edges for a graph with V vertices.
Uniqueness:
• If all edge weights are unique, the MST is unique.
• Duplicate weights may lead to multiple MSTs.
Note: MST doesn’t guarantee shortest paths between individual
nodes—only minimum total weight.
Properties of
MST
Kruskal's
Algorithm
Kruskal's Algorithm is used to find the minimum spanning tree for
a connected weighted graph. The main target of the algorithm is to
find the subset of edges by using which we can traverse every
vertex of the graph.
• Sort all edges by weight.
• Add edges to the MST in ascending order of weight.
• Only add edges that don’t create a cycle (using Union-Find data
structure).
Time Complexity: O(Elog⁡
E)O(E log E)O(ElogE), where EEE is the
number of edges.
Kruskal's
Algorithm
• Step 1: Create a forest F in such a way that every
vertex of the graph is a separate tree.
• Step 2: Create a set E that contains all the edges of
the graph.
• Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY
and F is not spanning
• Step 4: Remove an edge from E with minimum weight
• Step 5: IF the edge obtained in Step 4 connects two
different trees, then add it to the forest F
• (for combining two trees into one tree).
• ELSE
• Discard the edge
• Step 6: END
Prim's Algorithm
Prim's algorithm is a greedy algorithm that
starts from one vertex and continue to add the
edges with the smallest weight until the goal is
reached.
• Start from any vertex.
• Grow the MST by adding the smallest weight edge
connected to the MST.
• Continue until all vertices are included.
Time Complexity: O(V2)O(V^2)O(V2) for simple
version, or O(Elog⁡
V)O(E log V)O(ElogV) using priority
queue.
• Step 1: Select a starting vertex
• Step 2: Repeat Steps 3 and 4 until there are fringe
vertices
• Step 3: Select an edge 'e' connecting the tree
vertex and fringe vertex that has minimum weight
• Step 4: Add the selected edge and the vertex to
the minimum spanning tree T
• [END OF LOOP]
• Step 5: EXIT
Prim's Algorithm
Example of prim's
algorithm
• Suppose, a weighted graph is -
• Step 1 - First, we have to choose a vertex from the
above graph. Let's choose B.
• Step 2 - Now, we have to choose and add the shortest
edge from vertex B. There are two edges from vertex B
that are B to C with weight 10 and edge B to D with
weight 4. Among the edges, the edge BD has the
minimum weight. So, add it to the MST.
• Step 3- Now, again, choose the edge with the minimum
weight among all the other edges. 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. So, select the
edge DE and add it to the MST.
• Step 4 - Now, select the edge CD, and add it to the MST.
• Step 5 - Now, choose the edge CA. Here, we cannot
select the edge CE as it would create a cycle to the
graph. So, choose the edge CA and add it to the MST.
• So, the graph produced in
step 5 is the minimum
spanning tree of the given
graph .Cost of MST = 4 + 2 +
1 + 3 = 10 units.
Time Complexity of the
Algorithms
Algorithm Time Complexity
Kruskal's O(E log E)
Prim's O(E log V)
The time complexity depends on the data structures used, with Prim's
having a slight edge for sparse graphs.
Feature Prim’s Algorithm Kruskal’s Algorithm
Approach
Vertex-based, grows the MST
one vertex at a time
Edge-based, adds edges in
increasing order of weight
Data Structure Priority queue (min-heap) Union-Find data structure
Initialization Starts from an arbitrary vertex
Starts with all vertices as
separate trees (forest)
Edge Selection
Chooses the minimum weight
edge from the connected
vertices
Chooses the minimum weight edge
from all edges
Complexity
O(V^2) for adjacency matrix,
O((E + V) log V) with a priority
queue
O(E log E) or O(E log V), due to
edge sorting
Suitable for Dense graphs Sparse graphs
Memory Usage More memory for priority queue
Less memory if edges can be
sorted externally
Example Use Cases
Network design, clustering with
dense connections
Road networks,
telecommunications with sparse
connections
Comparison of Kruskal’s and Prim’s
Algorithms
Conclusion and Key Takeaways
• MSTs are fundamental in optimizing connected
networks.
• Kruskal’s and Prim’s algorithms are key tools for
finding MSTs.
• Applications span diverse fields like networking,
ML, and design.
Final Thoughts: Emphasize MST’s real-world
importance in cost reduction and resource
optimization.
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is for adjacency m-2.pptx

More Related Content

PPTX
Greedy Strategy.pptxbfasjbjfn asnfn anjn
PPTX
Greedy technique - Algorithm design techniques using data structures
PPTX
Graph Algorithms
PDF
Minimum Spanning Tree in design and analysis
PPTX
Bfs dfs mst
PDF
15 chapter9 graph_algorithms_mst
PPT
PPTX
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
Greedy Strategy.pptxbfasjbjfn asnfn anjn
Greedy technique - Algorithm design techniques using data structures
Graph Algorithms
Minimum Spanning Tree in design and analysis
Bfs dfs mst
15 chapter9 graph_algorithms_mst
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM

Similar to _A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is for adjacency m-2.pptx (20)

PPTX
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
PPTX
Presentation.pptx
PDF
Unit3_1.pdf
PPTX
1 sollins algorithm
PPTX
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
PDF
Algorithm chapter 9
PPTX
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
PPTX
Minimum Spinning Tree Full Explaination pptx
PDF
lec03 lec03 lec03 lec03 lec03 lec03 lec03
PPTX
MSadfadsfafdadfccadradfT_Presentation.pptx
PPT
Weighted graphs
PPTX
Data Structures and Algorithms Kruskals algorithm
PPTX
Minimum spanning tree.pptx data structure programming
PDF
Ijciras1101
PPTX
prim's and kruskal's algorithm
PPTX
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
PPT
MinSpanningTreespresantion.ppt
PPTX
Comprehensive Study on Dijkstra and Prim Algorithms
PPTX
Discrete Mathematics ( Dijkstra Algorithm & Prims Algorithm)
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
Presentation.pptx
Unit3_1.pdf
1 sollins algorithm
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Algorithm chapter 9
TREE ADT, TREE TRAVERSALS, BINARY TREE ADT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Minimum Spinning Tree Full Explaination pptx
lec03 lec03 lec03 lec03 lec03 lec03 lec03
MSadfadsfafdadfccadradfT_Presentation.pptx
Weighted graphs
Data Structures and Algorithms Kruskals algorithm
Minimum spanning tree.pptx data structure programming
Ijciras1101
prim's and kruskal's algorithm
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
MinSpanningTreespresantion.ppt
Comprehensive Study on Dijkstra and Prim Algorithms
Discrete Mathematics ( Dijkstra Algorithm & Prims Algorithm)
Ad

Recently uploaded (20)

PDF
[EN] Industrial Machine Downtime Prediction
PDF
Introduction to Data Science and Data Analysis
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
Introduction to the R Programming Language
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Lecture1 pattern recognition............
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PPT
Quality review (1)_presentation of this 21
PPTX
Introduction to machine learning and Linear Models
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
[EN] Industrial Machine Downtime Prediction
Introduction to Data Science and Data Analysis
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
ISS -ESG Data flows What is ESG and HowHow
Introduction to the R Programming Language
Introduction to Knowledge Engineering Part 1
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Lecture1 pattern recognition............
IBA_Chapter_11_Slides_Final_Accessible.pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Quality review (1)_presentation of this 21
Introduction to machine learning and Linear Models
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
climate analysis of Dhaka ,Banglades.pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
STERILIZATION AND DISINFECTION-1.ppthhhbx
IB Computer Science - Internal Assessment.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Ad

_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is for adjacency m-2.pptx

  • 1. NAME- SHREYA KHANNA ROLL NO- 202210101110196 SUBJECT- DESIGN AND ANALYSIS OF ALGORITHM COURSE- B.TECH (CS)-57/58 TOPIC-MINIMUM SPANNING TREE TEACHER- Mr. Anil Kumar COURSE CODE- BCS5001
  • 2. • A Spanning Tree is a subgraph that includes all vertices of the original graph, is connected, and contains no cycles. 1.Properties: • Includes all vertices of the graph. • Minimizes the number of edges (V-1 edges for V vertices). • Does not contain cycles. Spanning Tree
  • 3. Introduction to Minimum Spanning Tree A Minimum Spanning Tree (MST) is a spanning tree with the smallest possible total edge weight. • Example: Imagine connecting cities with minimum road length—MST helps find this optimized connection.
  • 4. MST only exists if the graph is connected and undirected. An MST will have V-1 edges for a graph with V vertices. Uniqueness: • If all edge weights are unique, the MST is unique. • Duplicate weights may lead to multiple MSTs. Note: MST doesn’t guarantee shortest paths between individual nodes—only minimum total weight. Properties of MST
  • 5. Kruskal's Algorithm Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The main target of the algorithm is to find the subset of edges by using which we can traverse every vertex of the graph. • Sort all edges by weight. • Add edges to the MST in ascending order of weight. • Only add edges that don’t create a cycle (using Union-Find data structure). Time Complexity: O(Elog⁡ E)O(E log E)O(ElogE), where EEE is the number of edges.
  • 6. Kruskal's Algorithm • Step 1: Create a forest F in such a way that every vertex of the graph is a separate tree. • Step 2: Create a set E that contains all the edges of the graph. • Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning • Step 4: Remove an edge from E with minimum weight • Step 5: IF the edge obtained in Step 4 connects two different trees, then add it to the forest F • (for combining two trees into one tree). • ELSE • Discard the edge • Step 6: END
  • 7. Prim's Algorithm Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add the edges with the smallest weight until the goal is reached. • Start from any vertex. • Grow the MST by adding the smallest weight edge connected to the MST. • Continue until all vertices are included. Time Complexity: O(V2)O(V^2)O(V2) for simple version, or O(Elog⁡ V)O(E log V)O(ElogV) using priority queue.
  • 8. • Step 1: Select a starting vertex • Step 2: Repeat Steps 3 and 4 until there are fringe vertices • Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight • Step 4: Add the selected edge and the vertex to the minimum spanning tree T • [END OF LOOP] • Step 5: EXIT Prim's Algorithm
  • 9. Example of prim's algorithm • Suppose, a weighted graph is - • Step 1 - First, we have to choose a vertex from the above graph. Let's choose B. • Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges from vertex B that are B to C with weight 10 and edge B to D with weight 4. Among the edges, the edge BD has the minimum weight. So, add it to the MST. • Step 3- Now, again, choose the edge with the minimum weight among all the other edges. 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. So, select the edge DE and add it to the MST. • Step 4 - Now, select the edge CD, and add it to the MST. • Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would create a cycle to the graph. So, choose the edge CA and add it to the MST. • So, the graph produced in step 5 is the minimum spanning tree of the given graph .Cost of MST = 4 + 2 + 1 + 3 = 10 units.
  • 10. Time Complexity of the Algorithms Algorithm Time Complexity Kruskal's O(E log E) Prim's O(E log V) The time complexity depends on the data structures used, with Prim's having a slight edge for sparse graphs.
  • 11. Feature Prim’s Algorithm Kruskal’s Algorithm Approach Vertex-based, grows the MST one vertex at a time Edge-based, adds edges in increasing order of weight Data Structure Priority queue (min-heap) Union-Find data structure Initialization Starts from an arbitrary vertex Starts with all vertices as separate trees (forest) Edge Selection Chooses the minimum weight edge from the connected vertices Chooses the minimum weight edge from all edges Complexity O(V^2) for adjacency matrix, O((E + V) log V) with a priority queue O(E log E) or O(E log V), due to edge sorting Suitable for Dense graphs Sparse graphs Memory Usage More memory for priority queue Less memory if edges can be sorted externally Example Use Cases Network design, clustering with dense connections Road networks, telecommunications with sparse connections Comparison of Kruskal’s and Prim’s Algorithms
  • 12. Conclusion and Key Takeaways • MSTs are fundamental in optimizing connected networks. • Kruskal’s and Prim’s algorithms are key tools for finding MSTs. • Applications span diverse fields like networking, ML, and design. Final Thoughts: Emphasize MST’s real-world importance in cost reduction and resource optimization.