2

How can the Floyd-Warshall algorithm be modified to find the shortest path of any negative cost cycle of a directed graph that maintains O(V^3) time complexity?

mkj
  • 2,761
  • 5
  • 24
  • 28
quinn1414
  • 21
  • 1

1 Answers1

7

There is no shortest path in a graph with negative cycle, for every path - one can find a shorter one by traversing the cycle one more time.

If you are referring to Shortest Simple Path (each vertex can be visited at most once) - it cannot be done, unless P=NP, and it most likely isn't.

Assume you have an efficient shortest simple path algorithm A.
Given an instance of the Longest Path Problem and a graph G=(V,E,w), create a new graph G'=(V,E,w') where w'(u,v) = -1*w(u,v). Now invoke A on G', and you got the shortest simple path on G' - which is the longest path on G.

However, since Longest Path is NP-Hard - such a solution is not possible unless P=NP.


tl;dr:

  • In a graph with negative cycle, there is no such thing as shortest path.
  • You cannot find a shortest simple path in a graph with negative cycles in O(V^3) time (unless P=NP, and even then it's not sure to be O(V^3)).
amit
  • 175,853
  • 27
  • 231
  • 333
  • This is what I thought, it make sense that there is no shortest path since each iteration of the cycle would result in a "smaller" weight. However, I have come across this question in homework and from looking online. My gut is telling me that the answer is that there is no such modification that could make this algorithm work for negative cycles. – quinn1414 Feb 24 '15 at 19:05
  • "In a graph with a negative cycle, there is no such thing as a shortest path": true, at least if you mean "finite shortest path". But it is possible to find the negative cycle with the smallest number of edges using a simple breadth-first search, and it is quite possible that "shortest path of any negative cost cycle" means exactly that. – rici Feb 24 '15 at 19:53
  • Can you elaborate on how to use BFS to find the smallest number of edges for a negative cycle? – quinn1414 Feb 24 '15 at 21:16