I am using networkx package of Python.
What if path does not exist between 2 given nodes? What does the function return then?
Bruce
2010-03-01 03:46:47
I don't want to find the shortest path. I just want to know if a path exists between 2 given nodes.
Bruce
2010-03-01 03:52:24
+2
A:
>>> 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
>>>
You can also use the result as a boolean value
>>> if nx.path.bidirectional_dijkstra(G,1,2): print "path exists"
...
path exists
>>> if nx.path.bidirectional_dijkstra(G,1,4): print "path exists"
...
>>>
gnibbler
2010-03-01 03:51:53
+1
A:
Use
shortest_path(G, source, target)
or one of the Shortest Path methods. Stay clear of the methods which return paths between all nodes however if you merely have two specific nodes to test for connectivity.
mjv
2010-03-01 03:52:41
A:
for e in G.edges():
if node1 in e and node2 in e:
return True
return False
inspectorG4dget
2010-03-01 03:53:09
This returns true only if an edge exists directly between the nodes. A path can be longer than one edge.
John Källén
2010-05-17 11:46:06