Ayn
Ayn

Reputation: 93

Networkx- IndexError: list index out of range while using (greedy_modularity_communities

I am using Python 3.7.1 and networkx 2.2. I used networkx to generate my directed graph and I want to calculate the communities of the graph with networkx.algorithms.community.modularity_max.greedy_modularity_communities in following steps:

import networkx as nx
from networkx.algorithms.community import greedy_modularity_communities
G=nx.DiGraph()
G.add_nodes_from([1,10])
G.add_edges_from([(1,2),(3,4),(5,6),(7,1),(2,10),(3,8),(9,8)])
c = list(greedy_modularity_communities(G))
sorted(c[0])

I recive an error:

IndexError: list index out of range

Upvotes: 3

Views: 1967

Answers (1)

Joel
Joel

Reputation: 23887

I suspect your problem is that your graph is directed. The documentation of greedy_modularity_communities suggests that it expects the input to be a Graph, but yours is a DiGraph.

If I do

H = nx.Graph(G)
c = list(greedy_modularity_communities(H))

I do not get an error. I'm not sure whether the communities it finds in H will be what you're interested in.

Upvotes: 3

Related Questions