# Detect cycle in an undirected graph using BFS

Given an undirected graph, how to check if there is a cycle in the graph? For example, the following graph has a cycle 1-0-2-1.

We have discussed cycle detection for directed graph. We have also discussed a union-find algorithm for cycle detection in undirected graphs.. The time complexity of the union-find algorithm is O(ELogV). Like directed graphs, we can use DFS to detect cycle in an undirected graph in O(V+E) time. We have discussed DFS based solution for cycle detection in undirected graph.

In this article, BFS based solution is discussed. We do a BFS traversal of the given graph. For every visited vertex ‘v’, if there is an adjacent ‘u’ such that u is already visited and u is not parent of v, then there is a cycle in graph. If we don’t find such an adjacent for any vertex, we say that there is no cycle. The assumption of this approach is that there are no parallel edges between any two vertices.

We use a parent array to keep track of parent vertex for a vertex so that we do not consider visited parent as cycle.

## CPP

`// C++ program to detect cycle in an undirected graph ` `// using BFS. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `addEdge(vector<` `int` `> adj[], ` `int` `u, ` `int` `v) ` `{ ` ` ` `adj[u].push_back(v); ` ` ` `adj[v].push_back(u); ` `} ` ` ` `bool` `isCyclicConntected(vector<` `int` `> adj[], ` `int` `s, ` ` ` `int` `V, vector<` `bool` `>& visited) ` `{ ` ` ` `// Set parent vertex for every vertex as -1. ` ` ` `vector<` `int` `> parent(V, -1); ` ` ` ` ` `// Create a queue for BFS ` ` ` `queue<` `int` `> q; ` ` ` ` ` `// Mark the current node as visited and enqueue it ` ` ` `visited[s] = ` `true` `; ` ` ` `q.push(s); ` ` ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `// Dequeue a vertex from queue and print it ` ` ` `int` `u = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// Get all adjacent vertices of the dequeued ` ` ` `// vertex u. If a adjacent has not been visited, ` ` ` `// then mark it visited and enqueue it. We also ` ` ` `// mark parent so that parent is not considered ` ` ` `// for cycle. ` ` ` `for` `(` `auto` `v : adj[u]) { ` ` ` `if` `(!visited[v]) { ` ` ` `visited[v] = ` `true` `; ` ` ` `q.push(v); ` ` ` `parent[v] = u; ` ` ` `} ` ` ` `else` `if` `(parent[u] != v) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `bool` `isCyclicDisconntected(vector<` `int` `> adj[], ` `int` `V) ` `{ ` ` ` `// Mark all the vertices as not visited ` ` ` `vector<` `bool` `> visited(V, ` `false` `); ` ` ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `if` `(!visited[i] && isCyclicConntected(adj, i, ` ` ` `V, visited)) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Driver program to test methods of graph class ` `int` `main() ` `{ ` ` ` `int` `V = 4; ` ` ` `vector<` `int` `> adj[V]; ` ` ` `addEdge(adj, 0, 1); ` ` ` `addEdge(adj, 1, 2); ` ` ` `addEdge(adj, 2, 0); ` ` ` `addEdge(adj, 2, 3); ` ` ` ` ` `if` `(isCyclicDisconntected(adj, V)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`import` `java.util.ArrayList; ` `import` `java.util.Arrays; ` `import` `java.util.LinkedList; ` `import` `java.util.Queue; ` ` ` `// Java program to detect cycle in ` `// an undirected graph using BFS. ` `class` `cycle ` `{ ` ` ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` ` ` `int` `V = ` `4` `; ` ` ` `ArrayList <Integer> adj[] = ` `new` `ArrayList[V]; ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `4` `; i++) ` ` ` `adj[i] = ` `new` `ArrayList<Integer>(); ` ` ` ` ` `addEdge(adj, ` `0` `, ` `1` `); ` ` ` `addEdge(adj, ` `1` `, ` `2` `); ` ` ` `addEdge(adj, ` `2` `, ` `0` `); ` ` ` `addEdge(adj, ` `2` `, ` `3` `); ` ` ` ` ` `if` `(isCyclicDisconntected(adj, V)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` ` ` ` ` `} ` ` ` ` ` `static` `void` `addEdge(ArrayList<Integer> adj[], ` `int` `u, ` `int` `v) ` ` ` `{ ` ` ` `adj[u].add(v); ` ` ` `adj[v].add(u); ` ` ` `} ` ` ` ` ` `static` `boolean` `isCyclicConntected(ArrayList<Integer> adj[], ` `int` `s, ` ` ` `int` `V, ` `boolean` `visited[]) ` ` ` `{ ` ` ` ` ` `// Set parent vertex for every vertex as -1. ` ` ` `int` `parent[] = ` `new` `int` `[V]; ` ` ` `Arrays.fill(parent, -` `1` `); ` ` ` ` ` `// Create a queue for BFS ` ` ` `Queue<Integer> q = ` `new` `LinkedList<>(); ` ` ` ` ` `// Mark the current node as ` ` ` `// visited and enqueue it ` ` ` `visited[s] = ` `true` `; ` ` ` `q.add(s); ` ` ` ` ` `while` `(!q.isEmpty()) ` ` ` `{ ` ` ` ` ` `// Dequeue a vertex from ` ` ` `// queue and print it ` ` ` `int` `u = q.poll(); ` ` ` ` ` ` ` `// Get all adjacent vertices ` ` ` `// of the dequeued vertex u. ` ` ` `// If a adjacent has not been ` ` ` `// visited, then mark it visited ` ` ` `// and enqueue it. We also mark parent ` ` ` `// so that parent is not considered ` ` ` `// for cycle. ` ` ` `for` `(` `int` `i=` `0` `; i<adj[u].size();i++) ` ` ` `{ ` ` ` `int` `v = adj[u].get(i); ` ` ` `if` `(!visited[v]) ` ` ` `{ ` ` ` `visited[v] = ` `true` `; ` ` ` `q.add(v); ` ` ` `parent[v] = u; ` ` ` `} ` ` ` `else` `if` `(parent[u] != v) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` ` ` `static` `boolean` `isCyclicDisconntected(ArrayList<Integer> adj[], ` `int` `V) ` ` ` `{ ` ` ` ` ` `// Mark all the vertices as not visited ` ` ` `boolean` `visited[] = ` `new` `boolean` `[V]; ` ` ` `Arrays.fill(visited,` `false` `); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < V; i++) ` ` ` `if` `(!visited[i] && ` ` ` `isCyclicConntected(adj, i, V, visited)) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` ` ` `} ` `} ` ` ` `// This code is contributed by mayukh Sengupta ` |

*chevron_right*

*filter_none*

**Output :**

Yes

**Time Complexity:** The program does a simple BFS Traversal of graph and graph is represented using adjacency list. So the time complexity is O(V+E)

## Recommended Posts:

- Detect cycle in an undirected graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Detect cycle in the graph using degrees of nodes of graph
- Detect Cycle in a Directed Graph
- Detect Cycle in a Directed Graph using BFS
- Shortest cycle in an undirected unweighted graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Detect Cycle in a directed graph using colors
- Find minimum weight cycle in an undirected graph
- Number of single cycle components in an undirected graph
- Detect a negative cycle in a Graph | (Bellman Ford)
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Clone an Undirected Graph
- Find k-cores of an 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.