Skip to content

@graphty/algorithms / index / leiden

Function: leiden() ​

leiden(s, options?): LeidenResult

Defined in: indexed/leiden.ts:409

Leiden community detection (Traag, Waltman and van Eck, Sci. Rep. 9:5233, 2019) over an undirected snapshot: fast local moving, refinement into well-connected sub-communities, and aggregation by contract() once per level. Whole passes repeat from the previous answer, keeping a pass only when it raises modularity, until three passes in a row improve it by no more than threshold or maxIterations passes have run; every pass starts by moving single original nodes, so the answer is also a local optimum of those moves.

Modularity follows graph-format's weightedDegree() convention (a self-loop counts twice), which on a graph without self-loops equals the legacy leiden's. Weights are the exact f64 ones when the snapshot keeps them. The partition is not the legacy function's move for move: the legacy leiden splits disconnected communities rather than refining them, and visits nodes in another order; what this guarantees is connected communities. Both are randomised heuristics, so for one seed either can come out ahead; on the shared fixtures this one's modularity is at least the legacy's at the default seed and on average over seeds.

Parameters ​

s ​

GraphSnapshot

An undirected snapshot

options? ​

LeidenOptions = {}

Resolution, seed, pass cap and stopping threshold

Returns ​

LeidenResult

The partition, its modularity, and the levels processed

Throws ​

Error on a directed snapshot; RangeError for a negative, NaN or infinite weight or a fractional seed