SlideShare a Scribd company logo
MINIMUM
COST
SPANNING
TREE
1. Adjacency Matrix
of a graph
2. Kruskal Algorithm
3. Prim’s Algorithm
Graph
Graph is a data structure that consists of following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge.
Following is an example of an undirected graph with 5 vertices
Graph representation
Adjacency Matrix : It is a 2D array of size V x V where V is the number of
vertices in a graph. Let the 2D array be Adj[V][V],
Adj[i][j] = 1 => indicates that there is an edge from vertex i to vertex j.
Adj[i][j] = 0 => indicates that there is no edge from vertex i to vertex j.
The adjacency matrix for the above example graph is
Minimum Spanning Tree
Here G=(V,E)
V={ a, b, c, d, e }
E={ (a, b) , ( a, c) , ( b ,e) , ( d, e) , ( c, d ) }
Minimum Spanning Tree
So Sum of edge’s weight of all spanning trees T1,T2,T3 and T4 is calculated .And minimum cost
spanning tree (MCST ) is the one whose sum is minimum.
Applications of Minimum Cost Spanning Tree
1. Spanning trees are widely used in designing an efficient network.
 For example, to design a network in which a group of differently located individuals wish to
be connected together in a telephone network.
 This problem can be converted into a graph problem in which nodes are telephones
 The goal is to pick enough of these edges that the gives less maintenance cost
 An answer to this question is to find MCST
2. Designing of efficient routing algorithm.
 Suppose we want to find a airline routes
 The vertices of the graph would represent cities, and the edges would represent routes
between the cities.
 Obviously, when we travel more, the more it will cost.
So MCST can be applied to optimize airline routes
Algorithms for finding MCST
The vertices of the graph would represent cities, and the edges would represent routes
between the cities. Obviously, when we travel more, the more it will cost. So MCST can be
applied to optimize airline routes
Kruskal Algorithm for finding MCST
The vertices of the graph would represent cities, and the edges would represent routes
between the cities. Obviously, when we travel more, the more it will cost. So MCST can be
applied to optimize airline routes
Kruskal Algorithm for finding MCST
The vertices of the graph would represent cities, and the edges would represent routes
between the cities. Obviously, when we travel more, the more it will cost. So MCST can be
applied to optimize airline routes
Kruskal Agorithm steps
Step 1: Sort all the edges in increasing order of weights
Step2 : The function MAKE_SET(v), make a new set {v} for all vertices of G. For a graph with n
vertices, it makes n components of disjoint set such as {1},{2},… and so
on.
Step 3: Consider each edge (u, v)ε E of minimum weight and add the set A, if and
only if it joins two nodes of different components/Sets.
• FIND_SET() function is used to check whether 2nodes belong to deifferent or same set.
• It returns a same integer value, if u and v belongs to same components
(In this case adding (u,v) to A creates a cycle),otherwise it returns a different integer
value)
Step 4: If an edge added to A then the two components containing its end points are merged
into a single component.
Step 5: Finally the algorithm stops, when there is just a single component.
Example
Consider the following graph to find MCST using Kruskal Algorithm.ed
to optimize airline routes
Example
Example
Total Cost of Spanning tree, T = 2+3+5+4+5+4=23
Analysis
Considering |E| as number of edges=a
|V| as number of vertices=n
• Initialize A: O(1)
• First for loop: O(|V|) as|V| MAKE-SET operations
• Sort E: O(|E| lg |E|)
• Second for loop: O(|E|) as |E| FIND-SETs and UNIONs
 Total Complexity=O(1)+O(|V|)+O(|E|)+O(|E| lg |E|)
=O(|V|+|E|)+O(|E| lg |E|)
Minimum Number of edges E=n-1
Maximum Number of edges E=n(n-1)/2
• Consider when E is minimum i.e n
Complexity=O(n+n-1)+O(n lg n)= O(2n+n lg n)= O(n lg n) =O( |V| lg |V|)
• Consider when E is maximum i.e n(n-1)/2
Complexity=O(n+n(n-1)/2)+O(n(n-1)/2 lg n(n-1)/2)
= O(|E|+|E| lg n2
)= O(|E|+2|E| lg n) =O( |E| lg |V|)
Prim’s Algorithm
Prim’s Pseudocode
Prim’s Steps
Example
Apply PRIM’s algorithm on the following graph to find minimum-cost-
spanning – tree (MCST)
Example
Total Cost of
the minimum
spanning tree
= 2+3+5+4+5+4
= 23
Analysis
THANK YOU
delightwithtech@gmail.com

More Related Content

PDF
directed-research-report
PDF
An analysis between exact and approximate algorithms for the k-center proble...
PDF
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
PPTX
Computer Science Assignment Help
PPT
Matlab Overviiew 2
PPTX
Dijkstra
PDF
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
directed-research-report
An analysis between exact and approximate algorithms for the k-center proble...
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Computer Science Assignment Help
Matlab Overviiew 2
Dijkstra
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK

Similar to kruskal and prims algorithm _ (20)

PDF
Daa chapter11
PPTX
Hashing in data base managementsystem_Updated.pptx
PPT
Weighted graphs
PDF
Thesis_Draft_Nurkhaidarov
PDF
On Edge Control Set of a Graph in Transportation Problems
PPTX
Introduction to MATLAB Programming for Engineers
PPT
Graphs in Data Structure
PPTX
Basic MATLAB-Presentation.pptx
PPT
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
PPTX
Introduction to matlab lecture 2 of 4
PPTX
logic.pptx
PPT
rachit.ppt for academic purposes for college students
PDF
Investigation on the Pattern Synthesis of Subarray Weights for Low EMI Applic...
PPT
PPTX
1. Introduction.pptx
PPTX
SN-BDA-MR-Analysis-6.pptx.................
PPTX
data structure and algorithm Array.pptx btech 2nd year
PDF
lecture 23 algorithm design and analysis
PPTX
Data structure - traveling sales person and mesh algorithm
Daa chapter11
Hashing in data base managementsystem_Updated.pptx
Weighted graphs
Thesis_Draft_Nurkhaidarov
On Edge Control Set of a Graph in Transportation Problems
Introduction to MATLAB Programming for Engineers
Graphs in Data Structure
Basic MATLAB-Presentation.pptx
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Introduction to matlab lecture 2 of 4
logic.pptx
rachit.ppt for academic purposes for college students
Investigation on the Pattern Synthesis of Subarray Weights for Low EMI Applic...
1. Introduction.pptx
SN-BDA-MR-Analysis-6.pptx.................
data structure and algorithm Array.pptx btech 2nd year
lecture 23 algorithm design and analysis
Data structure - traveling sales person and mesh algorithm
Ad

More from swati463221 (13)

PPTX
python ..... _
PPTX
python _
PPTX
matplotlib _
PPTX
image processing _
PPTX
numpy2 _
PPTX
Classes and Objects _
PPTX
Inheritance Super and MRO _
PDF
Function overloading or Polymorphism.pdf
PPTX
Classes & Objects _
PPT
switching.ppt
PPT
stop and wait
PPTX
subnetting
PPTX
FIREWALL
python ..... _
python _
matplotlib _
image processing _
numpy2 _
Classes and Objects _
Inheritance Super and MRO _
Function overloading or Polymorphism.pdf
Classes & Objects _
switching.ppt
stop and wait
subnetting
FIREWALL
Ad

Recently uploaded (20)

PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Well-logging-methods_new................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Digital Logic Computer Design lecture notes
PPT
Mechanical Engineering MATERIALS Selection
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
web development for engineering and engineering
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT 4 Total Quality Management .pptx
Well-logging-methods_new................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Internet of Things (IOT) - A guide to understanding
Digital Logic Computer Design lecture notes
Mechanical Engineering MATERIALS Selection
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Automation-in-Manufacturing-Chapter-Introduction.pdf
Lecture Notes Electrical Wiring System Components
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
R24 SURVEYING LAB MANUAL for civil enggi
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Sustainable Sites - Green Building Construction
Operating System & Kernel Study Guide-1 - converted.pdf
web development for engineering and engineering
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks

kruskal and prims algorithm _

  • 1. MINIMUM COST SPANNING TREE 1. Adjacency Matrix of a graph 2. Kruskal Algorithm 3. Prim’s Algorithm
  • 2. Graph Graph is a data structure that consists of following two components: 1. A finite set of vertices also called as nodes. 2. A finite set of ordered pair of the form (u, v) called as edge. Following is an example of an undirected graph with 5 vertices
  • 3. Graph representation Adjacency Matrix : It is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be Adj[V][V], Adj[i][j] = 1 => indicates that there is an edge from vertex i to vertex j. Adj[i][j] = 0 => indicates that there is no edge from vertex i to vertex j. The adjacency matrix for the above example graph is
  • 4. Minimum Spanning Tree Here G=(V,E) V={ a, b, c, d, e } E={ (a, b) , ( a, c) , ( b ,e) , ( d, e) , ( c, d ) }
  • 5. Minimum Spanning Tree So Sum of edge’s weight of all spanning trees T1,T2,T3 and T4 is calculated .And minimum cost spanning tree (MCST ) is the one whose sum is minimum.
  • 6. Applications of Minimum Cost Spanning Tree 1. Spanning trees are widely used in designing an efficient network.  For example, to design a network in which a group of differently located individuals wish to be connected together in a telephone network.  This problem can be converted into a graph problem in which nodes are telephones  The goal is to pick enough of these edges that the gives less maintenance cost  An answer to this question is to find MCST 2. Designing of efficient routing algorithm.  Suppose we want to find a airline routes  The vertices of the graph would represent cities, and the edges would represent routes between the cities.  Obviously, when we travel more, the more it will cost. So MCST can be applied to optimize airline routes
  • 7. Algorithms for finding MCST The vertices of the graph would represent cities, and the edges would represent routes between the cities. Obviously, when we travel more, the more it will cost. So MCST can be applied to optimize airline routes
  • 8. Kruskal Algorithm for finding MCST The vertices of the graph would represent cities, and the edges would represent routes between the cities. Obviously, when we travel more, the more it will cost. So MCST can be applied to optimize airline routes
  • 9. Kruskal Algorithm for finding MCST The vertices of the graph would represent cities, and the edges would represent routes between the cities. Obviously, when we travel more, the more it will cost. So MCST can be applied to optimize airline routes
  • 10. Kruskal Agorithm steps Step 1: Sort all the edges in increasing order of weights Step2 : The function MAKE_SET(v), make a new set {v} for all vertices of G. For a graph with n vertices, it makes n components of disjoint set such as {1},{2},… and so on. Step 3: Consider each edge (u, v)ε E of minimum weight and add the set A, if and only if it joins two nodes of different components/Sets. • FIND_SET() function is used to check whether 2nodes belong to deifferent or same set. • It returns a same integer value, if u and v belongs to same components (In this case adding (u,v) to A creates a cycle),otherwise it returns a different integer value) Step 4: If an edge added to A then the two components containing its end points are merged into a single component. Step 5: Finally the algorithm stops, when there is just a single component.
  • 11. Example Consider the following graph to find MCST using Kruskal Algorithm.ed to optimize airline routes
  • 13. Example Total Cost of Spanning tree, T = 2+3+5+4+5+4=23
  • 14. Analysis Considering |E| as number of edges=a |V| as number of vertices=n • Initialize A: O(1) • First for loop: O(|V|) as|V| MAKE-SET operations • Sort E: O(|E| lg |E|) • Second for loop: O(|E|) as |E| FIND-SETs and UNIONs  Total Complexity=O(1)+O(|V|)+O(|E|)+O(|E| lg |E|) =O(|V|+|E|)+O(|E| lg |E|) Minimum Number of edges E=n-1 Maximum Number of edges E=n(n-1)/2 • Consider when E is minimum i.e n Complexity=O(n+n-1)+O(n lg n)= O(2n+n lg n)= O(n lg n) =O( |V| lg |V|) • Consider when E is maximum i.e n(n-1)/2 Complexity=O(n+n(n-1)/2)+O(n(n-1)/2 lg n(n-1)/2) = O(|E|+|E| lg n2 )= O(|E|+2|E| lg n) =O( |E| lg |V|)
  • 18. Example Apply PRIM’s algorithm on the following graph to find minimum-cost- spanning – tree (MCST)
  • 19. Example Total Cost of the minimum spanning tree = 2+3+5+4+5+4 = 23