Breadth-first search

Imagine navigating a vast, intricate network—whether it's connections on social media, routes through a city, or states in a complex game. How do you find what you're looking for efficiently? Breadth-First Search, or BFS, offers a powerful and intuitive strategy, systematically exploring every possibility at one 'level' before diving deeper. BFS explores networks layer by layer, guaranteeing the discovery of the shortest path to a target if one exists. Unlike its 'deep-diving' counterpart, Depth-First Search, BFS uses more memory but is 'complete' for infinite graphs. From finding the quickest exit in a maze to powering modern network routing, BFS underpins many critical computational tasks.

Source: Wikipedia

AI Summary

Imagine navigating a vast, intricate network—whether it's connections on social media, routes through a city, or states in a complex game. How do you find what you're looking for efficiently? Breadth-First Search, or BFS, offers a powerful and intuitive strategy, systematically exploring every possibility at one 'level' before diving deeper.

The Level-by-Level Explorer

Breadth-First Search, often abbreviated to BFS, is an algorithm designed to explore a network, much like ripples spreading on a pond. It starts at a designated point, exploring all its immediate neighbors first. Only once every node at that 'depth' has been visited does it move on to the next layer of unvisited neighbors.

This systematic, layer-by-layer exploration is what gives BFS its name. Think of it as methodically checking every house on a street before moving to the next street. This approach guarantees that if there's a target you're looking for, you'll find the shortest path to it first, in terms of the number of steps or connections.

The Queue: BFS's Memory

To keep track of which nodes to visit next, BFS relies on a data structure called a 'queue.' Like people waiting in line, new nodes discovered are added to the back of the queue, and nodes are processed from the front. This 'first-in, first-out' principle ensures that all nodes at a given depth are explored before moving to the next.

BFS vs. DFS: A Tale of Two Strategies

BFS has a counterpart known as Depth-First Search (DFS). While BFS explores broadly, DFS dives deep—it follows a single path as far as it can go before backtracking. Imagine exploring a maze: BFS tries every door on your floor before going up or down, while DFS picks a door and follows that hallway until it hits a dead end, then returns.

This fundamental difference has significant implications. BFS is guaranteed to find the shortest path in an unweighted graph, and it will always find a solution if one exists, even in infinitely large theoretical networks like game trees. DFS, however, might get lost endlessly down a single 'infinite' path, never finding a solution just a few steps away on another branch.

The trade-off? BFS often requires more memory than DFS. It needs to store all the nodes at the current 'level' in its queue, which can grow quite large. DFS, by contrast, only needs to remember the current path it's on, making it more memory-efficient in some scenarios.

Beyond Trees: Graphs and Mazes

While often introduced with tree structures, BFS is incredibly versatile and can be applied to any graph—whether it's a simple network of roads, a complex social graph, or even the intricate wiring of a computer chip. Its ability to find the shortest path (in terms of edges) makes it indispensable.

For instance, a GPS system might use a variation of BFS to find the route with the fewest turns. In Artificial Intelligence, a chess engine might use BFS to explore all possible moves to a certain depth, identifying a winning strategy for a particular endgame scenario. It ensures a solution is found without getting stuck in unproductive branches.

A History of Reinvention

The concept behind Breadth-First Search has a fascinating history of independent discovery. It was first conceived in 1945 by German computer scientist Konrad Zuse in his (initially rejected) Ph.D. thesis on the Plankalkül programming language, though it wasn't published until much later.

Then, in 1959, it was reinvented by Edward F. Moore, who brilliantly used it to find the shortest path out of a maze. Just two years later, C. Y. Lee further developed it into a practical algorithm for wire routing in electronic circuits. It's a testament to its fundamental utility that such a powerful idea emerged multiple times.

Tracing the Path Back

After BFS has explored a graph, how do we reconstruct the shortest path it found? The trick is to keep track of each node's 'parent'—the node from which it was discovered. By starting at the destination and backtracking through these parent pointers, you can easily retrace the exact shortest path to your starting point.

To prevent endlessly re-visiting nodes in graphs with cycles (like a city where you can go in circles), BFS also needs a way to mark nodes as 'explored.' This is usually done by storing them in a set or adding a simple 'visited' attribute to each node, ensuring every node is processed only once.

Efficiency: Time and Space

The efficiency of BFS is measured by how much time and memory it consumes. In terms of time, BFS needs to examine every vertex (node) and every edge (connection) in the worst case to find its target. This makes its time complexity quite predictable.

This equation, O(|V| + |E|), expresses the time complexity, where |V| represents the number of vertices and |E| represents the number of edges. It means the time taken grows proportionally with the size of the graph itself.

O(|V| + |E|)

For space complexity—how much memory it uses—BFS primarily needs to store the nodes in its queue. In the worst case, this queue might hold all the nodes at the widest 'level' of the graph. Therefore, its space complexity is often proportional to the number of vertices.

The space complexity, O(|V|), indicates that the memory required grows linearly with the number of vertices. This is why BFS can be memory-intensive for very large or 'dense' graphs, as it must keep many nodes in active memory.

O(|V|)

The Promise of Completeness

In the world of Artificial Intelligence, a search algorithm is considered 'complete' if it guarantees finding a solution when one exists. BFS is a complete algorithm, making it a reliable choice for problem-solving in unknown or very large search spaces. It will never miss a solution if it's reachable.

This completeness is particularly critical when dealing with implicitly defined graphs that could theoretically be infinite. While DFS might wander off into an endless branch, BFS's methodical, level-by-level approach ensures it will eventually uncover any goal state that exists within the reachable parts of the graph.

BFS Ordering

The sequence in which BFS visits nodes creates what's known as a 'BFS ordering' of the graph's vertices. If you were to list the nodes as they are removed from the queue and processed, that list would be a BFS ordering for the given starting point. This ordering is unique for a specific starting node and graph structure.

Article

Breadth-first search

BFS on Maze-solving algorithm

Top part of Tic-tac-toe game tree

In computer science, breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies a given property. It starts at the tree root and explores all nodes at the present depth prior to moving on to the nodes at the next depth level. Extra memory, usually a queue, is needed to keep track of the child nodes that were encountered but not yet explored.

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

Breadth-first search

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

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

More details

An example map of Southern Germany with some connections between cities

The breadth-first tree obtained when running BFS on the given map and starting in Frankfurt

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

• it uses a queue (First In First Out) instead of a stack (Last In First Out) and • 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.

Analysis

Time and space complexity

The time complexity can be expressed as ${\displaystyle O(|V|+|E|)}$, as every vertex and every edge will be explored in the worst case. ${\displaystyle |V|}$ is the number of vertices and ${\displaystyle |E|}$ is the number of edges in the graph. Note that ${\displaystyle O(|E|)}$ may vary between ${\displaystyle O(1)}$ and ${\displaystyle O(|V|^{2})}$, depending on how sparse the input graph is.

When the number of vertices in the graph is known ahead of time, and additional data structures are used to determine which vertices have already been added to the queue, the space complexity can be expressed as ${\displaystyle O(|V|)}$, where ${\displaystyle |V|}$ is the number of vertices. This is in addition to the space required for the graph itself, which may vary depending on the graph representation used by an implementation of the algorithm.

When working with graphs that are too large to store explicitly (or infinite), it is more practical to describe the complexity of breadth-first search in different terms: to find the nodes that are at distance d from the start node (measured in number of edge traversals), BFS takes O(bd + 1) time and memory, where b is the "branching factor" of the graph (the average out-degree).

Completeness

In the analysis of algorithms, the input to breadth-first search is assumed to be a finite graph, represented as an adjacency list, adjacency matrix, or similar representation. However, in the application of graph traversal methods in artificial intelligence the input may be an implicit representation of an infinite graph. In this context, a search method is described as being complete if it is guaranteed to find a goal state if one exists. Breadth-first search is complete, but depth-first search is not. When applied to infinite graphs represented implicitly, breadth-first search will eventually find the goal state, but depth first search may get lost in parts of the graph that have no goal state and never return.

BFS ordering

Breadth-first search

An enumeration of the vertices of a graph is said to be a BFS ordering if it is a possible output of the application of BFS to this graph.

Let ${\displaystyle G=(V,E)}$ be a graph with ${\displaystyle n}$ vertices. Recall that ${\displaystyle N(v)}$ is the set of neighbors of ${\displaystyle v}$. Let ${\displaystyle \sigma =(v{1},\dots ,v{m})}$ be a list of distinct elements of ${\displaystyle V}$, for ${\displaystyle v\in V\setminus \{v{1},\dots ,v{m}\}}$, let ${\displaystyle \nu {\sigma }(v)}$ be the least ${\displaystyle i}$ such that ${\displaystyle v{i}}$ is a neighbor of ${\displaystyle v}$, if such a ${\displaystyle i}$ exists, and be ${\displaystyle \infty }$ otherwise.

Let ${\displaystyle \sigma =(v{1},\dots ,v{n})}$ be an enumeration of the vertices of ${\displaystyle V}$. The enumeration ${\displaystyle \sigma }$ is said to be a BFS ordering (with source ${\displaystyle v{1}}$) if, for all ${\displaystyle 1<i\leq n}$, ${\displaystyle v{i}}$ is the vertex ${\displaystyle w\in V\setminus \{v{1},\dots ,v{i-1}\}}$ such that ${\displaystyle \nu {(v{1},\dots ,v{i-1})}(w)}$ is minimal. Equivalently, ${\displaystyle \sigma }$ is a BFS ordering if, for all ${\displaystyle 1\leq i<j<k\leq n}$ with ${\displaystyle v{i}\in N(v{k})\setminus N(v{j})}$, there exists a neighbor ${\displaystyle v{m}}$ of ${\displaystyle v{j}}$ such that ${\displaystyle m<i}$.

Applications

Breadth-first search

Breadth-first search can be used to solve many problems in graph theory, for example:

• Copying garbage collection, Cheney's algorithm • Finding the shortest path between two nodes u and v, with path length measured by number of edges (an advantage over depth-first search) • (Reverse) Cuthill–McKee mesh numbering • Ford–Fulkerson method for computing the maximum flow in a flow network • Serialization/Deserialization of a binary tree vs serialization in sorted order, allows the tree to be re-constructed in an efficient manner. • Construction of the failure function of the Aho-Corasick pattern matcher. • Testing bipartiteness of a graph. • Implementing parallel algorithms for computing a graph's transitive closure.