A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Backtracking and Constraint Satisfaction

College Depth 83 in the knowledge graph ☐ I know this ☆ Set as goal
1topic build on this
399prerequisites beneath it
See this on the map →
Recursion and Tail-Recursion Optimization→→Backtracking Search for CSPs
backtracking csp search

Core Idea

Backtracking is a depth-first search strategy that explores solution space, undoing (backtracking) when a partial solution violates constraints. It's used to solve constraint satisfaction problems like N-Queens, Sudoku, and graph coloring by building solutions incrementally.

Explainer

You already know how recursion works — a function calling itself on smaller subproblems until reaching a base case. Backtracking uses recursion in a specific way: it builds a solution one decision at a time, and at each step, it checks whether the partial solution so far could possibly lead to a valid complete solution. If the answer is no, it immediately abandons that path and tries the next option — "backtracking" to the previous decision point. This is far more efficient than generating all possible combinations and checking each one, because it prunes entire branches of the search tree as soon as a constraint is violated.

Consider the N-Queens problem: place N queens on an N×N chessboard so that no two queens attack each other. A brute-force approach would try all possible placements of N queens on N² squares — an astronomical number of combinations. Backtracking instead places queens one row at a time. Place a queen in row 1, column 1. Move to row 2 and try column 1 — conflict with the queen above, so skip it. Try column 2 — diagonal conflict, skip. Try column 3 — no conflicts, place it. Move to row 3. If every column in row 3 conflicts with existing queens, backtrack: remove the queen from row 2, try the next column there, and continue. Each time a constraint is violated, the algorithm avoids exploring all the downstream possibilities from that invalid partial placement.

A constraint satisfaction problem (CSP) formalizes this pattern. A CSP has three components: variables (what you need to assign values to), domains (the possible values each variable can take), and constraints (rules about which combinations of values are allowed). In Sudoku, the variables are the 81 cells, each domain is {1-9}, and the constraints say no row, column, or 3×3 box can repeat a value. In graph coloring, the variables are vertices, the domain is the set of available colors, and the constraint says adjacent vertices must differ in color. Backtracking solves CSPs by assigning values to variables one at a time, checking constraints after each assignment, and undoing assignments that lead to violations.

The efficiency of backtracking depends heavily on two choices: variable ordering (which variable to assign next) and value ordering (which value to try first). A powerful heuristic is most constrained variable (also called MRV or "fail-first"): always assign next the variable with the fewest remaining legal values. If a variable has only one legal value left, assign it immediately. If a variable has zero legal values, backtrack immediately — you've detected failure as early as possible. Combined with constraint propagation (when you assign a value, immediately eliminate that value from the domains of all constrained neighbors), these heuristics can reduce exponential search spaces to manageable sizes. Sudoku solvers, for example, often solve puzzles with almost no backtracking at all once propagation handles the forced moves.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Recursion and Tail-Recursion Optimization → Backtracking and Constraint Satisfaction

Longest path: 84 steps · 399 total prerequisite topics

Prerequisites (1)

Leads To (1)