Breadth-first search (BFS) [00MK]
- September 21, 2026
Motivation for BFS [00ML]
- September 21, 2026
DFS can find a path between given vertices, if one exists; but it is not guaranteed to find the shortest path. For example, consider the graph consisting of four vertices connected into a cycle. The shortest path from any vertex to an adjacent vertex is clearly of length 1; but if DFS happens to search in the other direction first, it will find the path of length 3 instead.
So, if we want to find not just any path between vertices in a graph, but the shortest path, what can we do? This is a natural question in many contexts, where the shortest path corresponds to a cheapest/quickest/simplest solution.
BFS specification [00MM]
- September 21, 2026
The shortest path problem can be solved by the breadth-first search (BFS) algorithm. The basic idea behind BFS is to start at a vertex s and explore outward in all directions, adding vertices one “layer” at a time. That is, layer zero is just L_0 = \{s\}; layer 1 is all neighbors of s; layer 2 is all the neighbors of layer 1 vertices which were not already in layers 0 or 1; and so on.
Formally, we can define \begin {align*}L_0 &= \{s\} \\ L_{k+1} &= \left ( \bigcup _{v \in L_k} N(v) \right ) - \left ( \bigcup _{i \leq k} L_i \right )\end {align*} where N(v) denotes the set of all v's neighbors.
We claim that the layers defined in this way have the following properties. In particular, the first theorem below tells us that BFS (if we can implement it somehow) will solve the shortest path problem: to find the shortest path from s to t, just run BFS starting from s, and see what layer t shows up in. In fact, we don't need a particular target vertex in mind; just running BFS starting from s will tell us about the shortest path from s to every vertex. The second lemma is something we will use layer.
Theorem BFS layers correspond to shortest paths [00MN]
- September 21, 2026
For every k \geq 0 and every vertex v, the shortest path from s to v has length k if and only if v \in L_k.
Proof.
- September 21, 2026
Intuitively, this says that if v shows up in layer k, then that means the shortest path from s to v has length k, and vice versa. If v is in layer k, then there is definitely a path of length k from s to v, since we can get from each layer to the next via a single edge; and if there were a shorter path, v would have shown up in an earlier layer.
Formally, the proof is by induction on k.
- When k = 0, we must show that for any v, the shortest path from s to v has length 0 if and only if v \in L_0. This is true because s is the only vertex in L_0, and if a path from s to v has length 0 then v = s.
- Now let k \geq 1 and suppose as our induction hypothesis that for any j < k, the shortest path from s to any u has length j if and only if u \in L_{j}. Now consider some vertex v. We must show both directions of the if and only if.
Subproof. BFS layers correspond to shortest paths ›
- September 21, 2026
Suppose the shortest path from s to v has length k, and suppose the next-to-last vertex in the shortest path (that is, the vertex right before v) is u. The part of the path from s to u must also be a shortest path of length k-1 (if there were a shorter path from s to u, then we could just add on the edge from u to v to create a path from s to v of length < k), so by the induction hypothesis u \in L_{k-1}. Then since v is a neighbor of u, v \in L_k (v can't be in any earlier layers since then the induction hypothesis would imply there is a path to v shorter than k).Subproof. BFS layers correspond to shortest paths ›
- September 21, 2026
Now suppose v \in L_k. By definition, it must have a neighbor u \in L_{k-1}. By the induction hypothesis, the shortest path to u must have length k-1; adding back the edge between u and v yields a path to v of length k. This must be a shortest path, since if there were a shorter one, the induction hypothesis again tells us that v would have been in an earlier layer and hence not in L_k.
Lemma Neighbors are in adjacent layers [00MO]
- September 21, 2026
If u and v are neighbors, then the layers of u and v differ by at most one.
Proof.
- September 21, 2026
Algorithm BFS implementation [00MP]
- September 21, 2026
The parent and layer maps are optional, and can be used to find actual shortest paths and the shortest distance from s to each vertex, respectively.
BFS running time [00MQ]
- September 21, 2026
Since the implementation of BFS shown above has two nested loops, it must take \Theta (n^2) time, right? Not necessarily! (Also, we don't even have anything here named n.) However, by a similar argument as we used to analyze DFS, this algorithm examines each vertex at most once and each edge at most twice, performing various constant-time operations along the way (in particular we can implement a queue so that enqueue and dequeue both take constant time). Hence, the entire algorithm is O(V+E).
This is about the best we could possibly hope for—any interesting graph algorithm has to at least look at the entire graph, and simply reading every vertex and every edge takes \Theta (V+E) time.
Practical notes on implementing BFS [00MR]
- September 21, 2026
If you implement BFS in a real programming language (you should try it!), be sure you use a good data structure for your queue! For example, Python lists make an extremely poor list substitute: it is possible to append or remove items from the end of a list in constant time, but, say, removing an element from the beginning of a list takes linear time! Using a list instead of a real queue would therefore make BFS take O(V^2 + E) instead of O(V+E), which could make a huge difference. For example, processing a graph with one million vertices (as long as it does not have too many edges) would take a fraction of a second using an O(V+E) algorithm, but an O(V^2+E) algorithm could take on the order of (10^6)^2 / 10^8 = 10^4 seconds, or almost three hours. A graph with 10 million vertices would still take less than a second with an efficient BFS, but an O(V^2) algorithm could take more than a week.
- In Java, you can use the
ArrayDequeclass, which implements theQueueinterface. - In Python, you can use the
dequeclass from thecollectionslibrary. - In C++, you can use a
std::queue.