我想计算图的巨型组件的平均最短路径长度和直径。这些文件以.mat格式表示。有内置的功能吗?
data = loadmat("filename.mat")
data=coo_matrix(data.get('A'))
graph= igraph.Graph(zip(data.row.tolist(), data.col.tolist()))
答案 0 :(得分:0)
根据this answer,我们可以找到具有以下功能的巨型组件。
def giant_component(graph):
"""Compute giant component.
Returns:
The giant component of `graph` as an `igraph.Graph`.
"""
vc = graph.components()
vc_sizes = vc.sizes()
return vc.subgraph(vc_sizes.index(max(vc_sizes)))
其直径可以找到giant_component(graph).diameter()
。
Graph.shortest_paths
函数将返回一个包含所有最短路径长度的矩阵,然后您可以计算出其平均值。
np.mean(graph.shortest_paths())