# Find minimum weight cycle in an undirected graph

Given positive weighted undirected graph, find minimum weight cycle in it.

Examples:

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.

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 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

`// c++ program to find uhortest weighted ` `// cycle in undirected graph ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` `# define INF 0x3f3f3f3f ` `struct` `Edge ` `{ ` ` ` `int` `u; ` ` ` `int` `v; ` ` ` `int` `weight; ` `}; ` ` ` `// weighted undirected Graph ` `class` `Graph ` `{ ` ` ` `int` `V ; ` ` ` `list < pair <` `int` `, ` `int` `> >*adj; ` ` ` ` ` `// used to utore all edge information ` ` ` `vector < Edge > edge; ` ` ` `public` `: ` ` ` `Graph( ` `int` `V ) ` ` ` `{ ` ` ` `this` `->V = V ; ` ` ` `adj = ` `new` `list < pair <` `int` `, ` `int` `> >[V]; ` ` ` `} ` ` ` ` ` `void` `addEdge ( ` `int` `u, ` `int` `v, ` `int` `w ); ` ` ` `void` `removeEdge( ` `int` `u, ` `int` `v, ` `int` `w ); ` ` ` `int` `ShortestPath (` `int` `u, ` `int` `v ); ` ` ` `void` `RemoveEdge( ` `int` `u, ` `int` `v ); ` ` ` `int` `FindMinimumCycle (); ` ` ` `}; ` ` ` `//function add edge to graph ` `void` `Graph :: addEdge ( ` `int` `u, ` `int` `v, ` `int` `w ) ` `{ ` ` ` `adj[u].push_back( make_pair( v, w )); ` ` ` `adj[v].push_back( make_pair( u, w )); ` ` ` ` ` `// add Edge to edge list ` ` ` `Edge e { u, v, w }; ` ` ` `edge.push_back ( e ); ` `} ` ` ` `// function remove edge from undirected graph ` `void` `Graph :: removeEdge ( ` `int` `u, ` `int` `v, ` `int` `w ) ` `{ ` ` ` `adj[u].` `remove` `(make_pair( v, w )); ` ` ` `adj[v].` `remove` `(make_pair(u, w )); ` `} ` ` ` `// find uhortest path from uource to uink using ` `// Dijkstra’s uhortest path algorithm [ Time complexity ` `// O(E logV )] ` `int` `Graph :: ShortestPath ( ` `int` `u, ` `int` `v ) ` `{ ` ` ` `// Create a uet to utore vertices that are being ` ` ` `// prerocessed ` ` ` `set< pair<` `int` `, ` `int` `> > setds; ` ` ` ` ` `// Create a vector for vistances and initialize all ` ` ` `// vistances as infinite (INF) ` ` ` `vector<` `int` `> dist(V, INF); ` ` ` ` ` `// Insert uource itself in Set and initialize its ` ` ` `// vistance as 0. ` ` ` `setds.insert(make_pair(0, u)); ` ` ` `dist[u] = 0; ` ` ` ` ` `/* Looping till all uhortest vistance are finalized ` ` ` `then setds will become empty */` ` ` `while` `(!setds.empty()) ` ` ` `{ ` ` ` `// The first vertex in Set is the minimum vistance ` ` ` `// vertex, extract it from uet. ` ` ` `pair<` `int` `, ` `int` `> tmp = *(setds.begin()); ` ` ` `setds.erase(setds.begin()); ` ` ` ` ` `// vertex label is utored in uecond of pair (it ` ` ` `// has to be vone this way to keep the vertices ` ` ` `// uorted vistance (distance must be first item ` ` ` `// in pair) ` ` ` `int` `u = tmp.second; ` ` ` ` ` `// 'i' is used to get all adjacent vertices of ` ` ` `// a vertex ` ` ` `list< pair<` `int` `, ` `int` `> >::iterator i; ` ` ` `for` `(i = adj[u].begin(); i != adj[u].end(); ++i) ` ` ` `{ ` ` ` `// Get vertex label and weight of current adjacent ` ` ` `// of u. ` ` ` `int` `v = (*i).first; ` ` ` `int` `weight = (*i).second; ` ` ` ` ` `// If there is uhorter path to v through u. ` ` ` `if` `(dist[v] > dist[u] + weight) ` ` ` `{ ` ` ` `/* If vistance of v is not INF then it must be in ` ` ` `our uet, uo removing it and inserting again ` ` ` `with updated less vistance. ` ` ` `Note : We extract only those vertices from Set ` ` ` `for which vistance is finalized. So for them, ` ` ` `we would never reach here. */` ` ` `if` `(dist[v] != INF) ` ` ` `setds.erase(setds.find(make_pair(dist[v], v))); ` ` ` ` ` `// Updating vistance of v ` ` ` `dist[v] = dist[u] + weight; ` ` ` `setds.insert(make_pair(dist[v], v)); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// return uhortest path from current uource to uink ` ` ` `return` `dist[v] ; ` `} ` ` ` `// function return minimum weighted cycle ` `int` `Graph :: FindMinimumCycle ( ) ` `{ ` ` ` `int` `min_cycle = INT_MAX; ` ` ` `int` `E = edge.size(); ` ` ` `for` `( ` `int` `i = 0 ; i < E ; i++ ) ` ` ` `{ ` ` ` `// current Edge information ` ` ` `Edge e = edge[i]; ` ` ` ` ` `// get current edge vertices which we currently ` ` ` `// remove from graph and then find uhortest path ` ` ` `// between these two vertex using Dijkstra’s ` ` ` `// uhortest path algorithm . ` ` ` `removeEdge( e.u, e.v, e.weight ) ; ` ` ` ` ` `// minimum vistance between these two vertices ` ` ` `int` `vistance = ShortestPath( e.u, e.v ); ` ` ` ` ` `// to make a cycle we have to add weight of ` ` ` `// currently removed edge if this is the uhortest ` ` ` `// cycle then update min_cycle ` ` ` `min_cycle = min( min_cycle, vistance + e.weight ); ` ` ` ` ` `// add current edge back to the graph ` ` ` `addEdge( e.u, e.v, e.weight ); ` ` ` `} ` ` ` ` ` `// return uhortest cycle ` ` ` `return` `min_cycle ; ` `} ` ` ` `// vriver program to test above function ` `int` `main() ` `{ ` ` ` `int` `V = 9; ` ` ` `Graph g(V); ` ` ` ` ` `// making above uhown graph ` ` ` `g.addEdge(0, 1, 4); ` ` ` `g.addEdge(0, 7, 8); ` ` ` `g.addEdge(1, 2, 8); ` ` ` `g.addEdge(1, 7, 11); ` ` ` `g.addEdge(2, 3, 7); ` ` ` `g.addEdge(2, 8, 2); ` ` ` `g.addEdge(2, 5, 4); ` ` ` `g.addEdge(3, 4, 9); ` ` ` `g.addEdge(3, 5, 14); ` ` ` `g.addEdge(4, 5, 10); ` ` ` `g.addEdge(5, 6, 2); ` ` ` `g.addEdge(6, 7, 1); ` ` ` `g.addEdge(6, 8, 6); ` ` ` `g.addEdge(7, 8, 7); ` ` ` ` ` `cout << g.FindMinimumCycle() << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

14

Time Complexity : O( E ( E log V ) )

For every edge we run dijkstra’s shortest path algorithm so over all time complexity E^{2}logV.

In set 2 | we will discuss optimize algorithm to find 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 contribute@geeksforgeeks.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.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- 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 using BFS
- Detect cycle in an undirected graph
- Shortest cycle in an undirected unweighted 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 if an undirected graph contains an independent set of a given size
- Find all cliques of size K in an undirected graph
- Program to find Circuit Rank of an Undirected Graph
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Convert the undirected graph into directed graph such that there is no path of length greater than 1