3.7.17 · D3Algorithm Paradigms

Worked examples — Backtracking problems — N-Queens, Sudoku solver, all permutations - subsets

3,664 words17 min readBack to topic

This page is the "run every case" companion to the Backtracking topic note. There we learned the skeleton choose → explore → un-choose. Here we drive it through every edge case so you never meet a scenario you haven't already seen worked out.

Before any number appears, three plain words you must already own:


The scenario matrix

Every backtracking problem can throw one of these case classes at you. The examples below are labelled with the cell(s) they cover, so together they fill the whole grid.

Cell Case class What makes it tricky Covered by
A Base/degenerate input (empty list, n=0, n=1) Does the recursion still terminate and return something sensible? Ex 1, Ex 6
B "In-or-out" branching (subsets) Two children per node; copy-vs-reference trap Ex 2
C "Pick-an-unused" branching (permutations) The used[] reset; factorial blow-up Ex 3
D Geometric pruning, all signs of a key (N-Queens diagonals: r−c can be negative, zero, positive) Sign of r−c, sign of r+c, every quadrant of the board Ex 4, Ex 5
E Impossible instance (no solution exists) Must return empty / False, not crash or loop Ex 5, Ex 6
F "Find ONE" vs "find ALL" (boolean short-circuit) When to bubble True up the call stack Ex 7
G Real-world word problem Translate a story into choose/explore/undo Ex 8
H Exam twist (subsets with duplicates) Ordinary skeleton double-counts; needs a sort + skip Ex 9
I Complexity limiting behaviour How the node count grows as large Ex 10

The sign cases inside cell D are the ones people miss, so we spend two examples there — one per sign of the diagonal key.


Ex 1 — Cell A: the smallest possible subset call


Ex 2 — Cell B: the copy-vs-reference trap, seen live


Ex 3 — Cell C: permutations and the used[] reset


Ex 4 — Cell D (positive & zero diagonal key): N-Queens on the main diagonal


Ex 5 — Cell D (negative key) + Cell E (impossible): solve 4-Queens & prove 3-Queens has none


Ex 6 — Cell A + Cell E: the degenerate boards n=0 and n=1


Ex 7 — Cell F: "find ONE" short-circuit vs "find ALL"


Ex 8 — Cell G: real-world word problem (subset-sum for coins)


Ex 9 — Cell H: exam twist — subsets with duplicates


Ex 10 — Cell I: limiting behaviour of the node count


Recall Which cell does each example fill?

Ex1 base/empty (A) ::: subsets([])[[]], one subset Ex2 in/out + copy trap (B) ::: [[],[7]]; buggy version stores two [] Ex3 pick-unused + reset (C) ::: 6 permutations of [1,2,3] Ex4 diagonal keys, zero & positive (D) ::: queen at (2,0), keys r−c=2, r+c=2 Ex5 negative key + impossible (D,E) ::: 4-Queens solvable, 3-Queens empty Ex6 degenerate boards (A,E) ::: n=0[[]], n=1[["Q"]], each one solution Ex7 one-vs-all short-circuit (F) ::: boolean True cancels sibling branches Ex8 word problem (G) ::: only [5,6] sums to 11 Ex9 duplicates twist (H) ::: 6 unique subsets of [1,2,2] Ex10 growth (I) ::: 2^{n+1}-1 nodes; n! beats 2^n past n=3