Why is Girvan Newman algorithm in Networkx always returning 2 communities?

Viewed 398

I'm using the Krebs data from here: http://www-personal.umich.edu/~mejn/netdata/. I upload it into a graph from gml by using networkx.read_gml, then I run networkx.algorithms.community.centrality.girvan_newman on it.

Girvan_newman in networkx always return only two communities, although in this dataset more communities can be found.

Can anyone make me understand why that happens? Is girvan_newman always returning two communities? What else could I use in its place if I want to find more than two (if possible)?

1 Answers

The algorithm will iteratively yield partitions with increasing number of communities. If your original graph is connected, you will first get a partition with two communities. Then a partition with three communities and so on.

This behaviour is also documented. The example from the documentation:

G = nx.path_graph(10)
comp = girvan_newman(G)
tuple(sorted(c) for c in next(comp))
# ([0, 1, 2, 3, 4], [5, 6, 7, 8, 9])

And for retrieving more communities with itertools.islice:

import itertools
G = nx.path_graph(8)
k = 2
comp = girvan_newman(G)
for communities in itertools.islice(comp, k):
    print(tuple(sorted(c) for c in communities))
# ([0, 1, 2, 3], [4, 5, 6, 7])
# ([0, 1], [2, 3], [4, 5, 6, 7])
Related