LOGBOOK

HELP

Quiz Entry - updated: 2026.11.27

Why is every vertex in level $L_i$ of a BFS exactly i edges away from the start vertex s?

The discovery edges always lead from one level to the next, so the tree path from s to a vertex in $L_i$ has exactly i edges. No path in the graph can be shorter, because an edge never skips a level: when v in $L_j$ is processed, every still unvisited neighbour of v goes into $L_{j+1}$, so every neighbour of v ends up in level j + 1 or an earlier one. A path from s therefore gains at most one level per edge.

Stated in full, BFS(G, s) has three properties, where $G_s$ is the connected component of s:

  1. It visits all vertices and edges of $G_s$.
  2. Its discovery edges form a spanning tree $T_s$ of $G_s$.
  3. For every vertex v in $L_i$, the path from s to v in $T_s$ has i edges, and every path from s to v in $G_s$ has at least i edges.

Property 3 says that $L_i$ is precisely the set of vertices at distance i from s, and that the BFS tree is a tree of shortest paths: for a path with the fewest edges from s to v, follow the discovery edges from v back up to s.

The distance here counts edges. When edges have lengths, such as road distances, the path with the fewest edges can be longer than one with more but shorter edges, and shortest paths by length need a different algorithm (Dijkstra's). Where every edge costs the same, such as hops between routers or links between people, BFS gives the true distance. A mathematician's Erdős number, for example, is their BFS level in the graph of joint papers, with Paul Erdős at level 0.

Go deeper:

From Quiz: ADS / Graph Traversals: Depth-First and Breadth-First Search | Updated: Nov 27, 2026