Wolfram Language

Graphs and Networks

The Knight's Tour

Version 11 adds FindHamiltonianPath, extending the capabilities for finding paths, cycles and tours. Find a knight's tour in which a knight moves to every space on a chessboard only once.

show complete Wolfram Language input
In[1]:=
Click for copyable input
(* draw a checker board *) checkerboard = ArrayPlot[Table[Mod[j + i, 2], {i, 8}, {j, 8}], ColorRules -> {1 -> RGBColor[0, .55, .77], 0 -> RGBColor[.67, .9, .99]}, Frame -> False, DataRange -> {{1, 8}, {1, 8}}]; (* compute knight's tour *) g = KnightTourGraph[8, 8]; tour = FindHamiltonianPath[g]; (* show the tour *) Manipulate[ Show[{checkerboard, HighlightGraph[g, PathGraph[tour[[;; i]]], PlotTheme -> "Marketing", GraphHighlightStyle -> "DehighlightHide", EdgeStyle -> Thickness[.01]]}], {i, 1, Length[tour], 1}, SaveDefinitions -> True]
Out[1]=
Play Animation
Stop Animation

Related Examples

de es fr ja ko pt-br ru zh