BFS stands for Breadth First Search. It is a method to explore all nodes of a graph level by level, starting from a chosen node.
Click to reveal answer
beginner
In BFS, which data structure is primarily used to keep track of nodes to visit next?
A queue is used in BFS to keep track of nodes to visit next. It ensures nodes are explored in the order they are discovered.
Click to reveal answer
intermediate
What is the main difference between BFS and DFS in graph traversal?
BFS explores nodes level by level using a queue, while DFS explores as deep as possible along each branch using a stack or recursion.
Click to reveal answer
intermediate
What is the time complexity of BFS on a graph with V vertices and E edges?
The time complexity of BFS is O(V + E), where V is the number of vertices and E is the number of edges, because each vertex and edge is processed once.
Click to reveal answer
beginner
Why do we mark nodes as visited in BFS?
Marking nodes as visited prevents revisiting the same node multiple times, which avoids infinite loops and ensures correct traversal.
Click to reveal answer
Which data structure does BFS use to keep track of nodes to visit?
APriority Queue
BStack
CQueue
DHash Map
✗ Incorrect
BFS uses a queue to explore nodes in the order they are discovered.
What is the first step in BFS traversal starting from a node?
AAdd all neighbors to the queue
BAdd the starting node to the queue and mark it visited
CMark all nodes as visited
DRemove the starting node from the graph
✗ Incorrect
BFS starts by adding the starting node to the queue and marking it visited.
What happens when BFS visits a node's neighbor that is already visited?
AIt restarts the BFS
BIt adds it to the queue again
CIt removes the neighbor from the graph
DIt skips adding it to the queue again
✗ Incorrect
BFS skips neighbors already visited to avoid loops and repeated processing.
Which of these is a correct BFS traversal order for a graph starting at node 1 with neighbors 2 and 3?
A1 -> 2 -> 3
B1 -> 3 -> 2
C2 -> 3 -> 1
D3 -> 2 -> 1
✗ Incorrect
BFS visits the starting node first, then its neighbors in the order they are discovered.
What is the space complexity of BFS in terms of vertices V?
AO(V)
BO(1)
CO(V^2)
DO(log V)
✗ Incorrect
BFS requires space to store the queue and visited array, both proportional to the number of vertices.
Explain how BFS works step-by-step on a graph starting from a given node.
Think about how you explore friends in layers, starting from one person.
You got /4 concepts.
Describe why BFS is useful and what kind of problems it can solve.
Consider situations where you want to find the closest or all nodes reachable from a start.
You got /3 concepts.
Practice
(1/5)
1. What does BFS (Breadth First Search) primarily use to keep track of nodes to visit next in a graph?
easy
A. A queue to explore nodes level by level
B. A stack to explore nodes deeply first
C. A hash map to store visited nodes only
D. A priority queue to explore nodes by weight
Solution
Step 1: Understand BFS traversal method
BFS explores nodes in layers, visiting all neighbors before going deeper.
Step 2: Identify the data structure used
To achieve level-by-level traversal, BFS uses a queue to store nodes to visit next.
Final Answer:
A queue to explore nodes level by level -> Option A
Quick Check:
BFS uses queue = A [OK]
Hint: BFS uses a queue, DFS uses a stack [OK]
Common Mistakes:
Confusing BFS with DFS and choosing stack
Thinking BFS uses priority queue for weights
Ignoring the need for a data structure to track nodes
2. Which of the following is the correct way to enqueue a node `v` in a BFS implementation using C?
easy
A. queue.push(v);
B. queue.insert(v);
C. queue.add(v);
D. enqueue(queue, v);
Solution
Step 1: Recognize C queue operation syntax
In C, queues are usually implemented with functions like enqueue(queue, value).
Step 2: Identify correct enqueue function call
queue.push and queue.add are not standard C; enqueue(queue, v) is typical.
Final Answer:
enqueue(queue, v); -> Option D
Quick Check:
Use enqueue(queue, v) in C [OK]
Hint: C uses enqueue(queue, value) for queues [OK]
Common Mistakes:
Using push or add which are not C queue functions
Confusing queue insertion with array insertion
Using incorrect method names from other languages
3. Consider the following graph represented as adjacency list:
0: 1, 2
1: 0, 3
2: 0, 3
3: 1, 2
If BFS starts from node 0, what is the order of nodes visited?
medium
A. [0, 2, 1, 3]
B. [0, 1, 2, 3]
C. [0, 3, 1, 2]
D. [0, 1, 3, 2]
Solution
Step 1: Start BFS from node 0
Visit 0, enqueue neighbors 1 and 2.
Step 2: Visit nodes in queue order
Dequeue 1, enqueue its unvisited neighbor 3; then dequeue 2, neighbor 3 already visited.
Step 3: Visit remaining node 3
Dequeue 3, no new neighbors.
Final Answer:
[0, 1, 2, 3] -> Option B
Quick Check:
BFS order = [0, 1, 2, 3] [OK]
Hint: BFS visits neighbors in queue order [OK]
Common Mistakes:
Visiting nodes in DFS order instead
Mixing neighbor order causing wrong sequence
Not marking visited nodes causing repeats
4. The following BFS code snippet has a bug. What is the error?
void bfs(int start, int n, int adj[][n]) {
int visited[n];
for(int i=0; i<n; i++) visited[i] = 0;
int queue[n], front=0, rear=0;
queue[rear++] = start;
visited[start] = 1;
while(front < rear) {
int u = queue[front++];
printf("%d ", u);
for(int v=0; v<n; v++) {
if(adj[u][v] == 1 && visited[v] == 0) {
queue[rear++] = v;
// Missing visited mark here
}
}
}
}
medium
A. Visited array is not initialized
B. Queue overflow not checked
C. Visited mark missing inside neighbor loop
D. Start node not enqueued
Solution
Step 1: Check visited array initialization
Visited array is initialized to 0 for all nodes, so no issue here.
Step 2: Inspect neighbor loop for visited marking
Inside the neighbor loop, nodes are enqueued but visited[v] is not set to 1, causing repeated visits.
Step 3: Confirm other options
Queue overflow is not handled but not the main bug; start node is enqueued correctly.
Final Answer:
Visited mark missing inside neighbor loop -> Option C
Quick Check:
Mark visited when enqueueing neighbors [OK]
Hint: Always mark visited when enqueueing nodes [OK]
Common Mistakes:
Forgetting to mark visited inside neighbor loop
Assuming initialization fixes all visited issues
Ignoring queue overflow but it's not main bug
5. Given a graph with nodes 0 to 4 and edges: 0-1, 0-2, 1-3, 2-3, 3-4. Using BFS starting at node 0, which node is visited at the 3rd step (0-based index)?
hard
A. 2
B. 3
C. 1
D. 4
Solution
Step 1: List BFS order from node 0
Start at 0, enqueue neighbors 1 and 2. Visit order so far: [0]