Graph Algorithm - Cycle Detection in Directed Graph using DFS
Tags: java, programming, tutorial, algorithms
Published: 2022-05-02 11:48:30

## What is a Cycle
In graph theory, a path that starts from a given node and ends on the same node is a cycle.

## Cycle Detection in an Directed Graph


![Directed Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i77iv0pleifyxxrqabm8.png)

A directed graph is a set of objects, otherwise called vertices or nodes, connected together and all the edges are directed from one vertex to another.
A directed graph is an ordered pair G = (V, E) where,
V is a set of elements known as vertices or nodes.
E is a set of ordered pair of vertices called as edges or directed edges.

Cycle in a directed graph can be detected with the help of Depth-First Search algorithm.

DFS Algorithm for Cycle Detection in an Directed Graph

> The dfs algorithm for cycle detection in undirected graph will not work here because we cannot say that directed graph is having a cycle, if we get to a node which is already marked as visited and previous node is different.

{% embed https://dev.to/rohithv07/graph-algorithm-cycle-detection-in-undirected-graph-using-dfs-233j %}

📌 Initialise a visited boolean array with all nodes unvisited, a boolean recursion stack with all nodes set to false.
A recursion stack is to track the nodes that are currently in recursion. We mark node as true if the node has further recursion calls and change it to false for no recursion calls.
📌 Run a loop from 0 to n - 1 as the graph may have different components.
📌 If the current node is not visited, call the dfs recursive function passing the current node, visited array, recursion stack array.

> dfs(graph, node, visited, recursionStack)

📌 Inside the dfs function, check if the node is already in the recursion stack.
If it is already in the recursion stack, this means we are going to repeat the recursion call which results in a cycle. So we detect cycle in graph and return true.
📌 Check if the node is already visited. If yes, return false.
📌 Mark the node as visited and mark the node in recursion stack.
📌 Traverse through the children of the current node.
📌 Continue doing the recursion for all the children.
📌 If the recursion calls for the current node is over, reset the value to false in the recursion stack array.
📌 If we get out of the initial for loop and all the nodes are now visited, this means we have no cycle.

## Example
1. **A Directed Graph with No Cycle.**


![Directed Graph Example No Cycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v31b4bnyv25cheqgei1t.gif)

2. **A Directed Graph with Cycle.**


![Directed Graph With Cycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tdx932qo07zn1o55bo1.gif)


## Time and Space Complexity

* We are traversing through all the nodes and edges. So time complexity will be O(V + E) where V = vertices or node, E = edges.

* We use a visited array, recursion stack array and an adjacency list for the graph. So the space complexity will be O(V) + O(V) + O(V + E) + extra space for the recursion calls.

## Code


![Java Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk2rfz5u6ouo8y82gg5n.png)


Originally Published on : 

{% embed https://www.linkedin.com/newsletters/graph-algorithm-6905161199672025088/ %}

## Practice Problem

{% embed https://practice.geeksforgeeks.org/problems/detect-cycle-in-a-directed-graph/1 %}

## Github Link

{% github https://github.com/Rohithv07/LeetCodeTopInterviewQuestions %}