## Detect cycle in undirected graph

This video explains how to detect cycle in an undirected graph. I have explained the graph coloring method for this problem. This problem is very frequently asked in coding round as well as in interview.

```// { Driver Code Starts
#include<bits/stdc++.h>

using namespace std;

// } Driver Code Ends
/* This function is used to detect a cycle in undirected graph
*  adj[]: array of vectors to represent graph
*  V: number of vertices
*/
bool isCyclic_util(vector<int> adj[], vector<int> visited, int curr)
{
if(visited[curr]==2)
return true;

visited[curr] = 1;
bool FLAG = false;
{
else
{
if(FLAG==true)
return true;
}
}
return false;
}

{
vector<int> visited(V,0);
bool FLAG = false;
for(int i=0;i<V;++i)
{
visited[i] = 1;
{
if(FLAG==true)
return true;
}
visited[i] = 0;
}
return false;
}

// { Driver Code Starts.

int main()
{
int T;
cin>>T;
while(T--)
{
int V, E;
cin>>V>>E;

// array of vectors to represent graph

int u, v;
for(int i=0;i<E;i++)
{
cin>>u>>v;

// adding edge to the graph