Kruskal's Spanning Tree Algorithm
Kruskal's Spanning Tree Algorithm
Algorithm
-Johnson
Kruskal's Spanning Tree Algorithm
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy
approach.
This algorithm treats the graph as a forest and every node it has as an individual
tree.
A tree connects to another only and only if, it has the least cost among all available
options and does not violate MST properties.
To understand Kruskal's algorithm let us consider the following example −
Remove all loops and parallel edges from the given graph.
In case of parallel edges, keep the one which has the least cost associated and remove all others.
The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them does not violate
spanning tree properties, so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. We add them
again −
Next cost in the table is 4, and we observe that adding it will create a circuit in the graph. −
We ignore it. In the process we shall ignore/avoid all edges that create a
circuit.
We observe that edges with cost 5 and 6 also create circuits. We ignore them and move on.
Now we are left with only one node to be added. Between the two least cost edges available 7 and 8,
we shall add the edge with cost 7.
By adding edge S,A we have included all the nodes of the graph and we now have minimum cost
spanning tree.
// C code to implement Kruskal's algorithm
#include <stdio.h>
// Function to find the parent of a node
#include <stdlib.h>
int findParent(int parent[], int component)
// Comparator function to use in sorting
{
int comparator(const void* p1, const void* p2)
if (parent[component] == component)
{
return component;
const int(*x)[3] = p1;
const int(*y)[3] = p2;
return parent[component]
= findParent(parent, parent[component]);
return (*x)[2] - (*y)[2];
}
}
// Initialization of parent[] and rank[] arrays
void makeSet(int parent[], int rank[], int n)
{
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
// Function to unite two sets
void unionSet(int u, int v, int parent[], int rank[], int n) // Function to find the MST
{ void kruskalAlgo(int n, int edge[n][3])
// Finding the parents {
u = findParent(parent, u); // First we sort the edge array in ascending order
v = findParent(parent, v); // so that we can access minimum distances/cost
if (rank[u] < rank[v]) { qsort(edge, n, sizeof(edge[0]), comparator);
parent[u] = v;
} int parent[n];
else if (rank[u] > rank[v]) { int rank[n];
parent[v] = u;
} // Function to initialize parent[] and rank[]
else { makeSet(parent, rank, n);
parent[v] = u;
// Since the rank increases if the ranks of two sets are same // To store the minimun cost
rank[u]++; int minCost = 0;
}
}
printf("Following are the edges in the constructed MST\n");
for (int i = 0; i < n; i++) { // Driver code
int v1 = findParent(parent, edge[i][0]); int main()
int v2 = findParent(parent, edge[i][1]); {
int wt = edge[i][2]; int edge[5][3] = { { 0, 1, 10 },{ 0, 2, 6 },{ 0, 3, 5 },{ 1, 3, 15 },
// If the parents are different that means they are in different { 2, 3, 4 } };
sets so union them
if (v1 != v2) { kruskalAlgo(5, edge);
unionSet(v1, v2, parent, rank, n); return 0;
minCost += wt; }
printf("%d -- %d == %d\n", edge[i]
[0],
edge[i][1], wt);
}
}
printf("Minimum Cost Spanning Tree: %d\n", minCost);
}
Time Complexity: O(E * logE) or O(E * logV)
•Sorting of edges takes O(E * logE) time.
•After sorting, we iterate through all edges and apply the find-union algorithm. The
find and union operations can take at most O(logV) time.
•So overall complexity is O(E * logE + E * logV) time.
•The value of E can be at most O(V2), so O(logV) and O(logE) are the same.
Therefore, the overall time complexity is O(E * logE) or O(E*logV)
Auxiliary Space: O(V + E), where V is the number of vertices and E is the number
of edges in the graph.
How to find MST using Kruskal’s algorithm?
Below are the steps for finding MST using Kruskal’s algorithm:
1.Sort all the edges in non-decreasing order of their weight.
2.Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so
far. If the cycle is not formed, include this edge. Else, discard it.
3.Repeat step#2 until there are (V-1) edges in the spanning tree.
How to find MST using Kruskal’s algorithm?
Below are the steps for finding MST using Kruskal’s algorithm:
1.Sort all the edges in non-decreasing order of their weight.
2.Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so
far. If the cycle is not formed, include this edge. Else, discard it.
3.Repeat step#2 until there are (V-1) edges in the spanning tree.
Thank You