Skip to content

#3322 Fix modularity community weights at the first level and with self-loops - #3362

Merged
mbastian merged 2 commits into
masterfrom
fix/modularity-first-level-weights
Oct 25, 2026
Merged

mbastian merged 2 commits into
masterfrom
fix/modularity-first-level-weights

Conversation

@mbastian

@mbastian mbastian commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

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 CommunityStructure constructor seeded each node's community while the node's weight was still 0, so every first-level community started with a weightSum of 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_inside excludes loop edges) and networkx (neighbour weights skip the node itself) do the same. Self-loops now stay out of the node connection maps, both when zoomOut builds 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:

Graphs Before Seed fix + self-loop fix
3000 random graphs, up to 150 nodes, default settings 0.5441 0.5452 0.5484
4000 random graphs, up to 80 nodes, random resolution/weights/randomize 0.4465 0.4462 0.4497
1500 regular graphs with uniform weights (rings, grids, bipartite) 0.3432 0.3402 0.3431
Zachary's karate club, 1000 random node orders 0.4090 0.4147 0.4155

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

  • Merged with master beforehand

Added tests?

  • 👍 yes
  • 🙅 no, because they aren't needed

Added to documentation?

  • 👍 README.md
  • 👍 API Changes
  • 👍 Additional documentation in docs
  • 👍 Relevant code documentation
  • 🙅 no, because they aren’t needed

🤖 Generated with Claude Code

@mbastian
mbastian changed the base branch from fix/modularity-self-loop to master October 25, 2026 16:52
mbastian and others added 2 commits October 25, 2026 17:52
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
mbastian force-pushed the fix/modularity-first-level-weights branch from 6a7fbf6 to db704ab Compare October 25, 2026 16:52
@mbastian
mbastian marked this pull request as ready for review October 25, 2026 17:09
@mbastian
mbastian requested a review from a team as a code owner October 25, 2026 17:09
@mbastian mbastian added this to the 0.11.4 milestone Oct 25, 2026
@mbastian
mbastian merged commit 4e26700 into master Oct 25, 2026
5 of 6 checks passed
@mbastian
mbastian deleted the fix/modularity-first-level-weights branch November 17, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modularity: first-level community weights start at 0 because communities are seeded before node weights are summed

1 participant