Question

"Let G be a directed weighted graph with no negative cycles. Design an algorithm to find a minimum weight cycle in G that runs with a time complexity of O(|V|^3)."

The above is a question I have been working on as part of my coursework. When I first read it, I immediately thought that the Floyd-Warshall algorithm would solve this problem - mainly because F-W runs in O(|V|^3) time and it works for both positive and negative weighted graphs with no negative cycles. However, I soon remembered that F-W is designed to find the shortest path of a graph, not a minimum weight cycle.

Am I on the right track with this question? Would it be possible to modify the Floyd-Warshall algorithm to find a minimum weight cycle in a graph?

Was it helpful?

Solution

I think you are pretty close. Once you do FW loop in O(|V|^3), you have the weight of a shortest path between each pair of vertices, let's call it D(i, j). Now the solution to our main problem is as follows: Brute force for the vertex which is gonna be in a loop, say it's X. Also brute force on Y, which is gonna be the last vertex in the cycle, before X itself. The weight of this cycle is obviously D(X, Y) + W(Y, X). Then you just choose the one with the smallest value. It's obviously a correct answer, because: (1) All these values of D(X,Y)+D(Y,X) represent the weight of some(maybe non-simple) cycle; (2) If the optimal cycle is some a->...->b->a, then we consider it when X=a, Y=b;

To reconstruct the answer, first you need to keep track of which X, Y gave us the optimal result. Once we have this X and Y, the only thing remaining is to find the shortest path from X to Y, which can be done in various ways.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top