Check whether an undirected graph contains cycle or not
Given an undirected graph, the task is to check whether it has a cycle or not, and if it has a cycle return the vertices of the cycle.
Examples:
Input:
Example-1
Output: Cycle exists. 3 -> 2 -> 1
Explanation: Cycle exists for 3 -> 2 ->1
Input:
Example-2
Output: Cycle does not exist.
Approach: This problem can be solved using DFS of Graph and Stack to store vertices of Graph.
- Create a variable x to store starting of the cycle and create a stack to store the vertices of the cycle.
- DFS traverses the given graph and marks the node as visited.
- For every child of this node check if the child has not visited DFS traverse the child.
- Otherwise, if the child is visited and also it is not the parent of the current node then we have detected the cycle and thus the value of x becomes the child node value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > vis;
vector<vector< int > > adj;
stack< int > s;
int x;
bool cycleDetect( int node, int parent)
{
bool detected = false ;
vis[node] = 1;
s.push(node);
for ( auto child : adj[node]) {
if (!vis[child]) {
detected = cycleDetect(child, node);
if (detected)
break ;
}
else if (child != parent) {
x = child;
return true ;
}
}
if (!detected)
s.pop();
return detected;
}
int main()
{
int n = 4;
vis.resize(n + 1, 0);
adj.resize(n + 1);
adj[1].push_back(2);
adj[1].push_back(3);
adj[3].push_back(1);
adj[3].push_back(2);
adj[2].push_back(3);
adj[2].push_back(4);
adj[2].push_back(1);
adj[4].push_back(2);
bool detected = false ;
for ( int i = 1; i <= n; i++) {
if (!vis[i])
detected = cycleDetect(i, 0);
if (detected)
break ;
}
if (detected) {
cout << "Cycle exists.\n" ;
cout << s.top();
s.pop();
while (s.top() != x) {
cout << " -> " << s.top();
s.pop();
}
cout << " -> " << s.top();
}
else
cout << "Cycle does not exist.\n" ;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int [] vis;
static ArrayList<ArrayList<Integer> > adj;
static Stack<Integer> s;
static int x;
static boolean cycleDetect( int node, int parent)
{
boolean detected = false ;
vis[node] = 1 ;
s.push(node);
for (Integer child : adj.get(node)) {
if (vis[child] == 0 ) {
detected = cycleDetect(child, node);
if (detected)
break ;
}
else if (child != parent) {
x = child;
return true ;
}
}
if (!detected)
s.pop();
return detected;
}
public static void main(String[] args)
{
int n = 4 ;
vis = new int [n + 1 ];
s = new Stack<>();
adj = new ArrayList<>();
for ( int i = 0 ; i < n + 1 ; i++) {
adj.add( new ArrayList<>());
}
adj.get( 1 ).add( 2 );
adj.get( 1 ).add( 3 );
adj.get( 3 ).add( 1 );
adj.get( 3 ).add( 2 );
adj.get( 2 ).add( 3 );
adj.get( 2 ).add( 4 );
adj.get( 2 ).add( 1 );
adj.get( 4 ).add( 2 );
boolean detected = false ;
for ( int i = 1 ; i <= n; i++) {
if (vis[i] == 0 )
detected = cycleDetect(i, 0 );
if (detected)
break ;
}
if (detected) {
System.out.println( "Cycle exists." );
System.out.print(s.peek());
s.pop();
while (s.peek() != x) {
System.out.print( " -> " + s.peek());
s.pop();
}
System.out.print( " -> " + s.peek());
}
else
System.out.println( "Cycle does not exist." );
}
}
|
Python3
x = 0
n = 4
vis = []
s = []
def cycleDetect(node,parent,s,adj,x):
detected = False
vis[node] = 1
s.append(node)
child = adj[node][ 0 ]
if (vis[child] = = 0 ):
detected = cycleDetect(child, node,s,adj,x)
elif (child ! = parent):
x = child
return x, True
if (detected = = False ):
s.pop()
return detected
for i in range ( 0 ,n + 1 ):
vis.append( 0 )
adj = []
for i in range ( 0 ,n + 1 ):
a = []
adj.append(a)
adj[ 1 ].append( 2 )
adj[ 1 ].append( 3 )
adj[ 3 ].append( 1 )
adj[ 3 ].append( 2 )
adj[ 2 ].append( 3 )
adj[ 2 ].append( 4 )
adj[ 2 ].append( 1 )
adj[ 4 ].append( 2 )
detected = False
for i in range ( 1 ,n + 1 ):
if (vis[i] = = False ):
x, detected = cycleDetect(i, 0 ,s,adj,x)
if (detected):
break
ans = ""
if (detected):
print ( "Cycle exists." )
ans + = str (s[ len (s) - 1 ])
s.pop()
while (s[ len (s) - 1 ] ! = x):
ans + = " -> " + str (s[ len (s) - 1 ])
s.pop()
ans + = " -> " + str (s[ len (s) - 1 ])
print (ans)
else :
print ( "Cycle does not exist." )
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
public
class GFG {
public
static int x;
public
static bool cycleDetect( int node, int parent,
Stack< int > s, int [] vis,
List<List< int > > adj)
{
bool detected = false ;
vis[node] = 1;
s.Push(node);
int child = adj[node][0];
if (vis[child] == 0) {
detected
= cycleDetect(child, node, s, vis, adj);
}
else if (child != parent) {
x = child;
return true ;
}
if (!detected)
s.Pop();
return detected;
}
static public void Main()
{
int n = 4;
int [] vis = new int [n + 1];
Stack< int > s = new Stack< int >();
List<List< int > > adj = new List<List< int > >();
for ( int i = 0; i < n + 1; i++) {
adj.Add( new List< int >());
}
adj[1].Add(2);
adj[1].Add(3);
adj[3].Add(1);
adj[3].Add(2);
adj[2].Add(3);
adj[2].Add(4);
adj[2].Add(1);
adj[4].Add(2);
bool detected = false ;
for ( int i = 1; i <= n; i++) {
if (vis[i] == 0)
detected = cycleDetect(i, 0, s, vis, adj);
if (detected)
break ;
}
if (detected) {
Console.WriteLine( "Cycle exists." );
Console.Write(s.Peek());
s.Pop();
while (( int )s.Peek() != x) {
Console.Write( " -> " );
Console.Write(s.Peek());
s.Pop();
}
Console.Write( " -> " );
Console.Write(s.Peek());
}
else
Console.WriteLine( "Cycle does not exist." );
}
}
|
Javascript
let x = 0;
function cycleDetect(node,parent,s,adj)
{
let detected = false ;
vis[node] = 1;
s.push(node);
child = adj[node][0];
if (!vis[child]) {
detected = cycleDetect(child, node,s,adj);
}
else if (child != parent) {
x = child;
return true ;
}
if (!detected)
s.pop();
return detected;
}
let n = 4;
let vis = [];
let s = [];
for (let i=0;i<n+1;i++)
{
vis.push(0);
}
let adj = [];
for (let i=0;i<n+1;i++)
{
let a = [];
adj.push(a);
}
adj[1].push(2);
adj[1].push(3);
adj[3].push(1);
adj[3].push(2);
adj[2].push(3);
adj[2].push(4);
adj[2].push(1);
adj[4].push(2);
let detected = false ;
for (let i = 1; i <= n; i++) {
if (!vis[i])
detected = cycleDetect(i, 0,s,adj);
if (detected)
break ;
}
let ans = "" ;
if (detected){
console.log( "Cycle Exists" );
ans += s[s.length-1];
s.pop();
while (s[s.length-1] != x) {
ans += " -> " + s[s.length-1]
s.pop();
}
ans += " -> " + s[s.length-1];
console.log(ans);
}
else
console.log( "Cycle does not exist." );
|
Output
Cycle exists.
3 -> 2 -> 1
Time Complexity: O(V + E) where V is the number of vertices and E is the number of edges in the graph.
Auxiliary Space: O(V) where V is the number of vertices in the graph.
Another Method: Check cycle exists or Not:-
For finding the cycle in an undirected graph we use DFS. Use dfs from every unvisited node. There is a cycle in an undirected graph only if there is a back edge present in the graph. To find the back edge to any of its ancestors keep a visited array and if there is a back edge to any visited node then there is a loop and return true.
Approach:
Follow the below steps to implement the above approach:
- First iterate over all the nodes of the graph and keep vis[] array for keeping the track of the visited nodes.
- Run a DFS (Depth First Search) traversal on the given subgraph connected to the current node and then pass the parent of the current node.
- For every recursion set vis[root] = 1.
- Iterate over all adjacent nodes of the current node in the adjacency list
- if it is not visited
- then run DFS on that node, return the result of the DFS.
- Else if the adjacent node is visited
- if it is not the parent of the current node
- else return false.
C++
#include <bits/stdc++.h>
using namespace std;
class Graph {
int V;
list< int >* adj;
bool checkcycleUtil( int v, bool vis[], int parent);
public :
Graph( int V);
void addEdge( int v, int w);
bool checkcycle();
};
Graph::Graph( int V)
{
this ->V = V;
adj = new list< int >[V];
}
void Graph::addEdge( int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
bool Graph::checkcycleUtil( int v, bool vis[], int parent)
{
vis[v] = true ;
list< int >::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i) {
if (!vis[*i]) {
if (checkcycleUtil(*i, vis, v))
return true ;
}
else if (*i != parent)
return true ;
}
return false ;
}
bool Graph::checkcycle()
{
bool * vis = new bool [V];
for ( int i = 0; i < V; i++)
vis[i] = false ;
for ( int u = 0; u < V; u++) {
if (!vis[u])
if (checkcycleUtil(u, vis, -1))
return true ;
}
return false ;
}
int main()
{
Graph graph1(5);
graph1.addEdge(1, 0);
graph1.addEdge(0, 2);
graph1.addEdge(2, 1);
graph1.addEdge(0, 3);
graph1.addEdge(3, 4);
graph1.checkcycle() ? cout << "Graph1 contains cycle\n"
: cout << "Graph1 doesn't contain cycle\n" ;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static class Graph
{
int V;
ArrayList<ArrayList<Integer> > adj;
private boolean checkcycleUtil( int v, boolean vis[],
int parent)
{
vis[v] = true ;
for (Integer i : adj.get(v)) {
if (!vis[i]) {
if (checkcycleUtil(i, vis, v))
return true ;
}
else if (i != parent)
return true ;
}
return false ;
}
Graph( int V)
{
this .V = V;
adj = new ArrayList<>();
for ( int i = 0 ; i < V; i++) {
adj.add( new ArrayList<>());
}
}
public void addEdge( int v, int w)
{
adj.get(v).add(w);
adj.get(w).add(v);
}
public boolean checkcycle()
{
boolean [] vis = new boolean [V];
for ( int i = 0 ; i < V; i++)
vis[i] = false ;
for ( int u = 0 ; u < V; u++) {
if (!vis[u])
if (checkcycleUtil(u, vis, - 1 ))
return true ;
}
return false ;
}
}
public static void main(String[] args)
{
Graph graph1 = new Graph( 5 );
graph1.addEdge( 1 , 0 );
graph1.addEdge( 0 , 2 );
graph1.addEdge( 2 , 1 );
graph1.addEdge( 0 , 3 );
graph1.addEdge( 3 , 4 );
if (graph1.checkcycle())
System.out.println( "Graph1 contains cycle" );
else
System.out.println(
"Graph1 doesn't contain cycle" );
;
}
}
|
Python3
class Graph:
def __init__( self , V):
self .V = V
self .adj = [[] for _ in range (V)]
def addEdge( self , v, w):
self .adj[v].append(w)
self .adj[w].append(v)
def checkcycleUtil( self , v, vis, parent):
vis[v] = True
for i in self .adj[v]:
if not vis[i]:
if self .checkcycleUtil(i, vis, v):
return True
elif i ! = parent:
return True
return False
def checkcycle( self ):
vis = [ False for _ in range ( self .V)]
for u in range ( self .V):
if not vis[u]:
if self .checkcycleUtil(u, vis, - 1 ):
return True
return False
graph1 = Graph( 5 )
graph1.addEdge( 1 , 0 )
graph1.addEdge( 0 , 2 )
graph1.addEdge( 2 , 1 )
graph1.addEdge( 0 , 3 )
graph1.addEdge( 3 , 4 )
print ( "Graph1 contains cycle" if graph1.checkcycle()
else "Graph1 doesn't contain cycle" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
class Graph {
int V;
List<List< int > > adj;
private bool checkcycleUtil( int v, bool [] vis,
int parent)
{
vis[v] = true ;
foreach ( int i in adj[v])
{
if (!vis[i]) {
if (checkcycleUtil(i, vis, v))
return true ;
}
else if (i != parent)
return true ;
}
return false ;
}
public Graph( int V)
{
this .V = V;
adj = new List<List< int > >();
for ( int i = 0; i < V; i++) {
adj.Add( new List< int >());
}
}
public void addEdge( int v, int w)
{
adj[v].Add(w);
adj[w].Add(v);
}
public bool checkcycle()
{
bool [] vis = new bool [V];
for ( int i = 0; i < V; i++)
vis[i] = false ;
for ( int u = 0; u < V; u++) {
if (!vis[u])
if (checkcycleUtil(u, vis, -1))
return true ;
}
return false ;
}
}
public static void Main( string [] args)
{
Graph graph1 = new Graph(5);
graph1.addEdge(1, 0);
graph1.addEdge(0, 2);
graph1.addEdge(2, 1);
graph1.addEdge(0, 3);
graph1.addEdge(3, 4);
if (graph1.checkcycle())
Console.WriteLine( "Graph1 contains cycle" );
else
Console.WriteLine(
"Graph1 doesn't contain cycle" );
}
}
|
Javascript
class Graph {
V;
adj;
constructor(V) {
this .V = V;
this .adj = new Array();
for (let i = 0; i < V; i++) {
this .adj[i] = new Array();
}
}
addEdge(v, w) {
this .adj[v].push(w);
this .adj[w].push(v);
}
checkcycleUtil(v, vis, parent) {
vis[v] = true ;
for (const i of this .adj[v]) {
if (!vis[i]) {
if ( this .checkcycleUtil(i, vis, v)) {
return true ;
}
}
else if (i != parent) {
return true ;
}
}
return false ;
}
checkcycle() {
const vis = new Array( this .V).fill( false );
for (let u = 0; u < this .V; u++) {
if (!vis[u]) {
if ( this .checkcycleUtil(u, vis, -1)) {
return true ;
}
}
}
return false ;
}
}
const graph1 = new Graph(5);
graph1.addEdge(1, 0);
graph1.addEdge(0, 2);
graph1.addEdge(2, 1);
graph1.addEdge(0, 3);
graph1.addEdge(3, 4);
if (graph1.checkcycle()) {
console.log( "Graph1 contains cycle" );
} else {
console.log( "Graph1 doesn't contain cycle" );
}
|
Output:
Graph1 contains cycle
Complexity:
Time Complexity: O(V + E) where V is the number of vertices and E is the number of edges in the graph.
Auxiliary Space: O(V) where V is the number of vertices in the graph.
Last Updated :
01 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...