DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
Kruskal's algorithm is used to find the minimum spanning tree (MST) of a connected, undirected graph. It works by sorting the edges by weight and building the MST by adding edges one by one if they do not form cycles. The MST has the minimum total weight among all spanning trees of the graph. Ford-Fulkerson algorithm finds the maximum flow in a flow network and uses augmenting paths to incrementally increase the flow until no more augmenting paths exist. Dijkstra's algorithm solves the single-source shortest path problem to find the shortest paths from a source vertex to all other vertices in a weighted graph.
DATA STRUCTURE AND ALGORITHM LMS MST KRUSKAL'S ALGORITHM
1.
SRM Institute ofScience and Technology
Minimum Spanning Tree -
Kruskal’s Algorithm
2.
SRM Institute ofScience 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 ofspanning trees. Thus in the above graph N =3, therefore, it
has 3(3-2) = 3 spanning trees.
4.
SRM Institute ofScience 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 ofScience 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
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 ofScience 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 ofScience 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 ofScience 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 MSTusing 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience 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 ofScience and Technology
NETWORK FLOW PROBLEM – FORD
FULKERSON ALGORITHM
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
SRM Institute ofScience 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 ofScience 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 ofScience 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 ofScience 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.