Mathematica uses Bellman-Ford to find a shortest path between two vertices when the edge lengths are negative:
g = SparseArray[{{1,2} -> -1,{1,3} -> -1,{2,4} -> -1,{3,4} -> -1}]
GraphPath[g,1,4]
yields
{1,2,4}
I want to find all such shortest paths. In the example above, {1,3,4}
is another shortest path.
Goal: I'm testing a non-Mathematica shortest path algorithm against Mathematica. My algorithm also lazily finds only one shortest path, but it's different from the path Mathematica finds. I need to confirm my algorithm's shortest path really is a shortest path.
I realize I could just compare the path lengths (if they're equal, my algorithm has found a shortest path), but that wouldn't catch other errors in the algorithm (eg, if it's inventing edges or nodes or something).
I also realize I could have Mathematica find all paths or something and filter for the shortest ones, but that seems inefficient for larger graphs.
I've read Finding all shortest paths between two vertices, but
- I couldn't get it to work with Mathematica 9, and, more importantly
- it appears to be for positive-length edges.
NOTE: I realize I'm using a SparseArray for a graph above, but Mathematica seems to be OK with this.