How breadth-first search works
A queue holds the vertices waiting to be visited. Take one from the front; if it has already been visited, skip it; otherwise mark it, record it, and add its unvisited neighbours to the back. Because the queue is first-in, first-out, every vertex at distance k is processed before any vertex at distance k + 1.
The example graph has six vertices, A to F, stored as an adjacency list. Starting from A, BFS visits A, B, C, D, E, F.
What to watch for when it runs
- The graph is drawn with visited vertices marked and the current one highlighted.
- The queue is drawn as a queue — vertices join at the back and leave from the front.
- F is queued twice, once from D and once from E; the visited check is what stops it being processed twice.
Time and space complexity
- Time
- O(V + E)
- Space
- O(V)
Every vertex is processed once and every edge looked at a constant number of times. Popping the front of a Python list is itself O(n); collections.deque makes it O(1).