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 in an undirected graph
What is the output of the following code that detects a cycle in an undirected graph using DFS?
DSA Python
def dfs(node, parent, graph, visited): visited[node] = True for neighbor in graph[node]: if not visited[neighbor]: if dfs(neighbor, node, graph, visited): return True elif neighbor != parent: return True return False graph = {0: [1, 2], 1: [0, 2], 2: [0, 1, 3], 3: [2, 4], 4: [3]} visited = {k: False for k in graph} has_cycle = False for node in graph: if not visited[node]: if dfs(node, -1, graph, visited): has_cycle = True break print(has_cycle)
Attempts:
2 left
💡 Hint
Think about whether the graph contains a triangle or loop.
✗ Incorrect
The graph contains a cycle formed by nodes 0, 1, and 2. The DFS detects this cycle and returns True.
❓ Predict Output
intermediateOutput of cycle detection using BFS in an undirected graph
What is the output of the following code that detects a cycle in an undirected graph using BFS?
DSA Python
from collections import deque def bfs(start, graph, visited): queue = deque([(start, -1)]) visited[start] = True while queue: node, parent = queue.popleft() for neighbor in graph[node]: if not visited[neighbor]: visited[neighbor] = True queue.append((neighbor, node)) elif neighbor != parent: return True return False graph = {0: [1], 1: [0, 2], 2: [1, 3], 3: [2, 4], 4: [3]} visited = {k: False for k in graph} has_cycle = False for node in graph: if not visited[node]: if bfs(node, graph, visited): has_cycle = True break print(has_cycle)
Attempts:
2 left
💡 Hint
Check if the graph has any loops or back edges.
✗ Incorrect
The graph is a simple chain with no cycles, so the BFS returns False.
🔧 Debug
advancedIdentify the error in cycle detection code
What error does the following code produce when detecting cycles in an undirected graph?
DSA Python
def dfs(node, parent, graph, visited): visited[node] = True for neighbor in graph[node]: if not visited[neighbor]: if dfs(neighbor, node, graph, visited): return True elif neighbor == parent: return True return False graph = {0: [1], 1: [0, 2], 2: [1]} visited = {k: False for k in graph} has_cycle = False for node in graph: if not visited[node]: if dfs(node, -1, graph, visited): has_cycle = True break print(has_cycle)
Attempts:
2 left
💡 Hint
Look carefully at the condition that detects a cycle.
✗ Incorrect
The code incorrectly treats the edge to the parent as a cycle, so it returns True even though no cycle exists.
🚀 Application
advancedNumber of connected components with cycles
Given the graph below, how many connected components contain at least one cycle?
DSA Python
graph = {
0: [1],
1: [0, 2, 3],
2: [1, 3],
3: [1, 2],
4: [5],
5: [4],
6: [7],
7: [6, 8],
8: [7]
}
# Use DFS or BFS to find connected components and detect cycles in each.Attempts:
2 left
💡 Hint
Check each connected component for cycles separately.
✗ Incorrect
Only the component with nodes 0,1,2,3 contains a cycle (triangle 1-2-3). The others are simple chains.
🧠 Conceptual
expertWhy does cycle detection in undirected graphs require parent tracking?
Why is it necessary to track the parent node when detecting cycles in an undirected graph using DFS or BFS?
Attempts:
2 left
💡 Hint
Think about the difference between a back edge and the edge to the parent.
✗ Incorrect
In undirected graphs, the edge to the parent is always present and should not be counted as a cycle. Tracking the parent helps distinguish this from actual cycles.
