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 OptimizationBacktracking 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsRecursion and Tail-Recursion OptimizationBacktracking and Constraint Satisfaction

Longest path: 84 steps · 399 total prerequisite topics

Prerequisites (1)

Leads To (1)