Practice question · True or false
Swapping the queue in breadth-first search for a stack turns it into depth-first search, changing what the algorithm finds.
Hints
- Which node comes out next in each case?
- FIFO explores by distance; LIFO explores by depth.
Show the answer
True
Why
True. FIFO expands nodes in order of distance from the start, so the first time BFS reaches a node it has found the shortest path; LIFO plunges down one branch first, so DFS finds a path. One line changes the guarantee, which is why the container choice is the algorithm.
Practise Graph Algorithms and Traversal
The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.