VOOZH about

URL: https://www.geeksforgeeks.org/dsa/detect-cycle-direct-graph-using-colors/

โ‡ฑ Detect Cycle in a directed graph using colors - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Detect Cycle in a directed graph using colors

Last Updated : 23 Jul, 2025

Given a directed graph represented by the number of vertices V and a list of directed edges, determine whether the graph contains a cycle.

Your task is to implement a function that accepts V (number of vertices) and edges (an array of directed edges where each edge is a pair [u, v]), and returns true if the graph contains at least one cycle, otherwise returns false.

Example:

Input: V = 4, edges[][] = [[0, 1], [0, 2], [1, 2], [2, 0], [2, 3], [3, 3]]

๐Ÿ‘ 1
Cycle 0 โ†’ 2 โ†’ 0

Output: true
Explanation: This diagram clearly shows a cycle 0 โ†’ 2 โ†’ 0

Input: V = 4, edges[][] = [[0, 1], [0, 2], [1, 2], [2, 0], [2, 3], [3, 3]]

๐Ÿ‘ directed-1
No Cycle

Output: false
Explanation: The diagram clearly shows no cycle.

Understanding Back Edges in DFS and Their Role in Cycles

Depth-First Search (DFS) is a powerful technique that can be used to detect cycles in a directed graph. During a DFS traversal, if the graph is connected, it forms a DFS tree (or forest for disconnected graphs).

In such a tree, a cycle exists if and only if there is a back edge.
A back edge is an edge that points from a node to one of its ancestors in the DFS traversal this includes self-loops (an edge from a node to itself).

๐Ÿ‘ Depth First Traversal to detect cycle in a Graph

In the left graph, the DFS traversal begins from node 2, and during the traversal, several edges point back to nodes that are already in the current DFS call stack. These edges, marked with red โŒ symbols in the image, are known as back edges, and each one indicates the presence of a cycle in the graph. For example, the edge 3 โ†’ 3 is a self-loop, which forms a trivial cycle. The edge 2 โ†’ 0 connects to an ancestor in the DFS stack, creating a backward connection and thus a cycle. Similarly, the edge 1 โ†’ 2 completes a loop through multiple nodes, further confirming the existence of a cycle.

In the previous post, we have discussed a solution that stores visited vertices in a separate array which stores vertices of the current recursion call stack.

Approach:- Using Coloring - O(V+E) Time and O(V) Space

This algorithm solves the cycle detection problem in a directed graph by using Depth-First Search (DFS) with a coloring technique to track the state of each node during traversal. Nodes are marked as white(unvisited), gray (currently in the recursion stack), or black (fully processed). If during DFS we encounter a gray node, it means weโ€™ve found a back edgeโ€”an edge pointing to a node still in the current DFS pathโ€”indicating a cycle. This method efficiently detects cycles by ensuring each node is visited once and each edge is checked once, resulting in a linear time solution relative to the size of the graph (O(V + E)). 

Steps: 

  • Convert the edge list into an adjacency list for efficient traversal.
  • Initialize Color Array, white = 0 , gray = 1, black = 2 to track DFS state of each node.
  • Start dfs For every unvisited (white) node, start a DFS.
  • Detect Back Edge in dfs, If you visit a gray node again โ†’ cycle exists.
  • If any dfs finds a cycle, return true; else, return false.

Output
true

Time complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph.
Space Complexity: O(V), Since an extra color array is needed of size V.

We do not count the adjacency list in auxiliary space as it is necessary for representing the input graph.

Comment
Article Tags: