Discrete Mathsfor Engineers

Structures, proofs, and intuition

EN | PT

Breadth-First Search (BFS)

Part 7 — Applications

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

7. Applications


Roadmap (what you will learn in this part):
  1. Shortest paths in unweighted graphs
  2. Pathfinding on grids (games, robotics)
  3. Connectivity and reachability
  4. Bipartite checking (two-coloring)
  5. Connected components (undirected graphs)
  6. Levels, broadcasting, and “degrees of separation”
  7. Engineering patterns: routing, breadcrumbs, dependency impact

BFS is one of the most practical graph algorithms in engineering. Whenever edges represent unit cost (one hop, one move, one step), BFS becomes the default tool.

In this part, we map the theory to real-world tasks you will see in systems, products, and interviews.

7.1 Shortest Paths (Unweighted Graphs)

If each edge has the same “cost” (1 hop), BFS computes shortest-path distances:

d[v] = δ(s, v)

And the predecessor pointers π allow path reconstruction (Part 6).

Use cases: social graphs, routing by hop-count, shortest “click path” on a site map.

7.2 Grid Pathfinding (Games & Robotics)

A 2D grid can be modeled as a graph:

  • Each cell is a vertex
  • Edges connect valid neighbor moves (4-neighborhood or 8-neighborhood)
  • Walls/obstacles remove vertices or edges

BFS finds the shortest number of moves from start to goal.

Classic problems: shortest path in a maze, minimum steps in a grid, flood-fill with distances.

7.3 Reachability & Connectivity

BFS answers “can I get there from here?”:

  • Run BFS from s
  • All discovered vertices are reachable
Use cases: access control graphs, dependency reachability, network connectivity checks.

7.4 Connected Components (Undirected Graphs)

To find all connected components:

  1. Mark all vertices as unvisited
  2. For each unvisited vertex, run BFS and mark its entire component
  3. Each BFS run discovers exactly one component

The total time remains linear:

O(|V| + |E|)
Use cases: clustering by connectivity, detecting isolated subnetworks, grouping users by connectivity.

7.5 Bipartite Checking (Two-Coloring)

BFS can test whether a graph is bipartite by two-coloring layers:

  • Assign color A to s
  • All neighbors must be color B
  • Neighbors of B must be A, and so on
  • If an edge connects same-color vertices, the graph is not bipartite
Use cases: conflict graphs, scheduling constraints, checking if a graph has an odd cycle.

7.6 Levels, Broadcast, and “Degrees of Separation”

BFS naturally computes layers:

Layer meaning: Lk = { v ∈ V ∣ d[v] = k }

This models spreading processes:

  • Broadcast message propagation (hops)
  • Minimum time steps in discrete diffusion
  • “Friends within 2 hops” queries
Engineering insight: Layer computations are often more valuable than the path itself.

7.7 Engineering Patterns

BFS is a pattern, not just an algorithm:

  • Routing by hop-count: shortest number of hops in overlay networks
  • Breadcrumbs / parent pointers: store parents to rebuild paths or UI navigation
  • Impact analysis: “what depends on this?” (dependency graphs)
  • State-space search: explore transitions in minimal step count
Takeaway: When your system can be modeled as “states” and “unit transitions”, BFS is usually the first tool to try.

Next: Part 8 — BFS vs DFS

We have seen what BFS does and where it shines. Next, we put it side by side with Depth-First Search (DFS): how the exploration order differs, what structures each one produces, and how to choose the right traversal for engineering problems.