Computer Science I / Graph Algorithms and Traversal
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
  1. Which node comes out next in each case?
  2. 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.

Read the lesson: Graph Algorithms and Traversal →

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.

More questions on Graph Algorithms and Traversal