0
0
DSA Cprogramming~20 mins

Connected Components Using BFS in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BFS Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A2
B0
C3
D1
Attempts:
2 left
💡 Hint
Count how many times BFS starts from an unvisited node.
🧠 Conceptual
intermediate
1:30remaining
Understanding BFS Queue Behavior in Connected Components
In BFS for connected components, what is the role of the queue?
AIt stores nodes to visit next in FIFO order.
BIt stores only the starting node of BFS.
CIt stores all nodes in the graph at once.
DIt stores nodes already visited to avoid repetition.
Attempts:
2 left
💡 Hint
Think about how BFS explores neighbors level by level.
🔧 Debug
advanced
2: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;
}
AInfinite loop because front never increments
BNo output because visited array is never updated
CArray index out of bounds due to incorrect queue condition
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint
Check the while loop condition controlling queue processing.
🚀 Application
advanced
1: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?
AIt becomes zero
BIt increases by 1
CIt stays the same
DIt decreases by 1
Attempts:
2 left
💡 Hint
Connecting two separate groups merges them into one.
Predict Output
expert
2: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;
}
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Count connected groups including isolated nodes.