Skip to content

@graphty/algorithms / index / indexed / modularity

Function: modularity() ​

modularity(s, labels, options?): number

Defined in: indexed/modularity.ts:37

Newman's modularity of a partition: sum_c in_c / 2m - gamma * (tot_c / 2m)^2, where 2m is twice the total edge weight, in_c counts every edge inside community c twice and tot_c sums its nodes' weighted degrees. Degrees follow graph-format's weightedDegree() convention -- a self-loop counts twice -- so sum(tot_c) === 2m. On a directed snapshot the direction is ignored: every arc counts as an undirected edge, and a node's degree is its in-weight plus its out-weight.

On an undirected snapshot without self-loops this equals the legacy calculateModularity. On a directed snapshot or one with self-loops the two differ, and this one is the NetworkX value: the legacy degree counts a self-loop once and reads only out-neighbours on a directed graph, so its degrees do not sum to 2m.

It is NOT the legacy calculateMCLModularity, in two ways. That function subtracts the null-model term d(u) d(v) / 2m once per edge whatever the partition: on two separate triangles it scores one community and the two triangles both 1/3, where this returns 0 and 1/2. And it ignores edge weights, where this reads the snapshot's weights by default; pass weights filled with 1 for the unweighted value.

Parameters ​

s ​

AdjacencyView

Any snapshot or adjacency view

labels ​

U32

Community of every node; INVALID_INDEX leaves a node out of every community

options? ​

ModularityOptions = {}

Resolution and the weight override

Returns ​

number

The modularity, 0 when the snapshot has no edge weight