networkx 2.8.2 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturnsBackRef
modularity(G, communities, weight='weight', resolution=1)

Modularity is defined in as

$$Q = \frac{1}{2m} \sum_{ij} \left( A_{ij} - \gamma\frac{k_ik_j}{2m}\right) \delta(c_i,c_j)$$

where $m$ is the number of edges, $A$ is the adjacency matrix of G, $k_i$ is the degree of $i$, $\gamma$ is the resolution parameter, and $\delta(c_i, c_j)$ is 1 if $i$ and $j$ are in the same community else 0.

According to (and verified by some algebra) this can be reduced to

$$Q = \sum_{c=1}^{n} \left[ \frac{L_c}{m} - \gamma\left( \frac{k_c}{2m} \right) ^2 \right]$$

where the sum iterates over all communities $c$, $m$ is the number of edges, $L_c$ is the number of intra-community links for community $c$, $k_c$ is the sum of degrees of the nodes in community $c$, and $\gamma$ is the resolution parameter.

The resolution parameter sets an arbitrary tradeoff between intra-group edges and inter-group edges. More complex grouping patterns can be discovered by analyzing the same network with multiple values of gamma and then combining the results . That said, it is very common to simply use gamma=1. More on the choice of gamma is in .

The second formula is the one actually used in calculation of the modularity. For directed graphs the second formula replaces $k_c$ with $k^{in}_c k^{out}_c$.

Parameters

G : NetworkX Graph
communities : list or iterable of set of nodes

These node sets must represent a partition of G's nodes.

weight : string or None, optional (default="weight")

The edge attribute that holds the numerical value used as a weight. If None or an edge does not have that attribute, then that edge has weight 1.

resolution : float (default=1)

If resolution is less than 1, modularity favors larger communities. Greater than 1 favors smaller communities.

Raises

NotAPartition

If :None:None:`communities` is not a partition of the nodes of G.

Returns

Q : float

The modularity of the paritition.

Returns the modularity of the given partition of the graph.

Examples

>>> import networkx.algorithms.community as nx_comm
... G = nx.barbell_graph(3, 0)
... nx_comm.modularity(G, [{0, 1, 2}, {3, 4, 5}]) 0.35714285714285715
>>> nx_comm.modularity(G, nx_comm.label_propagation_communities(G))
0.35714285714285715
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.algorithms.community.modularity_max.greedy_modularity_communities networkx.algorithms.community.modularity_max.naive_greedy_modularity_communities networkx.algorithms.community.quality.modularity networkx.algorithms.community.modularity_max._greedy_modularity_communities_generator

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/algorithms/community/quality.py#245
type: <class 'function'>
Commit: