In graph traversal, what is the practical effect of using a stack (DFS) versus a queue (BFS) when searching for a target node?
Correct! Well done.
Incorrect.
The correct answer is B) A queue (BFS) explores nodes closest to the source first and finds the shortest path in an unweighted graph, while a stack (DFS) dives deep along one branch first and may find a longer path before a shorter one
Correct Answer
A queue (BFS) explores nodes closest to the source first and finds the shortest path in an unweighted graph, while a stack (DFS) dives deep along one branch first and may find a longer path before a shorter one
BFS processes nodes level by level using a queue, so the first time it reaches the target it has found a shortest path (in terms of edge count) in an unweighted graph. DFS uses a stack (or recursion) and explores one path fully before backtracking, which can reach the target via a longer route first.