All pairs shortest paths
Input
- A directed graph [math]\displaystyle{ G = (V,A) }[/math]
- An arc length [math]\displaystyle{ l(a) \in \mathbb{R} }[/math] for each arc [math]\displaystyle{ a \in A }[/math]
Output
For each pair [math]\displaystyle{ (v,w) \in A }[/math] with [math]\displaystyle{ v,w \in V }[/math], the length [math]\displaystyle{ \Delta(v,w) }[/math] of a shortest [math]\displaystyle{ (v,w) }[/math]-path in [math]\displaystyle{ G }[/math] with respect to [math]\displaystyle{ \ell }[/math] among all paths that have at most [math]\displaystyle{ |V| }[/math] arcs.
Remark: Obviously, if no [math]\displaystyle{ (v,w) }[/math]-path meets any negative cycle, a shortest path exists, and at least one shortest path is simple (because such a path may only contain cycles of zero total length, which may be removed). This path has at most [math]\displaystyle{ |V|-1 }[/math] arcs.
On the other hand, if there are negative cycles, there is, evidently, at least one simple negative cycle. A simple cycle hast at most [math]\displaystyle{ |V| }[/math] arcs. Therefore, the distance from [math]\displaystyle{ v }[/math] in the output is negative. Of course, the case [math]\displaystyle{ v=w }[/math] is included.
Complexity
Polynomial
Known algorithms
- Floyd-Warshall
- Bellman-Ford
- Shortest paths by repeated squaring (variant of Bellman-Ford)