greedy_modularity_communities(G, weight=None, resolution=1, n_communities=1)[source]

Find communities in G using greedy modularity maximization.

This function uses Clauset-Newman-Moore greedy modularity maximization [2].

Greedy modularity maximization begins with each node in its own community and joins the pair of communities that most increases modularity until no such pair exists or until number of communities n_communities is reached.

This function maximizes the generalized modularity, where resolution is the resolution parameter, often expressed as \(\gamma\). See modularity().

GNetworkX graph
weightstring or None, optional (default=None)

The name of an edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.

resolutionfloat (default=1)

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

n_communities: int

Desired number of communities: the community merging process is terminated once this number of communities is reached, or until modularity can not be further increased. Must be between 1 and the total number of nodes in G. Default is 1, meaning the community merging process continues until all nodes are in the same community or until the best community structure is found.

partition: list

A list of frozensets of nodes, one for each community. Sorted by length with largest communities first.

See also




Newman, M. E. J. “Networks: An Introduction”, page 224 Oxford University Press 2011.


Clauset, A., Newman, M. E., & Moore, C. “Finding community structure in very large networks.” Physical Review E 70(6), 2004.


Reichardt and Bornholdt “Statistical Mechanics of Community Detection” Phys. Rev. E74, 2006.


Newman, M. E. J.”Analysis of weighted networks” Physical Review E 70(5 Pt 2):056131, 2004.


>>> from import greedy_modularity_communities
>>> G = nx.karate_club_graph()
>>> c = greedy_modularity_communities(G)
>>> sorted(c[0])
[8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]