Challenge - 5 Problems
BFS Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BFS Connected Components Count
What is the output of the following C code that counts connected components using BFS on an undirected graph?
DSA C
#include <stdio.h> #include <stdlib.h> #define MAX 5 int graph[MAX][MAX] = { {0,1,0,0,0}, {1,0,1,0,0}, {0,1,0,0,0}, {0,0,0,0,1}, {0,0,0,1,0} }; int visited[MAX] = {0}; void bfs(int start) { int queue[MAX], front = 0, rear = 0; queue[rear++] = start; visited[start] = 1; while(front < rear) { int node = queue[front++]; for(int i = 0; i < MAX; i++) { if(graph[node][i] == 1 && !visited[i]) { visited[i] = 1; queue[rear++] = i; } } } } int main() { int count = 0; for(int i = 0; i < MAX; i++) { if(!visited[i]) { bfs(i); count++; } } printf("%d\n", count); return 0; }
Attempts:
2 left
💡 Hint
Count how many times BFS starts from an unvisited node.
✗ Incorrect
The graph has two connected components: nodes 0-1-2 and nodes 3-4. BFS runs twice, so output is 2.
🧠 Conceptual
intermediate1:30remaining
Understanding BFS Queue Behavior in Connected Components
In BFS for connected components, what is the role of the queue?
Attempts:
2 left
💡 Hint
Think about how BFS explores neighbors level by level.
✗ Incorrect
The queue holds nodes discovered but not yet explored, ensuring BFS visits nodes in order of distance from start.
🔧 Debug
advanced2:00remaining
Identify the Bug in BFS Connected Components Code
What error will occur when running this BFS connected components code snippet?
DSA C
#include <stdio.h> #define MAX 4 int graph[MAX][MAX] = { {0,1,0,0}, {1,0,1,0}, {0,1,0,1}, {0,0,1,0} }; int visited[MAX] = {0}; void bfs(int start) { int queue[MAX], front = 0, rear = 0; queue[rear++] = start; visited[start] = 1; while(front <= rear) { int node = queue[front++]; for(int i = 0; i < MAX; i++) { if(graph[node][i] == 1 && !visited[i]) { visited[i] = 1; queue[rear++] = i; } } } } int main() { int count = 0; for(int i = 0; i < MAX; i++) { if(!visited[i]) { bfs(i); count++; } } printf("%d\n", count); return 0; }
Attempts:
2 left
💡 Hint
Check the while loop condition controlling queue processing.
✗ Incorrect
The condition 'front <= rear' allows front to equal rear, causing queue[rear] access which is invalid. It should be 'front < rear'.
🚀 Application
advanced1:30remaining
Number of Connected Components After Adding an Edge
Given a graph with 3 connected components, if you add an edge connecting two nodes from different components, what happens to the number of connected components?
Attempts:
2 left
💡 Hint
Connecting two separate groups merges them into one.
✗ Incorrect
Adding an edge between two different components merges them, reducing total components by one.
❓ Predict Output
expert2:30remaining
Output of BFS Connected Components with Disconnected Single Node
What is the output of this C code counting connected components using BFS on a graph with one isolated node?
DSA C
#include <stdio.h> #define MAX 6 int graph[MAX][MAX] = { {0,1,0,0,0,0}, {1,0,1,0,0,0}, {0,1,0,0,0,0}, {0,0,0,0,1,0}, {0,0,0,1,0,0}, {0,0,0,0,0,0} }; int visited[MAX] = {0}; void bfs(int start) { int queue[MAX], front = 0, rear = 0; queue[rear++] = start; visited[start] = 1; while(front < rear) { int node = queue[front++]; for(int i = 0; i < MAX; i++) { if(graph[node][i] == 1 && !visited[i]) { visited[i] = 1; queue[rear++] = i; } } } } int main() { int count = 0; for(int i = 0; i < MAX; i++) { if(!visited[i]) { bfs(i); count++; } } printf("%d\n", count); return 0; }
Attempts:
2 left
💡 Hint
Count connected groups including isolated nodes.
✗ Incorrect
There are three connected components: nodes 0-1-2, nodes 3-4, and isolated node 5 alone.