我正在使用Python的networkx包。
>>> import networkx as nx >>> G=nx.empty_graph() >>> G.add_edge(1,2) >>> G.add_edge(2,3) >>> G.add_edge(4,5) >>> nx.path.bidirectional_dijkstra(G,1,2) (1, [1, 2]) >>> nx.path.bidirectional_dijkstra(G,1,3) (2, [1, 2, 3]) >>> nx.path.bidirectional_dijkstra(G,1,4) False >>> nx.path.bidirectional_dijkstra(G,1,5) False >>>
您还可以将结果用作布尔值
>>> if nx.path.bidirectional_dijkstra(G,1,2): print "path exists" ... path exists >>> if nx.path.bidirectional_dijkstra(G,1,4): print "path exists" ... >>>