BFS & DFS
IntermediateAlgorithms
The two fundamental graph traversal strategies. BFS explores level by level (shortest path in unweighted graphs). DFS dives deep first (connectivity, cycle detection, topological sort).
AVG TIME
O(V + E)
SPACE
O(V)
BEST
O(V + E)
WORST
O(V + E)
Step-by-Step Walkthrough
In Python
Math You Need For This
BFS frontier: imagine all nodes currently "in the queue" lit up at once. For a wide graph, this can be a large layer. DFS stack: only one path from root to current node is active at once - much narrower.
Required concepts
Key math ideas
1 / 2
Interactive 3D Visualization
Brute Force vs Optimized
Watch It Run
Python Implementation
Now try it yourself
1 challenge with test cases and AI feedback
Practice Now
Complexity Analysis
BFS & DFS
Next: Greedy Algorithms