Backtracking
Depth First Search
DFS
Algorithm
Computer Science

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.

Practice algorithms

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

  1. Initialization: Start with a stack, marking the first node as visited.
  2. Traversal:
    • Explore each adjacent, unvisited node.
    • Push visited nodes onto the stack.
    • Move as deep along the branch until no unvisited nodes remain.
  3. 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:

 
1    A
2   / \
3  B   C
4 / \   \
5D   E   F

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

  1. Initialization: Prepare an initial empty solution.
  2. Incrementally Build Solutions:
    • Make a decision and move forward.
    • If constraints are violated, backtrack to previous decisions.
  3. Check Constraints: Continuously check whether the current path satisfies the problem constraints.
  4. 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:

FeatureDepth First Search (DFS)Backtracking
Primary UseGraph traversal and pathfindingConstraint satisfaction and solution finding
Exploration MethodExhaustive path exploration within graphIncremental solution attempt with backtrack on failure
Decision PointsExplore all branches exhaustivelyEvaluate constraints at each step; prune invalid paths
Memory UsageStores nodes in a stack (linear space)May grow large due to multiple branching decisions
ApplicationsGraph traversal, shortest path problemsPuzzles, optimization, planning, constraint problems
OutcomeVisits all nodes in a single path until dead-endSearches for full solutions; partially explored paths are abandoned
Search StrategySystematic, fixed orderDynamic, 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 O(V+E)O(V + E) time complexity, where V is vertices, and E is edges.
  • Backtracking's complexity highly depends on the problem constraints and is often exponential, O(bd)O(b^d) 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.