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 email@example.com. 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.
- 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)
- Find any simple cycle in an undirected unweighted Graph
- Print negative weight cycle in a Directed Graph
- Detect cycle in an undirected graph
- Number of single cycle components in an undirected graph
- Detect cycle in an undirected graph using BFS
- Shortest cycle in an undirected unweighted graph
- Karp's minimum mean (or average) weight cycle algorithm
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Detect cycle in the graph using degrees of nodes of graph
- Sum of the minimum elements in all connected components of an undirected graph
- Minimum number of edges required to be removed from an Undirected Graph to make it acyclic
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Find k-cores of an undirected graph
- Program to find Circuit Rank of an Undirected Graph
- Find if an undirected graph contains an independent set of a given size
- Find all cliques of size K in an undirected graph
Improved By : Saksham Bhardwaj