Directed Graph Cycle Detection
For a directed graph, DFS cycle detection uses two states: visited and currently-in-recursion-stack. If DFS encounters an edge to a vertex that is already in the current recursion stack, it has found a back edge and therefore a cycle.
visited tracks globally discovered vertices.
inStack tracks the current DFS path.
An edge to an in-stack vertex indicates a directed cycle.
Time complexity: O(V+E).
Kahn's topological-sort approach can also detect directed cycles.