SlideShare a Scribd company logo
SRM Institute of Science and Technology
Minimum Spanning Tree -
Kruskal’s Algorithm
SRM Institute of Science and Technology
A Spanning tree can be defined as a subset of a graph, which
consists of all the vertices covering minimum possible edges and
does not have a cycle. Spanning tree cannot be disconnected.
Every connected and undirected graph has at least one spanning
tree. A disconnected graph does not have a spanning tree as it is
not possible to include all vertices.
NN-2 number of spanning trees. Thus in the above graph N =3, therefore, it
has 3(3-2) = 3 spanning trees.
SRM Institute of Science and Technology
Some of the properties of the spanning tree are listed below:
•A connected graph can have more than one spanning trees.
•All spanning trees in a graph have the same number of nodes and
edges.
•If we remove one edge from the spanning tree, then it will
become minimally connected and will make the graph
disconnected.
•On the other hand, adding one edge to the spanning tree will make
it maximally acyclic thereby creating a loop.
•A spanning tree does not have a loop or a cycle.
SRM Institute of Science and Technology
What Is A Minimum Spanning Tree (MST)
A minimum spanning tree is the one that contains the least weight
among all the other spanning trees of a connected weighted graph.
There can be more than one minimum spanning tree for a graph.
•Kruskal’s algorithm
•Prim’s algorithm
SRM Institute of Science and Technology
Minimum Spanning Tree -
Kruskal’s Algorithm
SRM Institute of Science and Technology
•Kruskal’s Algorithm is a famous greedy algorithm.
•It is used for finding the Minimum Spanning Tree (MST) of a given graph.
•To apply Kruskal’s algorithm, the given graph must be weighted, connected and undirected.
Kruskal’s Algorithm Implementation-
The implementation of Kruskal’s Algorithm is explained in the following steps-
Step-01:
•Sort all the edges from low weight to high weight.
Step-02:
•Take the edge with the lowest weight and use it to connect the vertices of graph.
•If adding an edge creates a cycle, then reject that edge and go for the next least weight edge.
SRM Institute of Science and Technology
Step-03:
•Keep adding edges until all the vertices are connected and a Minimum Spanning Tree
(MST) is obtained.
Step 1:Create a forest in such a way that each graph is a separate tree
Step 2: Create a priority queue Q that contains all the edges of the graph
Step 3: Repeat steps 4 and 5 while Q is NOT EMPTY
Step 4: Remove an edge from Q
Step 5: If the edge obtained in Step 4 connects two different trees, then Add it to
the forest(for combining two trees into one tree).
ELSE
Discard the edge
Step 6: END
Kruskal’s Algorithm
Note: Forest is a
collection of trees
SRM Institute of Science and Technology
Thumb Rule to Remember
The above steps may be reduced to the
following thumb rule-
•Simply draw all the vertices on the
paper.
•Connect these vertices using edges
with minimum weights such that no
cycle gets formed.
MST - Kruskal’s Algorithm
SRM Institute of Science and Technology
PRACTICE PROBLEMS BASED
ON KRUSKAL’S ALGORITHM-
Problem-01:
Construct the minimum spanning tree (MST) for the given graph using
Kruskal’s Algorithm-
Now we need to find the
weight of the minimum
spanning tree
Solution-
To construct MST using Kruskal’s Algorithm,
•Simply draw all the vertices on the paper.
•Connect these vertices using edges with minimum weights such that no cycle gets
formed.-
Step-01:
Draw all the vertices on the
paper as you can see in the Fig
No of edges = 9
SRM Institute of Science and Technology
Write all vertex pair
VERTEX PAIR WEIGHT
(1,6) 10
(1,2) 28
(2,3) 16
(2,7) 14
(3,4) 12
(4,5) 22
(4,7) 18
(5,6) 25
(5,7) 24
SRM Institute of Science and Technology
Vertex pair in ascending order
VERTEX PAIR WEIGHT
(1,6) 10
(3,4) 12
(2,7) 14
(2,3) 16
(4,7) 18
(4,5) 22
(5,7) 24
(5,6) 25
(1,2) 28
SRM Institute of Science and Technology
Step-02:
Step-03:
VERTEX PAIR WEIGHT ACTION
(1,6) 10 Accepte
d
(3,4) 12 Accepte
d
(2,7) 14
(2,3) 16
(4,7) 18
(4,5) 22
(5,7) 24
(5,6) 25
(1,2) 28
Accept the edge if it does not form cycle continue till
all nodes are visited
Mark 1 if visited the nodes
No cycle formation
No cycle formation
VERTEX KNOWN
1 1
2 0
3 1
4 1
5 0
6 1
7 0
SRM Institute of Science and Technology
Step-04:
Step-05:
VERTEX PAIR WEIGHT ACTION
(1,6) 10 Accepted
(3,4) 12 Accepted
(2,7) 14 Accepted
(2,3) 16 Accepted
(4,7) 18 Rejected
(4,5) 22
(5,7) 24
(5,6) 25
(1,2) 28
Accept the edge if it does not form cycle
continue till all nodes are visited
VERTEX KNOWN
1 1
2 1
3 1
4 1
5 0
6 1
7 1
Mark 1 if visited the nodes
No cycle
formation
SRM Institute of Science and Technology
Step-06:
Step-07:
Accept the edge if it does not form cycle
continue till all nodes are visited
Since all the vertices have been
connected / included in the MST, so we
stop.
Weight of the MST = Sum of all edge
weights
= 10 + 25 + 22 + 12 + 16 + 14
VERTEX PAIR WEIGHT ACTION
(1,6) 10 Accepted
(3,4) 12 Accepted
(2,7) 14 Accepted
(2,3) 16 Accepted
(4,7) 18 Rejected
(4,5) 22 Accepted
(5,7) 24 Rejected
(5,6) 25 Accepted
(1,2) 28
VERTEX KNOWN
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Mark 1 if visited the nodes
All nodes visited.
Stop the Algorithm
No cycle formation
No cycle formation
SRM Institute of Science and Technology
Problem 2:
Construct the minimum spanning tree (MST) for the given graph using Kruskal’s
Algorithm
Now we need to find the
weight of the minimum
spanning tree
SRM Institute of Science and Technology
V1
V2
V3 V4
V6
V5
Now Let us draw the minimum Spanning Tree using Kruskal’s Algorithm
Draw all the vertices on the
paper as you can see in the Fig
Step 1:
SRM Institute of Science and Technology
Step 2 Step 3
• We shall connect the vertices using
the edges with the least weight
• Now, the least weight edge is 3.So the
vertices V1 and V2 are connected
• Next the least weight edge is 4.So the
vertices V1 and V3 are connected.
No cycle formation.
So accepted. No cycle formation.
So accepted.
SRM Institute of Science and Technology
Step 4
Cycle is not
allowed. So
rejected.
Step 5
• Next least weight edge is 5 which is
connecting V3 and V2.But if we include
this edge, it will contain a cycle which is
not allowed.
• Next the least weight edge is
6.So the vertices V2 and V4 are
connected.
No cycle
formation.
So accepted.
SRM Institute of Science and Technology
Step 6 Step 7
Cycle is not allowed.
So rejected.
• Next least weight edge is 7 which is
connecting V3 and V4.But if we include
this edge, it will contain a cycle which is
not allowed.
No cycle
formation.
So accepted.
• Next the least weight edge is
8.So the vertices V3 and V5 are
connected.
SRM Institute of Science and Technology
Step 8 Step 9
• Next least weight edge is 9 which is
connecting V4 and V5.But if we include
this edge, it will contain a cycle which is
not allowed.
• Next the least weight edge is
10. So the vertices V4 and V6
are connected.
Cycle is not allowed.
So rejected.
No cycle
formation.
So accepted.
SRM Institute of Science and Technology
Step 10 Cycle is not allowed.
So rejected.
• Next least weight edge is 11 which is
connecting V5 and V6.But if we include
this edge, it will contain a cycle which is
not allowed.
Since all the vertices have been connected
/ included in the MST, so we stop.
Weight of the MST = Sum of all edge
weights
= 3 + 4 + 6 + 8 + 10
= 31 units
SRM Institute of Science and Technology
NETWORK FLOW PROBLEM – FORD
FULKERSON ALGORITHM
SRM Institute of Science and Technology
SRM Institute of Science and Technology
Source
S
A B
C D
T
Sink
flow = 0
0/4
0/9
Flow
0/2 0/8 0/6
Initially flow = 0
Source
S
A B
C D
T
Sink
flow = 8
0/4
0/9
Flow
0/2 0/8 0/6
Initially flow = 0
AUGMENTING PATHS
BOTTLE NECK
CAPCITY
S A D T 8
8
8
8
STEP: 1
Minimum capacity
Source
S
A B
C D
T
Sink
flow = 8+2=10
0/4
0/9
Flow
0/2 8/8 0/6
Initially flow = 0
AUGMENTING PATHS
BOTTLE NECK
CAPCITY
2
STEP: 2
Residual capacity
= 10 - 8 =2
2
2
8+2 =10
S C D T
Source
S
A B
C D
T
Sink
flow = 10+4=14
0/4
2/9
Flow
0/2 8/8 0/6
AUGMENTING PATHS BOTTLE NECK CAPCITY
S C D A B T 4
STEP: 3
R capacity = 4
2+4=6
2+4=6
8-4=4
0+4=4
0+4=4
Each and every vertex other than
source and sink should have same
In-flow and Out-flow
Source
S
A B
C D
T
Sink
flow = 14+2=16
4/4
6/9
Flow
0/2 4/8 0/6
AUGMENTING PATHS
BOTTLE NECK
CAPCITY
S A D B T 2
STEP: 4
Same In-flow and Out-flow has to
be maintained
R capacity = 10-8=2
8+2=10
4+2=6 0+2=2
4+2=6
Source
S
A B
C D
T
Sink
flow = 16+3=19
4/4
6/9
Flow
0/2 6/8 2/6
AUGMENTING PATHS
BOTTLE NECK
CAPCITY
S C D B T 3
STEP: 5
R capacity = 9-6=3
6+3=9
6+3=9
2+3=5
6+3=9
Source
S
A B
C D
T
Sink
flow = 16+3=19
4/4
9/9
Flow
0/2 6/8 5/6
AUGMENTING PATHS
BOTTLE NECK
CAPCITY
S C D B T 3
STEP: 6
R capacity = 9-6=3
SRM Institute of Science and Technology
SORTEST PATH ALGORITHM
SRM Institute of Science and Technology
•Shortest path problem is a problem of finding the shortest path(s) between vertices of a
given graph.
•Shortest path between two vertices is a path that has the least cost as compared to all other
existing paths.
Shortest Path Problem-
Shortest Path Algorithms-
Shortest path algorithms are a family of algorithms used for solving the shortest path
problem.
Applications-
•Google Maps
•Road Networks
•Logistics Research
SRM Institute of Science and Technology
Types of Shortest Path Problem-
Various types of shortest path problem are-
Shortest Path Problems
Single Pair Shortest Path Problems
Single Source Shortest Path Problems
Single destination Shortest Path Problems
All Pairs Shortest Path Problems
1.Single-pair shortest path problem
2.Single-source shortest path problem
3.Single-destination shortest path problem
4.All pairs shortest path problem
SRM Institute of Science and Technology
Single-Pair Shortest Path Problem-
•It is a shortest path problem where the shortest path between a given pair of vertices is
computed.
•A* Search Algorithm is a famous algorithm used for solving single-pair shortest path
problem.
Single-Source Shortest Path Problem-
•It is a shortest path problem where the shortest path from a given source vertex to all other
remaining vertices is computed.
•Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used for
solving single-source shortest path problem.
SRM Institute of Science and Technology
Single-Destination Shortest Path Problem-
•It is a shortest path problem where the shortest path from all the vertices to a single
destination vertex is computed.
•By reversing the direction of each edge in the graph, this problem reduces to single-source
shortest path problem.
•Dijkstra’s Algorithm is a famous algorithm adapted for solving single-destination shortest
path problem.
All Pairs Shortest Path Problem-
•It is a shortest path problem where the shortest path between every pair of vertices is
computed.
•Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous algorithms used for
solving All pairs shortest path problem.
Ad

Recommended

Np cooks theorem
Np cooks theorem
Narayana Galla
 
Graph coloring problem
Graph coloring problem
V.V.Vanniaperumal College for Women
 
Data mining :Concepts and Techniques Chapter 2, data
Data mining :Concepts and Techniques Chapter 2, data
Salah Amean
 
Computer architecture data representation
Computer architecture data representation
Anil Pokhrel
 
Flat unit 3
Flat unit 3
VenkataRaoS1
 
Stressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 
Fractional knapsack class 13
Fractional knapsack class 13
Kumar
 
Introduction to data structures and Algorithm
Introduction to data structures and Algorithm
Dhaval Kaneria
 
Homophonic Substitution Cipher
Homophonic Substitution Cipher
SHUBHA CHATURVEDI
 
Primitive Recursive Functions
Primitive Recursive Functions
Radhakrishnan Chinnusamy
 
Regular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Minimum Spanning Tree
Minimum Spanning Tree
zhaokatherine
 
Validation based protocol
Validation based protocol
BBDITM LUCKNOW
 
Decision Tree - ID3
Decision Tree - ID3
Xueping Peng
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets
Sampath Kumar S
 
Find Transitive closure of a Graph Using Warshall's Algorithm
Find Transitive closure of a Graph Using Warshall's Algorithm
Safayet Hossain
 
Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
Bayes Theorem
Bayes Theorem
sabareeshbabu
 
Classification and regression trees (cart)
Classification and regression trees (cart)
Learnbay Datascience
 
Turing Machine
Turing Machine
Rajendran
 
Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
classall
 
Pumping lemma for cfl
Pumping lemma for cfl
Muhammad Zohaib Chaudhary
 
Automata theory -RE to NFA-ε
Automata theory -RE to NFA-ε
Akila Krishnamoorthy
 
First Order Logic resolution
First Order Logic resolution
Amar Jukuntla
 
Working principle of Turing machine
Working principle of Turing machine
Karan Thakkar
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
Fenil Shah
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
Radhakrishnan Chinnusamy
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
SintooChauhan6
 

More Related Content

What's hot (20)

Homophonic Substitution Cipher
Homophonic Substitution Cipher
SHUBHA CHATURVEDI
 
Primitive Recursive Functions
Primitive Recursive Functions
Radhakrishnan Chinnusamy
 
Regular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Minimum Spanning Tree
Minimum Spanning Tree
zhaokatherine
 
Validation based protocol
Validation based protocol
BBDITM LUCKNOW
 
Decision Tree - ID3
Decision Tree - ID3
Xueping Peng
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets
Sampath Kumar S
 
Find Transitive closure of a Graph Using Warshall's Algorithm
Find Transitive closure of a Graph Using Warshall's Algorithm
Safayet Hossain
 
Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
Bayes Theorem
Bayes Theorem
sabareeshbabu
 
Classification and regression trees (cart)
Classification and regression trees (cart)
Learnbay Datascience
 
Turing Machine
Turing Machine
Rajendran
 
Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
classall
 
Pumping lemma for cfl
Pumping lemma for cfl
Muhammad Zohaib Chaudhary
 
Automata theory -RE to NFA-ε
Automata theory -RE to NFA-ε
Akila Krishnamoorthy
 
First Order Logic resolution
First Order Logic resolution
Amar Jukuntla
 
Working principle of Turing machine
Working principle of Turing machine
Karan Thakkar
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
Fenil Shah
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
Radhakrishnan Chinnusamy
 
Homophonic Substitution Cipher
Homophonic Substitution Cipher
SHUBHA CHATURVEDI
 
Regular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Minimum Spanning Tree
Minimum Spanning Tree
zhaokatherine
 
Validation based protocol
Validation based protocol
BBDITM LUCKNOW
 
Decision Tree - ID3
Decision Tree - ID3
Xueping Peng
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets
Sampath Kumar S
 
Find Transitive closure of a Graph Using Warshall's Algorithm
Find Transitive closure of a Graph Using Warshall's Algorithm
Safayet Hossain
 
Classification and regression trees (cart)
Classification and regression trees (cart)
Learnbay Datascience
 
Turing Machine
Turing Machine
Rajendran
 
Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
classall
 
First Order Logic resolution
First Order Logic resolution
Amar Jukuntla
 
Working principle of Turing machine
Working principle of Turing machine
Karan Thakkar
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
Fenil Shah
 

Similar to DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM (20)

APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
SintooChauhan6
 
Lec33
Lec33
Nikhil Chilwant
 
Greedy technique - Algorithm design techniques using data structures
Greedy technique - Algorithm design techniques using data structures
divyammo
 
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
MinSpanningTreespresantion.ppt
MinSpanningTreespresantion.ppt
baciprek
 
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
SatyamMishra828076
 
Minimum cost spanning tree for design and analysis
Minimum cost spanning tree for design and analysis
IamRiju2
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
himanshumishra19dec
 
prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
8_MST_pptx.pptx
8_MST_pptx.pptx
JhonCarloJacinto
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
minimum spanning tree
minimum spanning tree
Melaku Bayih Demessie
 
Comprehensive Study on Dijkstra and Prim Algorithms
Comprehensive Study on Dijkstra and Prim Algorithms
minahilsamreen138
 
Discrete Mathematics ( Dijkstra Algorithm & Prims Algorithm)
Discrete Mathematics ( Dijkstra Algorithm & Prims Algorithm)
SeeratFatima370803
 
Introduction to Greedy method, 0/1 Knapsack problem
Introduction to Greedy method, 0/1 Knapsack problem
DrSMeenakshiSundaram1
 
Minimum Spanning Tree in design and analysis
Minimum Spanning Tree in design and analysis
2022imscs011
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
KUSHDHIRRA2111026030
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
SintooChauhan6
 
Greedy technique - Algorithm design techniques using data structures
Greedy technique - Algorithm design techniques using data structures
divyammo
 
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
uva-201026072839.pptxvcvczcvzvcxbxcvbcxvbvcxbcx
avishekpradhan24
 
MinSpanningTreespresantion.ppt
MinSpanningTreespresantion.ppt
baciprek
 
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
_A C program for Prim's Minimum Spanning Tree (MST) algorithm. The program is...
SatyamMishra828076
 
Minimum cost spanning tree for design and analysis
Minimum cost spanning tree for design and analysis
IamRiju2
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
himanshumishra19dec
 
prim's and kruskal's algorithm
prim's and kruskal's algorithm
shreeuva
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
Sahil Kumar
 
Comprehensive Study on Dijkstra and Prim Algorithms
Comprehensive Study on Dijkstra and Prim Algorithms
minahilsamreen138
 
Discrete Mathematics ( Dijkstra Algorithm & Prims Algorithm)
Discrete Mathematics ( Dijkstra Algorithm & Prims Algorithm)
SeeratFatima370803
 
Introduction to Greedy method, 0/1 Knapsack problem
Introduction to Greedy method, 0/1 Knapsack problem
DrSMeenakshiSundaram1
 
Minimum Spanning Tree in design and analysis
Minimum Spanning Tree in design and analysis
2022imscs011
 
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
01-05-2023, SOL_DU_MBAFT_6202_Dijkstra’s Algorithm Dated 1st May 23.pdf
DKTaxation
 
Greedy Approach in Design Analysis and Algorithms
Greedy Approach in Design Analysis and Algorithms
NikunjGoyal20
 
Ad

Recently uploaded (20)

DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Ad

DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM

  • 1. SRM Institute of Science and Technology Minimum Spanning Tree - Kruskal’s Algorithm
  • 2. SRM Institute of Science and Technology A Spanning tree can be defined as a subset of a graph, which consists of all the vertices covering minimum possible edges and does not have a cycle. Spanning tree cannot be disconnected. Every connected and undirected graph has at least one spanning tree. A disconnected graph does not have a spanning tree as it is not possible to include all vertices.
  • 3. NN-2 number of spanning trees. Thus in the above graph N =3, therefore, it has 3(3-2) = 3 spanning trees.
  • 4. SRM Institute of Science and Technology Some of the properties of the spanning tree are listed below: •A connected graph can have more than one spanning trees. •All spanning trees in a graph have the same number of nodes and edges. •If we remove one edge from the spanning tree, then it will become minimally connected and will make the graph disconnected. •On the other hand, adding one edge to the spanning tree will make it maximally acyclic thereby creating a loop. •A spanning tree does not have a loop or a cycle.
  • 5. SRM Institute of Science and Technology What Is A Minimum Spanning Tree (MST) A minimum spanning tree is the one that contains the least weight among all the other spanning trees of a connected weighted graph. There can be more than one minimum spanning tree for a graph. •Kruskal’s algorithm •Prim’s algorithm
  • 6. SRM Institute of Science and Technology
  • 7. Minimum Spanning Tree - Kruskal’s Algorithm SRM Institute of Science and Technology •Kruskal’s Algorithm is a famous greedy algorithm. •It is used for finding the Minimum Spanning Tree (MST) of a given graph. •To apply Kruskal’s algorithm, the given graph must be weighted, connected and undirected. Kruskal’s Algorithm Implementation- The implementation of Kruskal’s Algorithm is explained in the following steps- Step-01: •Sort all the edges from low weight to high weight. Step-02: •Take the edge with the lowest weight and use it to connect the vertices of graph. •If adding an edge creates a cycle, then reject that edge and go for the next least weight edge.
  • 8. SRM Institute of Science and Technology Step-03: •Keep adding edges until all the vertices are connected and a Minimum Spanning Tree (MST) is obtained. Step 1:Create a forest in such a way that each graph is a separate tree Step 2: Create a priority queue Q that contains all the edges of the graph Step 3: Repeat steps 4 and 5 while Q is NOT EMPTY Step 4: Remove an edge from Q Step 5: If the edge obtained in Step 4 connects two different trees, then Add it to the forest(for combining two trees into one tree). ELSE Discard the edge Step 6: END Kruskal’s Algorithm Note: Forest is a collection of trees
  • 9. SRM Institute of Science and Technology Thumb Rule to Remember The above steps may be reduced to the following thumb rule- •Simply draw all the vertices on the paper. •Connect these vertices using edges with minimum weights such that no cycle gets formed. MST - Kruskal’s Algorithm
  • 10. SRM Institute of Science and Technology PRACTICE PROBLEMS BASED ON KRUSKAL’S ALGORITHM- Problem-01: Construct the minimum spanning tree (MST) for the given graph using Kruskal’s Algorithm- Now we need to find the weight of the minimum spanning tree
  • 11. Solution- To construct MST using Kruskal’s Algorithm, •Simply draw all the vertices on the paper. •Connect these vertices using edges with minimum weights such that no cycle gets formed.- Step-01: Draw all the vertices on the paper as you can see in the Fig No of edges = 9
  • 12. SRM Institute of Science and Technology Write all vertex pair VERTEX PAIR WEIGHT (1,6) 10 (1,2) 28 (2,3) 16 (2,7) 14 (3,4) 12 (4,5) 22 (4,7) 18 (5,6) 25 (5,7) 24
  • 13. SRM Institute of Science and Technology Vertex pair in ascending order VERTEX PAIR WEIGHT (1,6) 10 (3,4) 12 (2,7) 14 (2,3) 16 (4,7) 18 (4,5) 22 (5,7) 24 (5,6) 25 (1,2) 28
  • 14. SRM Institute of Science and Technology Step-02: Step-03: VERTEX PAIR WEIGHT ACTION (1,6) 10 Accepte d (3,4) 12 Accepte d (2,7) 14 (2,3) 16 (4,7) 18 (4,5) 22 (5,7) 24 (5,6) 25 (1,2) 28 Accept the edge if it does not form cycle continue till all nodes are visited Mark 1 if visited the nodes No cycle formation No cycle formation VERTEX KNOWN 1 1 2 0 3 1 4 1 5 0 6 1 7 0
  • 15. SRM Institute of Science and Technology Step-04: Step-05: VERTEX PAIR WEIGHT ACTION (1,6) 10 Accepted (3,4) 12 Accepted (2,7) 14 Accepted (2,3) 16 Accepted (4,7) 18 Rejected (4,5) 22 (5,7) 24 (5,6) 25 (1,2) 28 Accept the edge if it does not form cycle continue till all nodes are visited VERTEX KNOWN 1 1 2 1 3 1 4 1 5 0 6 1 7 1 Mark 1 if visited the nodes No cycle formation
  • 16. SRM Institute of Science and Technology Step-06: Step-07: Accept the edge if it does not form cycle continue till all nodes are visited Since all the vertices have been connected / included in the MST, so we stop. Weight of the MST = Sum of all edge weights = 10 + 25 + 22 + 12 + 16 + 14 VERTEX PAIR WEIGHT ACTION (1,6) 10 Accepted (3,4) 12 Accepted (2,7) 14 Accepted (2,3) 16 Accepted (4,7) 18 Rejected (4,5) 22 Accepted (5,7) 24 Rejected (5,6) 25 Accepted (1,2) 28 VERTEX KNOWN 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Mark 1 if visited the nodes All nodes visited. Stop the Algorithm No cycle formation No cycle formation
  • 17. SRM Institute of Science and Technology Problem 2: Construct the minimum spanning tree (MST) for the given graph using Kruskal’s Algorithm Now we need to find the weight of the minimum spanning tree
  • 18. SRM Institute of Science and Technology V1 V2 V3 V4 V6 V5 Now Let us draw the minimum Spanning Tree using Kruskal’s Algorithm Draw all the vertices on the paper as you can see in the Fig Step 1:
  • 19. SRM Institute of Science and Technology Step 2 Step 3 • We shall connect the vertices using the edges with the least weight • Now, the least weight edge is 3.So the vertices V1 and V2 are connected • Next the least weight edge is 4.So the vertices V1 and V3 are connected. No cycle formation. So accepted. No cycle formation. So accepted.
  • 20. SRM Institute of Science and Technology Step 4 Cycle is not allowed. So rejected. Step 5 • Next least weight edge is 5 which is connecting V3 and V2.But if we include this edge, it will contain a cycle which is not allowed. • Next the least weight edge is 6.So the vertices V2 and V4 are connected. No cycle formation. So accepted.
  • 21. SRM Institute of Science and Technology Step 6 Step 7 Cycle is not allowed. So rejected. • Next least weight edge is 7 which is connecting V3 and V4.But if we include this edge, it will contain a cycle which is not allowed. No cycle formation. So accepted. • Next the least weight edge is 8.So the vertices V3 and V5 are connected.
  • 22. SRM Institute of Science and Technology Step 8 Step 9 • Next least weight edge is 9 which is connecting V4 and V5.But if we include this edge, it will contain a cycle which is not allowed. • Next the least weight edge is 10. So the vertices V4 and V6 are connected. Cycle is not allowed. So rejected. No cycle formation. So accepted.
  • 23. SRM Institute of Science and Technology Step 10 Cycle is not allowed. So rejected. • Next least weight edge is 11 which is connecting V5 and V6.But if we include this edge, it will contain a cycle which is not allowed. Since all the vertices have been connected / included in the MST, so we stop. Weight of the MST = Sum of all edge weights = 3 + 4 + 6 + 8 + 10 = 31 units
  • 24. SRM Institute of Science and Technology NETWORK FLOW PROBLEM – FORD FULKERSON ALGORITHM
  • 25. SRM Institute of Science and Technology
  • 26. SRM Institute of Science and Technology
  • 27. Source S A B C D T Sink flow = 0 0/4 0/9 Flow 0/2 0/8 0/6 Initially flow = 0
  • 28. Source S A B C D T Sink flow = 8 0/4 0/9 Flow 0/2 0/8 0/6 Initially flow = 0 AUGMENTING PATHS BOTTLE NECK CAPCITY S A D T 8 8 8 8 STEP: 1 Minimum capacity
  • 29. Source S A B C D T Sink flow = 8+2=10 0/4 0/9 Flow 0/2 8/8 0/6 Initially flow = 0 AUGMENTING PATHS BOTTLE NECK CAPCITY 2 STEP: 2 Residual capacity = 10 - 8 =2 2 2 8+2 =10 S C D T
  • 30. Source S A B C D T Sink flow = 10+4=14 0/4 2/9 Flow 0/2 8/8 0/6 AUGMENTING PATHS BOTTLE NECK CAPCITY S C D A B T 4 STEP: 3 R capacity = 4 2+4=6 2+4=6 8-4=4 0+4=4 0+4=4 Each and every vertex other than source and sink should have same In-flow and Out-flow
  • 31. Source S A B C D T Sink flow = 14+2=16 4/4 6/9 Flow 0/2 4/8 0/6 AUGMENTING PATHS BOTTLE NECK CAPCITY S A D B T 2 STEP: 4 Same In-flow and Out-flow has to be maintained R capacity = 10-8=2 8+2=10 4+2=6 0+2=2 4+2=6
  • 32. Source S A B C D T Sink flow = 16+3=19 4/4 6/9 Flow 0/2 6/8 2/6 AUGMENTING PATHS BOTTLE NECK CAPCITY S C D B T 3 STEP: 5 R capacity = 9-6=3 6+3=9 6+3=9 2+3=5 6+3=9
  • 33. Source S A B C D T Sink flow = 16+3=19 4/4 9/9 Flow 0/2 6/8 5/6 AUGMENTING PATHS BOTTLE NECK CAPCITY S C D B T 3 STEP: 6 R capacity = 9-6=3
  • 34. SRM Institute of Science and Technology SORTEST PATH ALGORITHM
  • 35. SRM Institute of Science and Technology •Shortest path problem is a problem of finding the shortest path(s) between vertices of a given graph. •Shortest path between two vertices is a path that has the least cost as compared to all other existing paths. Shortest Path Problem- Shortest Path Algorithms- Shortest path algorithms are a family of algorithms used for solving the shortest path problem. Applications- •Google Maps •Road Networks •Logistics Research
  • 36. SRM Institute of Science and Technology Types of Shortest Path Problem- Various types of shortest path problem are- Shortest Path Problems Single Pair Shortest Path Problems Single Source Shortest Path Problems Single destination Shortest Path Problems All Pairs Shortest Path Problems 1.Single-pair shortest path problem 2.Single-source shortest path problem 3.Single-destination shortest path problem 4.All pairs shortest path problem
  • 37. SRM Institute of Science and Technology Single-Pair Shortest Path Problem- •It is a shortest path problem where the shortest path between a given pair of vertices is computed. •A* Search Algorithm is a famous algorithm used for solving single-pair shortest path problem. Single-Source Shortest Path Problem- •It is a shortest path problem where the shortest path from a given source vertex to all other remaining vertices is computed. •Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used for solving single-source shortest path problem.
  • 38. SRM Institute of Science and Technology Single-Destination Shortest Path Problem- •It is a shortest path problem where the shortest path from all the vertices to a single destination vertex is computed. •By reversing the direction of each edge in the graph, this problem reduces to single-source shortest path problem. •Dijkstra’s Algorithm is a famous algorithm adapted for solving single-destination shortest path problem. All Pairs Shortest Path Problem- •It is a shortest path problem where the shortest path between every pair of vertices is computed. •Floyd-Warshall Algorithm and Johnson’s Algorithm are the famous algorithms used for solving All pairs shortest path problem.