🧩 Constraint Solving POTD:Problem of the Day: N-Queens Problem #53975
Closed
Replies: 1 comment
|
This discussion has been marked as outdated by Constraint Solving — Problem of the Day. A newer discussion is available at Discussion #54212. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Problem Statement
The N-Queens Problem is one of the most iconic problems in constraint satisfaction. It asks: How can you place N queens on an N×N chessboard such that no two queens attack each other?
In chess, a queen can attack any piece in the same row, column, or diagonal. The challenge is to find a valid placement—or count all valid placements—without conflicts.
Concrete Instance
For the 4-Queens problem (shown below), one valid solution is:
Here, queens are placed at positions (1,2), (2,4), (3,1), (4,3). We can verify:
Input & Output
For N=8, there are exactly 92 distinct solutions (and 12 fundamental solutions under symmetry).
Why It Matters
Historical & Educational Significance
The N-Queens problem emerged in 1848 and became a foundational benchmark for constraint-solving algorithms. It inspired generations of researchers to develop new search and pruning techniques.
Modern Applications
Modeling Approaches
Approach 1: Constraint Programming (CP) — Permutation Model
Decision Variables:
queens[i]= column position of the queen in row i (i ∈ 1..N)queens[i]∈ {1..N}Constraints:
all_distinct(queens[1], queens[2], ..., queens[N])queens[i] - queens[j] ≠ i - jqueens[i] + queens[j] ≠ i + jWhy this model?
all_distinctis a powerful global constraint; arc consistency removes vast portions of the search space.Approach 2: Integer Linear Programming (ILP) — Positional Model
Decision Variables:
x[i][j]∈ {0, 1} for each cell (i, j) on the boardConstraints:
Why this model?
Approach 3: SAT Encoding
Encode the problem as a Boolean satisfiability instance with clause-based reasoning:
queen[i][j]= TRUE iff a queen is at (i, j)Why SAT?
Modeling Example (Pseudo-code)
Below is a concise CP model in pseudo-notation (resembling MiniZinc):
This model is so compact and natural that many CP solvers can derive strong propagation automatically—often finding a solution for N=1000 within milliseconds!
Key Techniques
1. Arc Consistency & Global Constraints
The
all_distinctconstraint enforces arc consistency, immediately pruning many combinations. Specialized propagation algorithms forall_distinctand diagonal constraints are far more efficient than checking each pair independently.2. Variable & Value Ordering Heuristics
3. Symmetry Breaking
N-Queens has rotational and reflectional symmetries. Adding redundant constraints like
queens[1] < queens[N]can halve the search tree without losing solutions:These symmetry-breaking constraints are optional but dramatically speed up complete enumeration.
Challenge Corner
🤔 Can you extend N-Queens to the "N-Queens + Bishops" variant, where you must place N bishops and N knights on an N×N board such that no piece attacks another? What new constraints emerge? How would propagation change?
References
Rossi, F., van Beek, P., & Walsh, T. (2006). Handbook of Constraint Programming. Elsevier.
Hooker, J. N. (2012). Integrated Methods for Optimization (2nd ed.). Springer.
Gecode Documentation: N-Queens Example
Helmert, M. (2006). The Metric-FFP Planner: System Description. ICAPS Competition.
All reactions