Depth-First Search
DFS explores a graph by going as deep as possible along each branch before backtracking. Uses a stack data structure.
IntermediateTime: O(V + E)Space: O(V)
Take QuizStep 1 / 0
Current Step
Click Play to start the visualization
## How Depth-First Search Works
DFS explores as far as possible along each branch before backtracking. It uses a stack (or recursion) to track nodes.
### Algorithm Steps:
1. Start at the source node
2. Mark current node as visited
3. Recursively visit each unvisited neighbor
4. Backtrack when no unvisited neighbors remain
5. Repeat until all reachable nodes are visited
### Key Characteristics:
- **Uses Stack**: Last-In-First-Out (LIFO) or recursion
- **Path Finding**: Good for finding any path
- **Memory Efficient**: Uses less memory than BFS for wide graphs
Time & Space Complexity
| Case | Time Complexity |
|---|---|
| Best | O(V + E) |
| Average | O(V + E) |
| Worst | O(V + E) |
| Space | O(V) |
Practice Problems
10 problems81 total points
1
Path Sum
Beginner5 pts
2
Symmetric Tree
Beginner5 pts
3
Invert Binary Tree
Beginner5 pts
4
Number of Provinces
Intermediate8 pts
5
Clone Graph
Intermediate8 pts
6
Course Schedule II
Advanced10 pts
7
Longest Increasing Path in Matrix
Advanced10 pts
8
Word Search II
Advanced10 pts
9
Critical Connections in a Network
Advanced10 pts
10
Alien Dictionary
Advanced10 pts