New update is available. Click here to update.

Last Updated: 1 Jan, 2021

Difficulty: Moderate

```
In the below graph, there exists a cycle between vertex 1, 2 and 3.
```

```
1. There are no parallel edges between two vertices.
2. There are no self-loops(an edge connecting the vertex to itself) in the graph.
3. The graph can be disconnected.
```

```
Input: N = 3 , Edges = [[1, 2], [2, 3], [1, 3]].
Output: Yes
Explanation : There are a total of 3 vertices in the graph. There is an edge between vertex 1 and 2, vertex 2 and 3 and vertex 1 and 3. So, there exists a cycle in the graph.
```

```
The first line of input contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two single space-separated integers ‘N’ and ‘M’ representing the total number of vertices and edges, respectively.
The next ‘M’ lines contain two single space-separated integers representing an edge of the graph.
```

```
For each test case, the only line of output will return “Yes” if there exists a cycle in the graph. Else print “No”.
```

```
You are not required to print the expected output, it has already been taken care of. Just implement the function.
```

```
1 <= T <= 10
1 <= N <= 5000
0 <= M <= min(5000, (N * (N - 1)) / 2)
1 <= edges[i][0] <= N
1 <= edges[i][1] <= N
Time Limit: 1 sec
```

There is a cycle in the graph only if there is a back edge (back edge is an edge that connects a vertex to another vertex that is discovered before it's parent) present in the graph. To detect a back edge, we will keep track of vertices that have been already visited. If we reach a vertex that is already visited and is not the parent vertex of the current vertex, then there is a cycle in the graph.

Here is the complete algorithm:

- We create a graph using the
*‘EDGES’*array and initialise an array*‘VISITED’*to keep track of the visited vertices. - We iterate over all vertices of the graph and if the vertex is unvisited, we call the ‘IS_CYCLE
*’*function from that vertex. The*‘IS_CYCLE’*function works as follows:- Mark the current vertex true in the
*‘VISITED’*array. - Find all the adjacent vertices of the current vertex.
- If an adjacent vertex is not visited
- Recursively call the
*‘IS_CYCLE’*function for the adjacent vertex. - If the recursive function returns true, then return true.

- Recursively call the
- Else if the adjacent vertex is already visited and is not the parent vertex of the current vertex.
- Return
*true*.

- Return

- If an adjacent vertex is not visited
- Finally, return
*false*.

- Mark the current vertex true in the
- If the
*‘IS_CYLCE’*function returns*true*, then return “Yes”. Else return “No”.

In this approach, we will use breadth-first search algorithm to find the cycle in the undirected graph.

The approach is similar to the previous approach with some changes in the *‘IS_CYCLE’ *function.

The *‘IS_CYCLE’ *function will work as follows:

- We push the current vertex in the queue.
- We then run a while loop until the queue becomes empty.
- Pop the front vertex from the queue.
- We mark it true in the
*‘VISITED’*array. - Find all the adjacent vertices of the current vertex.
- If the adjacent vertex is not visited:
- We mark it true in the
*‘VISITED’*array and push it in the queue.

- We mark it true in the
- Else if the adjacent vertex is visited and it is not the parent vertex of the current vertex.
- Return true

- If the adjacent vertex is not visited:
- Finally, return false.

In this approach, we will use the union-find algorithm to find the cycle in the undirected graph.

The main idea behind using this algorithm is that if we have two subsets that are already connected through an edge and if they get connected by another edge then they form a cycle.

The *union-find* algorithm is an algorithm that performs two useful operations on a disjoint-set data structure:

* Find:* Determine which subset a particular element is in. This can be used for determining if two elements are in the same subset.

* Union:* Join two subsets into a single subset.

We will iterate through all edges and whenever we find two vertices with the same parent we return “Yes”.

Here is the algorithm:

- We initialise two arrays
*‘PARENT’*and*‘RANK’*to keep track of the parent and rank of the subsets. Here rank denotes the depth of the tree (subset). - Now we will iterate through all edges of the graph:
- Find the parent of both vertices (say ‘PARENT1’ and ‘PARENT2’).
- If ‘PARENT1’ == ‘PARENT2’
- Return “Yes”. Here,
*‘PARENT1’*==*‘PARENT2’*represents that both subsets are initially connected and now we have another edge connecting them. Hence a cycle exists.

- Return “Yes”. Here,
- Else If ‘PARENT1’ != ‘PARENT2’
- Union both subsets into a single set. We are doing this because we have two subsets and an edge connecting them. Now both subsets combine and become a single subset.

- Finally, return “No”.

SIMILAR PROBLEMS

Find Center of Star Graph

Posted: 26 Jan, 2022

Difficulty: Easy

Critical Connections in a Network

Posted: 27 Jan, 2022

Difficulty: Hard

Critical Connections in a Network

Posted: 27 Jan, 2022

Difficulty: Hard

Critical Connections in a Network

Posted: 27 Jan, 2022

Difficulty: Hard

Good Bad Graph

Posted: 26 May, 2022

Difficulty: Hard

COUNT ISLANDS

Posted: 14 Sep, 2022

Difficulty: Moderate

Distance to a Cycle in Undirected Graph

Posted: 7 Nov, 2022

Difficulty: Moderate

Popular Interview Problems: