Chapter 32: DSA Graphs Cycle Detection

DSA Graph Cycle Detection – Let’s understand this topic properly and in depth, like I’m sitting next to you with a whiteboard, drawing graphs step by step, explaining every single detail slowly, comparing both main approaches side by side, showing multiple examples, and most importantly telling you why cycle detection is one of the most frequently asked graph problems in interviews.

What is a Cycle in a Graph?

A cycle exists in a graph when you can start at any vertex, follow some edges, and return to the same starting vertex without repeating any edge (or sometimes without repeating vertices, depending on definition).

In simple words:

If there is a loop that brings you back to where you started, there is a cycle.

Why Cycle Detection is Important

Real-world situations where cycles matter:

Situation Why cycle is bad / meaningful
Task scheduling / Course prerequisites Cycle means impossible to complete (circular dependency)
Directed Acyclic Graph (DAG) Many algorithms require no cycles
Deadlock detection Cycle in resource allocation graph → deadlock
Infinite loop in state machines Cycle means system may never terminate
Social network analysis Cycle can indicate tight-knit groups or redundancy
Financial transaction graphs Cycle may indicate money laundering patterns

Two Main Cases We Need to Handle

  1. Undirected Graph Cycle Detection
  2. Directed Graph Cycle Detection

Both are asked very frequently — and they require slightly different logic.

1. Cycle Detection in Undirected Graph

Key idea: A cycle exists if during DFS/BFS traversal, we visit a node that is already visited and it is not the parent of the current node.

Why parent check is needed:

In undirected graph, every edge is bidirectional. So when we go from A → B, we will also have edge B → A. We don’t want to consider this back-edge as a cycle.

Algorithm (DFS-based – most common in interviews)

text

Example – Undirected Graph with Cycle

Vertices: 0, 1, 2, 3, 4

Edges: 0-1, 1-2, 2-3, 3-0, 2-4

text

Start DFS from 0

  • Visit 0, parent = -1
  • Go to 1 (not visited), parent = 0
    • Visit 1
    • Go to 2 (not visited), parent = 1
      • Visit 2
      • Go to 3 (not visited), parent = 2
        • Visit 3
        • Neighbor 0 is visited and ≠ parent 2cycle found!

Output: Cycle exists

Code – Undirected Cycle Detection (DFS)

Python

2. Cycle Detection in Directed Graph

Key difference: In directed graphs, we don’t have automatic back-edges like in undirected. We need to detect if there is a back edge to an ancestor in the current recursion stack.

We need to track two states:

  • Visited — node has been visited before
  • In recursion stack (or “being visited”) — node is currently in the call stack

Three colors / states method (very common & clear):

  • White = not visited
  • Gray = being visited (in current recursion stack)
  • Black = finished visiting (all descendants processed)

Cycle exists if we try to visit a gray node again.

Algorithm

text

Example – Directed Graph with Cycle

Vertices: A → B → C → D → B (cycle B→C→D→B)

text

DFS from A

  • Visit A → GRAY
  • Go to B → GRAY
  • Go to C → GRAY
  • Go to D → GRAY
  • D → B, B is GRAYcycle found!

Code – Directed Cycle Detection (DFS)

Python

DFS vs BFS for Cycle Detection

Method Works for Undirected? Works for Directed? Notes
DFS Yes Yes Easier to implement, most common
BFS Yes Yes More code, less natural for cycle

Most interviews expect DFS approach — both for directed and undirected.

Summary – Cycle Detection Cheat Sheet

Graph Type Main Method Key Idea Time Complexity
Undirected DFS Back edge to visited node (except parent) O(V + E)
Directed DFS Back edge to node in recursion stack (gray node) O(V + E)
Disconnected Run on every component Must check all components O(V + E)

Would you like to continue with any specific cycle detection topic?

Very natural next steps:

  • Step-by-step dry run of both undirected and directed cycle detection
  • Cycle detection using BFS (coloring method or parent tracking)
  • Detect cycle in directed graph – all possible paths explanation
  • Find cycle path (not just existence)
  • Topological sort – how it relates to cycle detection
  • Union-Find approach for undirected cycle detection

Just tell me which one you want next — I’ll explain it with the same level of detail, visuals, step-by-step clarity, and examples! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *