Documentation
¶
Overview ¶
Example 09_leiden — modularity-optimising community detection with community.Leiden over a realistic, seeded planted-partition graph.
It generates a symmetric stochastic block model (SBM) — K equal-sized planted communities with a high intra-community edge probability and a low inter-community edge probability — freezes it into an immutable CSR snapshot, runs Leiden, and reports how well Leiden recovers the planted structure: the number of communities found and the Newman modularity Q of the returned partition, alongside the volatile build/detect timing.
Model ¶
The graph is undirected and unweighted. The N = communities × communitySize nodes are partitioned into K equal blocks. For every unordered node pair (i, j) an edge is drawn independently with probability pIn when i and j share a block and pOut when they do not. This is the planted-partition / symmetric-SBM construction; fixing -seed fixes the drawn edge set, hence the graph shape, exactly.
Why these parameters ¶
The graph-theory-expert sub-agent supplied the regime (recorded inline at sbmParams and computeModularity):
- Detectability follows the Kesten–Stigum / Decelle–Krzakala–Moore– Zdeborová threshold for the symmetric SBM: with intra-degree a = pIn·(s−1) and per-other-block inter-degree b = pOut·s over K blocks, weak recovery is possible iff SNR = (a−b)² / [K·(a + (K−1)·b)] > 1. The defaults sit ≈3× above that threshold, deep in the "easy" regime, so Leiden reliably recovers the planting.
- Each block is an Erdős–Rényi G(s, pIn) subgraph; it stays internally connected with high probability when pIn·(s−1) ≥ 2·ln(s), which validate enforces so a planted community cannot fragment and inflate the community count.
- The planted partition's expected modularity is Q ≈ (intra-edge fraction) − 1/K, which the small default targets at ≈0.70 (theoretical max 1 − 1/K = 0.75 for K=4).
Scale ¶
Run with no flags, the example builds the small deterministic default — four communities of twenty-five nodes (100 nodes, ~700 edges) — which completes in well under a second and is pinned by the regression test. Every dimension is a flag, so the same binary scales up to a size where the detect cost is observable:
go run ./examples/09_leiden -communities 8 -community-size 500 -p-in 0.06 -p-out 0.0008 -seed 7
The deterministic facts (community count and the modularity reported to two decimals) are reproducible for a fixed -seed; only the telemetry (lines prefixed with "# ") varies between runs and machines.