Graph Traversal Algorithms
Graph traversal algorithms systematically visit all vertices in a graph. Unlike trees, graphs don’t have a root node, so you must specify a starting vertex. The two fundamental traversal algorithms are Breadth-First Search (BFS) and Depth-First Search (DFS).Try It Out
Visualize both traversal algorithms in action:Graph Traversals Visualizer
Watch BFS and DFS algorithms traverse through graphs step-by-step
Why Track Visited Vertices?
Breadth-First Search (BFS)
BFS explores all vertices at the current level before moving to the next level. Think of it as exploring outward in waves from the starting vertex.
How BFS Works
Complete BFS Implementation
BFS Example
Consider this directed graph:- Order 1
- Order 2
Traversal Order: [1, 2, 3, 4]
- Visit 1 (starting vertex)
- Visit 1’s neighbors: 2, 3
- Visit 2’s neighbors: 4
- 3’s neighbors already visited
BFS Complexity
Time Complexity
Time Complexity
O(V²) with LinkedList, O(V) with HashSet
- Visit each vertex once: O(V)
- Check each edge once: O(E)
- Visited check with LinkedList: O(V) per check
- Total with LinkedList: O(V²)
- Total with HashSet: O(V + E) = O(V)
Space Complexity
Space Complexity
O(V)
- Queue can hold up to O(V) vertices
- Visited list stores O(V) vertices
- Total space: O(V)
Depth-First Search (DFS)
DFS explores as far as possible along each branch before backtracking. Think of it as diving deep into one path before exploring alternatives.
How DFS Works
Complete DFS Implementation
DFS can also be implemented recursively, but the stack-based iterative approach is shown here as it’s easier to understand and visualize.
DFS Example
Using the same directed graph:- Path 1
- Path 2
Traversal Order: [1, 2, 4, 3]
- Visit 1 (starting vertex)
- Push 2 and 3, pop 2 (go deep)
- Push 4 from 2
- Pop 4, then pop 3
DFS Complexity
Time Complexity
Time Complexity
O(V²) with LinkedList, O(V) with HashSet
- Visit each vertex once: O(V)
- Check each edge once: O(E)
- Visited check with LinkedList: O(V) per check
- Total with LinkedList: O(V²)
- Total with HashSet: O(V + E) = O(V)
Space Complexity
Space Complexity
O(V)
- Stack can hold up to O(V) vertices in worst case
- Visited list stores O(V) vertices
- Total space: O(V)
BFS vs DFS Comparison
- Data Structure
- Use Cases
- Performance
| Algorithm | Data Structure | Behavior |
|---|---|---|
| BFS | Queue (FIFO) | Explores level by level |
| DFS | Stack (LIFO) | Explores depth first |
Critical Implementation Details
Common Applications
BFS Applications
- Shortest path in unweighted graphs
- Social network friend recommendations (within N degrees)
- Web crawlers (level by level)
- Network broadcasting
DFS Applications
- Cycle detection
- Topological sorting
- Solving puzzles with one solution
- Finding strongly connected components
Interactive Learning
The visualizer allows you to:Next Steps
Graph Basics
Review graph structure and operations
Try the Visualizer
Practice BFS and DFS with the interactive tool
Key Takeaways
- BFS uses a queue and explores level by level (breadth-wise)
- DFS uses a stack and explores depth-first along each branch
- Always track visited vertices to prevent infinite loops in cyclic graphs
- Use HashSet for O(1) visited checks instead of LinkedList’s O(V)
- Both algorithms have O(V) time and space complexity when optimized
- Choose BFS for shortest paths, DFS for cycle detection and topological sorting