简体   繁体   中英

Find diameter and average shortest path length of a graph's giant component using igraph python

I want to calculate the average shortest path length and diameter of a graph's giant component. The files are represented in .mat format.Is there any built-in function to do so?

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

Diameter of giant components

As per this answer , we can find the giant component with the following function as follows.

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)))

Its diameter can be found as giant_component(graph).diameter() .

Average shortest path

The Graph.shortest_paths function will return a matrix containing all shortest path lengths, of which you can then compute the average.

np.mean(graph.shortest_paths())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM