Cycle Detection
Intro
Cycle detection is naturally associated with DFS. Using BFS is just unnatural since it doesn't keep track of visited but unfinished nodes on stack and is hard to keep a parent pointer.
Detect Cycle in Directed Graph
For directed graph, a cycle exists when there's a back edge in the DFS tree.
Why we can't just return cycleDFS(graph, neighbor, visited, onStack)
? We have to keep in mind what happens when the program backtracks. It will return false when it reaches the end of a path, we don't want to return false just yet. We want to return false at the root level. Therefore we have to "use" the boolean return value somewhere.
Detect Cycle in Undirected Graph
For undirected graph, a cycle exists when a node's neighbor is already visited and is not its parent.
Note: neighbor != parent
won't work here because they are two references to the Integer object. We want to compare the value not the reference ◟ʕ´∀`ʔ◞
Reference
Last updated