Math 2.0

Combinatorics Definition

Breadth-first search

Builds on AlgorithmGraphDirected graphDistance in a graph

Definition

Let G=(V,E)G = (V, E) be a finite graph or directed graph, given by adjacency lists, and let s∈Vs \in V. Breadth-first search from ss is the algorithm that computes d ⁣:V→ℕ∪{∞}d \colon V \to \N \cup \set{\infty} and a parent map π\pi as follows:

  1. set d(s)=0d(s) = 0 and d(v)=∞d(v) = \infty for v≠sv \ne s, and let QQ be a first-in first-out queue containing only ss;
  2. while QQ is nonempty, remove the first vertex uu of QQ and scan the neighbours (out-neighbours) of uu;
  3. for each scanned vv with d(v)=∞d(v) = \infty, set d(v)=d(u)+1d(v) = d(u) + 1, set π(v)=u\pi(v) = u and append vv to QQ; then go back to step 2.

Remarks

Each vertex enters QQ at most once and each adjacency list is scanned once, so the running time is O(∣V∣+∣E∣)O(\abs{V} + \abs{E}). At the end, d(v)d(v) is the distance from ss to vv, the least number of edges of a path from ss to vv. In particular d(v)=∞d(v) = \infty exactly when vv is not reachable from ss. The parent pointers form a tree of shortest paths rooted at ss. The key fact is that the vertices leave QQ in nondecreasing order of dd.

Example: on the cycle with vertices 1,2,3,4,51, 2, 3, 4, 5 and edges {i,i+1}\set{i, i+1} and {5,1}\set{5, 1}, BFS from 11 gives d=(0,1,2,2,1)d = (0, 1, 2, 2, 1).

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 55 and then 44, so vertex 33 is first reached from 44 and gets the value 33 instead of 22.

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
Each page uses the pages above it. The colour of the bar on a box shows how strong the evidence is, and the box also gives it as text. The list has the same pages.
  • no evidence level
  1. Algorithm concept
  2. Directed graph concept
  3. Distance in a graph concept
  4. Graph concept
  5. Breadth-first search definition

Further reading

  1. Web https://en.wikipedia.org/wiki/Breadth-first_search
About this page
TagDANEW2WH5M
URIm2://DANEW2WH5M
Kinddefinition
WritingW0 stub
MSC05C85, 68R10
DefinesBreadth-first search
UsesAlgorithm, Graph, Directed graph, Distance in a graph
Changed3 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.