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

Implementation of Louvain Algorithm #1141

Open
jamesdbaker opened this issue Mar 14, 2024 · 11 comments
Open

Implementation of Louvain Algorithm #1141

jamesdbaker opened this issue Mar 14, 2024 · 11 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jamesdbaker
Copy link
Contributor

The Louvain algorithm is commonly used to identify communities in graphs, and is present in other libraries/tools such as NetworkX and Neo4J.

It would be great if an implementation was available in RustworkX too.

@IvanIsCoding IvanIsCoding added the enhancement New feature or request label Mar 14, 2024
@IvanIsCoding
Copy link
Collaborator

I think this is a good ask. Right not we have no community detection algorithms implemented, but this could be an effort like #315 to start implementing those.

Sometimes it is just a matter that no one asked for them before

@jamestwebber
Copy link
Contributor

jamestwebber commented Apr 16, 2024

A general question: is the goal of rustworkx to include algorithms like this out-of-the-box, or would it be better to support third-party packages for things like this? I see that networkx includes Louvain now, but originally it was a separate package. Similarly leidenalg implements community-detection on igraph graphs.

One thing I don't know: how complicated is it to write a package that works with rustworkx graphs in a performant way (i.e. not going through python when possible)? One item on my ever-growing list of projects is to try to implement Leiden (or similar) in Rust in a way that's compatible with rustworkx, potentially using the underlying graph structures directly.

@IvanIsCoding
Copy link
Collaborator

Firstly, our goal is to expand the graph library. Most of the existing algorithms started like this, someone needed a specific algorithm and we eventually implemented it. There is a lot of upfront cost with implementation & testing for correctness, but once it is released generally our maintenance for algorithms has been pretty easy.

Secondly, using rustworkx from Python is easy, you can check some of the works citing our paper and Qiskit itself.
Also, using rustworkx-core from Rust is also easy, that is the approach some of Qiskit Rust modules use.

However, using rustworkx from other Rust code is currently blocked and you can see some discussion in #663. My general suggestion would be to call graph.edge_list() or graph.weighted_edge_list() from a Python object in PyO3 and then rebuild a rustworkx-core graph or just use that data directly in your algorithm.

@IvanIsCoding
Copy link
Collaborator

One last thought: for leiden specifically, I think if you build upon petgraph and rustworkx-core it would make life simpler to port your own version to be included in rustworkx. And we would gladly merge & distribute it if you made a PR submitting it!

@jpacold
Copy link
Contributor

jpacold commented Jul 1, 2024

I'm interested in taking this on if nobody has started on it.

@traviscross
Copy link

traviscross commented Jul 7, 2024

@jpacold: It seems the right place to start is probably to first make a PR upstream to petgraph with the new algorithm.

@jpacold
Copy link
Contributor

jpacold commented Jul 8, 2024

So far all I have done is implement the modularity function (which the Louvain algorithm tries to maximize) in this branch.

Before going any further I wanted to check whether the architecture makes sense. The idea is to eventually put the full algorithm in, e.g., rustworkx-core/src/community/louvain.rs.

Some open questions:

  • Is there a better way (in either rustworkx or petgraph) to specify a graph partition?
  • I agree that it would make sense to put this in petgraph, but will have to think a bit more about how to do that correctly. Among other things, the implementation I have now would need an architecture change in petgraph, since I'm using the num_traits crate to constrain the edge weight type.
  • Eventually, the pyo3 function(s) that wraps this will need to have a similar constraint on the input graph. I need to figure out how to do the type check at run time.

@tsitong
Copy link

tsitong commented Aug 3, 2024

It seems that the developers of petgraph believe that these community detection algorithms should be offered as a separate package, plz see petgraph/petgraph#185. However, I’m not sure if their opinion has changed.

@IvanIsCoding
Copy link
Collaborator

So far all I have done is implement the modularity function (which the Louvain algorithm tries to maximize) in this branch.

Before going any further I wanted to check whether the architecture makes sense. The idea is to eventually put the full algorithm in, e.g., rustworkx-core/src/community/louvain.rs.

Some open questions:

  • Is there a better way (in either rustworkx or petgraph) to specify a graph partition?
  • I agree that it would make sense to put this in petgraph, but will have to think a bit more about how to do that correctly. Among other things, the implementation I have now would need an architecture change in petgraph, since I'm using the num_traits crate to constrain the edge weight type.
  • Eventually, the pyo3 function(s) that wraps this will need to have a similar constraint on the input graph. I need to figure out how to do the type check at run time.

I completely missed this comment. Also, feel free to open a draft PR so that we can discuss. I will try to get a look at it.

@IvanIsCoding
Copy link
Collaborator

It seems that the developers of petgraph believe that these community detection algorithms should be offered as a separate package, plz see petgraph/petgraph#185. However, I’m not sure if their opinion has changed.

We can be the other crate to host the algorithm.

@traviscross
Copy link

I've asked over on the petgraph side whether the old answer is still accurate. I notice that they've recently been merging other new algorithms such as PageRank that seem not too dissimilar to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

6 participants