What does BFS stand for and what is its main use in graphs?
BFS stands for Breadth-First Search. It is mainly used to explore all nodes of a graph level by level and find the shortest path in an unweighted graph.
Click to reveal answer
beginner
Why is BFS suitable for finding the shortest path in an unweighted graph?
Because BFS explores neighbors level by level, the first time it reaches a node, it has found the shortest path to that node in terms of number of edges.
Click to reveal answer
beginner
In BFS for shortest path, what data structure is commonly used to keep track of nodes to visit next?
A queue is used to keep track of nodes to visit next, ensuring nodes are processed in the order they are discovered.
Click to reveal answer
intermediate
What additional information do we store during BFS to reconstruct the shortest path?
We store the parent (or predecessor) of each node, which helps us trace back the path from the destination to the source after BFS completes.
Click to reveal answer
intermediate
What is the time complexity of BFS when finding the shortest path in an unweighted graph?
The time complexity 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
What data structure does BFS use to explore nodes in order?
AQueue
BStack
CPriority Queue
DHash Map
✗ Incorrect
BFS uses a queue to explore nodes level by level.
Why does BFS find the shortest path in an unweighted graph?
ABecause it ignores visited nodes
BBecause it uses a stack to explore nodes deeply
CBecause it sorts nodes by their values
DBecause it explores nodes in increasing order of distance from the source
✗ Incorrect
BFS explores nodes level by level, so the first time it reaches a node, it is via the shortest path.
What extra information is stored during BFS to reconstruct the shortest path?
AParent of each node
BDistance from source only
CNumber of neighbors
DNode color
✗ Incorrect
Storing the parent of each node allows us to trace back the shortest path after BFS.
What is the time complexity of BFS on a graph with V vertices and E edges?
AO(E^2)
BO(V^2)
CO(V + E)
DO(log V)
✗ Incorrect
BFS visits each vertex and edge once, so the complexity is O(V + E).
Which of these is NOT true about BFS for shortest path in unweighted graphs?
AIt can find shortest path lengths
BIt works only on weighted graphs
CIt uses a queue
DIt stores parents to reconstruct paths
✗ Incorrect
BFS works on unweighted graphs; it does not require weights.
Explain how BFS finds the shortest path in an unweighted graph and how you can reconstruct the path after BFS completes.
Think about visiting nodes in layers and remembering where you came from.
You got /5 concepts.
Describe the time complexity of BFS in terms of vertices and edges and why it is efficient for shortest path in unweighted graphs.
Consider how many times nodes and edges are processed.
You got /5 concepts.
Practice
(1/5)
1. What does BFS (Breadth-First Search) find in an unweighted graph when used from a start node?
easy
A. A random path to any node
B. The longest path in the graph
C. The path with the maximum sum of edge weights
D. The shortest path in terms of number of edges to all reachable nodes
Solution
Step 1: Understand BFS traversal
BFS explores nodes level by level, starting from the source node, visiting all neighbors before moving deeper.
Step 2: BFS finds shortest paths in unweighted graphs
Because BFS visits nodes in order of increasing distance (edges), it finds the shortest path in terms of edge count.
Final Answer:
The shortest path in terms of number of edges to all reachable nodes -> Option D
Quick Check:
BFS = shortest path in unweighted graph [OK]
Hint: BFS finds shortest paths by layers in unweighted graphs [OK]
Common Mistakes:
Confusing BFS with DFS for shortest path
Thinking BFS finds longest paths
Assuming BFS works for weighted graphs without modification
2. Which Rust code snippet correctly initializes a queue for BFS traversal?
easy
A. let queue = Vec::new();
B. let mut queue = std::collections::VecDeque::new();
C. let mut queue = std::collections::HashMap::new();
D. let mut queue = std::collections::BinaryHeap::new();
Solution
Step 1: Identify the correct queue type for BFS
BFS requires a FIFO queue; VecDeque in Rust supports efficient push/pop from both ends.
Step 2: Check each option's suitability
let mut queue = std::collections::VecDeque::new(); uses VecDeque which is correct; Vec (A) is not efficient as a queue; HashMap (C) is a map, not a queue; BinaryHeap (D) is a priority queue, not FIFO.
Final Answer:
let mut queue = std::collections::VecDeque::new(); -> Option B
Quick Check:
VecDeque = correct BFS queue [OK]
Hint: Use VecDeque for BFS queue in Rust [OK]
Common Mistakes:
Using Vec instead of VecDeque for queue
Using HashMap or BinaryHeap incorrectly
Not mutably declaring the queue
3. Given the following Rust code snippet for BFS on an unweighted graph, what is the output distance vector?
let graph = vec![
vec![1, 2],
vec![0, 3],
vec![0, 3],
vec![1, 2, 4],
vec![3]
];
let start = 0;
let mut dist = vec![-1; 5];
dist[start] = 0;
let mut queue = std::collections::VecDeque::new();
queue.push_back(start);
while let Some(u) = queue.pop_front() {
for &v in &graph[u] {
if dist[v] == -1 {
dist[v] = dist[u] + 1;
queue.push_back(v);
}
}
}
println!("{:?}", dist);
medium
A. 0, 1, 1, 2, 3
B. 0, 1, 2, 1, 2
C. 0, 1, 1, 1, 2
D. 0, 2, 2, 3, 4
Solution
Step 1: Trace BFS distances from start node 0
Start at node 0 (distance 0). Neighbors 1 and 2 get distance 1. Then from 1 and 2, node 3 gets distance 2. From 3, node 4 gets distance 3.
4. Identify the bug in this BFS Rust code snippet that causes it to panic or produce incorrect distances:
let graph = vec![vec![1], vec![0, 2], vec![1]];
let start = 0;
let mut dist = vec![0; 3];
let mut queue = std::collections::VecDeque::new();
queue.push_back(start);
while let Some(u) = queue.pop_front() {
for &v in &graph[u] {
if dist[v] == 0 {
dist[v] = dist[u] + 1;
queue.push_back(v);
}
}
}
println!("{:?}", dist);
medium
A. Loop condition is incorrect
B. Queue is not mutable
C. Distance vector initialized with zeros causes confusion with unvisited nodes
D. Graph adjacency list is empty
Solution
Step 1: Check distance vector initialization
Distances initialized to 0 means unvisited nodes and start node share same value, causing logic errors.
Step 2: Explain impact on BFS logic
Condition 'if dist[v] == 0' fails to distinguish unvisited nodes, so some nodes never get updated or cause infinite loops.
Final Answer:
Distance vector initialized with zeros causes confusion with unvisited nodes -> Option C
Quick Check:
Use -1 for unvisited distances [OK]
Hint: Initialize distances with -1 to mark unvisited [OK]
Common Mistakes:
Using 0 as initial distance for all nodes
Not marking start node distance before BFS
Ignoring mutability of queue
5. You want to find the shortest path length from node 0 to node 5 in this unweighted graph using BFS. The graph edges are:
0 - 1, 0 - 2, 1 - 3, 2 - 3, 3 - 4, 4 - 5
Which BFS distance array after traversal correctly shows the shortest distance to each node from 0?
Assume nodes are 0 to 5 and unreachable nodes have distance -1.
hard
A. [0, 1, 1, 2, 3, 4]
B. [0, 1, 2, 3, 4, 5]
C. [0, 1, 1, 1, 2, 3]
D. [0, -1, -1, -1, -1, -1]
Solution
Step 1: Map shortest paths from node 0
From 0: nodes 1 and 2 are 1 edge away; node 3 is 2 edges away (via 1 or 2); node 4 is 3 edges away; node 5 is 4 edges away.