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 C# code that counts connected components using BFS?
DSA C#
using System; using System.Collections.Generic; class Program { static int CountConnectedComponents(int n, List<int>[] graph) { bool[] visited = new bool[n]; int count = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { count++; Queue<int> queue = new Queue<int>(); queue.Enqueue(i); visited[i] = true; while (queue.Count > 0) { int node = queue.Dequeue(); foreach (int neighbor in graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; queue.Enqueue(neighbor); } } } } } return count; } static void Main() { int n = 5; List<int>[] graph = new List<int>[n]; for (int i = 0; i < n; i++) graph[i] = new List<int>(); graph[0].Add(1); graph[1].Add(0); graph[2].Add(3); graph[3].Add(2); // Node 4 is isolated Console.WriteLine(CountConnectedComponents(n, graph)); } }
Attempts:
2 left
💡 Hint
Count how many groups of nodes are connected directly or indirectly.
✗ Incorrect
The graph has three connected components: {0,1}, {2,3}, and {4} isolated. So the count is 3.
🧠 Conceptual
intermediateUnderstanding BFS for Connected Components
Why does BFS help in finding connected components in an undirected graph?
Attempts:
2 left
💡 Hint
Think about how BFS visits nodes layer by layer.
✗ Incorrect
BFS starts from a node and visits all nodes reachable from it, marking them visited. This set of nodes forms one connected component. Repeating BFS from unvisited nodes finds other components.
❓ Predict Output
advancedOutput of BFS Connected Components with Cycle
What is the output of the following C# code counting connected components using BFS on a graph with a cycle?
DSA C#
using System; using System.Collections.Generic; class Program { static int CountConnectedComponents(int n, List<int>[] graph) { bool[] visited = new bool[n]; int count = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { count++; Queue<int> queue = new Queue<int>(); queue.Enqueue(i); visited[i] = true; while (queue.Count > 0) { int node = queue.Dequeue(); foreach (int neighbor in graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; queue.Enqueue(neighbor); } } } } } return count; } static void Main() { int n = 6; List<int>[] graph = new List<int>[n]; for (int i = 0; i < n; i++) graph[i] = new List<int>(); graph[0].Add(1); graph[1].Add(0); graph[1].Add(2); graph[2].Add(1); graph[2].Add(3); graph[3].Add(2); graph[3].Add(0); graph[0].Add(3); graph[4].Add(5); graph[5].Add(4); Console.WriteLine(CountConnectedComponents(n, graph)); } }
Attempts:
2 left
💡 Hint
Count groups of nodes connected directly or indirectly, cycles do not increase count.
✗ Incorrect
Nodes 0,1,2,3 form one connected component with a cycle. Nodes 4 and 5 form another. So total 2 components.
🔧 Debug
advancedIdentify the Bug in BFS Connected Components Code
What error will the following C# code produce when counting connected components using BFS?
DSA C#
using System; using System.Collections.Generic; class Program { static int CountConnectedComponents(int n, List<int>[] graph) { bool[] visited = new bool[n]; int count = 0; for (int i = 0; i <= n; i++) { // Note the <= instead of < if (!visited[i]) { count++; Queue<int> queue = new Queue<int>(); queue.Enqueue(i); visited[i] = true; while (queue.Count > 0) { int node = queue.Dequeue(); foreach (int neighbor in graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; queue.Enqueue(neighbor); } } } } } return count; } static void Main() { int n = 3; List<int>[] graph = new List<int>[n]; for (int i = 0; i < n; i++) graph[i] = new List<int>(); graph[0].Add(1); graph[1].Add(0); graph[1].Add(2); graph[2].Add(1); Console.WriteLine(CountConnectedComponents(n, graph)); }
Attempts:
2 left
💡 Hint
Check the loop boundary condition for array access.
✗ Incorrect
The for loop uses i <= n which tries to access visited[n], out of bounds causing IndexOutOfRangeException.
🚀 Application
expertNumber of Connected Components After Edge Removal
Given a graph with 7 nodes and edges: (0-1), (1-2), (2-3), (3-0), (4-5), (5-6), (6-4), if the edge (1-2) is removed, what is the number of connected components?
Attempts:
2 left
💡 Hint
In a cycle, removing one edge does not disconnect the graph; it becomes a path.
✗ Incorrect
The graph originally has two connected components: {0,1,2,3} and {4,5,6}. Removing edge (1-2) does not disconnect the first component because there is still a path 1-0-3-2. Thus, still 2 components.
