Back to Data Structures & Algorithms
2025-12-166 min read

Backtracking Algorithm (Data Structures & Algorithms)

Learn Backtracking Algorithm (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

The Backtracking Algorithm is an essential technique used to solve complex problems by systematically exploring all possible solutions within a problem space. It's crucial for tackling various problems, including the N-Queens problem, graph coloring, and finding the shortest path in a maze. Understanding backtracking can help you approach challenging problems and demonstrate your ability to think recursively in interviews.

Prerequisites

To fully grasp this lesson, you should have a solid understanding of:

  1. Basic Python syntax and control structures (if-else statements, loops)
  2. Recursion
  3. Data Structures (lists, tuples, and dictionaries)
  4. Depth-First Search (DFS)
  5. Understanding the concept of a problem space and its solution tree
  6. Familiarity with graph theory and graph traversal techniques

Core Concept

Backtracking is a depth-first search (DFS) technique used to explore all possible solutions within a problem space by recursively exploring each branch of the solution tree. It's an iterative process that involves generating candidate solutions, testing their validity, and backtracking when a dead end or invalid solution is encountered.

The backtracking algorithm can be summarized in the following steps:

  1. Initialize the problem state
  2. Recursively explore each branch by making a choice (e.g., placing a queen in the N-Queens problem)
  3. If a valid solution is found, record it and continue exploring other branches
  4. If an invalid solution or dead end is encountered, backtrack to the previous state and explore another branch
  5. Repeat steps 2–4 until all possible solutions have been explored

Backtracking Algorithm Example: N-Queens Problem

The N-Queens problem asks for placing N non-attacking queens on an N x N chessboard. The backtracking algorithm can be used to solve this problem by recursively placing a queen in each row, ensuring no two queens attack each other.

Here's the Python code for solving the N-Queens problem using backtracking:

def place_queen(board, n, row):
if row == n:
print_solution(board)
return

for col in range(n):
if is_safe(board, n, row, col):
board[row][col] = 1
place_queen(board, n, row + 1)
board[row][col] = 0

def is_safe(board, n, row, col):
for i in range(row):
if board[i][col] == 1:
return False

for j in range(col - 1, -1, -1):
if board[row][j] == 1 and (row - j) == abs(col - j):
return False

for j in range(col + 1, n):
if board[row][j] == 1 and (row - j) == abs(col - j):
return False

return True

def print_solution(board):
for row in board:
print(" ".join(["." for col in range(len(board)) if col not in row] + list(row)))
print()

n = int(input("Enter the number of queens: "))
board = [[0 for _ in range(n)] for _ in range(n)]
place_queen(board, n, 0)

Worked Example

Let's solve the 4-Queens problem using backtracking:

  1. Initialize the board with all zeros:
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
  1. Start placing queens in the first row:
place_queen(board, len(board), 0)
  1. The algorithm will explore all possible positions for the queen in the first row, and if a valid solution is found, it will continue exploring other rows. In this case, the first position (column 0) is safe to place the queen:
board = [[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
  1. The algorithm will then explore the second row and find that column 3 is safe to place the queen:
board = [[1, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
  1. The algorithm will continue exploring the third row and find that column 2 is safe to place the queen:
board = [[1, 0, 1, 0],
[0, 0, 3, 0],
[0, 2, 0, 0],
[0, 0, 0, 0]]
  1. The algorithm will continue exploring the fourth row and find that column 1 is safe to place the queen:
board = [[1, 0, 1, 0],
[0, 0, 3, 0],
[0, 2, 0, 1],
[0, 0, 0, 0]]
  1. The algorithm will print the solution:
1 . . 1
. . 3 .
2 . . 1
. . . .

Common Mistakes

  1. Not checking for attacks: Ensure that each queen is not attacking any other queens in the same row, column, or diagonal.
  2. Not backtracking when a dead end is encountered: If a solution branch leads to an invalid solution or a dead end, backtrack to the previous state and explore another branch.
  3. Ignoring pruning opportunities: Some branches can be pruned early if they lead to an impossible situation (e.g., placing two queens in the same row).
  4. Not handling recursion depth limits: Some problems may require a limit on the maximum recursion depth to avoid stack overflow errors.
  5. Not considering symmetry: In some cases, solutions can be symmetrical, so it's essential to prune redundant branches and only consider unique solutions.
  6. Not handling duplicate solutions: Backtracking algorithms may produce duplicate solutions if not careful; ensure that you only record unique solutions or use a method to avoid duplicates.
  7. Not optimizing the search order: In some cases, exploring branches in a specific order can reduce the number of redundant branches and improve efficiency.

Practice Questions

  1. Solve the 8-Queens problem using backtracking in Python.
  2. Implement a backtracking algorithm to find all permutations of a given list without repetitions.
  3. Write a backtracking algorithm to solve the knapsack problem (0/1 Knapsack or Fractional Knapsack).
  4. Implement a backtracking algorithm to find all paths from one vertex to another in a directed graph, using Depth-First Search.
  5. Write a Python function that finds all possible combinations of k unique elements chosen from a list of n elements using backtracking.
  6. Implement a backtracking algorithm to solve the Sudoku puzzle.
  7. Solve the Traveling Salesman Problem (TSP) using backtracking and find the shortest route visiting each city once.
  8. Write a backtracking algorithm to find all Hamiltonian paths in a given graph.
  9. Implement a backtracking algorithm to solve the N-Kings problem, where you must place N non-attacking kings on an N x N chessboard with the additional constraint that some squares are occupied by pawns.
  10. Write a Python function that finds all possible anagrams of a given word using backtracking.

FAQ

What is the difference between backtracking and depth-first search (DFS)?

Backtracking is a specialized form of DFS used to solve problems by exploring all possible solutions within a problem space. While DFS explores the graph without any specific goal, backtracking explores the graph with the goal of finding a valid solution.

How do I handle recursion depth limits in my backtracking algorithm?

You can add a global variable to keep track of the maximum recursion depth and check this variable before making a recursive call. If the current recursion depth exceeds the limit, return early without exploring further branches.

Why is pruning important in backtracking algorithms?

Pruning helps reduce the search space by eliminating branches that are unlikely to lead to a valid solution, which can significantly improve the algorithm's efficiency.

How do I handle symmetry in my backtracking algorithm for the N-Queens problem?

You can prune redundant branches by considering only unique solutions. For example, if a queen is placed at position (r, c), you can skip placing another queen at (r, -c) because they represent the same configuration with mirrored queens.

What are some common applications of backtracking algorithms?

Backtracking algorithms are used in various domains such as computer science, mathematics, and logic puzzles. Some common applications include solving the N-Queens problem, graph coloring, Sudoku, and finding the shortest path in a maze. They can also be applied to solve optimization problems like the knapsack problem or the traveling salesman problem.

Backtracking Algorithm (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn