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#
using System; using System.Collections.Generic; class Graph { int V; 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 v, int w) { adj[v].Add(w); adj[w].Add(v); } private bool DFS(int v, bool[] visited, int parent) { visited[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { if (DFS(i, visited, v)) return true; } else if (i != parent) { return true; } } return false; } public bool IsCyclic() { bool[] visited = new bool[V]; for (int u = 0; u < V; u++) { if (!visited[u]) { if (DFS(u, visited, -1)) 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, 0); Console.WriteLine(g.IsCyclic()); } }
Attempts:
2 left
💡 Hint
Think about whether the graph edges form a loop.
✗ Incorrect
The graph has edges forming a cycle: 0-1-2-3-0. The DFS detects this cycle and returns true.
❓ Predict Output
intermediateCycle detection output on a tree structure
What will the output be when running this C# code that checks for cycles in an undirected graph structured as a tree?
DSA C#
using System; using System.Collections.Generic; class Graph { int V; 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 v, int w) { adj[v].Add(w); adj[w].Add(v); } private bool DFS(int v, bool[] visited, int parent) { visited[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { if (DFS(i, visited, v)) return true; } else if (i != parent) { return true; } } return false; } public bool IsCyclic() { bool[] visited = new bool[V]; for (int u = 0; u < V; u++) { if (!visited[u]) { if (DFS(u, visited, -1)) 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()); } }
Attempts:
2 left
💡 Hint
A tree has no cycles.
✗ Incorrect
The graph is a tree with edges 0-1 and 1-2, so no cycles exist. The code returns false.
🔧 Debug
advancedIdentify the bug causing incorrect cycle detection
The following code is intended to detect cycles in an undirected graph. However, it incorrectly reports no cycle for a graph that has one. What is the bug?
DSA C#
using System; using System.Collections.Generic; class Graph { int V; 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 v, int w) { adj[v].Add(w); adj[w].Add(v); } private bool DFS(int v, bool[] visited, int parent) { visited[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { if (DFS(i, visited, v)) return true; } else if (i == parent) { return true; } } return false; } public bool IsCyclic() { bool[] visited = new bool[V]; for (int u = 0; u < V; u++) { if (!visited[u]) { if (DFS(u, visited, -1)) return true; } } return false; } }
Attempts:
2 left
💡 Hint
Check the condition that detects a back edge indicating a cycle.
✗ Incorrect
The code incorrectly returns true when the adjacent node is the parent, which is normal in undirected graphs. It should return true only if the adjacent node is visited and not the parent.
🧠 Conceptual
advancedUnderstanding cycle detection logic in undirected graphs
In cycle detection for undirected graphs using DFS, why do we pass the parent node as a parameter during recursion?
Attempts:
2 left
💡 Hint
Think about why revisiting the immediate previous node is not a cycle.
✗ Incorrect
In undirected graphs, the edge back to the parent is normal and should not be considered a cycle. Passing the parent helps ignore this edge.
🚀 Application
expertNumber of connected components with cycles
Given the following undirected graph edges, how many connected components contain at least one cycle?
Edges:
0-1, 1-2, 2-0, 3-4, 4-5, 5-3, 6-7
Use the cycle detection logic to determine the count.
DSA C#
using System; using System.Collections.Generic; class Graph { int V; 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 v, int w) { adj[v].Add(w); adj[w].Add(v); } private bool DFS(int v, bool[] visited, int parent) { visited[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { if (DFS(i, visited, v)) return true; } else if (i != parent) { return true; } } return false; } public int CountComponentsWithCycle() { bool[] visited = new bool[V]; int count = 0; for (int u = 0; u < V; u++) { if (!visited[u]) { if (DFS(u, visited, -1)) { count++; } } } return count; } } class Program { static void Main() { Graph g = new Graph(8); g.AddEdge(0, 1); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(5, 3); g.AddEdge(6, 7); Console.WriteLine(g.CountComponentsWithCycle()); } }
Attempts:
2 left
💡 Hint
Count connected groups and check which have cycles.
✗ Incorrect
There are three connected components: {0,1,2}, {3,4,5}, and {6,7}. The first two have cycles, the last does not. So count is 2.
