Practice Discrete Math

Algorithms / Graph Traversal

Least You Need to Know: BFS, DFS, and Connected Components

**Breadth-first search** explores layer by layer, while **depth-first search** follows one path deeply before backtracking. Both are core graph tools, and both can be used to discover reachability and connected components.

The least you need to know

Key notation

BFS breadth-first search
DFS depth-first search
component maximal connected piece of an undirected graph

Tiny worked example

  • Starting from a source vertex, BFS first visits all neighbors at distance 1.
  • Then it visits vertices at distance 2, then distance 3, and so on.
  • That is why BFS gives shortest paths in unweighted graphs.

Common mistakes

How to recognize this kind of problem

Start practice