Holooly Plus Logo

Question 18.P.6: (S) The Floyd-Warshall algorithm runs in O(n³) time on graph......

(S) The Floyd-Warshall algorithm runs in O(n³) time on graphs with n vertices and m edges, whether or not the input graph contains a negative cycle. Modify the algorithm so that it solves the all-pairs shortest path problem in O(mn) time for input graphs with a negative cycle and O(n³) time otherwise.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Modify the input graph G = (V,E) by adding a new source vertex s and a new zero-length edge from s to each vertex v ∈ V . The new graph G^{\prime} has a negative cycle reachable from s if and only if G has a negative cycle. Run the Bellman-Ford algorithm on G^{\prime} with source vertex s to check whether G contains a negative cycle. If not, run the Floyd-Warshall algorithm on G.

Related Answered Questions

Question: 18.P.4

Verified Answer:

With columns indexed by k and rows by vertex pairs...
Question: 18.P.5

Verified Answer:

With columns indexed by k and rows by vertex pairs...
Question: 18.P1

Verified Answer:

With columns indexed by i and rows by vertices: