Challenge - 5 Problems
Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of DFS-based Cycle Detection
What is the output of the following Java code that detects a cycle in an undirected graph using DFS?
DSA Java
import java.util.*; public class GraphCycle { private int V; private LinkedList<Integer>[] adj; public GraphCycle(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; i++) { adj[i] = new LinkedList<>(); } } void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); } boolean isCyclicUtil(int v, boolean[] visited, int parent) { visited[v] = true; for (Integer i : adj[v]) { if (!visited[i]) { if (isCyclicUtil(i, visited, v)) return true; } else if (i != parent) { return true; } } return false; } boolean isCyclic() { boolean[] visited = new boolean[V]; for (int u = 0; u < V; u++) { if (!visited[u]) { if (isCyclicUtil(u, visited, -1)) return true; } } return false; } public static void main(String[] args) { GraphCycle g = new GraphCycle(4); g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(3, 0); System.out.println(g.isCyclic()); } }
Attempts:
2 left
💡 Hint
Think about whether the graph edges form a closed loop.
✗ Incorrect
The graph has edges forming a cycle: 0-1-2-3-0. The DFS detects this cycle and returns true.
❓ Predict Output
intermediateOutput of BFS-based Cycle Detection
What will be the output of this Java code that uses BFS to detect a cycle in an undirected graph?
DSA Java
import java.util.*; public class GraphCycleBFS { private int V; private LinkedList<Integer>[] adj; public GraphCycleBFS(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; i++) { adj[i] = new LinkedList<>(); } } void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); } boolean isCyclic() { boolean[] visited = new boolean[V]; int[] parent = new int[V]; Arrays.fill(parent, -1); for (int i = 0; i < V; i++) { if (!visited[i]) { Queue<Integer> queue = new LinkedList<>(); visited[i] = true; queue.add(i); while (!queue.isEmpty()) { int u = queue.poll(); for (int v : adj[u]) { if (!visited[v]) { visited[v] = true; parent[v] = u; queue.add(v); } else if (parent[u] != v) { return true; } } } } } return false; } public static void main(String[] args) { GraphCycleBFS g = new GraphCycleBFS(3); g.addEdge(0, 1); g.addEdge(1, 2); System.out.println(g.isCyclic()); } }
Attempts:
2 left
💡 Hint
Check if the graph edges form any cycle.
✗ Incorrect
The graph is a simple chain 0-1-2 with no cycles, so the BFS cycle detection returns false.
🔧 Debug
advancedIdentify the bug in cycle detection code
What error will occur when running this Java code for cycle detection in an undirected graph?
DSA Java
import java.util.*; public class BuggyGraph { private int V; private LinkedList<Integer>[] adj; public BuggyGraph(int v) { V = v; adj = new LinkedList[V]; } void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); } boolean isCyclicUtil(int v, boolean[] visited, int parent) { visited[v] = true; for (int i : adj[v]) { if (!visited[i]) { if (isCyclicUtil(i, visited, v)) return true; } else if (i != parent) { return true; } } return false; } boolean isCyclic() { boolean[] visited = new boolean[V]; for (int u = 0; u < V; u++) { if (!visited[u]) { if (isCyclicUtil(u, visited, -1)) return true; } } return false; } public static void main(String[] args) { BuggyGraph g = new BuggyGraph(3); g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(2, 0); System.out.println(g.isCyclic()); } }
Attempts:
2 left
💡 Hint
Check how the adjacency list array is initialized.
✗ Incorrect
The adjacency list array is created with LinkedList[V] but Java does not allow generic array creation directly, causing a NullPointerException when accessing adj[i].add().
🧠 Conceptual
advancedWhy is parent tracking needed in cycle detection?
In cycle detection for an undirected graph using DFS, why do we track the parent node of the current node?
Attempts:
2 left
💡 Hint
Think about why revisiting the immediate previous node is not a cycle.
✗ Incorrect
In an undirected graph, the edge back to the parent is not a cycle. Tracking parent helps ignore this edge when checking for cycles.
🚀 Application
expertDetect cycle in a disconnected undirected graph
Given a disconnected undirected graph with 5 vertices and edges: (0-1), (1-2), (3-4), (4-3), what is the output of cycle detection using DFS?
DSA Java
import java.util.*; public class DisconnectedGraphCycle { private int V; private LinkedList<Integer>[] adj; public DisconnectedGraphCycle(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; i++) { adj[i] = new LinkedList<>(); } } void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); } boolean isCyclicUtil(int v, boolean[] visited, int parent) { visited[v] = true; for (Integer i : adj[v]) { if (!visited[i]) { if (isCyclicUtil(i, visited, v)) return true; } else if (i != parent) { return true; } } return false; } boolean isCyclic() { boolean[] visited = new boolean[V]; for (int u = 0; u < V; u++) { if (!visited[u]) { if (isCyclicUtil(u, visited, -1)) return true; } } return false; } public static void main(String[] args) { DisconnectedGraphCycle g = new DisconnectedGraphCycle(5); g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(3, 4); g.addEdge(4, 3); System.out.println(g.isCyclic()); } }
Attempts:
2 left
💡 Hint
Check if any connected component contains a cycle.
✗ Incorrect
The component with nodes 3 and 4 has edges forming a cycle (3-4 and 4-3). The cycle detection returns true.
