Algorithms · Backtracking: N-Queens, Subset Sum, Graph Colouring and Hamiltonian Cycle
N-Queens
Problem. Place non-attacking queens on an board. Two queens attack if they share a row, a column, or a diagonal.
State-space tree. Fix one queen per column, in column order . Component = the row of the queen in column , so the domain of each component is and the tree is -ary of depth . A node at depth is feasible iff the queen in column attacks none of the queens in columns .
attack check with arrays. Keep three boolean arrays indexed by the difference quantities, which are constant along each line:
| Line through | Invariant | Array index | Range |
|---|---|---|---|
| Row | row[r] | ||
| Main diagonal () | d1[r-c+n] | , shifted to | |
| Anti-diagonal () | d2[r+c] |
Placing a queen at sets row[r]=d1[r-c+n]=d2[r+c]=true; removing it clears them. Each test is three array lookups, hence per node. (Columns need no array because we place exactly one queen per column by construction.)
Solution counts (number of distinct solutions, i.e. counting reflections/rotations as different):
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|
| Solutions | 1 | 0 | 0 | 2 | 10 | 4 | 40 | 92 |
The 8-queens figure 92 is the one GATE quotes. Note have no solution — a common trap.
Full trace for . Write a partial solution as the row sequence for columns 1–4. Feasibility of a new row in column : unused, unused, unused.
Column 1 = row 1 (, ):
- Column 2: row 1 ✗ (row used), row 2 ✗ ( used), row 3 ✓ (, ).
- Column 3: row 1 ✗ (row), row 2 ✗ ( used), row 3 ✗ (row), row 4 ✗ ( used) → dead end, backtrack.
- Column 2: row 4 ✓ (, ).
- Column 3: row 1 ✗ (row), row 2 ✓ (, ).
- Column 4: row 1 ✗ (row), row 2 ✗ (row), row 3 ✗ ( used), row 4 ✗ (row) → dead end.
- Column 3: row 3 ✗ ( used), row 4 ✗ (row) → dead end.
- Column 3: row 1 ✗ (row), row 2 ✓ (, ).
- Column 2 exhausted → backtrack to column 1.
Column 1 = row 2 (, ):
- Column 2: row 1 ✗ ( used), row 2 ✗ (row), row 3 ✗ ( used), row 4 ✓ (, ).
- Column 3: row 1 ✓ (, ).
- Column 4: rows 1, 2 ✗ (rows used), row 3 ✓ (, ) → Solution 1: (2,4,1,3); row 4 ✗ (row).
- Column 3: row 2 ✗ (row), row 3 ✗ ( used), row 4 ✗ (row) → dead end.
- Column 3: row 1 ✓ (, ).
- Column 2 exhausted → backtrack.
Column 1 = row 3 (, ):
- Column 2: row 1 ✓ (, ).
- Column 3: row 1 ✗ (row), row 2 ✗ ( used), row 3 ✗ (row), row 4 ✓ (, ).
- Column 4: row 1 ✗ (row), row 2 ✓ (, ) → Solution 2: (3,1,4,2); rows 3, 4 ✗ (rows used).
- Column 3: row 1 ✗ (row), row 2 ✗ ( used), row 3 ✗ (row), row 4 ✓ (, ).
- Column 2: row 2 ✗ ( used), row 3 ✗ (row), row 4 ✗ ( used) → exhausted.
Column 1 = row 4 (, ):
- Column 2: row 1 ✓ (, ). Column 3: row 1 ✗ (row), row 2 ✗ ( used), row 3 ✓ (, ) → column 4: row 1 ✗ (row), row 2 ✗ ( used), rows 3, 4 ✗ (rows used) → dead end; column 3 row 4 ✗ (row) → dead end.
- Column 2: row 2 ✓ (, ). Column 3: row 1 ✗ ( used), row 2 ✗ (row), row 3 ✗ ( used), row 4 ✗ (row) → dead end.
- Column 2: row 3 ✗ ( used), row 4 ✗ (row) → exhausted; the search ends.
Total: exactly 2 solutions, and , matching the table. The trace also shows the two solutions are mirror images — for the 2 solutions form one symmetry class.
Complexity. The tree has at most leaves in the worst case (branching , depth ), and each node costs to test, so the worst case is ; the standard bound quoted is because at most one queen per row means at most row-assignments, and pruning only removes nodes. Space is (recursion depth plus the three arrays of size ).