Computer Science I / Graph Algorithms and Traversal
Practice question · Put in order

Order the steps of breadth-first search from a start vertex.

Hints
  1. BFS uses a queue: first in, first out.
  2. You dequeue a vertex before you can look at its neighbours.
Show the answer
  1. Put the start vertex in the queue and mark it visited
  2. Remove the front vertex from the queue
  3. Visit each unvisited neighbour, marking it and adding it to the queue
  4. Repeat until the queue is empty
Why

BFS seeds the queue, then repeatedly dequeues a vertex and enqueues its unvisited neighbours, rippling outward level by level. The FIFO queue is what produces the level-order and the shortest paths.

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