Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding union find data structure #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,434 changes: 7,379 additions & 55 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src/graphs/union-find/union-find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const unionFind = (edges) => {
let parent = [];

for (let i = 0; i <= edges.length; i += 1) {
parent.push(i);
}

// finding the parent of p
const find = (p) => {
if (parent[p] === p) {
return p;
} else {
return find(parent[p]);
}
};

const union = (u, v) => {
let parentU = find(u);
let parentV = find(v);

// u becomes the parent of v ;
parent[parentU] = parentV;
};
};
7,935 changes: 3,890 additions & 4,045 deletions yarn.lock

Large diffs are not rendered by default.