Networkx - Shortest path length

后端 未结 2 1384
醉梦人生
醉梦人生 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'
    
    0 讨论(0)
  • 2021-02-04 10:49

    Alternatively, depending on the type of graph--namely, directed, strongly or weakly connected, or undirected--create component subgraphs (sub_G), that is,

    (G.subgraph(c) for c in connected_components(G))

    or if directed:

    nx.weakly_connected_component_subgraphs(G) or nx.strongly_connected_component_subgraphs(G)

    Furthermore, given sub_G is a directed graph, check for the strength of its connections, e.g.

    nx.is_strongly_connected(sub_G) or ng.is_weakly_connected(sub_G)

    Combined or individually, these recommendations'll reduce unnecessary checking of paths that do not exist due to the nature of the component subgraph(s).

    0 讨论(0)
提交回复
热议问题