Shortest Path routines are commonly used within more complex optimization methods that work to ensure there are no negative cycles even when there are negative weight edges. Often there are 0 weight cycles, which raises obvious precision issues. But we would expect some consistency in how those are handled.
Here's a case where
FindShortestPath[graph, i, 1]
succeeds for all vertices i
,
GraphDistanceMatrix[graph]
succeeds, but
FindShortestPath[graph, All, 1]
fails.
Any thoughts on why this occurs?
graph =
Graph[List[DirectedEdge[2, 4], DirectedEdge[1, 2], DirectedEdge[3, 3],
DirectedEdge[4, 4], DirectedEdge[2, 1], DirectedEdge[1, 1],
DirectedEdge[3, 2], DirectedEdge[4, 2]],
Rule[EdgeWeight,
List[
Rule[DirectedEdge[1,2], -0.01091784833945534840442836344586391687`18.130853970762928],
Rule[DirectedEdge[2,1], 0.01091784836435579080721835523637724884`18.13083252738191],
Rule[DirectedEdge[1,3], 0.05856056395863386924668038076285580822`19.161321314068676],
Rule[DirectedEdge[3,1], -0.0392119168158420557184565732775045898`18.98709027996202],
Rule[DirectedEdge[1,4], 0.04898657905283750869314558522357293851`18.57086486584574],
Rule[DirectedEdge[4,1], -0.27016936835296771144136460026425837676`18.996775256537283],
Rule[DirectedEdge[2,3], 0.05060741925025987557014918123693047741`19.097959401559123],
Rule[DirectedEdge[3,2], -0.05060741924007811018893480135813097558`19.097498250879173],
Rule[DirectedEdge[2,4], 0.28828503026977619631171524636984558167`18.63396198032326],
Rule[DirectedEdge[4,2], -0.28828503026977619631171524636984558168`18.778871438162028],
Rule[DirectedEdge[3,4], 0.06260132441378883638716246196054271422`24.48295254456055],
Rule[DirectedEdge[4,3], -0.13558038477833085576576255146647013499`18.744066844400113]]]]
There are no negative weight cycles:
GraphDistanceMatrix[graph]
yields {{0., 0.288285, 0.0109178, [Infinity]}, {-0.288285, 0., -0.277367, [Infinity]}, {-0.0109178, 0.277367, 0., [Infinity]}, {-0.0506074, 0.237678, -0.0396896, 0.}}
And FindShortestPath correctly handles vertex to vertex requests:
FindShortestPath[graph, 4, 1]
yields {4, 2, 1}
FindShortestPath[graph, 3, 1]
yields {3, 2, 1}
FindShortestPath[graph, 2, 1]
yields {2, 1}
But
pathfunc = FindShortestPath[graph, All, 1];
pathfunc[4]
yields {} as does pathfunc[i]
with any other vertex i