1. Gyanastra
  2. Algorithms
  3. Backtracking: N-Queens, Subset Sum, Graph Colouring and Hamiltonian Cycle
  4. N-Queens
Free lesson, no account needed 4 min read

Algorithms · Backtracking: N-Queens, Subset Sum, Graph Colouring and Hamiltonian Cycle

N-Queens

Problem. Place nn non-attacking queens on an n×nn\times n 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 1,2,…,n1,2,\dots,n. Component kk = the row of the queen in column kk, so the domain of each component is {1,…,n}\{1,\dots,n\} and the tree is nn-ary of depth nn. A node at depth kk is feasible iff the queen in column kk attacks none of the queens in columns 1..k−11..k-1.

O(1)O(1) attack check with arrays. Keep three boolean arrays indexed by the difference quantities, which are constant along each line:

Line through (r,c)(r,c)InvariantArray indexRange
Rowrrrow[r]1..n1..n
Main diagonal (↘\searrow)r−cr-cd1[r-c+n]1−n..n−11-n..n-1, shifted to 1..2n−11..2n-1
Anti-diagonal (↙\swarrow)r+cr+cd2[r+c]2..2n2..2n

Placing a queen at (r,c)(r,c) sets row[r]=d1[r-c+n]=d2[r+c]=true; removing it clears them. Each test is three array lookups, hence O(1)O(1) 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):

nn12345678
Solutions10021044092

The 8-queens figure 92 is the one GATE quotes. Note n=2,3n=2,3 have no solution — a common trap.

Full trace for n=4n=4. Write a partial solution as the row sequence (r1,r2,r3,r4)(r_1,r_2,r_3,r_4) for columns 1–4. Feasibility of a new row rr in column cc: rr unused, r−cr-c unused, r+cr+c unused.

Column 1 = row 1 (d1=0d_1=0, d2=2d_2=2):

  • Column 2: row 1 ✗ (row used), row 2 ✗ (d1=0d_1=0 used), row 3 ✓ (d1=1d_1=1, d2=5d_2=5).
    • Column 3: row 1 ✗ (row), row 2 ✗ (d2=5d_2=5 used), row 3 ✗ (row), row 4 ✗ (d1=1d_1=1 used) → dead end, backtrack.
  • Column 2: row 4 ✓ (d1=2d_1=2, d2=6d_2=6).
    • Column 3: row 1 ✗ (row), row 2 ✓ (d1=−1d_1=-1, d2=5d_2=5).
      • Column 4: row 1 ✗ (row), row 2 ✗ (row), row 3 ✗ (d1=−1d_1=-1 used), row 4 ✗ (row) → dead end.
    • Column 3: row 3 ✗ (d1=0d_1=0 used), row 4 ✗ (row) → dead end.
  • Column 2 exhausted → backtrack to column 1.

Column 1 = row 2 (d1=1d_1=1, d2=3d_2=3):

  • Column 2: row 1 ✗ (d2=3d_2=3 used), row 2 ✗ (row), row 3 ✗ (d1=1d_1=1 used), row 4 ✓ (d1=2d_1=2, d2=6d_2=6).
    • Column 3: row 1 ✓ (d1=−2d_1=-2, d2=4d_2=4).
      • Column 4: rows 1, 2 ✗ (rows used), row 3 ✓ (d1=−1d_1=-1, d2=7d_2=7) → Solution 1: (2,4,1,3); row 4 ✗ (row).
    • Column 3: row 2 ✗ (row), row 3 ✗ (d2=6d_2=6 used), row 4 ✗ (row) → dead end.
  • Column 2 exhausted → backtrack.

Column 1 = row 3 (d1=2d_1=2, d2=4d_2=4):

  • Column 2: row 1 ✓ (d1=−1d_1=-1, d2=3d_2=3).
    • Column 3: row 1 ✗ (row), row 2 ✗ (d1=−1d_1=-1 used), row 3 ✗ (row), row 4 ✓ (d1=1d_1=1, d2=7d_2=7).
      • Column 4: row 1 ✗ (row), row 2 ✓ (d1=−2d_1=-2, d2=6d_2=6) → Solution 2: (3,1,4,2); rows 3, 4 ✗ (rows used).
  • Column 2: row 2 ✗ (d2=4d_2=4 used), row 3 ✗ (row), row 4 ✗ (d1=2d_1=2 used) → exhausted.

Column 1 = row 4 (d1=3d_1=3, d2=5d_2=5):

  • Column 2: row 1 ✓ (d1=−1d_1=-1, d2=3d_2=3). Column 3: row 1 ✗ (row), row 2 ✗ (d1=−1d_1=-1 used), row 3 ✓ (d1=0d_1=0, d2=6d_2=6) → column 4: row 1 ✗ (row), row 2 ✗ (d2=6d_2=6 used), rows 3, 4 ✗ (rows used) → dead end; column 3 row 4 ✗ (row) → dead end.
  • Column 2: row 2 ✓ (d1=0d_1=0, d2=4d_2=4). Column 3: row 1 ✗ (d2=4d_2=4 used), row 2 ✗ (row), row 3 ✗ (d1=0d_1=0 used), row 4 ✗ (row) → dead end.
  • Column 2: row 3 ✗ (d2=5d_2=5 used), row 4 ✗ (row) → exhausted; the search ends.

Total: exactly 2 solutions, (2,4,1,3)(2,4,1,3) and (3,1,4,2)(3,1,4,2), matching the table. The trace also shows the two solutions are mirror images — for n=4n=4 the 2 solutions form one symmetry class.

Complexity. The tree has at most nnn^n leaves in the worst case (branching nn, depth nn), and each node costs O(1)O(1) to test, so the worst case is O(nn)O(n^n); the standard bound quoted is O(n!)O(n!) because at most one queen per row means at most n!n! row-assignments, and pruning only removes nodes. Space is O(n)O(n) (recursion depth nn plus the three arrays of size O(n)O(n)).

Pages 1–2 of 2