使用igraph python查找图的巨型组件的直径和平均最短路径长度

时间:2020-03-12 06:44:28

标签: python igraph

我想计算图的巨型组件的平均最短路径长度和直径。这些文件以.mat格式表示。有内置的功能吗?

data = loadmat("filename.mat")
data=coo_matrix(data.get('A'))
graph= igraph.Graph(zip(data.row.tolist(), data.col.tolist()))

1 个答案:

答案 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())