# Kruskal's algorithm in Javascript

Kruskal's algorithm is a greedy algorithm that works as follows:

1. It Creates a set of all edges in the graph.

2. While the above set is not empty and not all vertices are covered,

1. It removes the minimum weight edge from this set
2. It checks if this edge is forming a cycle or just connecting 2 trees. If it forms a cycle, we discard this edge, else we add it to our tree.

3. When the above processing is complete, we have a minimum spanning tree.

In order to implement this algorithm, we need 2 more data structures.

First, we need a priority queue that we can use to keep the edges in a sorted order and get our required edge on each iteration.

Next, we need a disjoint set data structure. A disjoint-set data structure (also called a union-find data structure or merge–find set) is a data structure that tracks a set of elements partitioned into a number of disjoint (non-overlapping) subsets. Whenever we add a new node to a tree, we will check if they are already connected. If yes, then we have a cycle. If no, we will make a union of both vertices of the edge. This will add them to the same subset.

Let us look at the implementation of UnionFind or DisjointSet data structure:

class UnionFind {
constructor(elements) {
// Number of disconnected components
this.count = elements.length;

// Keep Track of connected components
this.parent = {};

// Initialize the data structure such that all
// elements have themselves as parents
elements.forEach(e => (this.parent[e] = e));
}

union(a, b) {
let rootA = this.find(a);
let rootB = this.find(b);

// Roots are same so these are already connected.
if (rootA === rootB) return;

// Always make the element with smaller root the parent.
if (rootA < rootB) {
if (this.parent[b] != b) this.union(this.parent[b], a);
this.parent[b] = this.parent[a];
} else {
if (this.parent[a] != a) this.union(this.parent[a], b);
this.parent[a] = this.parent[b];
}
}

// Returns final parent of a node
find(a) {
while (this.parent[a] !== a) {
a = this.parent[a];
}
return a;
}

// Checks connectivity of the 2 nodes
connected(a, b) {
return this.find(a) === this.find(b);
}
}

You can test this using:

let uf = new UnionFind(["A", "B", "C", "D", "E"]);
uf.union("A", "B"); uf.union("A", "C");
uf.union("C", "D");

console.log(uf.connected("B", "E"));
console.log(uf.connected("B", "D"));

This will give the output:

false
true

Now let us look at the implementation of Kruskal's algorithm using this data structure:

kruskalsMST() {
// Initialize graph that'll contain the MST
const MST = new Graph();
if (this.nodes.length === 0) {
return MST;
}

// Create a Priority Queue
edgeQueue = new PriorityQueue(this.nodes.length * this.nodes.length);

// Add all edges to the Queue:
for (let node in this.edges) {
this.edges[node].forEach(edge => {
edgeQueue.enqueue([node, edge.node], edge.weight);
});
}

let uf = new UnionFind(this.nodes);

// Loop until either we explore all nodes or queue is empty
while (!edgeQueue.isEmpty()) {
// Get the edge data using destructuring
let nextEdge = edgeQueue.dequeue();
let nodes = nextEdge.data;
let weight = nextEdge.priority;

if (!uf.connected(nodes, nodes)) {
uf.union(nodes, nodes);
}
}
return MST;
}

You can test this using:

let g = new Graph();

g.kruskalsMST().display();

This will give the output:

A->B, D
B->A, G
C->D
D->C, A, E
E->D, F
F->E
G->B