Lably
Skip to content

Breadth-first search

Algorithm to search the nodes of a graph

  • 6 min read
  • 4 chapters

Related on Lably

Through Computer science, Algorithm and Graph (discrete mathematics)

For example, in a chess endgame, a chess engine may build the game tree from the current position by applying all possible moves and use breadth-first search to find a winning position for White. Implicit trees (such as game trees or other problem-solving trees) may be of infinite size; breadth-first search is guaranteed to find a solution node if one exists.

In contrast, (plain) depth-first search (DFS), which explores the node branch as far as possible before backtracking and expanding other nodes, may get lost in an infinite branch and never make it to the solution node. Iterative deepening depth-first search avoids the latter drawback at the price of exploring the tree's top parts over and over again. On the other hand, both depth-first algorithms typically require far less extra memory than breadth-first search.

Breadth-first search can be generalized to both undirected graphs and directed graphs with a given start node (sometimes referred to as a 'search key'). In state space search in artificial intelligence, repeated searches of vertices are often allowed, while in theoretical analysis of algorithms based on breadth-first search, precautions are typically taken to prevent repetitions.

BFS and its application in finding connected components of graphs were invented in 1945 by Konrad Zuse, in his (rejected) Ph.D. thesis on the Plankalkül programming language, but this was not published until 1972. It was reinvented in 1959 by Edward F. Moore, who used it to find the shortest path out of a maze, and later developed by C. Y. Lee into a wire routing algorithm (published in 1961).

Pseudocode

The below pseudocode finds the shortest path from a given root vertex to all other vertices in the graph using BFS.

Input: A graph G and a starting vertex root of G

Output: Goal state. The parent links trace the shortest path back to root

1 <b id="mwXg">procedure</b> BFS(<i id="mwXw">G</i>, <i id="mwYA">root</i>) <b id="mwYQ">is</b> 2 let <i id="mwYg">Q</i> be a queue 3 label <i id="mwYw">root</i> as explored 4 <i id="mwZA">Q</i>.enqueue(<i id="mwZQ">root</i>) 5 <b id="mwZg">while</b> <i id="mwZw">Q</i> is not empty <b id="mwaA">do</b> 6 <i id="mwaQ">v</i> := <i id="mwag">Q</i>.dequeue() 7 <b id="mwaw">if</b> <i id="mwbA">v</i> is the goal <b id="mwbQ">then</b> 8 <b id="mwbg">return</b> <i id="mwbw">v</i> 9 <b id="mwcA">for all</b> edges from <i id="mwcQ">v</i> to <i id="mwcg">w</i> <b id="mwcw">in</b> <i id="mwdA">G</i>.adjacentEdges(<i id="mwdQ">v</i>) <b id="mwdg">do</b> 10 <b id="mwdw">if</b> <i id="mweA">w</i> is not labeled as explored <b id="mweQ">then</b> 11 label <i id="mweg">w</i> as explored 12 <i id="mwew">w</i>.parent := <i id="mwfA">v</i> 13 <i id="mwfQ">Q</i>.enqueue(<i id="mwfg">w</i>)

More details

This non-recursive implementation is similar to the non-recursive implementation of depth-first search, but differs from it in two ways:

  1. it uses a queue (First In First Out) instead of a stack (Last In First Out) and
  2. it checks whether a vertex has been explored before enqueueing the vertex rather than delaying this check until the vertex is dequeued from the queue.

If G is a tree, replacing the queue of this breadth-first search algorithm with a stack will yield a depth-first search algorithm. For general graphs, replacing the stack of the iterative depth-first search implementation with a queue would also produce a breadth-first search algorithm, although a somewhat nonstandard one.

The queue Q contains the frontier along which the algorithm is currently searching.

Nodes can be labelled as explored by storing them in a set, or by an attribute on each node, depending on the implementation.

The parent attribute of each node is useful for accessing the nodes in a shortest path, for example by backtracking from the destination node up to the starting node, once the BFS has been run, and the predecessors nodes have been set.

Breadth-first search produces a breadth-first tree. The diagrams on the right show the breadth-first tree obtained by running a BFS on an example graph of German cities (upper diagram) starting from Frankfurt.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Theoretical computer science