What is the main idea behind using BFS to find the shortest path in an unweighted graph?
BFS explores nodes level by level, so the first time it reaches a node, it has found the shortest path to that node because all edges have equal weight.
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 in the order they were discovered, ensuring level-by-level exploration.
Click to reveal answer
intermediate
How do you keep track of the actual shortest path from the start node to a target node in BFS?
By storing the parent (or predecessor) of each visited node, you can backtrack from the target node to the start node to reconstruct the path.
Click to reveal answer
intermediate
Why can't BFS be used directly to find shortest paths in weighted graphs?
Because BFS assumes all edges have equal weight; in weighted graphs, edges have different costs, so BFS might not find the true shortest path.
Click to reveal answer
intermediate
What is the time complexity of BFS when used to find 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 node and edge is processed once.
Click to reveal answer
What data structure does BFS use to explore nodes in an unweighted graph?
AQueue
BStack
CPriority Queue
DHash Map
✗ Incorrect
BFS uses a queue to explore nodes level by level.
When BFS first visits a node, what does it guarantee about the path to that node?
AIt is the heaviest path
BIt is the longest path
CIt is the shortest path
DIt is a random path
✗ Incorrect
BFS finds the shortest path in an unweighted graph because it explores neighbors level by level.
How do you reconstruct the shortest path after BFS completes?
ABy using a stack to store visited nodes
BBy following parent pointers from target to start
CBy sorting nodes by distance
DBy running DFS again
✗ Incorrect
Parent pointers let you backtrack from the target node to the start node to get the path.
Which of these graphs is BFS NOT suitable for finding shortest paths?
AWeighted graph with different edge costs
BUnweighted graph
CGraph with equal edge weights
DUndirected graph
✗ Incorrect
BFS does not handle weighted graphs with different edge costs correctly for shortest paths.
What is the time complexity of BFS in terms of vertices (V) and edges (E)?
AO(V^2)
BO(log V)
CO(E^2)
DO(V + E)
✗ Incorrect
BFS visits each vertex and edge once, so the complexity is O(V + E).
Explain how BFS finds the shortest path in an unweighted graph and how to reconstruct that path.
Think about how BFS visits nodes and how parents help build the path.
You got /5 concepts.
Why is BFS not suitable for shortest path in weighted graphs and what algorithm should be used instead?
Consider edge weights and how BFS treats them.
You got /4 concepts.
Practice
(1/5)
1. What does BFS (Breadth-First Search) help find in an unweighted graph?
easy
A. The node with the highest degree
B. The longest path in the graph
C. The shortest path from a start node to all other nodes
D. The minimum spanning tree
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, the first time it reaches a node is via the shortest path.
Final Answer:
The shortest path from a start node to all other nodes -> Option C
Quick Check:
BFS = Shortest path in unweighted graph [OK]
Hint: BFS visits nodes by distance layers to find shortest paths [OK]
Common Mistakes:
Confusing BFS with DFS for shortest path
Thinking BFS finds longest paths
Mixing BFS with minimum spanning tree algorithms
2. Which Go data structure is best to implement the queue needed for BFS?
easy
A. map
B. channel
C. struct
D. slice
Solution
Step 1: Identify queue requirements in BFS
BFS requires a FIFO (first-in, first-out) structure to process nodes in order.
Step 2: Use slice as queue in Go
Slices can be used to append at the end and remove from the front, effectively acting as a queue.
Final Answer:
slice -> Option D
Quick Check:
Queue for BFS = slice [OK]
Hint: Use slice with append and slicing for queue in Go [OK]
Common Mistakes:
Using map which is unordered
Using struct without queue logic
Using channel without concurrency context
3. What is the output distance array after running BFS from node 0 on this graph?