Nepedia
Back to Discover
Advanced Reader
🔢

Breadth-first search

math Maturity 11-13 Next article →

Breadth-first search, often called BFS, is a fundamental algorithm used to explore data structures. It is specifically designed to search through trees or graphs to find a node that meets a specific property. A tree or graph is a collection of points, called nodes or vertices, connected by lines. In a tree, there is a starting point known as the root. BFS is highly valued because it is a complete algorithm. This means if a solution exists, BFS is guaranteed to find it. This completeness is vital when dealing with implicit trees that may be infinite in size.

Animated BFS
Animated BFS

The mechanism of BFS relies on exploring a structure level by level. It begins at the root node and examines all nodes at the current depth before moving deeper. To manage this process, the algorithm requires extra memory, typically in the form of a queue. A queue is a data structure that follows a first-in, first-out principle. The algorithm starts by labeling the root as explored and adding it to the queue. While the queue is not empty, the algorithm removes the first node and checks if it is the goal. If it is not the goal, the algorithm looks at all adjacent edges to find neighboring nodes. Any neighbor that has not been explored is labeled, assigned a parent link, and added to the queue.

Tic-tac-toe-game-tree
Tic-tac-toe-game-tree

There are different ways to traverse these structures, which define different types of searches. BFS is distinct from depth-first search, or DFS. While BFS explores all neighbors at one level before moving down, DFS explores a single branch as far as possible before backtracking. Because of this, DFS can sometimes get lost in an infinite branch and never find a solution. To solve this, researchers use iterative deepening depth-first search. This method avoids getting lost but must explore the top parts of the tree many times over. Additionally, BFS and DFS differ in their memory needs. While BFS requires more memory to maintain its queue, DFS typically requires much less extra memory.

The history of BFS involves several key figures and rediscoveries. Konrad Zuse is credited with inventing BFS and its application to finding connected components in 1945. He described these ideas in his Ph.D. thesis regarding the Plankalkül programming language. However, his work was not published until 1972. The algorithm was later reinvented in 1959 by Edward F. Moore. Moore applied the method to the specific problem of finding the shortest path out of a maze. In 1961, C. Y. Lee further developed the concept into a wire routing algorithm.

BFS-Algorithm Search Way
BFS-Algorithm Search Way

Mathematical analysis allows us to understand the efficiency of BFS through complexity. The time complexity is expressed as O(V + E), where V is the number of vertices and E is the number of edges. This represents the worst-case scenario where every vertex and edge must be explored. The space complexity is O(V), as the algorithm must store vertices in its queue. When dealing with very large or infinite graphs, we use the branching factor, denoted as b. In these cases, the time and memory required to find a node at distance d are expressed in terms of b and d.

MapGermanyGraph
MapGermanyGraph

BFS has many practical applications in computer science and mathematics. In artificial intelligence, chess engines use BFS to build game trees from a current position. This helps the engine identify winning positions for a player by exploring possible moves. BFS is also used for finding the shortest path between two nodes in a graph, where the path length is measured by the number of edges. Other uses include Cheney's algorithm for copying garbage collection and the Ford-Fulkerson method for computing maximum flow in networks. It can even be used to test if a graph is bipartite.

GermanyBFS
GermanyBFS

Ultimately, BFS connects to broader fields like graph theory and state space search. It can be generalized to work on both undirected and directed graphs. In directed graphs, edges act like one-way streets, but BFS still functions effectively from a starting search key. The algorithm also produces a breadth-first tree, which organizes the graph based on the order of discovery. By using parent links, the algorithm allows users to backtrack from a destination node to the starting node. This tracing provides the exact shortest path between the two points.

MapGermanyGraph
MapGermanyGraph
723 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (5)
File:Animated BFS.gif
Animated BFS.gif
File:BFS-Algorithm Search Way.gif
BFS-Algorithm Search Way.gif
File:Tic-tac-toe-game-tree.svg
Tic-tac-toe-game-tree.svg
File:MapGermanyGraph.svg
MapGermanyGraph.svg
File:GermanyBFS.svg
GermanyBFS.svg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.