Interactive visualization of DFS cycle detection with adjacency lists and three-color marking
DFS with WHITE / GRAY / BLACK coloring for course prerequisites
Choose a preset and run DFS cycle detection
1. WHITE
Node hasn't been visited yet. We start DFS from it.
2. GRAY
Node is being processed (on the call stack). If we reach a GRAY node, we found a back edge = cycle!
3. BLACK
Node and all its descendants are fully explored. Safe to skip.
GRAY nodes are on the current DFS path. If node A is GRAY and we reach A again from one of its descendants, that means there's a path from A back to A — a cycle. This is called a back edge in graph terminology.
Course Schedule meaning: If course A requires B, and B (directly or indirectly) requires A, you can never complete either — that's a cycle in the prerequisite graph.
| Operation | Time | Why |
|---|---|---|
| Build adj list | O(E) | One pass through all edges |
| DFS cycle detection | O(V + E) | Visit each node once, check each edge once |
| Space | O(V + E) | Adjacency list + color array + recursion stack |