Skip to content

Instantly share code, notes, and snippets.

@adslaton
Last active April 25, 2020 04:38
Show Gist options
  • Save adslaton/b2d37e3c223550ac7e61925ea464f7a2 to your computer and use it in GitHub Desktop.
Save adslaton/b2d37e3c223550ac7e61925ea464f7a2 to your computer and use it in GitHub Desktop.
union-find-type
const connections = [];
// create objects in the array
const uf = (n) => {
for (const x of Array(n).keys()) {
connections.push({ id: x, name: x});
}
}
// union
uf.union = (p, q) => {
connections.map((element, i) => {
if (element.id === p) {
element.id = q;
}
})
};
// find
uf.connected = (p, q) => {
let connection = false;
connections.map((element, i) => {
if (element.name === p && element.id === q) {
connection = true;
}
})
return connection;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment