Last active
April 25, 2020 04:38
-
-
Save adslaton/b2d37e3c223550ac7e61925ea464f7a2 to your computer and use it in GitHub Desktop.
union-find-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}); | |
} | |
} | |
// 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