# Breadth-first search

## Algorithm to search the nodes of a graph / From Wikipedia, the free encyclopedia

#### Dear Wikiwand AI, let's keep it short by simply answering these key questions:

Can you list the top facts and stats about Breadth-first search?

Summarize this article for a 10 year old

**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.

This article needs additional citations for verification. (April 2012) |

**Quick Facts**Class, Data structure ...

Class | Search algorithm |
---|---|

Data structure | Graph |

Worst-case performance | $O(|V|+|E|)=O(b^{d})$ |

Worst-case space complexity | $O(|V|)=O(b^{d})$ |

Optimal | yes (for unweighted graphs) |

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 win 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^{[1]} 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,^{[2]} 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.^{[3]}

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').^{[4]} 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.^{[5]} It was reinvented in 1959 by Edward F. Moore, who used it to find the shortest path out of a maze,^{[6]}^{[7]} and later developed by C. Y. Lee into a wire routing algorithm (published in 1961).^{[8]}