Repository navigation
#3322 Fix modularity community weights at the first level and with self-loops - #3362
Merged
Merged
Conversation
The CommunityStructure constructor seeded each node's community while the node's weight was still 0, so every first-level community started with a weightSum short of its node's weight, and went negative once the node left it. Seed them after the weights are summed, as zoomOut already does for the following levels (#3322). This changes the gains of the first pass, so results can differ. The measured effect is mixed but small: on Zachary's karate club the mean modularity over 1000 random node orders goes from 0.4090 to 0.4147 (best 0.4198 either way), over 3000 random graphs from 0.5442 to 0.5453, while regular graphs with uniform weights lose about 0.002. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After the first level, each node carries a self-loop holding the internal weight of the community it stands for. That loop was also stored as a link from the node to its own community, so staying where it was looked better than it is by the resolution times the loop weight. The loop moves with the node whatever community it joins, so it only belongs in the community totals, as in igraph (weight_inside excludes loop edges) and networkx (neighbour weights skip the node itself). Keep self-loops out of the node connection maps when zoomOut builds them and when a node joins or leaves a community. The community connection maps still count each loop once. Communities may differ. Mean modularity goes from 0.5452 to 0.5484 over 3000 random graphs, from 0.4462 to 0.4497 over 4000 graphs with random settings and resolution, and from 0.4147 to 0.4155 on Zachary's karate club over 1000 random node orders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mbastian
force-pushed
the
fix/modularity-first-level-weights
branch
from
October 25, 2026 16:52
6a7fbf6 to
db704ab
Compare
mbastian
marked this pull request as ready for review
October 25, 2026 17:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two small fixes to the bookkeeping of the Louvain modularity, one commit each. Follow-up to #3358.
Fixes #3322
Seed first-level communities once node weights are known. The
CommunityStructureconstructor seeded each node's community while the node's weight was still 0, so every first-level community started with aweightSumof 0 instead of its node's weight, and went negative once the node left it.zoomOut()already seeds after the weights are computed; the constructor now does the same.Stop counting a node's self-loop as a link to its own community. From the second level on, each node carries a self-loop holding the internal weight of the community it stands for. That loop was also stored as a link from the node to its own community, so staying looked better than it is by the resolution times the loop weight. The loop moves with the node whatever community it joins, so it only belongs in the community totals. igraph (
weight_insideexcludes loop edges) and networkx (neighbour weights skip the node itself) do the same. Self-loops now stay out of the node connection maps, both whenzoomOutbuilds them and when a node joins or leaves a community. The community maps still count each loop once, so the add/remove symmetry from #3358 is unchanged. Most of that diff is re-indentation, so it reads best with whitespace hidden.This changes results: communities and modularity values can differ from earlier versions. Mean modularity of the final partition, recomputed independently:
The seed fix on its own has a mixed but small effect: it is a little lower on regular graphs with uniform weights and within noise on the second set. The self-loop fix improves every set. Both versions reach the best known karate club modularity, 0.4198. A throwaway randomized harness checked the structure's maps and community weights after every move and every level: it found no inconsistencies and no runs that failed to finish, over the graphs above plus 26000 more.
Checklist
Added tests?
Added to documentation?
🤖 Generated with Claude Code