CMD Guide
HomeDSABacktracking

Backtracking — Choose / Explore / Un-choose, with Pruning

Systematically trying every option — and undoing

Permutations, subsets, combinations, N-Queens, Sudoku: explore a decision tree, and on a dead end or after recording a result, undo the last choice and try the next. The template is always the same three moves: choose → explore (recurse) → un-choose.

A backtracking tree for combinations C(3,2) from [1,2,3]: choose an element, recurse with a start index, then un-choose; the [3] branch is pruned because too few elements remain
A backtracking tree for combinations C(3,2) from [1,2,3]: choose an element, recurse with a start index, then un-choose; the [3] branch is pruned because too few elements remain

The template

void backtrack(State path, Choices remaining) {
    if (isComplete(path)) { results.add(copyOf(path)); return; }   // copy! path is mutated
    for (Choice c : remaining) {
        if (!valid(c, path)) continue;        // PRUNE early — the whole point
        path.add(c);                          // choose
        backtrack(path, without(remaining, c)); // explore
        path.remove(path.size() - 1);         // un-choose (backtrack)
    }
}

Combinations C(3,2) from [1,2,3], traced

The diagram above is the combinations tree: pick k=2 of n=3 elements, passing a start index (0-based) so you never go backwards. Walk it with the template:

[]        choose 1 → [1]
[1]         choose 2 → [1,2]  complete: record copy, un-choose 2
[1]         choose 3 → [1,3]  complete: record copy, un-choose 3
[]        un-choose 1, choose 2 → [2]
[2]         choose 3 → [2,3]  complete: record copy, un-choose 3
[]        un-choose 2 — next start index is 2 (element 3):
          PRUNE: start > n−k  (2 > 3−2), i.e. even taking
          everything left, [3] can't reach size 2. Done: [1,2],[1,3],[2,3].

(The prune test start > n−k uses the 0-based index of the next candidate, not its value.) For subsets you'd record every node — nothing is ever pruned, so [3] would be a valid answer; for permutations you pick any unused element each level instead of passing start.

Pruning is what makes it tractable

The naive tree is exponential. Cut branches that can't lead to a solution: a constraint already violated (N-Queens: same column/diagonal), a duplicate (sort, then skip c == prev), or a bound exceeded. Good pruning turns "times out" into "passes."

Pitfalls

Costs, and when NOT to backtrack

Takeaways


Re-authored for this guide; backtracking-tree diagram hand-authored as SVG. See also: Recursion, the Pattern Recognition index.

🤖 Don't fully get this? Learn it with Claude

Stuck on Backtracking — Choose / Explore / Un-choose, with Pruning? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Backtracking — Choose / Explore / Un-choose, with Pruning** (DSA) and want to truly understand it. Explain Backtracking — Choose / Explore / Un-choose, with Pruning from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Backtracking — Choose / Explore / Un-choose, with Pruning** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Backtracking — Choose / Explore / Un-choose, with Pruning** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Backtracking — Choose / Explore / Un-choose, with Pruning** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes