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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

4


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.