Introduction to Kruskal’s Algorithm: Greedy
Algorithm
Here we will discuss Kruskal’s algorithm to find the MST of a given
weighted graph.
In Kruskal’s algorithm, sort all edges of the given graph in increasing order.
Then it keeps on adding new edges and nodes in the MST if the newly
added edge does not form a cycle. It picks the minimum weighted edge at
first and the maximum weighted edge at last. Thus we can say that it makes
a locally optimal choice in each step in order to find the optimal solution.
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.
Step 2 uses the Union-Find algorithm to detect cycles.
Before this, let us learn about disjoint set of data structures:
What is a Disjoint set data structure?
Two sets are called disjoint sets if they don’t have any element in common,
the intersection of sets is a null set.
A data structure that stores non overlapping or disjoint subset of elements is
called disjoint set data structure. The disjoint set data structure supports
following operations:
Adding new sets to the disjoint set.
Merging disjoint sets to a single disjoint set using Union operation.
Finding representative of a disjoint set using Find operation.
Check if two sets are disjoint or not.
Data Structures used are:
Array: An array of integers is called Parent[]. If we are dealing with N items,
i’th element of the array represents the i’th item. More precisely, the i’th
element of the Parent[] array is the parent of the i’th item. These
relationships create one or more virtual trees.
Tree: It is a Disjoint set. If two elements are in the same tree, then they are
in the same Disjoint set. The root node (or the topmost node) of each tree is
called the representative of the set. There is always a single unique
representative of each set. A simple rule to identify a representative is if ‘i’ is
the representative of a set, then Parent[i] = i. If i is not the representative of
his set, then it can be found by traveling up the tree until we find the
representative.
Operations on Disjoint Set Data Structures:
1. Find
2. Union
1. Find:
Can be implemented by recursively traversing the parent array until we hit a
node that is the parent of itself.
// Finds the representative of the set
// that i is an element of
#include<bits/stdc++.h>
using namespace std;
int find(int i)
// If i is the parent of itself
if (parent[i] == i) {
// Then i is the representative of
// this set
return i;
}
else {
// Else if i is not the parent of
// itself, then i is not the
// representative of his set. So we
// recursively call Find on its parent
return find(parent[i]);
}
}
//
Time complexity: This approach is inefficient and can take O(n) time in
worst case.
2. Union:
It takes two elements as input and finds the representatives of their sets
using the Find operation, and finally puts either one of the trees
(representing the set) under the root node of the other tree.
// Unites the set that includes i
// and the set that includes j
#include <bits/stdc++.h>
using namespace std;
void union(int i, int j) {
// Find the representatives
// (or the root nodes) for the set
// that includes i
int irep = this.Find(i),
// And do the same for the set
// that includes j
int jrep = this.Find(j);
// Make the parent of i’s representative
// be j’s representative effectively
// moving all of i’s set into j’s set)
this.Parent[irep] = jrep;
}
Time complexity: This approach is inefficient and could lead to tree of
length O(n) in worst case.