Explore a graph level by level
Exploring a graph one layer at a time
Breadth-First Search starts at a source vertex and explores the graph in expanding rings:
Drop a stone into still water. The ripples expand outward in concentric circles. BFS works exactly the same way -- it radiates outward from the source, reaching everything at distance k before anything at distance k+1.
BFS guarantees that when you first reach a vertex, you have found the shortest path (in terms of number of edges) from the source to that vertex.
FIFO ordering is the secret ingredient
A queue processes vertices in the order they were discovered. All level-1 vertices are enqueued before any level-2 vertices, so level-1 is fully processed first. This is what makes it "breadth-first."
If you replace the queue with a stack, you get DFS (Depth-First Search), which goes deep before going wide. The data structure choice is the fundamental difference.
With distance and parent tracking
| Color | Meaning |
|---|---|
| WHITE | Undiscovered |
| GRAY | Discovered, in the queue |
| BLACK | Fully explored (dequeued) |
| Array | Purpose |
|---|---|
dist[v] | Shortest distance from source to v |
parent[v] | Predecessor of v on shortest path |
color[v] | Visit status (WHITE/GRAY/BLACK) |
We mark a vertex as discovered (GRAY) when we enqueue it, not when we dequeue it. This prevents the same vertex from being enqueued multiple times.
Source = A on the following graph
Processing level 1 vertices
Processing level 2 and level 3 vertices
The queue emptied naturally. Every reachable vertex was visited exactly once. Total dequeue operations = 8 (one per vertex).
| Vertex | A | B | C | D | E | F | G | H |
|---|---|---|---|---|---|---|---|---|
| Dist | 0 | 1 | 1 | 1 | 2 | 2 | 2 | 3 |
| Parent | -- | A | A | A | B | C | D | E |
Tree edges vs. cross edges
During BFS, each vertex (except the source) is discovered from exactly one other vertex. The edge used for that discovery is called a tree edge.
All tree edges together form a spanning tree of the connected component -- the BFS tree.
The BFS tree encodes shortest paths. The path from any vertex back to the root in the BFS tree is the shortest path in the original graph.
| Edge Type | Definition | Example |
|---|---|---|
| Tree edge | Used to discover a new vertex | A-B, A-C, A-D, B-E, C-F, D-G, E-H |
| Cross edge | Connects vertices at same or adjacent levels, not used for discovery | D-G* already tree, C-A back, E-G cross |
In an undirected BFS, cross edges can only connect vertices whose levels differ by at most 1. There are no edges skipping levels.
Level = distance from source
In an unweighted graph (all edges have cost 1), BFS discovers every vertex at the minimum possible distance from the source.
v from vertex u, we set dist[v] = dist[u] + 1.u was dequeued first, dist[u] is already optimal.dist[v] is also optimal.BFS does NOT find shortest paths in weighted graphs. For weighted graphs, use Dijkstra's algorithm (non-negative weights) or Bellman-Ford (negative weights allowed).
dist[v] = dist[u] + 1 when discovering v from u
| After processing | A | B | C | D | E | F | G | H |
|---|---|---|---|---|---|---|---|---|
| Init | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| Dequeue A | 0 | 1 | 1 | 1 | ∞ | ∞ | ∞ | ∞ |
| Dequeue B | 0 | 1 | 1 | 1 | 2 | ∞ | ∞ | ∞ |
| Dequeue C | 0 | 1 | 1 | 1 | 2 | 2 | ∞ | ∞ |
| Dequeue D | 0 | 1 | 1 | 1 | 2 | 2 | 2 | ∞ |
| Dequeue E | 0 | 1 | 1 | 1 | 2 | 2 | 2 | 3 |
Distances are computed as vertices are discovered (enqueued), not when they are processed (dequeued). Each vertex's distance is set exactly once and never changes.
Follow the parent pointers back to the source
| Vertex | A | B | C | D | E | F | G | H |
|---|---|---|---|---|---|---|---|---|
| parent | -- | A | A | A | B | C | D | E |
The parent array is like leaving breadcrumbs. Each vertex remembers who led it there. To find your way back to the source, just follow the breadcrumbs!
Same algorithm, but only follow outgoing edges
In a directed graph, edge A-->B does NOT mean you can go from B to A. BFS only follows outgoing edges from the current vertex.
| Vertex | A | B | C | D | E | F | G |
|---|---|---|---|---|---|---|---|
| Dist | 0 | 1 | 1 | 2 | 2 | 3 | 3 |
| Parent | -- | A | A | B | B | D | D |
Finding all pieces of a disconnected graph
Each BFS call explores one entire connected component. We count how many times we need to start a new BFS.
A single BFS from one vertex explores only its connected component. To cover the entire graph, loop through all vertices and start a new BFS whenever you find an unvisited vertex.
Think of each component as an island. BFS explores one island completely. You need a new "boat trip" (new BFS) to reach each separate island.
Every vertex and every edge examined exactly once
Each vertex is dequeued once (not E times). Each edge is checked once from each endpoint (undirected) or once total (directed). The work is distributed across vertices, not repeated.
| Representation | BFS Time |
|---|---|
| Adjacency List | O(V + E) -- optimal |
| Adjacency Matrix | O(V2) -- must scan each row |
Linear in the number of vertices
| Data Structure | Space | Purpose |
|---|---|---|
| Queue | O(V) | At most V vertices in queue |
| Visited array | O(V) | One boolean per vertex |
| Distance array | O(V) | One integer per vertex |
| Parent array | O(V) | One pointer per vertex |
| Total | O(V) |
In the worst case, the queue can hold O(V) vertices. Imagine a star graph where the center connects to all other V-1 vertices -- after processing the center, all V-1 neighbors are in the queue.
"Six degrees of separation"
Given a social network graph where vertices are people and edges are friendships, find the shortest friendship chain between two people.
The famous theory states that any two people on Earth are connected by at most 6 friendship links. BFS is how you'd actually verify this -- start from any person and measure BFS distances to everyone else.
BFS naturally computes the "degree of separation" between any two people. This is the foundation for features like LinkedIn's "2nd connection" and Facebook's "mutual friends."
BFS is everywhere
A web crawler uses BFS to systematically discover web pages:
BFS finds pages close to the root first, which are usually the most important. DFS might get lost in deep, low-value chains of links.
Imagine flooding the maze with water from the start. The water expands one cell at a time in all directions. The first time water reaches the exit is the shortest path. That is BFS.
Everything you need to know about BFS
| Adj List | Adj Matrix | |
|---|---|---|
| Time | O(V + E) | O(V2) |
| Space | O(V) | O(V) |
| Use BFS When... | Use DFS When... |
|---|---|
| Shortest path (unweighted) | Topological sort |
| Level-order traversal | Cycle detection |
| Nearest neighbor search | Path existence check |
| Connected components | Connected components |
| Web crawling (breadth) | Maze generation |