Shortest cycle in an undirected unweighted graph
Given an undirected unweighted graph. The task is to find the length of the shortest cycle in the given graph. If no cycle exists print -1.
Examples:
Input:
Output: 4
Cycle 6 -> 1 -> 5 -> 0 -> 6
Input:
Output: 3
Cycle 6 -> 1 -> 2 -> 6
Prerequisites: Dijikstra
Approach: For every vertex, we check if it is possible to get the shortest cycle involving this vertex. For every vertex first, push current vertex into the queue and then it’s neighbours and if vertex which is already visited comes again then the cycle is present.
Apply the above process for every vertex and get the length of the shortest cycle.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define N 100200 vector<int> gr[N]; // Function to add edge void Add_edge(int x, int y) { gr[x].push_back(y); gr[y].push_back(x); } // Function to find the length of // the shortest cycle in the graph int shortest_cycle(int n) { // To store length of the shortest cycle int ans = INT_MAX; // For all vertices for (int i = 0; i < n; i++) { // Make distance maximum vector<int> dist(n, (int)(1e9)); // Take a imaginary parent vector<int> par(n, -1); // Distance of source to source is 0 dist[i] = 0; queue<int> q; // Push the source element q.push(i); // Continue until queue is not empty while (!q.empty()) { // Take the first element int x = q.front(); q.pop(); // Traverse for all it's childs for (int child : gr[x]) { // If it is not visited yet if (dist[child] == (int)(1e9)) { // Increase distance by 1 dist[child] = 1 + dist[x]; // Change parent par[child] = x; // Push into the queue q.push(child); } // If it is already visited else if (par[x] != child and par[child] != x) ans = min(ans, dist[x] + dist[child] + 1); } } } // If graph contains no cycle if (ans == INT_MAX) return -1; // If graph contains cycle else return ans; } // Driver code int main() { // Number of vertices int n = 7; // Add edges Add_edge(0, 6); Add_edge(0, 5); Add_edge(5, 1); Add_edge(1, 6); Add_edge(2, 6); Add_edge(2, 3); Add_edge(3, 4); Add_edge(4, 1); // Function call cout << shortest_cycle(n); return 0; } |
4
Recommended Posts:
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Shortest path in an unweighted graph
- Number of shortest paths in an unweighted and directed graph
- Multi Source Shortest Path in Unweighted Graph
- Detect cycle in an undirected graph using BFS
- Detect cycle in an undirected graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Number of single cycle components in an undirected graph
- Find minimum weight cycle in an undirected graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Detect cycle in the graph using degrees of nodes of graph
- Clone an Undirected Graph
- Sum of degrees of all nodes of a undirected graph
- Print all the cycles in an undirected graph
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





