๐ Introduction
Recursion and backtracking are two core techniques that unlock powerful problem-solving patterns โ especially when dealing with trees, permutations, combinations, puzzles, and pathfinding.
In this post, weโll explore:
โ What recursion is and how it works
โ Visualizing the call stack
โ Backtracking explained with templates
โ Real-world problems like permutations, combinations, and Sudoku solver
โ Tips to avoid common pitfalls like infinite recursion and stack overflows
๐ 1๏ธโฃ What is Recursion?
Recursion is when a function calls itself to solve a smaller sub-problem.
๐น Classic Example: Factorial
def factorial(n): if n == 0: return 1 return n * factorial(n - 1)
๐ง Think in 3 parts:
1. Base case โ when to stop 2. Recursive case โ how the problem shrinks 3. Stack โ Python uses a call stack to track function calls
๐ง Visualizing the Call Stack
factorial(3) => 3 * factorial(2) => 2 * factorial(1) => 1 * factorial(0) => 1
Each recursive call is paused until the next one returns. This LIFO behavior is similar to a stack.
๐งฉ 2๏ธโฃ What is Backtracking?
Backtracking is a strategy to solve problems by exploring all possibilities and undoing decisions when needed.
Itโs used when:
1. Youโre generating permutations or combinations 2. Solving constraint problems (like Sudoku) 3. Exploring paths in a grid or tree
๐ง 3๏ธโฃ Backtracking Template
def backtrack(path, choices): if goal_reached(path): results.append(path) return for choice in choices: if is_valid(choice): make_choice(choice) backtrack(path + [choice], updated_choices) undo_choice(choice)
This is the core idea behind all backtracking solutions.
๐งช 4๏ธโฃ Example: Generate All Permutations
def permute(nums): results = [] def backtrack(path, remaining): if not remaining: results.append(path) return for i in range(len(remaining)): backtrack(path + [remaining[i]], remaining[:i] + remaining[i+1:]) backtrack([], nums) return results print(permute([1, 2, 3]))
๐ฏ 5๏ธโฃ Example: N-Queens Problem
Place N queens on an NรN chessboard so that no two queens threaten each other.
def solve_n_queens(n): solutions = [] def backtrack(row, cols, diag1, diag2, board): if row == n: solutions.append(["".join(r) for r in board]) return for col in range(n): if col in cols or (row + col) in diag1 or (row - col) in diag2: continue board[row][col] = 'Q' backtrack(row + 1, cols | {col}, diag1 | {row + col}, diag2 | {row - col}, board) board[row][col] = '.' board = [["."] * n for _ in range(n)] backtrack(0, set(), set(), set(), board) return solutions
๐ข 6๏ธโฃ Example: Combinations
def combine(n, k): results = [] def backtrack(start, path): if len(path) == k: results.append(path[:]) return for i in range(start, n + 1): path.append(i) backtrack(i + 1, path) path.pop() backtrack(1, []) return results
โ Backtracking often involves modifying state, recursing, and then undoing that change.
๐ฒ 7๏ธโฃ Example: Solving a Sudoku Board
def solve_sudoku(board): def is_valid(r, c, val): for i in range(9): if board[r][i] == val or board[i][c] == val or board[r//3*3 + i//3][c//3*3 + i%3] == val: return False return True def backtrack(): for r in range(9): for c in range(9): if board[r][c] == ".": for num in map(str, range(1, 10)): if is_valid(r, c, num): board[r][c] = num if backtrack(): return True board[r][c] = "." return False return True backtrack()
๐ A great example of recursive state search with constraint pruning.
๐ง 8๏ธโฃ Tips and Best Practices
โ Always define a base case
โ Use sets or visited arrays to avoid cycles
โ Use path[:] or path.copy() when passing lists
โ Try to write recursive + backtracking templates once and reuse
โ ๏ธ Be careful with Python's recursion limit (sys.setrecursionlimit())
๐งช Classic Problems to Practice
Problem | Type |
---|---|
Fibonacci | Recursion + Memoization |
Permutations | Backtracking |
N-Queens | Backtracking + Pruning |
Sudoku Solver | Backtracking |
Word Search in Grid | DFS + Backtracking |
Letter Combinations of a Phone Number | Backtracking |
Subsets / Combinations | Backtracking |
โ Summary
โ๏ธ Recursion is calling a function within itself to break problems into sub-problems
โ๏ธ Backtracking is about exploring, committing, and undoing choices
โ๏ธ Use backtracking for problems involving all possible combinations/permutations
โ๏ธ Python makes recursion intuitive with simple syntax โ just be mindful of stack depth
โ๏ธ Think in terms of state, choices, and constraints
๐ Coming Up Next:
๐ Part 6: Sorting Algorithms โ From Bubble Sort to Merge Sort (with Python Code and Complexity Analysis)
Weโll cover:
1. Selection, Bubble, Insertion Sort Merge Sort and Quick Sort
Built-in sort and Timsort
When to use what
Top comments (0)