Find cycle of shortest length in a directed graph with positive weights

minimum weight cycle in directed graph
maximum weight cycle
dijkstra's algorithm
floyd-warshall algorithm
weighted undirected graph
unweighted graph
weighted graph geeksforgeeks
give an algorithm to find the negative weight cycle with the minimum number of edges in a graph

I was asked this question in an interview, but I couldn't come up with any decent solution. So, I told them the naive approach of finding all the cycles then picking the cycle with the least length.

I'm curious to know what is an efficient solution to this problem.

You can easily modify Floyd-Warshall algorithm. (If you're not familiar with graph theory at all, I suggest checking it out, e.g. getting a copy of Introduction to Algorithms).

Traditionally, you start path[i][i] = 0 for each i. But you can instead start from path[i][i] = INFINITY. It won't affect algorithm itself, as those zeroes weren't used in computation anyway (since path path[i][j] will never change for k == i or k == j).

In the end, path[i][i] is the length the shortest cycle going through i. Consequently, you need to find min(path[i][i]) for all i. And if you want cycle itself (not only its length), you can do it just like it's usually done with normal paths: by memorizing k during execution of algorithm.

In addition, you can also use Dijkstra's algorithm to find a shortest cycle going through any given node. If you run this modified Dijkstra for each node, you'll get the same result as with Floyd-Warshall. And since each Dijkstra is O(n^2), you'll get the same O(n^3) overall complexity.

Final Solutions, How do you find the shortest path in a directed graph? Minimum weighted cycle is : Minimum weighed cycle : 7 + 1 + 6 = 14 or 2 + 6 + 2 + 4 = 14. The idea is to use shortest path algorithm. We one by one remove every edge from graph, then we find shortest path between two corner vertices of it. We add an edge back before we process next edge.

The pseudo code is a simple modification of Dijkstra's algorithm.

for all u in V:
   for all v in V:
      path[u][v] = infinity

for all s in V:
   path[s][s] = 0
   H = makequeue (V) .. using pathvalues in path[s] array as keys
   while H is not empty:
      u = deletemin(H)
      for all edges (u,v) in E:
         if path[s][v] > path[s][u] + l(u, v) or path[s][s] == 0:
            path[s][v] = path[s][u] + l(u,v)
         decreaseKey(H, v)

lengthMinCycle = INT_MAX

for all v in V:
   if path[v][v] < lengthMinCycle & path[v][v] != 0 :
      lengthMinCycle = path[v][v]

if lengthMinCycle == INT_MAX:
   print("The graph is acyclic.")

else:
   print("Length of minimum cycle is ", lengthMinCycle)

Time Complexity: O(|V|^3)

Shortest Path in a weighted Graph where weight of an edge is 1 or 2 , How do you find the shortest cycle on a graph? )-time algorithm that nds a shortest cycle in a directed or undirected graph with integer edge weights in f1;:::;Mg. Their algorithm can also handle weights in f M;:::;Mgin the directed setting. If one settles for an approximation algorithm, then faster algorithms exist. For undirected graphs, Itai and Rodeh’s algorithm can be

Shortest Path Algorithms Tutorials & Notes | Algorithms, How do you find the shortest path in a weighted graph? Detect Cycle in Directed Graph Algorithm - Duration: 10:43. Tushar Roy - Coding Made Simple 122,323 views

What you will have to do is to assign another weight to each node which is always 1. Now run any shortest path algorithm from one node to the same node using these weights. But while considering the intermediate paths, you will have to ignore the paths whose actual weights are negative.

[PDF] Solution: A shortest cycle containing s must be composed of some , between vertices in a graph such that the total sum of the edges weights is minimum. This problem could be solved easily using (BFS) if all edge weights were ( ), but here weights can take any value. Shortest Path Problem Shortest Path with Negative Weights Given directed graph G with weighted edges (weights may be positive or negative), nd the shortest path from s to t.

We can also use branch and bound algorithm for travelling salesman problem, as your question matches with TSP. http://lcm.csa.iisc.ernet.in/dsa/node187.html

Find minimum weight cycle in an undirected graph, Given a directed graph G = (V, E) with non-negative edge lengths l(e),e ∈ E describe an algorithm to find the length of a shortest cycle containing the node s. vertex into two pieces, and insert an edge between them with weight equal to  FindShortestPath[g, s, t] finds the shortest path from source vertex s to target vertex t in the graph g. FindShortestPath[g, s, All] generates a ShortestPathFunction[] that can be applied repeatedly to different t.

Shortest path with exactly k edges in a directed and weighted graph , Convert the undirected graph into directed graph such that there is no path of length greater than 1 · Find the weight of the minimum spanning tree. Article Tags :. The algorithm you have suggested will indeed find the shortest path in this graph, but not all graphs in general. For example, consider this graph: Assume the edges are directed from left to right as in your example, Your algorithm will work as follows: First, you set d(A) to zero and the other distances to infinity.

[PDF] CS 3510 - Design & Analysis of Algorithms, path in a Matrix · Longest alternating subsequence in terms of positive and negative integers Given a directed and two vertices 'u' and 'v' in it, find shortest path from 'u' to 'v' with exactly k edges on the path. The idea is to browse through all paths of length k from u to v using the Loop for number of edges from 0 to k. Recall that in Dijkstra's algorithm, once a vertex is marked as "closed" (and out of the open set) - the algorithm found the shortest path to it, and will never have to develop this node again - it assumes the path developed to this path is the shortest. But with negative weights - it might not be true.

Shortest Paths, is considered only once (in directed graphs) or twice (in the undirected Dijkstra's algorithm: Task - given a weighted (directed or undirected graph with positive to determine the length of the shortest cycle in the graph. Bellman–Ford algorithm is used to compute the shortest paths from a single source vertex to all of the other vertices in given weighted digraph. It can be modified to report any negative-weight cycle in the graph. To find if the graph contains negative weight cycle, we run Bellman-Ford once from each vertex.

Comments
  • This is not as per the requirements. In these algorithms the shortest means the least weighted and not the least length. e.g. if there are two cycles like 1,2,3 and then 100,500; then cycle 1 will be chosen, but what is required is cycle 2 as it has shortest length. Correct me if I am wrong.
  • @Manoj Second problem is a subset of first. Just assign weight 1 to each edge and you'll receive path with smallest number of edges. The real problem (although small one) is that neither Dijkstra, nor Floyd-Warshal find a shortest path from node back to itself. You'll have to tweak them a little.
  • Thanks..i used the modified version of Dijkstra and it worked
  • I think you can do this in O(n^2) times. With 2 dijkstra runs, and two nested for loops will do that.
  • Could you make this work for undirected graphs as well?
  • You first set path[s][s] to 0, then in if you have ... or path[s][s] == 0, so keep changing things, even if it's longer??? Typo?
  • Nice try, but this has exponential complexity at best. I suspect, Robert Sedgwick is solving some more general and complex problem in his book, as this one is much easier.