LearnThatStack Ace your next interview
Graphs · question
Question 6 of 60

How does breadth-first search explore a graph, step by step?

beginner
← All Graphs questions
Re-explain
Visual ↓

Breadth-first search fans out in layers, visiting everything one step away before anything two steps away. You keep a queue of vertices to process and a visited set so you never revisit. Start by marking the source visited and putting it in the queue.

Then repeat until the queue empties:

  • Remove the front vertex
  • Look at each of its neighbors
  • For any unvisited neighbor, mark it visited and add it to the back

Because the queue is first-in first-out, closer vertices always come out first. That is what produces the ring-by-ring order. Each vertex enters the queue once and each edge is examined once, so the cost is O(V + E).

This layered order is exactly why BFS finds the fewest-edge path in an unweighted graph. The first time you reach a vertex, you reached it by the shortest number of hops.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Graphs One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
What the BFS queue holds at each step
from collections import deque
graph = {
'A': ['B', 'C'],
'B': ['A', 'D'],
'C': ['A', 'D', 'E'],
'D': ['B', 'C'],
'E': ['C']}
visited = {'A'}
queue = deque(['A'])
while queue:
v = queue.popleft()
print(v)
for n in graph[v]:
if n not in visited:
visited.add(n)
queue.append(n)
Verticesordered by hops from A
ABCDE
1 hop2 hopsvisited
Queuefirst in, first out
A
Console
1step

Start at the source, A. Mark it visited, which means we have seen it, and put it in the queue of vertices waiting their turn.

1 / 9

The queue is first in, first out, so vertices one hop away come out before any vertex two hops away. That is why BFS finds the path with the fewest edges.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. BFS explores a graph in layers, visiting everything one edge from the start before anything two edges away.
  2. It keeps a queue of vertices to process and a visited set, and it starts by marking the source visited and enqueuing it.
  3. Then, until the queue is empty, I take the front vertex and mark and enqueue every neighbor that isn't visited yet.
  4. The queue is first-in first-out, so closer vertices always come out before farther ones, and that gives the ring-by-ring order.
  5. Each vertex enters the queue once and each adjacency list gets scanned once, so it's O(V + E).
  6. That layering is why BFS finds the fewest-edge path in an unweighted graph, but with weighted edges fewest hops isn't cheapest, so I'd use Dijkstra.

Weak answers to avoid

  • Leaves out the visited set On a graph with a cycle the search loops forever or reprocesses vertices. A tree can get away without one and a graph cannot, so name it up front.
  • Marks vertices visited only when dequeued The same vertex then gets enqueued once for every neighbor that sees it, which adds duplicate work. Mark it on enqueue so each vertex enters the queue once.
  • Says BFS finds shortest paths in any graph That only holds when every edge counts the same. Say it's for unweighted graphs and name Dijkstra for weighted ones, or expect the interviewer to push there.
  • Recites the loop but never says why it's layered The point is the chain from first-in first-out to ring-by-ring order to fewest hops. Without it, the steps sound memorized.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Graphs cheatsheet.

← Back to all Graphs questions
Pro · $10/mo

51 of 60 Graphs answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime