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 using DFS
What is the output of the following C# code that detects a cycle in a directed graph using DFS?
DSA C#
using System; using System.Collections.Generic; class Graph { private int V; private List<int>[] adj; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) { adj[i] = new List<int>(); } } public void AddEdge(int u, int v) { adj[u].Add(v); } private bool DFS(int v, bool[] visited, bool[] recStack) { if (!visited[v]) { visited[v] = true; recStack[v] = true; foreach (int neighbour in adj[v]) { if (!visited[neighbour] && DFS(neighbour, visited, recStack)) return true; else if (recStack[neighbour]) return true; } } recStack[v] = false; return false; } public bool IsCyclic() { bool[] visited = new bool[V]; bool[] recStack = new bool[V]; for (int i = 0; i < V; i++) { if (DFS(i, visited, recStack)) return true; } return false; } } class Program { static void Main() { Graph g = new Graph(4); g.AddEdge(0, 1); g.AddEdge(1, 2); g.AddEdge(2, 3); g.AddEdge(3, 1); Console.WriteLine(g.IsCyclic() ? "Cycle detected" : "No cycle"); } }
Attempts:
2 left
💡 Hint
Trace the DFS calls and check if any node is revisited in the current recursion stack.
✗ Incorrect
The graph has edges 0->1, 1->2, 2->3, and 3->1. The edge 3->1 creates a cycle involving nodes 1, 2, and 3. The DFS detects this cycle and returns true, so the output is 'Cycle detected'.
❓ Predict Output
intermediateOutput when graph has no cycle
What will be the output of the following C# code that checks for cycles in a directed graph with no cycles?
DSA C#
using System; using System.Collections.Generic; class Graph { private int V; private List<int>[] adj; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) { adj[i] = new List<int>(); } } public void AddEdge(int u, int v) { adj[u].Add(v); } private bool DFS(int v, bool[] visited, bool[] recStack) { if (!visited[v]) { visited[v] = true; recStack[v] = true; foreach (int neighbour in adj[v]) { if (!visited[neighbour] && DFS(neighbour, visited, recStack)) return true; else if (recStack[neighbour]) return true; } } recStack[v] = false; return false; } public bool IsCyclic() { bool[] visited = new bool[V]; bool[] recStack = new bool[V]; for (int i = 0; i < V; i++) { if (DFS(i, visited, recStack)) return true; } return false; } } class Program { static void Main() { Graph g = new Graph(3); g.AddEdge(0, 1); g.AddEdge(1, 2); Console.WriteLine(g.IsCyclic() ? "Cycle detected" : "No cycle"); } }
Attempts:
2 left
💡 Hint
Check if any node is revisited in the current recursion stack during DFS.
✗ Incorrect
The graph edges are 0->1 and 1->2, which form a linear chain with no cycles. The DFS completes without finding any back edge, so the output is 'No cycle'.
🔧 Debug
advancedIdentify the error in cycle detection code
The following code is intended to detect cycles in a directed graph using DFS. What error will it produce when run?
DSA C#
using System; using System.Collections.Generic; class Graph { private int V; private List<int>[] adj; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) { adj[i] = new List<int>(); } } public void AddEdge(int u, int v) { adj[u].Add(v); } private bool DFS(int v, bool[] visited, bool[] recStack) { if (!visited[v]) { visited[v] = true; recStack[v] = true; foreach (int neighbour in adj[v]) { if (!visited[neighbour] && DFS(neighbour, visited, recStack)) return true; else if (recStack[neighbour]) return true; } } recStack[v] = false; return false; } public bool IsCyclic() { bool[] visited = new bool[V]; bool[] recStack = new bool[V]; for (int i = 0; i < V; i++) { if (DFS(i, visited, recStack)) return true; } return false; } }
Attempts:
2 left
💡 Hint
Check the loop boundary in IsCyclic method.
✗ Incorrect
The for loop in IsCyclic uses 'i <= V', which tries to access adj[V], causing IndexOutOfRangeException because valid indices are 0 to V-1.
❓ Predict Output
advancedOutput of cycle detection with multiple disconnected components
What is the output of the following C# code that detects cycles in a directed graph with multiple disconnected components?
DSA C#
using System; using System.Collections.Generic; class Graph { private int V; private List<int>[] adj; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) { adj[i] = new List<int>(); } } public void AddEdge(int u, int v) { adj[u].Add(v); } private bool DFS(int v, bool[] visited, bool[] recStack) { if (!visited[v]) { visited[v] = true; recStack[v] = true; foreach (int neighbour in adj[v]) { if (!visited[neighbour] && DFS(neighbour, visited, recStack)) return true; else if (recStack[neighbour]) return true; } } recStack[v] = false; return false; } public bool IsCyclic() { bool[] visited = new bool[V]; bool[] recStack = new bool[V]; for (int i = 0; i < V; i++) { if (DFS(i, visited, recStack)) return true; } return false; } } class Program { static void Main() { Graph g = new Graph(6); g.AddEdge(0, 1); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(3, 4); g.AddEdge(4, 5); Console.WriteLine(g.IsCyclic() ? "Cycle detected" : "No cycle"); } }
Attempts:
2 left
💡 Hint
Check each disconnected component for cycles.
✗ Incorrect
The first component (0->1->2->0) forms a cycle. The second component (3->4->5) has no cycle. Since one component has a cycle, the output is 'Cycle detected'.
🧠 Conceptual
expertWhy use recursion stack in cycle detection for directed graphs?
In cycle detection for directed graphs using DFS, why do we maintain a recursion stack (recStack) array alongside the visited array?
Attempts:
2 left
💡 Hint
Think about how cycles appear in the current path of DFS traversal.
✗ Incorrect
The recursion stack tracks nodes currently in the DFS path. If we revisit a node in this stack, it means a back edge exists, indicating a cycle. The visited array alone cannot detect this.
