Computer ScienceAlgorithmsMedium

Backtracking

Also known as:Constraint Propagation SearchExhaustive Search with Pruning

Backtracking is an algorithmic technique for solving constraint satisfaction and combinatorial problems by incrementally building a solution and abandoning (backtracking from) a partial solution as soon as it is determined to violate the problem constraints, then trying the next possibility. It is essentially a depth-first search through the space of candidate solutions, pruning branches that cannot lead to a valid solution. Backtracking is used to solve N-Queens, Sudoku, crossword puzzles, subset-sum, and graph coloring problems.

Backtracking Problem Examples and Complexity

ProblemState SpacePruning StrategyWorst Case
N-Queensn^n placementsRow/column/diagonal conflictsO(n!)
Sudoku9^81 cellsRow/col/box constraint checksO(9^81) naive
Subset Sum2^n subsetsSum exceeds targetO(2^n)
Graph Coloringk^V coloringsAdjacent same-color checkO(k^V)
Hamiltonian PathV! pathsRevisit checkO(V!)

Interactive Tools

VisuAlgo – Backtracking

Animated backtracking visualizer for N-Queens and similar problems

Open Tool

Brilliant.org – Backtracking

Guided exploration of backtracking with constraint problems

Open Tool

Khan Academy – Algorithms

Foundational algorithms course covering recursive and backtracking strategies

Open Tool
Animated solution of the 8-Queens problem using backtracking

Wikimedia Commons, CC BY-SA

Related Terms

The term "backtracking" was coined by American mathematician D.H. Lehmer in the 1950s. The word combines "back" (Old English bæc) and "track" (Middle English trak), meaning to retrace one's path. The systematic formalization of backtracking as an algorithmic technique was developed by Robert Floyd and others in the 1960s.

constraint-satisfactioncombinatoricspruningrecursionalgorithmssearch