Given a positive weighted undirected graph, find the minimum weight cycle in it.
The idea is to use shortest path algorithm. We one by one remove every edge from the graph, then we find the shortest path between two corner vertices of it. We add an edge back before we process the next edge.
1). create an empty vector 'edge' of size 'E' ( E total number of edge). Every element of this vector is used to store information of all the edge in graph info 2) Traverse every edge edge[i] one - by - one a). First remove 'edge[i]' from graph 'G' b). get current edge vertices which we just removed from graph c). Find the shortest path between them "Using Dijkstra’s shortest path algorithm " d). To make a cycle we add the weight of the removed edge to the shortest path. e). update min_weight_cycle if needed 3). return minimum weighted cycle
Below c++ implementation of above idea
Time Complexity: O( E ( E log V ) )
For every edge, we run Dijkstra’s shortest path algorithm so over all time complexity E2logV.
In set 2 | we will discuss optimize the algorithm to find a minimum weight cycle in undirected graph.
This article is contributed by Nishant Singh . 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if there is a cycle with odd weight sum in an undirected graph
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Detect cycle in an undirected graph
- Detect cycle in an undirected graph using BFS
- Shortest cycle in an undirected unweighted graph
- Print negative weight cycle in a Directed Graph
- Number of single cycle components in an undirected graph
- Karp's minimum mean (or average) weight cycle algorithm
- Sum of the minimum elements in all connected components of an undirected graph
- Find k-cores of an undirected graph
- Find all cliques of size K in an undirected graph
- Find if there is a path between two vertices in an undirected graph
- Find if an undirected graph contains an independent set of a given size
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Program to find Circuit Rank of an Undirected 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
- Detect cycle in the graph using degrees of nodes of graph
- Convert undirected connected graph to strongly connected directed graph
Improved By : Saksham Bhardwaj