Find minimum weight cycle in an undirected graph
Given a positive weighted undirected graph, find the minimum weight cycle in it.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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 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 is the implementation of the 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.