Challenge - 5 Problems
BFS 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 JavaScript code that counts connected components using BFS?
DSA Javascript
function bfs(graph, start, visited) {
let queue = [start];
visited.add(start);
while (queue.length > 0) {
let node = queue.shift();
for (let neighbor of graph[node]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
}
function countConnectedComponents(graph) {
let visited = new Set();
let count = 0;
for (let node in graph) {
if (!visited.has(node)) {
bfs(graph, node, visited);
count++;
}
}
return count;
}
const graph = {
'0': ['1'],
'1': ['0', '2'],
'2': ['1'],
'3': ['4'],
'4': ['3'],
'5': []
};
console.log(countConnectedComponents(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: {0,1,2}, {3,4}, and {5}. So the output is 3.
❓ Predict Output
intermediateOutput of BFS Traversal Order
What is the order of nodes visited by BFS starting from node '2' in the graph below?
DSA Javascript
function bfsOrder(graph, start) {
let visited = new Set();
let queue = [start];
let order = [];
visited.add(start);
while (queue.length > 0) {
let node = queue.shift();
order.push(node);
for (let neighbor of graph[node]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
return order;
}
const graph = {
'0': ['1', '2'],
'1': ['0', '3'],
'2': ['0', '4'],
'3': ['1'],
'4': ['2']
};
console.log(bfsOrder(graph, '2'));Attempts:
2 left
💡 Hint
BFS visits neighbors level by level, starting from the start node.
✗ Incorrect
Starting at '2', neighbors are '0' and '4'. Then from '0', neighbors '1' is visited next, then from '1' neighbor '3'. So order is ['2', '0', '4', '1', '3'].
🔧 Debug
advancedIdentify the error in BFS connected components code
What error will this code produce when counting connected components using BFS?
DSA Javascript
function bfs(graph, start, visited) {
let queue = [start];
visited.add(start);
while (queue.length > 0) {
let node = queue.shift();
for (let neighbor of graph[node]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
}
function countConnectedComponents(graph) {
let visited = new Set();
let count = 0;
for (let node in graph) {
if (!visited.has(node)) {
bfs(graph, node, visited);
count++;
}
}
return count;
}
const graph = {
'0': ['1'],
'1': ['0', '2'],
'2': ['1'],
'3': ['4'],
'4': ['3'],
'5': []
};
console.log(countConnectedComponents(graph));Attempts:
2 left
💡 Hint
Check how keys are used in the graph object and if they match the visited set keys.
✗ Incorrect
The graph keys are numbers but JavaScript converts them to strings when used as object keys. The visited set stores strings, so the code works fine and outputs 3.
❓ Predict Output
advancedOutput of BFS on Disconnected Graph
What is the output of BFS traversal starting from node 'A' on this disconnected graph?
DSA Javascript
function bfsTraversal(graph, start) {
let visited = new Set();
let queue = [start];
let result = [];
visited.add(start);
while (queue.length > 0) {
let node = queue.shift();
result.push(node);
for (let neighbor of graph[node]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
return result;
}
const graph = {
'A': ['B'],
'B': ['A'],
'C': ['D'],
'D': ['C'],
'E': []
};
console.log(bfsTraversal(graph, 'A'));Attempts:
2 left
💡 Hint
BFS only visits nodes connected to the start node.
✗ Incorrect
Starting at 'A', BFS visits 'B' next. Nodes 'C', 'D', and 'E' are disconnected and not visited.
🧠 Conceptual
expertMinimum BFS Runs to Find All Connected Components
Given an undirected graph with N nodes and M edges, what is the minimum number of BFS runs needed to find all connected components?
Attempts:
2 left
💡 Hint
Think about how BFS explores nodes and when you need to start a new BFS.
✗ Incorrect
Each BFS run explores one connected component fully. So the minimum BFS runs equal the number of connected components.
