Professional Documents
Culture Documents
Kruskal Print
Kruskal Print
On
SUBMITTED BY
1. INTRODUCTION 2
3. SPANNING TREE 3
5. KRUSKAL’S ALGORITHM 7
5.1. WORKING 7
5.2.EXAMPLE OF THE ALGORITHM 8
5.3.MST COST 11
5.4.TIME COMPLEXITY OF THE ALGORITHM 11
5.5.CODE IMPLEMENTATION 12
6. CONCLUSION 16
REFERENCE 17
1
1. INTRODUCTION
TYPES OF TREE:
• BINARY TREE
• BINARY SEARCH TREE
• AVL TREE
• B-TREE
FIG 1: A TREE
2
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) .
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
FIG 2: A GRAPH
3
Some of the possible spanning trees that can be created from the above graph are:
4
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.
Let's understand the minimum spanning tree with the help of an example.
5
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 –
6
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.
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 -
7
Example of Kruskal Algorithm:
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
8
Now, let's start constructing the minimum spanning tree.
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.
9
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 –
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 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
11
Code Implementation:
12
FIG 14: CODE PART 2
13
FIG 15: CODE PART 3
14
FIG 16: CODE PART 4
Output:
15
6. CONCLUSION
In this report, we have focused on the algorithm of the shortest path location. It draws
the idea of the Spanning Tree and Minimum Spanning Tree algorithm (MST) and 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.
16
REFERENCES
[1] Paryati, & Salahddine, Krit & Salah-ddine, Krit. (2021). THE IMPLEMENTATION OF
KRUSKAL'S ALGORITHM FOR MINIMUM SPANNING TREE IN A GRAPH.
[2] https://www.programiz.com/dsa/trees
[3] https://www.javatpoint.com/kruskal-algorithm
[4] https://www.simplilearn.com/tutorials/data-structure-tutorial/kruskal-
algorithm
[5] https://www.geeksforgeeks.org/properties-of-minimum-spanning-tree-mst/
17