aryagraph.algorithms.community.louvain_communities#
- aryagraph.algorithms.community.louvain_communities(g: Any, weight: str | Callable[[Hashable, Hashable, dict], float] | None = 'weight', resolution: float = 1.0, threshold: float = 1e-07, seed: Any = None, max_level: int | None = None, refine: bool = True) list[set][source]#
Louvain community detection (Blondel et al. 2008) with node aggregation between levels.
Each level moves nodes, in a random order drawn from seed, to the neighbouring community with the largest modularity gain until no move helps, then collapses communities into super-nodes; the run stops when a level gains at most threshold modularity (or after max_level levels). This is networkx’s schedule, directed graphs included. With refine (default), the final partition is then polished by multilevel refinement: the local-moving phase is re-run on every finer level, starting from the final communities, so modularity can only go up.
refine=Falsereturns the plain Louvain result (louvain_hierarchy(...)[-1]).Weights must be non-negative. Roughly
O(m)per sweep. Returns the communities as a list of sets, largest first.>>> comms = louvain_communities(g, seed=1) >>> labels = community_labels(comms) # node -> community index for colouring