Skip to content

Instantly share code, notes, and snippets.

@adslaton
Last active April 25, 2020 17:43
Show Gist options
  • Save adslaton/88455fbb1ee0587ba741769e28f763f1 to your computer and use it in GitHub Desktop.
Save adslaton/88455fbb1ee0587ba741769e28f763f1 to your computer and use it in GitHub Desktop.
deduce-dynamic-connectivity
const connections = [ // O(1)
{
id: 1,
name: 1
},
{
id: 2,
name: 2
},
{
id: 3,
name: 3
}
];
const union = (p, q) => { // O(n)
connections.map((element, i) => {
if (element.id === p) {
element.id = q;
}
})
};
const connected = (p, q) => { // O(n)
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