Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Every square once, by always taking the hardest one first
Moves the knight each time to the square with the fewest onward moves and visits every square once: a fast greedy rule for Hamiltonian paths
Select an algorithm and generate steps to begin visualization
A knight's tour is a sequence of knight moves that visits every square of a chessboard exactly once. On the knight's graph (one vertex per square, an edge for each knight move) it is a Hamiltonian path, and if the last square is a knight's move from the first it is a closed tour, a Hamiltonian cycle. The puzzle is centuries old: Euler studied it in 1759. Finding a Hamiltonian path is NP-hard in general, but on chessboards a simple greedy rule almost always succeeds.
Warnsdorff's rule (1823): from the current square, move to the unvisited square that has the fewest unvisited squares onward. Squares near the corners and edges have few exits, and visiting them early keeps them from becoming dead ends that would strand the knight later. Each move looks at the candidates and their onward counts, so a tour of n squares takes O(n · d²) with d at most 8. Ties matter: this visualizer breaks them in favour of the square farthest from the centre (Roth's rule), which completes every possible start on 5×5, 6×6 and 8×8 boards. Plain first-in-order ties fail from some squares. On 7×7, and on other graphs, the rule can still get stuck, and the visualizer says so.
The knight's tour is a classic exercise for backtracking and greedy heuristics in programming courses and interviews, and a gentle introduction to Hamiltonian paths and NP-hard problems. The same "most constrained first" idea runs through constraint solving (the minimum remaining values heuristic in sudoku and scheduling solvers) and graph colouring (DSatur). Knight's tours have also been used in puzzles, magic squares, art and simple encryption schemes.
A greedy loop: from the last square, always go where the fewest onward moves remain.
WarnsdorffTour(G, start):
tour = [start], mark start visited
while |tour| < n:
C = unvisited neighbours of the last square
if C is empty: return tour // stuck
for each c in C:
onward[c] = number of unvisited neighbours of c
next = c in C with the smallest onward[c]
(ties: farthest from the centre)
append next to tour, mark it visited
return tourThe onward count of a candidate does not count the current square, which is already visited. The rule never backtracks, so it either finishes in n − 1 moves or stops stuck.
The tour from a1 on the 6 × 6 board loaded on this page.
Example graph: One vertex per square, a1 to f6, and an edge for each knight move: 36 vertices, 80 edges. Corners have 2 moves, central squares 8.
A complete open tour ending on d5: d5 is not a knight's move from a1, so the tour is a Hamiltonian path, not a cycle.
Time: O(n · d²) · Space: O(n)
There are n − 1 moves. Each looks at the at most d candidates and counts the onward moves of each, at most d again, so O(d²) per move and O(n · d²) in all. With d ≤ 8 on a chessboard the tour is linear in the number of squares. A full backtracking search, by contrast, can take exponential time.
Knight's tours can be found in several ways, from greedy to exhaustive.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Plain backtracking | Small boards, or to prove that no tour exists. Very slow on 8 × 8 without a good move order. | Exponential |
| Warnsdorff + backtracking | Use the rule to order the moves and backtrack when stuck: finds tours on nearly every board almost instantly. | Usually linear |
| Divide and conquer (Parberry) | Very large boards: glue small precomputed tours together. Guaranteed closed tours on many sizes. | O(n) |
Related algorithms: Hamiltonian Path, Traveling Salesman Problem, Eulerian Path (Undirected), Depth-First Search, DSatur Coloring