Networkx - Shortest path length

后端 未结 2 1381
醉梦人生
醉梦人生 2021-02-04 10:17

I\'m using networkx to manage large network graph which consists of 50k nodes.

I want to calculate the shortest path length between a specific set of node

2条回答
  •  执念已碎
    2021-02-04 10:47

    import networkx as nx
    G=nx.Graph()
    G.add_nodes_from([1,2,3,4])
    G.add_edge(1,2)
    G.add_edge(3,4)
    try:
        n=nx.shortest_path_length(G,1,4)
        print n
    except nx.NetworkXNoPath:
        print 'No path'
    

提交回复
热议问题