Tutelage

Lessons › coding and computer science · taught by Leo

Graphs and BFS: breadth-first search basics

Learn what graphs store, how BFS uses a queue and visited set, and why it finds shortest paths in unweighted graphs.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

What a graph stores

A graph is just a way to store connections. Think of cities and roads, or people and friendships.

The circles are nodes. The lines between them are edges.

BFS means breadth-first search. It explores a graph layer by layer from one start node.

BFS explores by distance

Breadth-first search visits all nodes at distance 1, then distance 2, then distance 3.

The big idea is distance from the start. BFS does not rush down one long path first.

It checks all the nearest neighbors before moving outward.

That is why BFS finds the shortest path in an unweighted graph.

Predict BFS order

If you start BFS at A, what should it do first?

  1. Visit all neighbors of A
  2. Go as deep as possible down one path
  3. Pick a random node
Show answer

Visit all neighbors of A: Yes. BFS begins with the nodes one edge away from A.

Queue makes BFS work

BFS usually uses a queue. A queue is first in, first out.

You add new neighbors to the back, then take from the front.

That queue order is what keeps the search level by level.

BFS on one small graph

Let’s walk one example. Start at A in this graph and keep a visited list.

First you visit A, then put B and C in the queue.

Next comes B, then C, then the next layer after that.

What comes next in BFS?

After visiting a node, what does BFS usually do next?

  1. Add its unvisited neighbors to the back of the queue
  2. Jump to the farthest node
  3. Erase the queue and restart
Show answer

Add its unvisited neighbors to the back of the queue: Correct. BFS expands from the current node by enqueuing unvisited neighbors.

Why visited matters

Graphs can have cycles, so you can come back to a node you already saw.

A visited set stops infinite loops and repeated work.

So BFS usually keeps two things: a queue and a visited set.

Visited set blocks repeats

Without visited, a cycle can send you in circles.

With visited, each node is handled once.

That makes BFS correct and efficient.

Spot the BFS rule

Why do we mark nodes as visited?

  1. To avoid revisiting the same node in a cycle
  2. To make edges disappear
  3. To sort nodes by name
Show answer

To avoid revisiting the same node in a cycle: Exactly. Visited prevents repeats and infinite loops.

Shortest path idea

In an unweighted graph, BFS finds the shortest path by number of edges.

That happens because it finishes one layer before moving to the next.

So the first time you reach a node, you have the shortest route to it.

Your turn: BFS layer

Starting from S, which nodes are in layer 1?

  1. All neighbors of S
  2. Nodes two edges away
  3. Only the last node
Show answer

All neighbors of S: Good. Layer 1 means exactly one edge away from S.

BFS checklist

Here is the checklist you can use alone.

First put the start node in the queue.

Then repeat: pop the front, mark it visited, and push unvisited neighbors.

BFS in one sentence

BFS is a queue-based graph search that expands outward one layer at a time.

Use it for shortest paths in unweighted graphs, or anytime you need level order.

If you want, I can also show DFS next and compare it with BFS.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app