Breadth-first search
Definition
Let be a finite graph or directed graph, given by adjacency lists, and let . Breadth-first search from is the algorithm that computes and a parent map as follows:
- set and for , and let be a first-in first-out queue containing only ;
- while is nonempty, remove the first vertex of and scan the neighbours (out-neighbours) of ;
- for each scanned with , set , set and append to ; then go back to step 2.
Remarks
Each vertex enters at most once and each adjacency list is scanned once, so the running time is . At the end, is the distance from to , the least number of edges of a path from to . In particular exactly when is not reachable from . The parent pointers form a tree of shortest paths rooted at . The key fact is that the vertices leave in nondecreasing order of .
Example: on the cycle with vertices and edges and , BFS from gives .
Non-example: replacing the queue by a stack, so that the last vertex added is removed first, does not compute distances in general. On the same cycle, scan neighbours in increasing order. The stack pops and then , so vertex is first reached from and gets the value instead of .
Reading path
To understand this page from the ground up, read these 5 pages in order. The foundations come first.
Show the path as a graph
- no evidence level
- Algorithm concept
- Directed graph concept
- Distance in a graph concept
- Graph concept
- Breadth-first search definition
Further reading
About this page
| Tag | DANEW2WH5M |
|---|---|
| URI | m2://DANEW2WH5M |
| Kind | definition |
| Writing | W0 stub |
| MSC | 05C85, 68R10 |
| Defines | Breadth-first search |
| Uses | Algorithm, Graph, Directed graph, Distance in a graph |
| Changed | 3 November 2026 |
Markdown · JSON · Edit on GitHub
What this page needs for W1
- an inventory row: the row of the inventory that the node accounts for (0 of 1)
- a source with a locator (0 of 1)
The writing levels say how complete the text of a page is. They do not measure its truth.
What links here
No page links to this page yet.