The Wayback Machine - https://web.archive.org/web/20200609213414/https://github.com/TheAlgorithms/C-Plus-Plus/issues/764
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

Union-Find algorithm for finding components of a Graph #764

Open
deadshotsb opened this issue May 19, 2020 · 19 comments
Open

Union-Find algorithm for finding components of a Graph #764

deadshotsb opened this issue May 19, 2020 · 19 comments
Assignees
Labels

Comments

@deadshotsb
Copy link
Member

@deadshotsb deadshotsb commented May 19, 2020

A DFS approach has been added for finding the number of components of a graph, but an approach using Union-Find algorithm is more than welcomed.

An Union-Find Approach using rank or path-compression can be a better solution

@cclauss
Copy link
Member

@cclauss cclauss commented May 19, 2020

We want a benchmark that proves that it is a better solution.

@ayaankhan98
Copy link
Contributor

@ayaankhan98 ayaankhan98 commented May 19, 2020

i want to work on this.

@coderanant
Copy link
Contributor

@coderanant coderanant commented May 20, 2020

I was just wondering how "Union-Find Approach using rank or path-compression" could be a better approach in finding the number of connected components. It'll take O(n log n) while the dfs approach works in O(n).
Union-Find Structure (DSU) is rather generally used for the case where we are given several elements, each of which is a separate set. A DSU will have an operation to combine any two sets, and it will be able to tell in which set a specific element is and can create a set from a new element.
An implementation describing this use of DSU would be more helpful.

@cclauss
Copy link
Member

@cclauss cclauss commented May 20, 2020

Why write all this theoretical text?? Write up both algorithms and a benchmark instead.

@coderanant
Copy link
Contributor

@coderanant coderanant commented May 21, 2020

I have added Pull Request #773 regarding this issue.

@deadshotsb
Copy link
Member Author

@deadshotsb deadshotsb commented May 21, 2020

@coderanant DFS will take O(E) and O(E) = [O(V), O(V2)].

@coderanant
Copy link
Contributor

@coderanant coderanant commented May 21, 2020

@deadshotsb Sorry for not making it clear.
DFS will take O(E) = [O(V), O(V2)]
DSU will take O(E) = [O(V log(V)), O(V2 log(V))]
I have however added approach with both Union by size of tree and Path Compression which might work very closer to DFS approach in most cases.

@cclauss
Copy link
Member

@cclauss cclauss commented May 21, 2020

Where is the benchmark?

@coderanant
Copy link
Contributor

@coderanant coderanant commented May 22, 2020

How am I supposed to write a benchmark?
Please pardon if this is very intuitive as I am kinda new to Open Source.

@deadshotsb
Copy link
Member Author

@deadshotsb deadshotsb commented May 23, 2020

@coderanant The best method to write a benchmark according to test the algorithms based on some randomly generated Graphs and showing a relation between time and the size of graph for the algorithms under consideration.

@ayaankhan98
Copy link
Contributor

@ayaankhan98 ayaankhan98 commented May 24, 2020

what is the current status of this issue, as far as i know i had merged the PR related to this issue, what the status of benchmark @coderanant ?

@testitem
Copy link

@testitem testitem commented May 29, 2020

I was just wondering how "Union-Find Approach using rank or path-compression" could be a better approach in finding the number of connected components. It'll take O(n log n) while the dfs approach works in O(n).
Union-Find Structure (DSU) is rather generally used for the case where we are given several elements, each of which is a separate set. A DSU will have an operation to combine any two sets, and it will be able to tell in which set a specific element is and can create a set from a new element.
An implementation describing this use of DSU would be more helpful.

Let's say you are given Q queries. Each query is to add an edge into a graph. After each query you are to return the amount of connected components in this graph.

DFS is O(NQ), while DSU is (Q a(N)).

@kvedala
Copy link
Contributor

@kvedala kvedala commented May 29, 2020

I was just wondering how "Union-Find Approach using rank or path-compression" could be a better approach in finding the number of connected components. It'll take O(n log n) while the dfs approach works in O(n).
Union-Find Structure (DSU) is rather generally used for the case where we are given several elements, each of which is a separate set. A DSU will have an operation to combine any two sets, and it will be able to tell in which set a specific element is and can create a set from a new element.
An implementation describing this use of DSU would be more helpful.

Let's say you are given Q queries. Each query is to add an edge into a graph. After each query you are to return the amount of connected components in this graph.

DFS is O(NQ), while DSU is (Q a(N)).

Since this repository is presenting multiple algorithms from an educational perspective, older inefficient algorithm implementations would also be welcome along with faster and more efficient implementations.

@testitem
Copy link

@testitem testitem commented May 29, 2020

In that case I am confused as for why cclauss is obsessed with speed benchmarks.

@cclauss
Copy link
Member

@cclauss cclauss commented May 29, 2020

We want to be able to see the theoretical performance DFS is O(NQ), while DSU is (Q a(N)) as well as the actual performance 32 sec. vs. 97 sec.

As your username says, let's test the item. ;-)

@coderanant
Copy link
Contributor

@coderanant coderanant commented May 29, 2020

I was just wondering how "Union-Find Approach using rank or path-compression" could be a better approach in finding the number of connected components. It'll take O(n log n) while the dfs approach works in O(n).
Union-Find Structure (DSU) is rather generally used for the case where we are given several elements, each of which is a separate set. A DSU will have an operation to combine any two sets, and it will be able to tell in which set a specific element is and can create a set from a new element.
An implementation describing this use of DSU would be more helpful.

Let's say you are given Q queries. Each query is to add an edge into a graph. After each query you are to return the amount of connected components in this graph.

DFS is O(NQ), while DSU is (Q a(N)).

Thanks for the clarification.
I didn't consider this use-case.

@Chillee
Copy link

@Chillee Chillee commented May 29, 2020

If the "point" of this repo is to present algorithms from an "educational" perspective, what use are benchmarks?

On a similar note, if the goal is to present algorithms from an "educational" perspective, the repo should do a better job of documenting/explaining each algorithm.

@kvedala
Copy link
Contributor

@kvedala kvedala commented May 29, 2020

If the "point" of this repo is to present algorithms from an "educational" perspective, what use are benchmarks?

On a similar note, if the goal is to present algorithms from an "educational" perspective, the repo should do a better job of documenting/explaining each algorithm.

Documentation is being updated. A major re-work in these regards can be found here https://kvedala.github.io/C-Plus-Plus. However, for this documentation to automatically stay up-to-date with latest commits, the code submitted must be properly structured and documented.

@kvedala
Copy link
Contributor

@kvedala kvedala commented May 29, 2020

If the "point" of this repo is to present algorithms from an "educational" perspective, what use are benchmarks?
On a similar note, if the goal is to present algorithms from an "educational" perspective, the repo should do a better job of documenting/explaining each algorithm.

Documentation is being updated. A major re-work in these regards can be found here https://kvedala.github.io/C-Plus-Plus. However, for this documentation to automatically stay up-to-date with latest commits, the code submitted must be properly structured and documented.

Also, any help towards this herculean effort would be greatly appreciated 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.