Knight's Tour

Backtracking & Puzzles

Finds a path for a knight to visit every square on the chessboard exactly once.

The Knight's Tour is a classic chess puzzle: move a knight across the board so that it visits every square exactly once. A knight moves in an "L" shape -- two squares in one direction and one square perpendicular -- giving it up to eight possible moves from any position. This ancient problem sits at the intersection of recreational mathematics, graph theory, and algorithm design, and it has captivated mathematicians for over a thousand years.

Historical Context

The earliest known reference to the Knight's Tour appears in a 9th-century Arabic manuscript by al-Adli ar-Rumi, one of the strongest chess players of the Abbasid caliphate. The problem gained European attention in the 18th century when Leonhard Euler presented a systematic analysis to the Berlin Academy of Sciences in 1759. Euler developed methods for constructing closed tours (where the knight can return to its starting square in one move) and open tours. In the 19th century, C.F. de Jaenisch published extensive analyses, and H.C. von Warnsdorff proposed the elegant heuristic that bears his name. The problem continues to inspire modern research in Hamiltonian path algorithms and computational complexity theory.

How It Works

  1. Start: Place the knight on any starting square on the N*N board.
  2. Generate moves: From the current position, calculate all valid L-shaped moves. A move is valid if it stays within the board boundaries and lands on an unvisited square.
  3. Choose and move: Select one of the valid moves and advance the knight to that square, marking it as visited with the current move number.
  4. Recurse: From the new position, recursively attempt to continue the tour by repeating the move generation and selection process.
  5. Backtrack: If no valid moves remain and the tour is incomplete (not all N*N squares have been visited), undo the last move, mark the square as unvisited, and try the next candidate move from the previous position.
  6. Complete: When all N*N squares have been visited, a valid tour has been found.

Warnsdorff's Heuristic: Rather than trying moves in an arbitrary order, Warnsdorff's rule selects the move that leads to the square with the fewest onward moves. This greedy strategy dramatically reduces backtracking because it avoids painting the knight into a corner. Remarkably, for standard 8*8 boards, Warnsdorff's heuristic almost always finds a solution on the first try without any backtracking, effectively reducing an exponential search to linear time.

Complexity

MetricValue
Board squaresN*N
Time (brute force)O(8^(N*N)) worst case
Time (Warnsdorff)Nearly O(N*N) in practice
SpaceO(N*N)

The brute-force bound reflects the worst case where every square has 8 candidate moves. In reality, corner and edge squares have far fewer moves (2-4), and Warnsdorff's heuristic exploits this structure to find solutions with minimal search. For the standard 8*8 board, there are 26,534,728,821,064 directed open tours.

Graph Theory Perspective

The Knight's Tour is a Hamiltonian path problem on the "knight's graph," where each square is a vertex and each legal move is an edge. Finding Hamiltonian paths is NP-complete in general graphs, but the knight's graph has special structural properties -- regularity, symmetry, and predictable degree distribution -- that make efficient solutions possible. Schwenk's theorem (1991) fully characterizes which rectangular boards admit a knight's tour: an m*n board has a closed knight's tour if and only if m >= 5, n >= 5, and mn is even, with additional conditions for open tours.

When to Use

The Knight's Tour teaches backtracking, graph traversal, and heuristic search. Warnsdorff's rule is a striking example of how a simple greedy heuristic can solve an exponential search problem efficiently. These concepts transfer to route planning, VLSI circuit design, genome sequencing, and any domain requiring Hamiltonian path computation on structured graphs.