Skip to content
breadth-first search

File:Animated_BFS.gif · Wikimedia Commons · See Wikimedia Commons

EntityQ325904· pop 41· linked from 174 articles

breadth-first search

Sign in to save

Also known as BFS, breadth first search

algorithm for searching the nodes of a graph in order by their hop count from a starting node

Key facts

Class
Search algorithm
Data structure
Graph
Worst case performance
O ( | V | + | E | ) {\displaystyle O(|V|+|E|)}
Worst case space complexity
O ( | V | ) {\displaystyle O(|V|)}
Optimal
Yes (always finds shortest paths)

via Wikipedia infobox

~9 min read

Encyclopedic overview

BFS on Maze-solving algorithm Top part of Tic-tac-toe game tree 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.

Excerpted from Wikipedia’s “breadth-first search” article, available under the CC BY-SA 4.0 licence.

Gallery (5)