pythonnetworkxfloyd-warshall

Python networkx: Does the floyd_warshall_numpy work properly?


Can someone confirm my finding the implementation of floyd_warshall_numpy method of networkx 2.5 is incorrect?

The code to reproduce is:

G = nx.balanced_tree(2, 3)
print(G.nodes())
print(nx.shortest_path(G, 2, 13))
print(nx.floyd_warshall_numpy(G, [2, 8, 13]))

My output is

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[2, 6, 13]
[[ 0. inf inf]
 [inf  0. inf]
 [inf inf  0.]] 

I expected non Inf distances are computed for all [2, 8, 13] node pairs as the shortest path exists among all of them. It seems to me this implementation tries somehow to find path in a subgraph.

nx.floyd_warshall_numpy(G)

works correctly for all nodes. I find the documentation not intuitive here. https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.dense.floyd_warshall_numpy.html#networkx.algorithms.shortest_paths.dense.floyd_warshall_numpy


Solution

  • I've looked at the source code, and what's happening is that the algorithm only considers paths that involve the nodes you've given it.

    So since there is no path between 2 and 8 that only includes the nodes 2, 8, and 13, it's returning inf.

    I am not sure how best it should be fixed - whether it's better to update the documentation or the method.