Repository navigation
🧩 Constraint Solving POTD:Problem of the Day: Knight's Tour Problem #72652
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 #72953. |
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 Knight's Tour problem asks: can a knight on a chessboard visit every square exactly once, moving according to chess rules (an L-shaped move: two squares in one direction, one square perpendicular)?
Concrete Instance:
On a standard 8×8 chessboard, find a sequence of 64 knight moves starting from square (1,1) that visits each of the 64 squares exactly once. One famous solution is the closed tour, where the knight can return to its starting square in one final move.
Input/Output:
n×n, starting position (optional)[(x1,y1), (x2,y2), ..., (xn2,yn2)]representing a valid knight's tour, or "no solution" if none existsNote: The problem is complete for certain board sizes (e.g., 8×8 always has a tour) but is NP-hard in general; solutions may not exist for small boards like 3×3.
Why It Matters
Historical significance: The Knight's Tour has fascinated mathematicians since the 9th century and appears in recreational mathematics, puzzle design, and algorithmic game theory.
Real-world applications:
Modeling Approaches
Approach 1: Constraint Programming (Graph-Based Model)
Decision variables:
position[t]∈ {1..n2} for each stept∈ {1..n2}: the square visited at steptvisited[x,y]∈ {1..n2}: at which step square (x,y) is visitedConstraints:
alldifferent(position[1..n2]): each square visited exactly onceposition[t+1]is a valid knight move fromposition[t](encode via a transition table or tuple set)position[n2+1] = position[1]for a closed tourTrade-offs:
alldifferentprovides powerful domain reductionApproach 2: SAT Encoding (Boolean Satisfiability)
Decision variables:
visit[x,y,t]∈ {0,1} for each square (x,y) and stept∈ {1..n2}: true iff the knight is at (x,y) at steptClauses:
t, at least onevisit[*,*,t]is true; at most one via 2-CNF clausestsatisfiesvisit[x,y,t]visit[x,y,t]is true, thenvisit[x',y',t+1]must be true for some (x',y') reachable by a knight moveTrade-offs:
Example CP Model (MiniZinc-inspired pseudocode)
This pseudo-code shows the key idea: sequential position tracking with L-shaped move validation.
Key Techniques
1. Warnsdorff's Heuristic (Greedy Ordering)
At each step, move the knight to a square from which it has the fewest onward moves. This greedy heuristic is remarkably effective for finding tours on 8×8 boards and larger, often finding a solution in linear time. However, it can fail on certain starting positions or board sizes—pure heuristic search requires backtracking as fallback.
2. Arc Consistency & Domain Reduction (CP-specific)
Compute the knight's reachability graph (edges between adjacent knight moves) and apply arc consistency algorithms to prune infeasible variable assignments. Constraint propagation tightens domains dynamically, reducing the search tree significantly compared to brute-force backtracking.
3. Symmetry Breaking & Search Space Pruning
Challenge Corner
Question for you:
Symmetry-breaking: How would you modify the model to forbid finding symmetric copies of the same tour (e.g., rotations or reflections) without exploring the entire search space for each symmetry class?
Parameterization: Can you generalize the knight's tour to a directed acyclic graph where each node represents a square and edges represent valid knight moves, then find a Hamiltonian path? What additional constraints (if any) would you add for a "fairness" metric (e.g., prefer tours that visit the board edges last)?
Approximation: Warnsdorff's heuristic fails for small boards like 5×5. Can you design a hybrid algorithm that uses Warnsdorff as a warm-start, then applies constraint propagation to repair violations?
References
Warnsdorff, H. (1823). Des Rösselsprunges einfachste und allgemeinste Lösung. Schmalkalden. — The original paper on Warnsdorff's heuristic, still elegant after 200 years.
van Horn, C. (2007). "A Database of Knight's Tours" — Comprehensive catalog and analysis of known tours; see
www.ktn.freeservers.com.Rossi, F., van Beek, P., & Walsh, T. (Eds.). (2006). Handbook of Constraint Programming, Chapter 1 & 3. — Foundational CP reference covering modeling, propagation, and search for problems like Knight's Tour.
Sosic, R. & Gu, J. (1994). "Efficient Local Search with Conflict-Driven Heuristics for Constraint Satisfaction." IEEE Trans. on Knowledge and Data Engineering, 6(5), 617–633. — Seminal paper on local search + conflict analysis for CSPs; applicable to dynamic heuristic selection for the Knight's Tour.
Next time: We'll explore a scheduling problem with resource constraints. Until then, happy touring! ♞
All reactions