Discrete Mathsfor Engineers

Structures, proofs, and intuition

EN | PT

Breadth-First Search (BFS)

Part 6 — BFS Tree & Path Reconstruction

Series: Breadth-First Search (BFS) Part 6 of 8

6. BFS Tree & Path Reconstruction


Roadmap (what you will learn in this part):
  1. Understand what BFS outputs (π and d arrays)
  2. Define the predecessor subgraph and why it is a tree
  3. Formalize “path reconstruction” from π pointers
  4. Implement a reconstruction procedure (CLRS-style)
  5. See a small worked example
  6. Extract engineering patterns (parents, routing, breadcrumbs)

BFS does more than compute distances. It produces a predecessor relation π that encodes a BFS tree rooted at the source.

In this part, we make that output explicit and show how to reconstruct a shortest path from s to any reachable vertex v.

6.1 What BFS Produces

After running BFS from a source s, each vertex v has:

  • d[v] — shortest-path distance from s to v (in edges)
  • π[v] — predecessor of v on a shortest path (or NIL)
Key rule: When π[v] = u, BFS enforced d[v] = d[u] + 1 at discovery time.

6.2 The Predecessor Subgraph

Define the predecessor edge set:

Eπ = { (π[v], v) ∣ v ∈ V, v ≠ s, π[v] ≠ NIL }

The graph T = (V, Eπ) is called the BFS predecessor subgraph.

If we restrict to vertices reachable from s, this subgraph forms a tree rooted at s.

6.3 Why This Is a Tree

Two facts give the tree structure:

  1. Unique parent: Every reachable vertex v ≠ s gets exactly one predecessor π[v].
  2. Acyclic: Distances strictly decrease when following predecessors: d[π[v]] = d[v] − 1. So you can never come back to a vertex with the same distance.
Consequence: Following π pointers from any reachable v must eventually reach s, and the path is unique.

6.4 Small Formal Diagram

The predecessor pointers connect consecutive layers:

Layer + tree view (ASCII):
L0:   s
      |
L1:  a   b
     |   |
L2:  c   d   e
         |
L3:      f

Edges are (π[v], v).
Each edge goes from layer k to layer k+1.

This is why the predecessor subgraph is both a tree and a shortest-path structure.

6.5 Path Reconstruction (Definition)

A reconstructed path from s to a reachable vertex v is:

s = v0, v1, …, vk = v   where   vi−1 = π[vi]

The length is k = d[v].

Important: This reconstruction is valid only if v is reachable from s (i.e., BFS discovered it).

6.6 CLRS-Style Procedure — PRINT-PATH

CLRS presents reconstruction as a recursive procedure on predecessors:

PRINT-PATH(π, s, v):

    if v == s:
        print s
    else if π[v] == NIL:
        print "no path"
    else:
        PRINT-PATH(π, s, π[v])
        print v
What it does: It follows π pointers backward to the source, then prints forward on unwind.

6.7 Iterative Reconstruction (Engineering-Friendly)

In practice, you often build the path in reverse, then reverse it:

RECONSTRUCT-PATH(π, s, v):

    path = []
    cur = v

    while cur != NIL:
        path.append(cur)
        if cur == s:
            break
        cur = π[cur]

    if path.last != s:
        return []   // no path

    reverse(path)
    return path
Tip: The loop is bounded by d[v] + 1, so reconstruction is linear in path length.

6.8 Worked Example

Suppose BFS from s produced:

v d[v] π[v]
s0NIL
a1s
b1s
d2b
f3d

Reconstructing the path to f follows predecessors:

f → d → b → s

Reversing gives the shortest path:

s → b → d → f

6.9 Engineering Patterns

The BFS tree and predecessor pointers show up everywhere:

  • Routing / shortest hops: reconstruct the hop-by-hop path in networks
  • Breadcrumbs: parent pointers from a node up to a root (UI navigation)
  • Org charts / hierarchies: store parent pointers to rebuild chains
  • Dependency exploration: discover layers of impact from a starting component
Takeaway: π is not just “extra metadata” — it is the concrete structure that turns BFS into a usable output.

Next: Part 7 — Applications

Now that we understand how BFS builds a tree and reconstructs shortest paths, we explore where this structure is actually used in practice. In Part 7, we examine real-world applications of BFS across networks, grids, dependency graphs, and engineering systems.