-
-
Save navjotahuja92/6d3ee2617cc08ecfc8d48d34fe2f3ce9 to your computer and use it in GitHub Desktop.
Javascript Disjoint Set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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