Last active
April 25, 2020 18:27
-
-
Save adslaton/0ecc1c795180dd0e049f4b21b1ffa2b5 to your computer and use it in GitHub Desktop.
quick-union-type
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
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; | |
} |
Author
adslaton
commented
Apr 25, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment