Complete the code to add an edge between two nodes in the undirected graph.
graph[[1]].push(v);
graph[v].push(u);The edge is added from node u to v and vice versa because the graph is undirected.
Complete the code to mark the current node as visited during DFS.
visited[[1]] = true;We mark the current node as visited to avoid revisiting it.
Fix the error in the DFS condition to detect a cycle when a visited neighbor is not the parent.
if visited[neighbor] && neighbor != [1] { return true; }
The cycle is detected if a visited neighbor is not the parent of the current node.
Fill both blanks to complete the DFS recursive call and cycle check.
if !visited[[1]] { if dfs(graph, visited, [2], node) { return true; } }
We recurse DFS on the neighbor node, passing the current node as its parent.
Fill all three blanks to complete the cycle detection function that iterates over all nodes.
for i in 0..graph.len() { if !visited[[1]] { if dfs(&graph, &mut visited, [2], [3]) { return true; } } } return false;
We check each node i if unvisited, call DFS with i and usize::MAX as parent (no parent).
