def communities(self, nCommunities, weight=None):
"""
Compute communities.
Parameters
----------
nCommunities - number of communities to be returned.
This is added to simplify the process, the original GN algorithm doesn't
need predecided number of communities.
Other measures like a threshold on betweenness centrality can be used instead.
weight (string) - If None, all edge weights are considered equal.
Otherwise holds the name of the edge attribute used as weight.
Returns
--------
A list of communities where each community is a list of the nodes in the community.
"""
gr = self.g
n = nx.number_connected_components(gr)
components = nx.connected_components(gr)
while (n < nCommunities):
gr = self.communitySplits(gr, weight=weight)
components = nx.connected_components(gr)
n = nx.number_connected_components(gr)
if gr.number_of_edges() == 0:
break
return components
评论列表
文章目录