Created
April 25, 2020 18:55
-
-
Save adslaton/1cd1b727a78a97e39de7a02c5a8bf30f to your computer and use it in GitHub Desktop.
quick-union-merge
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 = new Map(); | |
// create objects in the array | |
const uf = (n) => { | |
for (const x of Array(n).keys()) { | |
connections.set({ id: x, name: x}, x); | |
} | |
} | |
// root | |
uf.root = (n) => { | |
return connections.get(n); | |
} | |
// union | |
uf.union = (p, q) => { | |
const i = uf.root(p); | |
const j = uf.root(q); | |
if (i < j) { | |
connections.set(j, i); | |
} else { | |
connections.set(i, j); | |
} | |
}; | |
// find | |
uf.connected = (p, q) => { | |
return (connections.get(p) === connections.get(q)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment