Challenge - 5 Problems
Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS Connected Components Count
What is the output of the following Python code that counts connected components using BFS?
DSA Python
from collections import deque def bfs(graph, start, visited): queue = deque([start]) visited.add(start) while queue: node = queue.popleft() for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) queue.append(neighbor) def count_connected_components(graph): visited = set() count = 0 for node in graph: if node not in visited: bfs(graph, node, visited) count += 1 return count graph = { 1: [2], 2: [1, 3], 3: [2], 4: [5], 5: [4], 6: [] } print(count_connected_components(graph))
Attempts:
2 left
💡 Hint
Count how many groups of nodes are connected without crossing to other groups.
✗ Incorrect
The graph has three connected components: {1,2,3}, {4,5}, and {6}. So the output is 3.
🧠 Conceptual
intermediateUnderstanding BFS Role in Connected Components
Why do we use BFS to find connected components in an undirected graph?
Attempts:
2 left
💡 Hint
Think about how BFS visits nodes layer by layer.
✗ Incorrect
BFS visits all nodes connected to the start node before moving on, so it identifies one whole connected component at a time.
❓ Predict Output
advancedOutput of BFS with Disconnected Graph
What is the output of this code that uses BFS to find connected components in a graph with isolated nodes?
DSA Python
from collections import deque def bfs(graph, start, visited): queue = deque([start]) visited.add(start) while queue: node = queue.popleft() for neighbor in graph.get(node, []): if neighbor not in visited: visited.add(neighbor) queue.append(neighbor) def count_connected_components(graph): visited = set() count = 0 for node in graph: if node not in visited: bfs(graph, node, visited) count += 1 return count graph = { 1: [2], 2: [1], 3: [], 4: [5], 5: [4], 6: [], 7: [] } print(count_connected_components(graph))
Attempts:
2 left
💡 Hint
Count each isolated node and each connected group separately.
✗ Incorrect
The connected components are {1,2}, {3}, {4,5}, {6}, and {7}, totaling 5 components.
🔧 Debug
advancedIdentify the Error in BFS Connected Components Code
What error will this code produce when counting connected components using BFS?
DSA Python
from collections import deque def bfs(graph, start, visited): queue = deque([start]) visited.add(start) while queue: node = queue.pop() for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) queue.append(neighbor) def count_connected_components(graph): visited = set() count = 0 for node in graph: if node not in visited: bfs(graph, node, visited) count += 1 return count graph = { 1: [2], 2: [1, 3], 3: [2] } print(count_connected_components(graph))
Attempts:
2 left
💡 Hint
Check how nodes are removed from the queue.
✗ Incorrect
Using queue.pop() removes from the right end, making it DFS order instead of BFS, but connected components count remains correct.
🚀 Application
expertNumber of Connected Components After Edge Removal
Given the graph and the edges removed, what is the number of connected components after removal?
DSA Python
from collections import deque def bfs(graph, start, visited): queue = deque([start]) visited.add(start) while queue: node = queue.popleft() for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) queue.append(neighbor) def count_connected_components(graph): visited = set() count = 0 for node in graph: if node not in visited: bfs(graph, node, visited) count += 1 return count graph = { 1: [2, 3], 2: [1, 3], 3: [1, 2, 4], 4: [3, 5], 5: [4] } # Remove edges (3,4) and (4,5) graph[3].remove(4) graph[4].remove(3) graph[4].remove(5) graph[5].remove(4) print(count_connected_components(graph))
Attempts:
2 left
💡 Hint
Removing edges splits the graph into separate parts.
✗ Incorrect
After removing edges, the graph splits into three connected components: {1,2,3}, {4}, and {5}.
