Skip to content

Instantly share code, notes, and snippets.

@adslaton
Last active April 25, 2020 18:27
Show Gist options
  • Save adslaton/0ecc1c795180dd0e049f4b21b1ffa2b5 to your computer and use it in GitHub Desktop.
Save adslaton/0ecc1c795180dd0e049f4b21b1ffa2b5 to your computer and use it in GitHub Desktop.
quick-union-type
const connections = [];
// create objects in the array
const uf = (n) => {
for (const x of Array(n).keys()) {
connections.push({ id: x, name: x});
}
}
// root
uf.root = (n) => {
while(n !== connections[n].id) n = connections[n].id;
return n;
}
// union
uf.union = (p, q) => {
const i = uf.root(p);
const j = uf.root(q);
connections[i].id = j;
};
// find
uf.connected = (p, q) => {
let connection = false;
connections.map((element, i) => {
if (element.name === p && element.id === q) {
connection = true;
}
})
return connection;
}
@adslaton
Copy link
Author

Screen Shot 2020-04-25 at 2 25 03 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment