# Detect cycle in the graph using degrees of nodes of graph

Given a graph, the task is to detect a cycle in the graph using degrees of the nodes in the graph and print all the nodes that are involved in any of the cycles. If there is no cycle in the graph then print **-1**.

**Examples:**

Input:

Output:0 1 2

**Approach:** Recursively remove all vertices of degree 1. This can be done efficiently by storing a map of vertices to their degrees.

Initially, traverse the map and store all the vertices with degree = 1 in a queue. Traverse the queue as long as it is not empty. For each node in the queue, mark it as visited, and iterate through all the nodes that are connected to it (using the adjacency list), and decrement the degree of each of those nodes by one in the map. Add all nodes whose degree becomes equal to one to the queue. At the end of this algorithm, all the nodes that are unvisited are part of the cycle.

Below is the implementation of the above approach:

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `// Graph class ` `class` `Graph { ` ` ` ` ` `// No. of vertices of graph ` ` ` `int` `v; ` ` ` ` ` `// Adjacency List ` ` ` `LinkedList l[]; ` ` ` ` ` `Graph(` `int` `v) ` ` ` `{ ` ` ` `this` `.v = v; ` ` ` `this` `.l = ` `new` `LinkedList[v]; ` ` ` `for` `(` `int` `i = ` `0` `; i < v; i++) { ` ` ` `l[i] = ` `new` `LinkedList(); ` ` ` `} ` ` ` `} ` ` ` `void` `addedge(` `int` `i, ` `int` `j) ` ` ` `{ ` ` ` `l[i].add(j); ` ` ` `l[j].add(i); ` ` ` `} ` `} ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find a cycle in the given graph if exists ` ` ` `static` `void` `findCycle(` `int` `n, ` `int` `e, Graph g) ` ` ` `{ ` ` ` ` ` `// HashMap to store the degree of each node ` ` ` `HashMap degree = ` `new` `HashMap(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < g.l.length; i++) ` ` ` `degree.put(i, g.l[i].size()); ` ` ` ` ` `// Array to track visited nodes ` ` ` `int` `visited[] = ` `new` `int` `[g.v]; ` ` ` ` ` `// Initially all nodes are not visited ` ` ` `for` `(` `int` `i = ` `0` `; i < visited.length; i++) ` ` ` `visited[i] = ` `0` `; ` ` ` ` ` `// Queue to store the nodes of degree 1 ` ` ` `LinkedList q = ` `new` `LinkedList(); ` ` ` ` ` `// Continuously adding those nodes whose ` ` ` `// degree is 1 to the queue ` ` ` `while` `(` `true` `) { ` ` ` ` ` `// Adding nodes to queue whose degree is 1 ` ` ` `// and is not visited ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < degree.size(); i++) ` ` ` `if` `((` `int` `)degree.get(i) == ` `1` `&& visited[i] == ` `0` `) ` ` ` `q.add(i); ` ` ` ` ` `// If queue becomes empty then get out ` ` ` `// of the continuous loop ` ` ` `if` `(q.isEmpty()) ` ` ` `break` `; ` ` ` ` ` `while` `(!q.isEmpty()) { ` ` ` ` ` `// Remove the front element from the queue ` ` ` `int` `temp = (` `int` `)q.remove(); ` ` ` ` ` `// Mark the removed element visited ` ` ` `visited[temp] = ` `1` `; ` ` ` ` ` `// Decrement the degree of all those nodes ` ` ` `// adjacent to removed node ` ` ` `for` `(` `int` `i = ` `0` `; i < g.l[temp].size(); i++) { ` ` ` `int` `value = (` `int` `)degree.get((` `int` `)g.l[temp].get(i)); ` ` ` `degree.replace(g.l[temp].get(i), --value); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `flag = ` `0` `; ` ` ` ` ` `// Checking all the nodes which are not visited ` ` ` `// i.e. they are part of the cycle ` ` ` `for` `(` `int` `i = ` `0` `; i < visited.length; i++) ` ` ` `if` `(visited[i] == ` `0` `) ` ` ` `flag = ` `1` `; ` ` ` ` ` `if` `(flag == ` `0` `) ` ` ` `System.out.print(` `"-1"` `); ` ` ` `else` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < visited.length; i++) ` ` ` `if` `(visited[i] == ` `0` `) ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// No of nodes ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `// No of edges ` ` ` `int` `e = ` `5` `; ` ` ` `Graph g = ` `new` `Graph(n); ` ` ` ` ` `g.addedge(` `0` `, ` `1` `); ` ` ` `g.addedge(` `0` `, ` `2` `); ` ` ` `g.addedge(` `0` `, ` `3` `); ` ` ` `g.addedge(` `1` `, ` `2` `); ` ` ` `g.addedge(` `3` `, ` `4` `); ` ` ` ` ` `findCycle(n, e, g); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

0 1 2

## Recommended Posts:

- Detect cycle in an undirected graph
- Detect Cycle in a Directed Graph using BFS
- Detect Cycle in a Directed Graph
- Detect cycle in an undirected graph using BFS
- Detect Cycle in a directed graph using colors
- Detect a negative cycle in a Graph | (Bellman Ford)
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Sum of degrees of all nodes of a undirected graph
- Construct a graph from given degrees of all vertices
- Finding in and out degrees of all vertices in a graph
- Check whether given degrees of vertices represent a Graph or Tree
- Coloring a Cycle Graph
- Degree of a Cycle Graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Find minimum weight cycle 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.