CF84 - 5 months ago 11x

Python Question

I am computing all the possible shortest paths between any two nodes in a regular network. If the network is connected (e.g., largest component = entire network), I have no problems.

The problem arises when I have disconnected components: given that there is no path between node

`n`

`j`

`NetworkXNoPath`

`if`

My code for computing all the possible shortest paths between any two nodes in the graph:

`import networkx as nx`

counts=OrderedDict()

for n in F.nodes(): counts[n]=0

for n in F.nodes():

for j in F.nodes():

if (n!=j):

gener=nx.all_shortest_paths(F,source=n,target=j)

for p in gener:

for v in p: counts[v]+=1

`nx.bidirectional_dijkstra(F, n, j)`

`n`

`j`

`NetworkXNoPath`

Answer

you can use error handling in python for this.

```
try:
nx.bidirectional_dijkstra(F, n, j)
except NetworkXNoPath:
# do whatever you want
```

you can use this link for more help

Source (Stackoverflow)

Comments