# How can I get the diameter of each community

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_values = data.values
g = Graph()
new_data = data_values.tolist()
data_graph = g.Adjacency(new_data, mode = 'undirected')
s = data_graph.community_infomap()
print(s)
s_List = list(s)
print(s_List)
for ic in s_List:
y = data_graph.diameter(ic)
print(y)

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.

• 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. – Ben Reiniger Jun 11 '19 at 19:03
• yes this is python-igraph. Do you know how to get the subgraph? @BenReiniger – HBCU Kid Jun 11 '19 at 19:23