How do you find a cycle using DFS?
You need to keep track of the path you are currently walking on. If you run into a node that is already in your current recursion stack, you have found a circle. You cannot use a simple visited boolean. You need three states. A regular visited set marks nodes you have finished, but does not tell you if a node is in the current path. That is why you use a separate "visiting" state for nodes in the recursion stack.