Skip to content

Clausets local community detection algorithm - #7691

Merged
dschult merged 21 commits into
networkx:mainfrom
MattThorne:add-initial-local-commuinity-detection
Nov 13, 2024
Merged

dschult merged 21 commits into
networkx:mainfrom
MattThorne:add-initial-local-commuinity-detection

Conversation

@MattThorne

Copy link
Copy Markdown
Contributor

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 complete graph is unknown or only partially observable.
  • The graph is too large for global community detection methods to be computationally feasible.

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

  • The original paper by Clauset (2005) can be found here.
  • For a broader introduction to local community detection, see here.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked in detail, but IMO this algorithm seems like a good fit for NetworkX!

@dschult

dschult commented Oct 24, 2024

Copy link
Copy Markdown
Member

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:

  • can you add a section to the documentation hooks that let sphinx (our doc builder) know where to pull the doc_strings from your module as it builds the docs? The file that is relevant (I think...) is doc/reference/algorithsm/community.rst and I think you can copy/paste/change an existing section to make it work with your code. The module name is listed in some preamble stuff, and then the function names are listed with appropriate whitespace around them.
  • Can you wrap your doc_string lines to something like 80 chars? Code is <88 chars on a line, so the doc_strings look good between 75-88 or so. We don't have an automated tool to do that yet I believe, but we're working on it.

Thanks!

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. :)

Comment thread doc/reference/algorithms/community.rst Resolved
Comment thread networkx/algorithms/community/local.py Resolved
Comment thread networkx/algorithms/community/local.py Resolved
Comment thread networkx/algorithms/community/local.py Resolved
Comment thread networkx/algorithms/community/local.py Resolved
Comment thread networkx/algorithms/community/local.py Outdated
Comment on lines +125 to +126
U.update(set(G.neighbors(best_node)) - C - U)
U.remove(best_node)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread networkx/algorithms/community/local.py Resolved
@MattThorne

Copy link
Copy Markdown
Contributor Author

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. :)

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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread networkx/algorithms/community/local.py Resolved
@rossbar

rossbar commented Nov 4, 2024

Copy link
Copy Markdown
Contributor

RE: naming - I was unsure on this as well, I went back and forward on this a few times in my commits. In the early commits I called it greedy_source_expansion exactly as you suggested. I went for clauset in the end as there seemed to be several different greedy source expansion algorithms that each had pros and cons in different situations. This touches on a number of the different algorithms. My thought was in future PR's I could add some of the other algorithms. Maybe clauset_greedy_source_expansion is a more descriptive name?

In that case, another option might be to call the function greedy_source_expansion but add a "method" keyword argument to specify which method of greedy source expansion is being used. AFAIK this is a (relatively) common pattern within the library: for example, see steiner_tree. WDYT?

@MattThorne

Copy link
Copy Markdown
Contributor Author

@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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread doc/reference/algorithms/community.rst Resolved
@MattThorne

Copy link
Copy Markdown
Contributor Author

In that case, another option might be to call the function greedy_source_expansion but add a "method" keyword argument to specify which method of greedy source expansion is being used. AFAIK this is a (relatively) common pattern within the library: for example, see steiner_tree. WDYT?

I like this idea, keeps things neat. I have made these changes and updated the docs to reflect the name change.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread networkx/algorithms/community/local.py Resolved

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks good!
Thanks very much!

@dschult
dschult merged commit 0ce98be into networkx:main Nov 13, 2024
@jarrodmillman jarrodmillman added this to the 3.5 milestone Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

6 participants