# Backtracking

> Try a choice, and step back to try another when it leads to a dead end.

**Field:** Software Development  
**Category:** Algorithms & Data Structures  
**Also known as:** Backtracking Algorithm, Backtrack Search

## Think of it like

It is like a maze game. At a dead end, you walk back to the last turn and try a new way.

## Definition

Backtracking builds an answer one choice at a time. After each choice, it checks whether the answer can still work. If it cannot, it undoes the last choice and tries the next one. This skips whole groups of choices that are sure to fail. Sudoku solvers, maze solvers and puzzles like eight queens use it.

## Example

A Sudoku solver puts a 4 in an empty box. Three moves later nothing fits, so it goes back and tries a 5.

## Why it matters

It solves puzzles where trying every full answer would take far too long. Cutting dead ends early saves most of the work.

## Common mistake

Checking only at the very end. Test each choice as soon as it is made, or it is just brute force.

## Three principles

- **One choice at a time.** Build the answer step by step.
- **Check early.** Stop as soon as it cannot work.
- **Undo and retry.** Step back to the last choice.

## Related terms

- [Recursion](https://learn.benibix.com/term/development/recursion)
- [Brute Force](https://learn.benibix.com/term/development/brute-force)
- [Depth-First Search (DFS)](https://learn.benibix.com/term/development/depth-first-search)
- [Base Case](https://learn.benibix.com/term/development/base-case)
- [Algorithm](https://learn.benibix.com/term/development/algorithm)

## Told apart from

- [Backtracking vs Brute Force](https://learn.benibix.com/compare/backtracking-vs-brute-force): Brute force tries every full answer. Backtracking builds an answer step by step and gives up on a path as soon as it cannot work, so it skips most of the tries.

![Two panels. On the left, a countdown calls itself with 3, then 2, then 1, then 0, where a box marked base case says stop, and the calls return. On the right, a small maze tree: a path tries left, hits a dead end, steps back, tries right and reaches the goal.](https://learn.benibix.com/diagrams/dev7-base-case-backtracking.svg)

---

Source: https://learn.benibix.com/term/development/backtracking
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
