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

问题描述 投票:0回答:1

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

data = loadmat("filename.mat")
data=coo_matrix(data.get('A'))
graph= igraph.Graph(zip(data.row.tolist(), data.col.tolist()))
python igraph
1个回答
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函数将返回一个包含所有最短路径长度的矩阵,然后您可以计算出其平均值。

Graph.shortest_paths
© www.soinside.com 2019 - 2024. All rights reserved.