Discrete Mathsfor Engineers

Structures, proofs, and intuition

EN | PT

Breadth-First Search (BFS)

Part 5 — Complexity

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

5. Complexity


Roadmap (what we analyze in this part):
  1. Time complexity with adjacency lists
  2. Why each vertex is processed once
  3. Why each edge is examined at most twice
  4. Comparison with adjacency matrix
  5. Space complexity
  6. Structural insight

Now that correctness has been established, we analyze the algorithm from a computational perspective.

5.1 Time Complexity (Adjacency List)

O(|V| + |E|)

This bound is optimal for graph traversal.

5.2 Vertex Processing

  • Discovered once
  • Enqueued once
  • Dequeued once
  • Colored BLACK once
O(|V|)

5.3 Edge Examination

Each edge is examined at most twice (undirected case).

O(|E|)

5.4 Adjacency Matrix Case

O(|V|^2)
For sparse graphs, adjacency lists are asymptotically superior.

5.5 Space Complexity

O(|V|)

5.6 Structural Insight

  • No revisits
  • No redundant exploration
  • Strict layer order enforced by FIFO

Next: Part 6 — BFS Tree & Path Reconstruction

With correctness and complexity understood, we now focus on what BFS actually produces: the predecessor pointers form a BFS tree, and that tree lets us reconstruct shortest paths from the source to any reachable vertex.