Complete the code to initialize the visited list for all nodes.
visited = [False] * [1]
The visited list should have one entry per node, so its length must be the number of nodes, which is len(graph).
Complete the code to mark the current node as visited in the DFS function.
visited[node] = [1]When visiting a node, we mark it as True in the visited list to indicate it has been visited.
Fix the error in the condition to detect a cycle when a node is in the recursion stack.
if rec_stack[neighbor] == [1]:
A cycle is detected if a neighbor is already in the recursion stack, which means rec_stack[neighbor] is True.
Fill both blanks to correctly update the recursion stack when entering and leaving a node.
rec_stack[node] = [1] ... rec_stack[node] = [2]
When entering a node, mark it True in the recursion stack. When leaving, mark it False.
Fill all three blanks to complete the DFS function header and the loop over neighbors.
def dfs([1], graph, visited, rec_stack): visited[node] = True rec_stack[node] = True for [2] in graph[node]: if not visited[neighbor]: if dfs([3], graph, visited, rec_stack): return True rec_stack[node] = False return False
The DFS function takes node as the current node. The loop variable is neighbor. The recursive call uses neighbor as the current node argument.
Specifically: def dfs(node, ...), for neighbor in graph[node], and dfs(neighbor, ...).
