# Minimum Spanning Tree – MST using Kruskal’s Algo

Problem Statement: Given a weighted, undirected, and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree.

Definition: A minimum spanning tree consists of N nodes and N-1 edges connecting all the nodes which have the minimum cost(sum of edge weights).

Note: It is known as a tree since a tree doesn’t have cycles involved. A graph with N nodes and N-1 edges is equivalent to a tree.

Example:

Input:

Output:

Explanation: Given the following graph, the minimum spanning tree is the one given in the output. It consists of 5 nodes and 4 edges and this configuration gives us the minimum weight(cost = 16)

### Solution

Intuition: In Prim’s algorithm our idea was to get hold of the minimum edge weight possible. The idea remains the same. Could we somehow not use our adjacency list and try to incorporate what we learned in the Disjoint Set data structure? Intuitively there are primarily two things we need to keep in mind:

• We must always try to get the minimum edge weights
• The only constraint is that an edge must not be picked up such that it forms a cycle.

Can we address this constraint using the findParent function?

#### Approach:

• Sort all the edges according to their weight
• Greedily pick minimum edge and make sure the two nodes belong to different components(using disjoint set data structure findParent operation). Remember, if they belong to same component it would indicate we would be having cycles which is not possible in a MST.
• Also, once we a node to be a part of the MST, we must join the two components using the union operation of DSU.

Code:

## C++ Code

``````#include<bits/stdc++.h>
using namespace std;
struct node {
int u;
int v;
int wt;
node(int first, int second, int weight) {
u = first;
v = second;
wt = weight;
}
};

bool comp(node a, node b) {
return a.wt < b.wt;
}

int findPar(int u, vector<int> &parent) {
if(u == parent[u]) return u;
return parent[u] = findPar(parent[u], parent);
}

void unionn(int u, int v, vector<int> &parent, vector<int> &rank) {
u = findPar(u, parent);
v = findPar(v, parent);
if(rank[u] < rank[v]) {
parent[u] = v;
}
else if(rank[v] < rank[u]) {
parent[v] = u;
}
else {
parent[v] = u;
rank[u]++;
}
}
int main(){
int N=5,m=6;
vector<node> edges;
edges.push_back(node(0,1,2));
edges.push_back(node(0,3,6));
edges.push_back(node(1,0,2));
edges.push_back(node(1,2,3));
edges.push_back(node(1,3,8));
edges.push_back(node(1,4,5));
edges.push_back(node(2,1,3));
edges.push_back(node(2,4,7));
edges.push_back(node(3,0,6));
edges.push_back(node(3,1,8));
edges.push_back(node(4,1,5));
edges.push_back(node(4,2,7));
sort(edges.begin(), edges.end(), comp);

vector<int> parent(N);
for(int i = 0;i<N;i++)
parent[i] = i;
vector<int> rank(N, 0);

int cost = 0;
vector<pair<int,int>> mst;
for(auto it : edges) {
if(findPar(it.v, parent) != findPar(it.u, parent)) {
cost += it.wt;
mst.push_back({it.u, it.v});
unionn(it.u, it.v, parent, rank);
}
}
cout << cost << endl;
for(auto it : mst) cout << it.first << " - " << it.second << endl;
return 0;
}
``````

Output:

16
0 – 1
1 – 2
1 – 4
0 – 3

Time Complexity: O(ElogE)+O(E*4*alpha), ElogE for sorting and E*4*alpha for findParent operation ‘E’ times

Space Complexity: O(N). Parent array+Rank Array

## Java Code

``````import java.util.*;

class Node
{
private int u;
private int v;
private int weight;

Node(int _u, int _v, int _w) { u = _u; v = _v; weight = _w; }

Node() {}

int getV() { return v; }
int getU() { return u; }
int getWeight() { return weight; }

}

class SortComparator implements Comparator<Node> {
@Override
public int compare(Node node1, Node node2)
{
if (node1.getWeight() < node2.getWeight())
return -1;
if (node1.getWeight() > node2.getWeight())
return 1;
return 0;

}
}

class Main
{
private int findPar(int u, int parent[]) {
if(u==parent[u]) return u;
return parent[u] = findPar(parent[u], parent);
}
private void union(int u, int v, int parent[], int rank[]) {
u = findPar(u, parent);
v = findPar(v, parent);
if(rank[u] < rank[v]) {
parent[u] = v;
}
else if(rank[v] < rank[u]) {
parent[v] = u;
}
else {
parent[v] = u;
rank[u]++;
}
}
{
int parent[] = new int[N];
int rank[] = new int[N];

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

int costMst = 0;
ArrayList<Node> mst = new ArrayList<Node>();
if(findPar(it.getU(), parent) != findPar(it.getV(), parent)) {
costMst += it.getWeight();
union(it.getU(), it.getV(), parent, rank);
}
}
System.out.println(costMst);
for(Node it: mst) {
System.out.println(it.getU() + " - " +it.getV());
}
}
public static void main(String args[])
{
int n = 5;

Main obj = new Main();

}
}``````

Output:

16
0 – 1
1 – 2
1 – 4
0 – 3

Time Complexity: O(ElogE)+O(E*4*alpha), ElogE for sorting and E*4*alpha for findParent operation ‘E’ times

Space Complexity: O(N). Parent array+Rank Array