Skip to content

Count self-loops in modularity and Louvain as networkx does - #18

Merged
arclabs561 merged 1 commit into
mainfrom
fix/modularity-self-loops-20261104
Nov 4, 2026
Merged

arclabs561 merged 1 commit into
mainfrom
fix/modularity-self-loops-20261104

Conversation

@arclabs561

Copy link
Copy Markdown
Owner

modularity counted a self-loop once in its node's degree and as half an internal edge. It now follows networkx: twice in the degree, once as an internal edge. A test pins the networkx 3.7 value (0.22) on a small graph with a self-loop.

Louvain and connectivity-refined Louvain, weighted and unweighted, started a self-loop at its plain weight while their aggregation step stores internal edges at twice their weight. All four now start self-loops at twice their weight. Graphs without self-loops are unaffected.

https://claude.ai/code/session_01UJZpXWnaKuGTdguKe8WFFv

modularity counted a self-loop once in its node's degree and as half an internal edge, so 2m was short by one per loop. It now adds 2 to the degree and 1 to L_c; the new test matches networkx 3.7 (0.22 on a triangle with a pendant self-looped node).

The four optimizers built their first-level adjacency with a self-loop at its plain weight, while aggregation stores internal edges at twice their weight. They now start a self-loop at twice its weight so both levels use one convention. Graphs without self-loops are unaffected.

Checked with cargo fmt, clippy -D warnings and cargo test --all-features.

Claude-Session: https://claude.ai/code/session_01UJZpXWnaKuGTdguKe8WFFv
@arclabs561
arclabs561 merged commit 967012d into main Nov 4, 2026
2 checks passed
@arclabs561
arclabs561 deleted the fix/modularity-self-loops-20261104 branch November 4, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant