Breadth-First Search

What does BFS answer?

Starting from one vertex s: which vertices can I reach, and what is the fewest number of edges to each one?

The idea

Visit vertices in order of their distance from s:

All vertices at distance k are found before any vertex at distance k + 1, so the first time a vertex is found, the distance recorded for it is its shortest distance. A FIFO queue gives exactly this order: vertices leave the queue in the order they were added.

Bookkeeping - one entry per vertex u

Cost: O(V + E). Each vertex enters the queue at most once (only while it is white), and each adjacency list is scanned once, when its vertex leaves the queue.

Step through it

white: not found gray: in the queue black: done u (popped) v (neighbour checked) prev link (BFS tree) shortest path
Keys: ← / → step, space play/pause, Home reset, End finish. Click a vertex to make it the source; once finished, click a vertex to see its shortest path.
Adjacency list neighbours are checked in this order
bfs(g, s)
Queue q
front
back
color / d / prev

BFS vs DFS in one table

BFSDFS
Containerqueue - deque.popleft()stack - list.pop()
Orderby distance from the source: all of distance k before any of k + 1as deep as possible along one branch, then back up
prev links giveshortest paths (fewest edges)a spanning tree whose paths can be longer than necessary
CostO(V + E)O(V + E)
Typical usesshortest path in an unweighted graph, "within k hops" queries, level-by-level processingcycle detection, topological sort, strongly connected components, maze generation

BFS applications

Each problem below is the same bfs() on a different graph. Often the graph is never stored: the neighbours of a vertex are worked out when the vertex is popped. Then d starts empty and "v not in d" does the job of "white". Each step below is one pop: u leaves the queue and all its new neighbours join it.

Word ladder

Vertices are 4-letter words; two words share an edge when they differ in exactly one letter. BFS from the start word gives the fewest one-letter changes to reach the end word.

Knight moves

Vertices are the 64 squares; an edge joins two squares a knight can jump between in one move. BFS from the start square gives the fewest moves to every square.

Click a square to set the

Puzzle solving: wolf, goat and cabbage

A farmer must take a wolf, a goat and a cabbage across a river in a boat that holds the farmer and one more. Left alone, the wolf eats the goat and the goat eats the cabbage. Vertices are the safe states (which bank each one is on; the boat is always with the farmer); edges are single crossings. BFS from "all on the left" gives the fewest crossings. Try it by hand on the wolf-goat-cabbage page.

F farmer, W wolf, G goat, C cabbage; each box shows left bank | right bank

Count islands

Vertices are land cells; an edge joins two land cells that touch on a side. Each BFS run finds every cell of one island, so the number of islands is the number of runs needed to see all the land. Click a cell to switch it between land and water.

Nearest of many (multi-source BFS)

Vertices are the open cells of a map (dark cells are walls); edges join cells that touch on a side. Lettered cells are hospitals. Putting every hospital in the queue at d = 0 before the loop gives, in one pass, each cell's distance to its nearest hospital and which one it is. Click an open cell to add or remove a hospital.

Friends at distance 2

Vertices are people; an edge means two people are friends. BFS from one person, stopped at distance 2, gives their friends (d = 1) and friends of friends who are not yet friends (d = 2), the usual "people you may know" list. Click a person to start from them.

Further Reading

1945 - Konrad Zuse. BFS and its use for finding connected components appear in Zuse's rejected Ph.D. thesis on the Plankalkül programming language; it was not published until 1972.

1959 - Edward F. Moore. Moore reinvents BFS to find the shortest path out of a maze ("The shortest path through a maze").

1961 - C. Y. Lee. Lee develops it into a wire-routing algorithm for circuit boards, still known as the Lee algorithm.

DFS. A version of depth-first search was studied in the 19th century by Charles Pierre Trémaux as a method for solving mazes. In the 1970s John Hopcroft and Robert Tarjan built linear-time graph algorithms on DFS (biconnected components, planarity testing); their work on algorithms and data structures earned them the 1986 Turing Award.

See Breadth-first search and Depth-first search on Wikipedia, and CLRS (Introduction to Algorithms), chapter "Elementary Graph Algorithms", whose example graph is the first preset above.