DFS applications — cycle detection (directed and undirected)
WHY two different rules?
WHY colors matter (directed): In a directed graph, reaching a black node again is fine — it just means two paths merge (a cross/forward edge), no cycle. But reaching a gray node means you found a path that loops back to an ancestor you're still inside → cycle.
WHY parent-exception (undirected): Each undirected edge is stored both ways. When you go , then from you'll see the edge back to which is already visited. That is NOT a cycle — it's the same edge. So you must ignore the immediate parent. But seeing any other already-visited node means a genuine second path back → cycle.

HOW: Directed cycle detection (3-color DFS)
HOW: Undirected cycle detection
Recall Feynman: explain to a 12-year-old
Imagine walking through a maze while unrolling a string behind you. Directed maze (one-way doors): if a door leads you to a spot where your string is still lying on the floor in front of you (you haven't packed it up yet), you've gone in a circle. If the string there is already rolled back up (you finished that area), it's fine. Undirected maze (normal doors): every door you walk through, the door behind you is the one you just came from — ignore it! But if you reach a room you've already been in through a different door, the maze has a loop.
Complexity
Flashcards
Directed graph: a cycle exists iff DFS finds what kind of edge?
What do the three DFS colors WHITE/GRAY/BLACK mean?
Why doesn't reaching a BLACK node in a directed graph indicate a cycle?
Undirected cycle detection: condition for a cycle during DFS?
Why the v != parent check in undirected DFS?
Why can't you use the parent-trick for directed graphs?
Time complexity of DFS cycle detection?
A tree on n nodes has how many edges, and what does an extra edge cause?
How do you ensure you detect cycles in disconnected graphs?
Smallest directed cycle and smallest undirected cycle (simple)?
Connections
- Depth-First Search — the traversal engine these rely on.
- Topological Sort — only possible iff the directed graph is acyclic (DAG); same back-edge test.
- Union-Find (DSU) — alternative undirected cycle detection without DFS.
- Back Edges and Edge Classification — tree/back/forward/cross edges.
- Strongly Connected Components — built on DFS finish times.
- Bipartite Checking — another DFS/BFS coloring application.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, cycle detection ka idea simple hai: DFS se hum graph mein "deep" jaate hain, aur agar chalte-chalte hum kisi aise node pe wapas pahunch jaayein jisko abhi explore kar hi rahe the, toh matlab loop ban gaya. Bas directed aur undirected mein "rule" thoda alag hota hai.
Directed graph mein hum teen colors use karte hain — WHITE (abhi tak nahi gaye), GRAY (gaye hain par abhi recursion stack pe hai), aur BLACK (poora khatam). Agar DFS ke dauraan koi edge GRAY node ki taraf jaaye, toh wo back edge hai aur cycle confirm. BLACK node milna theek hai — wo doosri branch pe finish ho chuka, koi loop nahi.
Undirected graph mein har edge dono taraf store hoti hai, isliye jis node se aaye ho (parent) usko wapas dekhna normal hai — usko ignore karo. Lekin agar koi doosra already-visited node mil jaaye (jo parent nahi hai), toh samajh lo cycle hai, kyunki ek hi node tak do raste ban gaye. Yaad rakhne ka trick: "GRAY means cycle (directed), Parent pardon karo aur baaki visited node pe panic karo (undirected)." Dono ka time complexity hai, aur disconnected graph ke liye har component se DFS chalana mat bhoolna.