Which of the following modifications will break the cycle detection algorithm that uses DFS with a recursion stack?
hard
A. Starting DFS from node 0 instead of node 4
B. Adding a visited set to track visited nodes
C. Removing the recursion stack and using only a visited set
D. Using an iterative BFS instead of DFS
Solution
Step 1: Understand the role of recursion stack in cycle detection
The recursion stack tracks nodes in the current path to detect cycles (back edges).
Step 2: Effect of removing recursion stack
Without recursion stack, the algorithm cannot detect cycles correctly, even if visited set is used.
Step 3: Other options explained
Adding visited set helps. Starting DFS from different nodes does not break detection if all nodes are visited. Using BFS does not detect cycles in directed graphs reliably.
Final Answer:
Removing the recursion stack and using only a visited set -> Option C
Quick Check:
Recursion stack needed for cycle detection [OK]
Hint: Recursion stack is essential for detecting cycles in DFS [OK]