Detection of Cycle in an Undirected Graph
Last modified: July 21, 2026
Approach 1: Using BFS
- You traverse through the graph using BFS from any starting point. but instead of just storing nodes we store nodes and their parents as tuple (node,parent)
- When you are adding the neighbours to the queue after visiting them you first check if they have been visited.
- Now in case they are visited already just check if that neighbour is the parent of the current node or not. If there is no cycle , the node is connected to only one neighbour and if has been visited means its through that neighbour only, and hence neighbour = parent.
- If not then there must be a cycle, return True.
#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
bool detect(int src, vector<int> adj[], int vis[]) {
vis[src] = 1;
// store <source node, parent node>
queue<pair<int,int>> q;
q.push({src, -1});
// traverse until queue is not empty
while(!q.empty()) {
int node = q.front().first;
int parent = q.front().second;
q.pop();
// go to all adjacent nodes
for(auto adjacentNode: adj[node]) {
// if adjacent node is unvisited
if(!vis[adjacentNode]) {
vis[adjacentNode] = 1;
q.push({adjacentNode, node});
}
// if adjacent node is visited and is not it's own parent node
else if(parent != adjacentNode) {
// yes it is a cycle
return true;
}
}
}
// there's no cycle
return false;
}
public:
// Function to detect cycle in an undirected graph.
bool isCycle(int V, vector<int> adj[]) {
// initialise them as unvisited
int vis[V] = {0};
for(int i = 0;i<V;i++) {
if(!vis[i]) {
if(detect(i, adj, vis)) return true;
}
}
return false;
}
};
int main() {
// V = 4, E = 2
vector<int> adj[4] = {{}, {2}, {1, 3}, {2}};
Solution obj;
bool ans = obj.isCycle(4, adj);
if (ans)
cout << "1\n";
else
cout << "0\n";
return 0;
}