Backtracking
backtracking
Algorithms
Backtracking (Intro)
Listing every subset of a 20-element set produces over a million results, and there is no shortcut: you have to enumerate them. The trick is to do it without writing 20 nested loops. Backtracking solves the entire family of "generate all configurations" problems with a single recursive template that walks an implicit decision tree. **Backtracking (Intro)** introduces that template as the "choose, explore, unchoose" pattern. You will see how each recursive frame picks an option, recurses into the resulting state, and then undoes the choice on the way back up so the next branch starts from a clean slate. Classic problems include generating all subsets and permutations, the N-Queens placement puzzle, and a first look at Sudoku. The lesson also introduces pruning, where invalid partial solutions are cut off early so the algorithm explores far less than the full `O(2^n)` or `O(n!)` worst case. In **Recursion Fundamentals**, you learned to define a problem in terms of smaller subproblems. Backtracking adds one more idea: state mutated during recursion has to be restored before returning, so siblings in the call tree see the same starting state. From here, **BFS & DFS (Intro)** generalizes the same tree-of-states view to actual graphs.
Not Started
0%
Branch and Bound
0/1 Knapsack is exponentially many subsets to consider, but commercial solvers like CPLEX and Gurobi answer instances with thousands of items in seconds. The trick is not better hardware but smarter exploration: at every node of the search tree, compute a cheap upper bound on what any completion of the partial solution could possibly achieve, and prune the entire subtree if that bound cannot beat the best solution found so far. That single discipline of provably-suboptimal pruning is what defines branch and bound. **Branch and Bound** introduces the framework as a refinement of backtracking. You will design bounding functions (typically a relaxation, like fractional knapsack as a bound for 0/1 knapsack), build state-space trees, and choose between three exploration strategies: FIFO (BFS-like), LIFO (DFS-like), and least-cost (priority queue). Classic applications include 0/1 Knapsack with the fractional-relaxation bound, the Traveling Salesman Problem, and the job-assignment problem. The lesson also draws the line between B&B and DP so you know when each is the right approach. In **Backtracking (Intro)**, you walked an implicit decision tree with choose, explore, unchoose. **Greedy (Intro)** taught you that locally optimal choices sometimes give global optima. Branch and bound borrows the search structure of one and the bounding intuition of the other. Next, **Divide and Conquer (Advanced)** turns to algebraic D&C tricks like Karatsuba and Strassen.
Not Started
0%
Practice Problems
N-Queens II
Given an integer n, return the number of distinct solutions to the n-queens puzzle, where n queens are placed on an n x n chessboard so that no two queens attack each other.
N-Queens
Place n queens on an n x n chessboard so that no two queens attack each other. Return all distinct board configurations as lists of strings, where 'Q' represents a queen and '.' represents an empty space.
Word Search II
Given a 2D board of characters and a list of words, find all words that can be formed by sequentially adjacent cells on the board, using each cell at most once per word.
Combination Sum II
Given a collection of candidate numbers (which may contain duplicates) and a target, find all unique combinations where the chosen numbers sum to the target. Each number may only be used once.
Combination Sum
Given an array of distinct integers and a target, return all unique combinations where the chosen numbers sum to the target. Each number may be used an unlimited number of times.
Combinations
Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. Return the answer in any order.
Generate Parentheses
Given n pairs of parentheses, generate all combinations of well-formed (valid) parentheses.
Letter Combinations of a Phone Number
Given a string containing digits from 2-9, return all possible letter combinations that the number could represent on a phone keypad.
Palindrome Partitioning
Given a string, partition it such that every substring of the partition is a palindrome. Return all possible palindrome partitions.
Path Sum II
Given the root of a binary tree and a target sum, return all root-to-leaf paths where the sum of the values equals the target.
Permutations
Given an array of distinct integers, return all possible permutations in any order.
Subsets II
Given an integer array that may contain duplicates, return all possible subsets (the power set) without duplicate subsets.
Subsets
Given an integer array of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets.
Word Search
Given an m x n grid of characters and a string word, determine if the word exists in the grid by following a path of adjacent cells (horizontally or vertically) without reusing any cell.
Question Banks
Community
Restore IP Addresses
Given a string of digits, return every valid IPv4 address that can be formed by inserting three dots, using bounded-depth backtracking.
Word Break II
Return every sentence that can be formed by space-segmenting a string into dictionary words, using memoized DFS keyed by start index.
Backtracking: The Template That Solves 30 Problems
One choose, explore, unchoose template. Walk it on N-queens, then map the variations: permutations, subsets, word-search, sudoku. The pattern stays put; the predicate moves.
Combination Sum III
Find all unique k-digit combinations from 1-9 that sum to a target n, using cardinality-bounded backtracking.
Expression Add Operators
Insert +, -, * between digits of a string so the resulting expression equals a target, using backtracking that handles operator precedence in O(1) per step.
Sudoku Solver
Solve a partially-filled 9x9 Sudoku in place using backtracking with row, column, and 3x3-box constraint sets.
Beautiful Arrangement
Count the permutations of 1..n where every i either divides perm[i] or is divided by perm[i], using backtracking with divisibility pruning.
