I started with Kahn's algorithm because the queue-based approach feels more intuitive to explain out loud.
Start by clarifying the problem constraints and edge cases, then explain Kahn's algorithm (BFS with in-degree) and DFS with post-order, highlighting cycle detection. Walk through a small example for each, and discuss time/space complexity and trade-offs.
Pro tip: Emphasize that Kahn's algorithm naturally detects cycles when the result size is less than the number of nodes, while DFS uses a recursion stack to detect back edges. Mention that both are O(V+E) but BFS is often preferred for its simplicity and iterative nature.
Ask about graph size, input format, and whether nodes are labeled 0 to n-1. Discuss edge cases like empty graph, single node, disconnected components, and self-loops.
Describe computing in-degrees, initializing a queue with zero in-degree nodes, and iteratively removing nodes and decrementing neighbors' in-degrees. If the result size is less than the number of nodes, a cycle exists.
Describe performing DFS, tracking visited and recursion stack states, and adding nodes to the result in post-order. If a node is encountered in the recursion stack, a cycle exists.
Choose a small DAG and a cyclic graph to demonstrate both approaches step-by-step, showing how cycle detection works.
State that both algorithms run in O(V+E) time and O(V+E) space. Compare BFS (iterative, easier to understand) vs DFS (recursive, may cause stack overflow for large graphs).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.