Skip to content

Instantly share code, notes, and snippets.

@peterkhayes
Created May 3, 2017 05:33
Show Gist options
  • Save peterkhayes/b4ff2c9838f9744ec2ec497590380ec7 to your computer and use it in GitHub Desktop.
Save peterkhayes/b4ff2c9838f9744ec2ec497590380ec7 to your computer and use it in GitHub Desktop.
Javascript Disjoint Set
/*
Useful for HackerRank problems.
*/
function main (numNodes, edges) {
const parent = [];
const rank = [];
function makeSet (x) {
parent[x] = x;
rank[x] = 0;
}
function find (x) {
if (x !== parent[x]) parent[x] = find(parent[x]);
return parent[x];
}
function union (x, y) {
const xRoot = find(x);
const yRoot = find(y);
if (xRoot === yRoot) {
return false; // items were already in the same set
}
if (rank[xRoot] < rank[yRoot]) {
parent[xRoot] = yRoot;
} else if (rank[xRoot] > rank[yRoot]) {
parent[yRoot] = xRoot;
} else {
parent[yRoot] = xRoot
rank[xRoot] = rank[xRoot] + 1
}
return true; // items were not already in the same set
}
for (let node = 0; node < numNodes; node++) {
makeSet(node);
}
for (let e = 0; e < edges.length; e++) {
const [node1, node2] = edges[e];
const didSomething = union(node1, node2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment