aryagraph.algorithms.community.modularity#

aryagraph.algorithms.community.modularity(g: Any, communities: Iterable[Iterable[Hashable]], weight: str | Callable[[Hashable, Hashable, dict], float] | None = 'weight', resolution: float = 1.0) → float[source]#

Modularity of a partition (networkx’s definition, directed graphs included).

Parameters:
  • communities – Iterable of node collections that must partition the nodes (ValueError otherwise).

  • weight – Edge weight (missing attribute ⇒ 1); None treats the graph as unweighted.

  • resolution – γ; below 1 favours larger communities, above 1 smaller ones.

Raises:

ValueError – If communities is not a partition of the nodes, or for a graph whose total edge weight is 0 (modularity is undefined).

Examples

>>> modularity(Graph([(0, 1), (2, 3)]), [{0, 1}, {2, 3}])
0.5