Repository navigation
Clausets local community detection algorithm - #7691
Conversation
rossbar
left a comment
There was a problem hiding this comment.
I haven't looked in detail, but IMO this algorithm seems like a good fit for NetworkX!
|
This looks like an excellent addition to NetworkX! I haven't looked at the PR in depth yet, but have two requests on a quick look:
Thanks! |
dschult
left a comment
There was a problem hiding this comment.
The suggestions/comments below are mostly silly typos and tweaks of code that don't change the outcome.
About the bigger picture, it seems like there is a lot of computation getting R for each step. Would it be cheaper to compute the change in modularity rather than the new modularity? I know some other algorithms do that. But maybe it isn't effective here.
For the tests, you don't need to add the nodes explicitly if they appear in at least 1 edge. And the edges can be provided directly to the class constructor. So we have a slight preference for something like:
G_edges = [.....]
G = np.DiGraph(G_edges)rather than using add_edges_from and add_nodes_from.
Clearly in cases where there are isolated nodes you need to add the nodes with add_nodes_from.
This PR looks good. :)
| U.update(set(G.neighbors(best_node)) - C - U) | ||
| U.remove(best_node) |
There was a problem hiding this comment.
It looks like you don't need to compute - U here because set.update ignores duplicate entries. And by including {best_node} inside C in the previous line, you don't need to remove best_node from U in the second line here.
Lastly (and least important -- if you find this uglier stick with what you've got), since Python 3.10 you can use the dict.keys() as a set when operating with other sets. So set(G.neighbors(best_node)) can be G[best_node].keys() to save creating a set. Similar idiom is in line 97, so if you change it here you should change it there too.
There was a problem hiding this comment.
Amazing, thanks for this. I didn't need to have the - U. I think I still need to keep the U.remove(best_node) line though as the - C in the previous line only removes it from the G[best_node].keys() set, not from U. Unless there is a better way to update U?
Thanks for your comments and suggestions they are really useful. I have addressed most of them now. With regard to the bigger picture, I think that is a really good idea to just compute Modularity change, and that is actually what Clauset suggests in the paper. If I am honest I went for the simplest implementation to get this working. I will take a look at doing this with modularity change and get back to you. |
rossbar
left a comment
There was a problem hiding this comment.
I still haven't gone over the implementation yet, but wanted to take the opportunity to discuss a couple high-level topics related to the desired API.
Co-authored-by: Ross Barnowski <[email protected]>
In that case, another option might be to call the function |
|
@dschult I have just updated the implementation to calculate modularity change, it reduces the run time on even the tiny karate club graph by at least 50%, and I think the performance gains would be exponential as the graphs get larger so I think this is definitely the approach to go with. I just need to do some code tidy up on this now. |
rossbar
left a comment
There was a problem hiding this comment.
IMO this is very clearly written - thanks very much @MattThorne ! Generally LGTM, there are a few minor things I noticed in the docstring etc. but no blockers - I'll go ahead and push them up later. As far as the algorithm itself goes though this is ready for another set of eyes!
I like this idea, keeps things neat. I have made these changes and updated the docs to reflect the name change. |
dschult
left a comment
There was a problem hiding this comment.
This looks good. Thanks!
Here's a nit-picky change that might make it slightly easier to read.
NetworkX has chosen to spell neighbor (without the "u"). And in one variable name it is also incorrectly spelled neighour instead of neighbour. My suggestion for variable names is to use nbr wherever you want to use variable names with neighbor/neighbour. It avoids the whole Us vs Uk spelling issue and the brain cycles some readers inevitably spend whichever way you spell it. Plus it is shorter yet unambiguous in meaning.
dschult
left a comment
There was a problem hiding this comment.
This all looks good!
Thanks very much!
This PR adds Clauset’s original local community detection (LCD) algorithm. The algorithm enables identification of communities starting from a source node, using a greedy expansion approach based on local modularity optimisation.
This method is particularly advantageous when:
The algorithm is efficient for discovering localized communities without requiring access to global network information, making it useful for applications such as web crawling, social network analysis, or large-scale graph exploration.
There are many LCD algorithms, but Clausets original algorithm will be a good place to start including LCD into NetworkX
References