Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

PROJECT REPORT ON THE TOPIC

KRUSKAL’S ALGORITHM - MINIMUM SPANNING TREE


COURSE: DATA STRUCTURE AND ALGORITHM

SUBMITTED TO

ASSAM DON BOSCO UNIVERSITY


SUBMITTED BY

MANSI SINGH (DC2022MCA0001)

SHIULI SINHA (DC2022MCA0010)

ABHISHEK MANDAL (DC2022MCA0011)

DIPLU KINGKAR DAS (DC2022MCA0012)

FIRST SEMESTER
OF
MASTERS OF COMPUTER APPLICATION(MCA)

DEPARTMENT OF COMPUTER APPLICATIONS


SCHOOL OF TECHNOLOGY
ASSAM DON BOSCO UNIVERSITY
AZARA, GUWAHATI 781 017,
ASSAM, INDIA.
BATCH (2022-2024)
ACKNOWLEDGEMENT

We are extremely grateful to our supervisor Dr. Alok Choudhury, Assistant


Professor, Department of Computer Application, Assam Don Bosco University,
Guwahati for his valuable advice, encouragement, endless support, and
guidance with patience during this project work and write up this project report.

We would also like to express our sincere gratitude to the all the faculty
members, office staff members of the Department of Computer Application,
Assam Don Bosco University, Guwahati, for their help and moral support in
various forms during this project work.

MANSI SINGH (DC2022MCA0001)

SHIULI SINHA (DC2022MCA0010)

ABHISHEK MANDAL (DC2022MCA0011)

DIPLU KINGKAR DAS (DC2022MCA0012)

1
TABLE OF CONTENTS PAGE NO.

1. INTRODUCTION 3

2. TREE DATA STRUCTURE 3

3. SPANNING TREE 4

4. MINIMUM SPANING TREE 6

5. KRUSKAL’S ALGORITHM 8

5.1. WORKING 8
5.2.EXAMPLE OF THE ALGORITHM 9
5.3.MST COST 12
5.4.TIME COMPLEXITY OF THE ALGORITHM 12
5.5.CODE IMPLEMENTATION 13

6. CONCLUSION 16

7. REFERENCE 16

2
1. INTRODUCTION
Kruskal's Algorithm is an algorithm used to find the minimum spanning tree in
graphical connectivity that provides the option to continue processing the least-
weighted margins. In the Kruskal algorithm, ordering the weight of the ribs
makes it easy to find the shortest path. This algorithm is independent in nature
which will facilitate and improve path creation

2. TREE DATA STRUCTURE

A tree is a nonlinear hierarchical data structure that consists of nodes connected by


edges.

Other data structures such as arrays, linked list, stack, and queue are linear data
structures that store data sequentially. In order to perform any operation in a
linear data structure, the time complexity increases with the increase in the data
size. But, it is not acceptable in today's computational world.
Different tree data structures allow quicker and easier access to the data as it is
a non-linear data structure.

TYPES OF TREE:

• BINARY TREE
• BINARY SEARCH TREE
• AVL TREE
• B-TREE

FIG: A TREE

3
3. SPANNING TREE

A spanning tree is a sub-graph of an undirected connected graph, which includes


all the vertices of the graph with a minimum possible number of edges. If a
vertex is missed, then it is not a spanning tree.The edges may or may not have
weights assigned to them.

The total number of spanning trees with n vertices that can be created from a
complete graph is equal to n(n-2) .

If we have n = 4, the maximum number of possible spanning trees is equal to


44-2 = 16. Thus, 16 spanning trees can be formed from a complete graph with 4
vertices.

Basically, a spanning tree is used to find a minimum path to connect all nodes of
the graph. Some of the common applications of the spanning tree are listed as
follows -
• Cluster Analysis
• Civil network planning
• Computer network routing protocol

Let's understand the spanning tree with examples below:

Let the original graph be:

4
FIG: A GRAPH
Some of the possible spanning trees that can be created from the above graph
are:

FIG: FEW POSSIBLE SPANNING TREES OF THE ABOVE GRAPH

5
4. MINIMUM SPANNING TREE (MST)
For a connected and undirected graph, a spanning tree of that graph is a
subgraph that is a tree and connects all the vertices together. A single graph can
have multiple spanning trees. A Minimum Spanning Tree(MST) or minimum
weight spanning tree for a weighted, connected, undirected graph is a spanning
tree having a weight less than or equal to the weight of every other possible
spanning tree. The weight of a spanning tree is the sum of weights given to each
edge of the spanning tree.

Necessary conditions for Minimum Spanning Tree:

• It must not form a cycle i.e, no edge is traversed twice.


• There must be no other spanning tree with lesser weight.

Example of minimum spanning tree:

Let's understand the minimum spanning tree with the help of an example.

FIG: A GRAPH

6
The sum of the edges of the above graph is 16.
Now, some of the possible spanning trees created from the above graph are –

So, the minimum spanning tree that is selected from the above spanning
trees for the given weighted graph is –

7
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. It follows the greedy approach
that finds an optimum solution at every stage instead of focusing on a global optimum.

Working of the Algorithm:

In Kruskal's algorithm, we start from edges with the lowest weight and keep
adding the edges until the goal is reached. The steps to implement Kruskal's
algorithm are listed as follows -

• First, sort all the edges from low weight to high.


• Now, take the edge with the lowest weight and add it to the spanning
tree. If the edge to be added creates a cycle, then reject the edge.
• Continue to add the edges until we reach all vertices, and a minimum
spanning tree is created.

The applications of Kruskal's algorithm are -

• Kruskal's algorithm can be used to layout electrical wiring among cities.


• It can be used to lay down LAN connections

Steps for finding MST using Kruskal's Algorithm

1. Arrange the edge of G in order of increasing weight.


2. Starting only with the vertices of G and proceeding sequentially add each
edge which does not result in a cycle, until (n - 1) edges are used.
3. EXIT.

8
Example of Kruskal Algorithm:

Now, let's see the working of Kruskal's algorithm using an example.

Suppose a weighted graph is –

The weight of the edges of the above graph is given in the below table –

Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2

Now, sort the edges given above in the ascending order of their weights.

Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10

9
Now, let's start constructing the minimum spanning tree.

Step 1 - First, add the edge AB with weight 1 to the MST.

Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.

Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle
or loop.

10
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the
cycle.

Step 5 - After that, pick the edge AE with weight 5. Including this edge will
create the cycle, so discard it.

Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle,
so discard it.

Step 7 - Pick the edge AD with weight 10. Including this edge will also create the
cycle, so discard it.

So, the final minimum spanning tree obtained from the given weighted graph
by using Kruskal's algorithm is –

11
Cost of the Minimum Spanning Tree:

The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.

Now, the number of edges in the above tree equals the number of vertices
minus 1. So, the algorithm stops here.

Algorithm:

Step 1: Create a forest F in such a way that every vertex of the graph is separate
tree.

Step 2: Create a set E that contains all the edges of the graph.

Step 3: Repeat step 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

Time complexity of Kruskal's algorithm:

The Time complexity of Kruskal's algorithm is

O(E logE) or O(V logV),

where E is the no. of edges,


V is the no. of vertices.

12
Code Implementation:

#include<bits/stdc++.h>
using namespace std;
class DSU{

int *parent;
int *rank;
public:
DSU(int n){

parent=new int[n];
rank=new int[n];

for(int i=0;i<n;i++)
{
parent[i]=i;
rank[i]=0;
}
}

int find(int node){

if(node==parent[node]) return node;

return parent[node]=find(parent[node]);
}

void Union(int u,int v){

u=find(u);

v=find(v);

if(u!=v)
{
if(rank[u]<rank[v])
{
int temp=u;

13
u=v;
v=temp;
}
parent[v]=u;

if(rank[u]==rank[v])
rank[u]++;
}
}
};
class Edge{
public:
int u,v,weight;
Edge(int U,int V,int Weight){
u=U;
v=V;
weight=Weight;
}
};
class Graph{
public:
int V, E;
vector<Edge> edges;
Graph(int v,int e){
V=v;
E=e;
}
static bool comparator(Edge e1, Edge e2)
{
return e1.weight < e2.weight;
}
void MST_Kruskal(){
int e=0,i=0,sum=0;
DSU dsu(V);
sort(edges.begin(), edges.end(), comparator);

while(e<V-1)
{
Edge edge=edges[i++];
int u=dsu.find(edge.u);

14
int v=dsu.find(edge.v);
if(u!=v)
{
cout<<"Adding edge "<<edge.u<<" <---> "<<edge.v<<" to MST\n";
sum+=edge.weight;
dsu.Union(u,v);
e++;
}
}
cout<<"MST has a weight of "<<sum;
}
void addEdge(int u,int v,int weight){
edges.push_back(Edge(u,v,weight));
}
};
int main(){
Graph g(6,9);
g.addEdge(0,1,7);
g.addEdge(0,2,8);
g.addEdge(1,2,3);
g.addEdge(1,4,6);
g.addEdge(2,3,3);
g.addEdge(2,4,4);
g.addEdge(3,4,2);
g.addEdge(3,5,2);
g.addEdge(4,5,2);
g.MST_Kruskal();
return 0;
}
Output:

15
CONCLUSION

In the report we have focused on the algorithm of the shortest path location. It
draws the idea of the minimum spanning tree algorithm(MST) how to construct
the minimal spanning tree for a given graph topology. Through the example
analysis, it is concluded that the selection path of Kruskal algorithm is more
accurate and stable and has the advantage of time.

REFERENCES
Paryati, & Salahddine, Krit & Salah-ddine, Krit. (2021). THE IMPLEMENTATION
OF KRUSKAL'S ALGORITHM FOR MINIMUM SPANNING TREE IN A GRAPH.

https://www.programiz.com/dsa/trees

https://www.javatpoint.com/kruskal-algorithm

https://www.simplilearn.com/tutorials/data-structure-tutorial/kruskal-
algorithm#conclusion

https://www.geeksforgeeks.org/properties-of-minimum-spanning-tree-mst/

16

You might also like