Challenge - 5 Problems
Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of cycle detection on a simple graph
What is the output of the following C++ code that detects a cycle in an undirected graph?
DSA C++
#include <iostream> #include <vector> using namespace std; bool dfs(int node, int parent, vector<vector<int>>& adj, vector<bool>& visited) { visited[node] = true; for (int neighbor : adj[node]) { if (!visited[neighbor]) { if (dfs(neighbor, node, adj, visited)) return true; } else if (neighbor != parent) { return true; } } return false; } bool hasCycle(int V, vector<vector<int>>& adj) { vector<bool> visited(V, false); for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, -1, adj, visited)) return true; } } return false; } int main() { int V = 4; vector<vector<int>> adj(V); adj[0] = {1, 2}; adj[1] = {0, 2}; adj[2] = {0, 1, 3}; adj[3] = {2}; cout << (hasCycle(V, adj) ? "Cycle detected" : "No cycle") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if any back edge exists in the undirected graph.
✗ Incorrect
The graph contains a cycle between nodes 0, 1, and 2. The DFS detects this cycle and returns true.
❓ Predict Output
intermediateCycle detection output on a tree graph
What will be printed by this C++ code that checks for cycles in an undirected graph structured as a tree?
DSA C++
#include <iostream> #include <vector> using namespace std; bool dfs(int node, int parent, vector<vector<int>>& adj, vector<bool>& visited) { visited[node] = true; for (int neighbor : adj[node]) { if (!visited[neighbor]) { if (dfs(neighbor, node, adj, visited)) return true; } else if (neighbor != parent) { return true; } } return false; } bool hasCycle(int V, vector<vector<int>>& adj) { vector<bool> visited(V, false); for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, -1, adj, visited)) return true; } } return false; } int main() { int V = 5; vector<vector<int>> adj(V); adj[0] = {1, 2}; adj[1] = {0}; adj[2] = {0, 3, 4}; adj[3] = {2}; adj[4] = {2}; cout << (hasCycle(V, adj) ? "Cycle detected" : "No cycle") << endl; return 0; }
Attempts:
2 left
💡 Hint
A tree has no cycles by definition.
✗ Incorrect
The graph is a tree structure with no cycles, so the DFS never finds a back edge and returns false.
🔧 Debug
advancedIdentify the bug causing incorrect cycle detection
The following code is intended to detect cycles in an undirected graph but always returns "Cycle detected" even when no cycle exists. What is the bug?
DSA C++
#include <iostream> #include <vector> using namespace std; bool dfs(int node, int parent, vector<vector<int>>& adj, vector<bool>& visited) { visited[node] = true; for (int neighbor : adj[node]) { if (!visited[neighbor]) { if (dfs(neighbor, node, adj, visited)) return true; } else if (neighbor == parent) { return true; } } return false; } bool hasCycle(int V, vector<vector<int>>& adj) { vector<bool> visited(V, false); for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, -1, adj, visited)) return true; } } return false; } int main() { int V = 3; vector<vector<int>> adj(V); adj[0] = {1}; adj[1] = {0, 2}; adj[2] = {1}; cout << (hasCycle(V, adj) ? "Cycle detected" : "No cycle") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the logic that detects a back edge in DFS.
✗ Incorrect
The code incorrectly returns true when neighbor equals parent, which is the opposite of the correct logic. It should return true when neighbor is visited and not the parent.
🧠 Conceptual
advancedWhy does DFS detect cycles in undirected graphs?
Which statement best explains why DFS can detect cycles in an undirected graph?
Attempts:
2 left
💡 Hint
Think about what a back edge means in DFS for undirected graphs.
✗ Incorrect
In undirected graphs, a cycle exists if DFS finds a visited node that is not the parent, indicating a back edge.
❓ Predict Output
expertOutput of cycle detection on disconnected graph with multiple components
What is the output of the following C++ code that detects cycles in an undirected graph with two disconnected components, one with a cycle and one without?
DSA C++
#include <iostream> #include <vector> using namespace std; bool dfs(int node, int parent, vector<vector<int>>& adj, vector<bool>& visited) { visited[node] = true; for (int neighbor : adj[node]) { if (!visited[neighbor]) { if (dfs(neighbor, node, adj, visited)) return true; } else if (neighbor != parent) { return true; } } return false; } bool hasCycle(int V, vector<vector<int>>& adj) { vector<bool> visited(V, false); for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, -1, adj, visited)) return true; } } return false; } int main() { int V = 6; vector<vector<int>> adj(V); // Component 1 with cycle: 0-1-2-0 adj[0] = {1, 2}; adj[1] = {0, 2}; adj[2] = {0, 1}; // Component 2 without cycle: 3-4-5 adj[3] = {4}; adj[4] = {3, 5}; adj[5] = {4}; cout << (hasCycle(V, adj) ? "Cycle detected" : "No cycle") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check all disconnected components for cycles.
✗ Incorrect
The first component contains a cycle, so the function returns true even though the second component has no cycle.
