What's the difference between backtracking and depth first search?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Backtracking and Depth First Search (DFS) are two fundamental concepts in computer science and algorithm design, often used in solving computational problems involving decision making and pathfinding. While they may share some similarities, they are distinct techniques with specific use cases and characteristics. This article explores the differences between backtracking and depth first search, providing technical insights, examples, and comparisons.
Understanding Depth First Search (DFS)
Depth First Search is a graph traversal algorithm used primarily in searching and pathfinding problems. DFS explores a graph by traveling as far down one branch as possible before backtracking and exploring other branches. The approach essentially follows the "depth" of the data structure before backtracking.
How DFS Works
- Initialization: Start with a stack, marking the first node as visited.
- Traversal:
- Explore each adjacent, unvisited node.
- Push visited nodes onto the stack.
- Move as deep along the branch until no unvisited nodes remain.
- Backtrack:
- When a node has no unvisited neighbors, backtrack to the last visited node through the stack.
- Repeat the process until all nodes are visited.
DFS Example
Consider a graph like the below:
Starting from node A:
- DFS visits A -> B -> D, backtracks to B.
- From B, it then visits E, backtracks to B, then A.
- Then A's unexplored C is visited, followed by F.
DFS is commonly used for scenarios like:
- Cycle detection in graphs
- Topological sorting
- Finding connected components in a graph
Understanding Backtracking
Backtracking is an algorithmic paradigm mainly used for solving constraint satisfaction problems, finding solutions to a problem incrementally, and abandoning them if they fail to satisfy constraints. The technique involves depth-first search to exhaustively enumerate candidate solutions.
How Backtracking Works
- Initialization: Prepare an initial empty solution.
- Incrementally Build Solutions:
- Make a decision and move forward.
- If constraints are violated, backtrack to previous decisions.
- Check Constraints: Continuously check whether the current path satisfies the problem constraints.
- Solution Finding: If complete and valid, a solution is found. Otherwise, backtrack and attempt alternative paths.
Backtracking Example
Consider solving a N-Queens problem (placing N queens on an N×N chessboard so no two queens threaten each other).
- Start at the first row, place a queen.
- Move to the next row; attempt placing a queen without threats.
- If a row cannot place a queen, backtrack to the previous row and relocate the last-placed queen.
Backtracking is commonly used for:
- Solving puzzles like Sudoku, N-Queens
- Combinatorial optimization problems
- Subset and permutation problems
Comparisons and Differences
To better illustrate the differences, consider the following table:
| Feature | Depth First Search (DFS) | Backtracking |
| Primary Use | Graph traversal and pathfinding | Constraint satisfaction and solution finding |
| Exploration Method | Exhaustive path exploration within graph | Incremental solution attempt with backtrack on failure |
| Decision Points | Explore all branches exhaustively | Evaluate constraints at each step; prune invalid paths |
| Memory Usage | Stores nodes in a stack (linear space) | May grow large due to multiple branching decisions |
| Applications | Graph traversal, shortest path problems | Puzzles, optimization, planning, constraint problems |
| Outcome | Visits all nodes in a single path until dead-end | Searches for full solutions; partially explored paths are abandoned |
| Search Strategy | Systematic, fixed order | Dynamic, constraint-driven |
Additional Considerations
Similarities
- Stack Usage: Both algorithms utilize a stack. In DFS, it's explicit with data structures or recursion, and in backtracking, recursive function calls serve the stack's role.
- Recursive Nature: Both techniques can be implemented using recursion, benefiting from recursive function calls for stack management.
Complexity
- DFS is likely more efficient for traversal where less backtracking occurs. It requires time complexity, where V is vertices, and E is edges.
- Backtracking's complexity highly depends on the problem constraints and is often exponential, in worst-case scenarios, where b is the branching factor and d is the depth of the tree.
In summary, while depth first search and backtracking share similarities, fundamentally, they serve different primary purposes. DFS focuses on efficient traversal through structures, while backtracking aims to find viable solutions within constrained contexts. Understanding these differences enables developers to apply these techniques to the problems they are most suited to solving.
Related reading
- What's the difference between git diff --patience and git diff --histogram?
- What's the difference between greedy and heuristic algorithm?
- What's the difference between LibSVM and LibLinear
- what's the difference between list.sort and stdsort?
- What's the difference between Minimmum Spanning Tree and Travelling Salesman Problems
- What''s the difference between recursion, memoization dynamic programming?
- What's the fastest algorithm for sorting a linked list?
- What's the fastest way to brush up on algorithms for a technical interview on Monday?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.