Given a directed and strongly connected graph with non-negative edge weights. We define the mean weight of a cycle as the summation of all the edge weights of the cycle divided by the no. of edges. Our task is to find the minimum mean weight among all the directed cycles of the graph.
Input : Below Graph
Output : 1.66667
Method to find the smallest mean weight value cycle efficiently
Step 1: Choose first vertex as source. Step 2: Compute the shortest path to all other vertices on a path consisting of k edges 0 <= k <= V where V is number of vertices. This is a simple dp problem which can be computed by the recursive solution dp[k][v] = min(dp[k][v], dp[k-1][u] + weight(u,v) where v is the destination and the edge(u,v) should belong to E Step 3: For each vertex calculate max(dp[n][v]-dp[k][v])/(n-k) where 0<=k<=n-1 Step 4: The minimum of the values calculated above is the required answer.
Please refer to the solution of problem 9.2 here for proof that the above steps find minimum average weight.
Here the graph with no cycle will return value as -1.
Introduction to Algorithms Third Edition page 681 by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein
This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find minimum weight cycle in an undirected graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Print negative weight cycle in a Directed Graph
- Detect a negative cycle in a Graph using Shortest Path Faster Algorithm
- Find the weight of the minimum spanning tree
- Product of minimum edge weight between all pairs of a Tree
- Remove all outgoing edges except edge with minimum weight
- Minimum colors required such that edges forming cycle do not have same color
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- k'th heaviest adjacent node in a graph where each vertex has weight
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Count the nodes whose weight is a perfect square
- Count the nodes in the given tree whose weight is prime
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is a power of two
- Count the nodes in the given tree whose weight is even parity
- Count the nodes in the given tree whose sum of digits of weight is odd
- Count the nodes of the given tree whose weight has X as a factor
- Path with smallest product of edges with weight >= 1