I am trying to calculate the diameter of each community in my dataset, Zachary's karate club using Jupyter. I created a loop to iterate through, but it gives me the diameter of the whole network rather than of each community.

import pandas as pd 
data = pd.read_csv('zachary.txt',sep =" ", header = None)
data_values = data.values
g = Graph()
new_data = data_values.tolist()
data_graph = g.Adjacency(new_data, mode = 'undirected')
s = data_graph.community_infomap()
s_List = list(s)
for ic in s_List:
    y = data_graph.diameter(ic)

I expect the output to be like "$1,2,2$" or "$1,3,1$" but the actual output is "$5,5,5$", which is the diameter of the whole community.

  • $\begingroup$ Is this python-igraph? First thought (I'm not too familiar with the package): you might need to actually get the induced subgraph on each cluster and call diameter on those; I don't see an argument for diameter that would take a vertex set like you're passing. $\endgroup$
    – Ben Reiniger
    Jun 11, 2019 at 19:03
  • $\begingroup$ yes this is python-igraph. Do you know how to get the subgraph? @BenReiniger $\endgroup$ Jun 11, 2019 at 19:23

1 Answer 1


The method diameter doesn't appear to take vertex sets as a parameter. Instead, call diameter on the appropriate induced subgraphs:

for ic in s_List:
    y = data_graph.induced_subgraph(ic).diameter()

Edit: Erm, I guess this depends on exactly what you want to measure. While it's rather against the purpose of "communities," it is possible that the distance between a pair of vertices in your community is longer if the paths are restricted to the community than if they can pass through the rest of the graph. Inducing the subgraphs as above would miss that.

  • $\begingroup$ Thank you so much, that was it! @Ben Reiniger $\endgroup$ Jun 12, 2019 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.