A connected component is a group of nodes in a graph where each node is reachable from any other node in the same group.
Click to reveal answer
beginner
How does BFS help in finding connected components?
BFS explores all nodes reachable from a starting node, marking them as visited. This helps identify one connected component at a time.
Click to reveal answer
beginner
What data structure is commonly used to implement BFS?
A queue is used to keep track of nodes to visit next in BFS.
Click to reveal answer
intermediate
In BFS for connected components, what do you do when you find an unvisited node?
Start a BFS from that node to find all nodes in its connected component and mark them visited.
Click to reveal answer
intermediate
Why do we need to check all nodes when finding connected components?
Because the graph may have multiple disconnected parts, so we must start BFS from every unvisited node to find all components.
Click to reveal answer
What does BFS use to keep track of nodes to visit?
AQueue
BStack
CSet
DArray
✗ Incorrect
BFS uses a queue to visit nodes in the order they are discovered.
What is the main goal when using BFS to find connected components?
ACount edges in the graph
BFind shortest path between two nodes
CSort nodes by value
DVisit all nodes in one connected component
✗ Incorrect
BFS visits all nodes reachable from a starting node, identifying one connected component.
If a node is already visited, what should BFS do when it encounters it again?
AIgnore it
BAdd it to the queue twice
CRemove it from the graph
DVisit it again
✗ Incorrect
Visited nodes are ignored to avoid repeated processing and infinite loops.
How do you find all connected components in a graph using BFS?
ARun BFS from every node regardless of visited status
BRun BFS from every unvisited node
CRun BFS once from any node
DRun BFS only from nodes with highest degree
✗ Incorrect
Running BFS from every unvisited node ensures all components are found.
What type of graph can BFS be used on to find connected components?
AOnly directed graphs
BOnly trees
CAny graph (directed or undirected)
DOnly weighted graphs
✗ Incorrect
BFS can be used on any graph type to find connected components.
Explain step-by-step how BFS is used to find connected components in a graph.
Think about visiting nodes layer by layer and marking them.
You got /5 concepts.
Why is it important to mark nodes as visited during BFS when finding connected components?
Consider what happens if you visit the same node multiple times.
You got /4 concepts.
Practice
(1/5)
1. What does the BFS algorithm help find in an undirected graph when used for connected components?
easy
A. All nodes reachable from a starting node forming one connected component
B. The shortest path between two nodes only
C. The maximum degree of any node in the graph
D. The total number of edges in the graph
Solution
Step 1: Understand BFS purpose in connected components
BFS explores all nodes reachable from a starting node, marking them as part of the same connected component.
Step 2: Differentiate BFS from other graph properties
BFS does not directly find shortest paths between arbitrary nodes, maximum degree, or edge count in this context.
Final Answer:
All nodes reachable from a starting node forming one connected component -> Option A
Quick Check:
BFS finds connected components = D [OK]
Hint: BFS visits all nodes connected to start node [OK]
Common Mistakes:
Confusing BFS with shortest path only
Thinking BFS counts edges or degrees
Mixing BFS with DFS concepts
2. Which of the following is the correct way to initialize a queue for BFS in JavaScript?
easy
A. const queue = new Set(); queue.add(startNode);
B. const queue = []; queue.push(startNode);
C. const queue = {}; queue.enqueue(startNode);
D. const queue = null; queue.push(startNode);
Solution
Step 1: Identify correct queue initialization
In JavaScript, a queue for BFS is commonly implemented using an array with push() to add elements.
Step 2: Check other options for errors
Set does not have push(), object {} has no enqueue method, and null cannot be used as a queue.
Final Answer:
const queue = []; queue.push(startNode); -> Option B
Quick Check:
Array with push() for queue = B [OK]
Hint: Use array with push/pop or shift for BFS queue [OK]
Common Mistakes:
Using Set instead of array for queue
Using object without queue methods
Initializing queue as null
3. What will be the output of the following code snippet that finds connected components using BFS?
const graph = {
0: [1],
1: [0, 2],
2: [1],
3: [4],
4: [3]
};
function bfs(start, visited) {
const queue = [start];
visited[start] = true;
const component = [];
while (queue.length) {
const node = queue.shift();
component.push(node);
for (const neighbor of graph[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.push(neighbor);
}
}
}
return component;
}
const visited = {};
const components = [];
for (const node in graph) {
if (!visited[node]) {
components.push(bfs(node, visited));
}
}
console.log(components);
medium
A. [[0,1,2],[3,4]]
B. [[0,1],[2,3,4]]
C. [[0,1,2,3,4]]
D. [[1,0,2],[4,3]]
Solution
Step 1: Trace BFS from node 0
BFS visits 0, then 1, then 2 forming component [0,1,2].
Step 2: Trace BFS from node 3
Next unvisited node is 3, BFS visits 3 and 4 forming component [3,4].
Final Answer:
[[0,1,2],[3,4]] -> Option A
Quick Check:
Two connected components found = A [OK]
Hint: BFS groups connected nodes in order visited [OK]
Common Mistakes:
Assuming all nodes in one component
Mixing order of nodes in components
Ignoring visited tracking
4. Identify the bug in this BFS connected components code snippet:
function bfs(start, visited, graph) {
const queue = [start];
visited[start] = true;
const component = [];
while (queue.length > 0) {
const node = queue.pop();
component.push(node);
for (const neighbor of graph[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.push(neighbor);
}
}
}
return component;
}
medium
A. For loop syntax is incorrect
B. Not marking start node as visited
C. Using queue.pop() instead of queue.shift() causes wrong BFS order
D. queue.push(neighbor) should be queue.unshift(neighbor)
Solution
Step 1: Check queue operation for BFS
BFS requires removing from front (queue.shift()), but code uses pop() which removes from end, making it DFS behavior.
Step 2: Verify other parts
Start node is marked visited, for loop syntax is correct, and push() is correct to add neighbors at end.
Final Answer:
Using queue.pop() instead of queue.shift() causes wrong BFS order -> Option C
Quick Check:
pop() breaks BFS order = A [OK]
Hint: Use shift() to dequeue in BFS, pop() is for DFS [OK]
Common Mistakes:
Confusing pop() with shift() in queue
Not marking visited nodes
Incorrect loop syntax
5. Given a graph represented as an adjacency list, how would you modify the BFS connected components algorithm to count the number of connected components without storing the nodes in each component?
hard
A. Use DFS instead of BFS to count components
B. Store all nodes in a global array and count unique nodes after BFS
C. Count edges visited during BFS to find components
D. Increment a counter each time BFS starts from an unvisited node, without collecting nodes
Solution
Step 1: Understand BFS role in counting components
Each BFS call from an unvisited node explores one connected component fully.
Step 2: Modify BFS to count components
Instead of storing nodes, just increment a counter when BFS starts from an unvisited node.
Final Answer:
Increment a counter each time BFS starts from an unvisited node, without collecting nodes -> Option D
Quick Check:
Count BFS starts = connected components [OK]
Hint: Count BFS calls from unvisited nodes for components [OK]
Common Mistakes:
Storing all nodes unnecessarily
Confusing edge count with component count
Switching BFS to DFS without reason
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.