graph algorithms
Graph Traversal (BFS/DFS)
You should know: graph basics
Overview
Graph traversal is the process of systematically visiting every vertex in a graph. The two fundamental strategies are breadth-first search (BFS), which explores all neighbors of a vertex before moving further out, and depth-first search (DFS), which plunges as deep as possible along one branch before backtracking. Tree traversal is a special case of graph traversal. These algorithms are the foundation on which shortest-path, connectivity, and cycle-detection algorithms are built.
Intuition
BFS explores like ripples spreading outward from a stone dropped in a pond — it visits everything at distance 1, then everything at distance 2, and so on, using a queue (first-in-first-out) to track what to visit next. DFS explores like a person in a maze who always takes an unexplored corridor and only backtracks when stuck — using a stack (or recursion) to remember the path back.
Interactive Graph
Formal Definition
Both algorithms take a graph G = (V, E) and a start vertex s and produce an order in which vertices are discovered, using a 'visited' set to avoid repeats:
Queue-based, level-by-level exploration
Stack-based (or recursive) exploration, depth-first
Notation
| Notation | Meaning |
|---|---|
| Breadth-first search | |
| Depth-first search | |
| BFS distance — number of edges on the shortest path from the source to v |
Properties
BFS finds shortest paths (unweighted)
Condition: Only holds for unweighted (or equal-weight) graphs
Time complexity
Example: Both BFS and DFS visit each vertex once and examine each edge once (with an adjacency list)
DFS produces a DFS tree / forest
Applications
Worked Examples
Visit A, enqueue its neighbors B and C (alphabetical order).
Dequeue B, visit it, enqueue its unvisited neighbor D.
Dequeue C, visit it; D is already enqueued so nothing new is added.
Dequeue D, visit it; all neighbors already visited.
Answer: A, B, C, D
Practice Problems
A graph has 6 vertices and 9 edges, represented as an adjacency list. What is the time complexity of running BFS on it?
Which data structure does BFS use to decide which vertex to explore next?
You must find the FEWEST-step path out of a grid maze (each move costs the same). Should you use BFS or DFS, and why?
A build system needs to detect a circular dependency among tasks. Which traversal detects a cycle in a directed graph, and what signals the cycle?
Common Mistakes
Believing DFS also finds shortest paths in unweighted graphs.
Only BFS guarantees shortest paths (in edge count) in unweighted graphs, because it explores level-by-level. DFS may reach a vertex via a long, winding path first.
Forgetting to mark vertices as visited before or as soon as they are enqueued in BFS, leading to duplicate enqueues.
Mark a vertex visited at the moment it is enqueued (not when dequeued) — otherwise the same vertex can be added to the queue multiple times via different neighbors.
Quiz
Summary
- Graph traversal visits every vertex in a graph; BFS and DFS are the two fundamental strategies.
- BFS uses a queue and explores level-by-level; it finds shortest paths in unweighted graphs.
- DFS uses a stack (or recursion) and explores as deep as possible before backtracking.
- Both run in O(|V| + |E|) time with an adjacency-list representation.
- DFS underlies topological sort, cycle detection, and connected-component algorithms.
References
- WebsiteWikipedia — Graph traversal
Mathematics