Function Repository Resource:

ChessTourGraph

Source Notebook

Generate a chessboard graph where vertices are squares and edges represent legal moves of a chosen piece

Contributed by: Alejandra Ortiz Duran

ResourceFunction["ChessTourGraph"][piece,n]

generates an undirected graph with n2 vertices corresponding to an n×n chessboard, with edges indicating all legal piece moves.

ResourceFunction["ChessTourGraph"][piece,{n,m}]

generates a graph corresponding to an n×m chessboard.

ResourceFunction["ChessTourGraph"]["Bishop",color,dims]

generates a graph corresponding to a chessboard of dimension dims, with moves restricted to color squares.

Details and Options

Allowed pieces include "Queen", "King", "Knight", "Bishop" and "Rook".
The dims argument can be a single integer n, for {n,n} or two integers {n,m}. These specify the dimensions of the chessboard.
ResourceFunction["ChessTourGraph"] takes the same options as Graph.
In chess, the king moves one square in any direction (horizontally, vertically, or diagonally). The king graph thus connects vertices representing squares that are adjacent in any of these eight directions.
A queen moves any number of squares along a row, column, or diagonal. The queen's graph thus connects two vertices if their corresponding squares share the same row, column, or diagonal.
A rook moves any number of squares along a row or column. The rook graph thus connects two vertices if their corresponding squares share the same row or the same column.
A knight moves in an "L" shape: two squares in one direction followed by one square perpendicular to it. The knight's graph thus connects two vertices if their corresponding squares are a knight's move apart.
A bishop moves diagonally on squares of the same color; hence, the bishop graph is always disconnected, with two components (white and black).
The color, if specified, must be "White" or "Black", and only applies to bishops. For other pieces, the color specification will be ignored.
ResourceFunction["ChessTourGraph"] can generate any regular or irregular kings graph, including both classical well-known chessboard sizes and non-regular or non-standard boards.
The larger queen, rook and bishop graphs are degenerate in the sense that, when drawn with standard layouts, vertices can lie along edges to which they are not connected. Additionally, edges often overlap or completely cover each other due to the high symmetry of the graph. As a result, sometimes not all edges are visible, even though they are present in the graph structure.
Knight graphs are much sparser and have lower symmetry. Moreover, depending on the board dimensions, some vertices may belong to disconnected components, meaning that certain positions on the board are not reachable from others.

Examples

Basic Examples (1) 

Generate the king graph in an 8×8 board:

In[1]:=
ResourceFunction["ChessTourGraph"]["King", 8]
Out[1]=

Scope (18) 

King (2) 

Generate the first few square king graphs:

In[2]:=
Table[ResourceFunction["ChessTourGraph"]["King", i], {i, 2, 4}]
Out[2]=

Generate some rectangular king graphs:

In[3]:=
Flatten[Table[
  ResourceFunction["ChessTourGraph"]["King", {i, j}, PlotLabel -> {i, j}], {i, 5, 6}, {j, 3, 4}]]
Out[3]=

Queen (3) 

Generate the queen graph in an 8×8 board:

In[4]:=
ResourceFunction["ChessTourGraph"]["Queen", 8]
Out[4]=

Generate the first square queen graphs:

In[5]:=
Table[ResourceFunction["ChessTourGraph"]["Queen", i, PlotLabel -> Subscript[S, i]], {i, 2, 4}]
Out[5]=

Generate some rectangular queen graphs:

In[6]:=
Table[ResourceFunction["ChessTourGraph"]["Queen", {i, j}, PlotLabel -> Subscript[Q, i, j]], {i, 5, 6}, {j, 3, 4}]
Out[6]=

Rook (3) 

Generate the rook graph in an 8×8 board:

In[7]:=
ResourceFunction["ChessTourGraph"]["Rook", 8]
Out[7]=

Generate the first few square rook graphs:

In[8]:=
Table[ResourceFunction["ChessTourGraph"]["Rook", i], {i, 2, 4}]
Out[8]=

Generate some rectangular rook graphs:

In[9]:=
Table[ResourceFunction["ChessTourGraph"]["Rook", {i, j}, PlotLabel -> {i, j}], {i, 5, 6}, {j, 3, 4}]
Out[9]=

Knight (3) 

Generate the knight graph in an 8×8 board:

In[10]:=
ResourceFunction["ChessTourGraph"]["Knight", 8]
Out[10]=

Generate the first few square knight graphs:

In[11]:=
Table[ResourceFunction["ChessTourGraph"]["Knight", i], {i, 2, 4}]
Out[11]=

Generate some rectangular rook graphs:

In[12]:=
Table[ResourceFunction["ChessTourGraph"]["Knight", {i, j}, PlotLabel -> {i, j}], {i, 5, 6}, {j, 3, 4}]
Out[12]=

Bishop (7) 

Generate a standard 8×8 bishop graph:

In[13]:=
ResourceFunction["ChessTourGraph"]["Bishop", 8]
Out[13]=

Generate a rectangular bishop graph for a 5×3 board:

In[14]:=
ResourceFunction["ChessTourGraph"]["Bishop", {5, 3}]
Out[14]=

Show the white bishop movement on any size board:

In[15]:=
ResourceFunction["ChessTourGraph"]["Bishop", {24, 13}, "White"]
Out[15]=

Show the black bishop movement on any size board:

In[16]:=
ResourceFunction["ChessTourGraph"]["Bishop", {24, 13}, "Black"]
Out[16]=

Generate the first few square bishop graphs:

In[17]:=
Grid[{Table[ResourceFunction["ChessTourGraph"]["Bishop", i], {i, 2, 4}]}]
Out[17]=

Generate some rectangular bishop graphs:

In[18]:=
Grid[Table[
  ResourceFunction["ChessTourGraph"]["Bishop", {i, j}, PlotLabel -> {i, j}], {i, 5, 6}, {j, 3, 4}]]
Out[18]=

The graph is automatically laid out in the order of the appearance of the square numbering on the chessboard:

In[19]:=
ResourceFunction["ChessTourGraph"][#, 4, VertexSize -> Medium, VertexLabels -> Placed[Automatic, Center]] & /@ {"King", "Queen", "Rook", "Knight", "Bishop"}
Out[19]=

Options (7) 

GraphHighlight (2) 

Highlight vertex 1:

In[20]:=
HighlightGraph[
 ResourceFunction["ChessTourGraph"]["Bishop", 6, VertexSize -> Medium], {1}, VertexLabels -> Automatic]
Out[20]=

Highlight the edge 26:

In[21]:=
HighlightGraph[
 ResourceFunction["ChessTourGraph"]["Queen", 5, VertexSize -> Medium],
  2 \[UndirectedEdge] 6, EdgeLabels -> Automatic]
Out[21]=

VertexLabels (2) 

Label individual vertices:

In[22]:=
ResourceFunction["ChessTourGraph"]["Rook", 4, VertexSize -> Large, VertexLabels -> {2 -> Placed["A", Center]}]
Out[22]=

Label all vertices:

In[23]:=
ResourceFunction["ChessTourGraph"]["King", 5, VertexSize -> Large, VertexLabels -> Placed[Automatic, Center]]
Out[23]=

VertexSize (2) 

By default, the size of vertices is computed automatically:

In[24]:=
ResourceFunction["ChessTourGraph"]["King", 4, VertexSize -> Automatic]
Out[24]=

Specify the size of all vertices using symbolic vertex size:

In[25]:=
Table[ResourceFunction["ChessTourGraph"]["Queen", 4, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]
Out[25]=

EdgeShapeFunction (1) 

Use curved edges to show all the edges:

In[26]:=
ResourceFunction["ChessTourGraph"][#, 5, EdgeShapeFunction -> "CurvedEdge"] & /@ {"Rook", "Bishop", "Queen"}
Out[26]=

Applications (2) 

Visualize valid moves for a given piece located at a given position:

In[27]:=
HighlightGraph[ResourceFunction["ChessTourGraph"][#, {12, 10}], NeighborhoodGraph[ResourceFunction["ChessTourGraph"][#, {12, 10}], 53], VertexStyle -> {53 -> Blue}, VertexSize -> {53 -> Medium}, VertexSize -> Medium] & /@ {"King", "Queen", "Rook", "Knight", "Bishop"}
Out[27]=

The vertex coloring of the queen graph:

In[28]:=
g = ResourceFunction["ChessTourGraph"]["Queen", 8, VertexSize -> Large];
In[29]:=
Annotate[g, {VertexStyle -> Thread[VertexList[g] -> FindVertexColoring[g, ColorData[106, "ColorList"]]]}]
Out[29]=

Properties and Relations (10) 

King (2) 

The king graph can also be constructed as the strong product of two path graphs. The strong product of any two graphs can be constructed as the union of two other products of the same two graphs, the Cartesian product of graphs and the tensor product of graphs:

In[30]:=
CartesianProductG = GraphProduct[PathGraph[Range[4]], PathGraph[Range[4]], "Cartesian"];
TensorProductG = GraphProduct[PathGraph[Range[4]], PathGraph[Range[4]], "Tensor"];
StrongProductG = GraphUnion[CartesianProductG, TensorProductG];
In[31]:=
IsomorphicGraphQ[
 ResourceFunction["ChessTourGraph"]["King", 4], StrongProductG]
Out[31]=

In some board cases, the king graph can also be constructed using GraphData:

In[32]:=
GraphData[{"King", {5, 10}}]
Out[32]=

However, not all board sizes are supported by GraphData:

In[33]:=
GraphData[{"King", {50, 10}}]
Out[33]=

Rook (4) 

Rook graphs are the Cartesian product of two complete graphs:

In[34]:=
CartesianProductG = GraphProduct[CompleteGraph[4], CompleteGraph[4], "Cartesian"];
In[35]:=
IsomorphicGraphQ[
 ResourceFunction["ChessTourGraph"]["Rook", 4], CartesianProductG]
Out[35]=

Rook graphs are the line graphs of complete bipartite graphs:

In[36]:=
lcb = LineGraph[CompleteGraph[{4, 4}]];
In[37]:=
IsomorphicGraphQ[lcb, ResourceFunction["ChessTourGraph"]["Rook", 4]]
Out[37]=

Rook graphs are highly symmetric: for square boards, they are vertex-transitive, edge-transitive, and distance-transitive:

In[38]:=
VertexTransitiveGraphQ[ResourceFunction["ChessTourGraph"]["Rook", 4]]
Out[38]=
In[39]:=
EdgeTransitiveGraphQ[ResourceFunction["ChessTourGraph"]["Rook", 4]]
Out[39]=

In some board cases, a rook graph can also be constructed using GraphData:

In[40]:=
GraphData[{"Rook", {5, 10}}]
Out[40]=

However, not all board sizes are supported by GraphData:

In[41]:=
GraphData[{"Rook", {50, 10}}]
Out[41]=

Bishop (4) 

Observe the connected components of the bishop graph on a 3×3 board that correspond to the white and black bishop:

In[42]:=
ConnectedGraphComponents[
 ResourceFunction["ChessTourGraph"]["Bishop", 3]]
Out[42]=

All bishop graphs are perfect:

In[43]:=
ResourceFunction["PerfectGraphQ"][
 ResourceFunction["ChessTourGraph"]["Bishop", 4, "White"]]
Out[43]=
In[44]:=
ResourceFunction["PerfectGraphQ"][
 ResourceFunction["ChessTourGraph"]["Bishop", 4, "Black"]]
Out[44]=

S. Wagon showed that the white bishop graph B(m,n) is Hamiltonian for 4mn and when m=3 and n4:

In[45]:=
HamiltonianGraphQ[
 ResourceFunction["ChessTourGraph"]["Bishop", 4, "White"]]
Out[45]=
In[46]:=
HamiltonianGraphQ[
 ResourceFunction["ChessTourGraph"]["Bishop", {5, 6}, "White"]]
Out[46]=
In[47]:=
HamiltonianGraphQ[
 ResourceFunction["ChessTourGraph"]["Bishop", {3, 4}, "White"]]
Out[47]=

And nonhamiltonian for m=n=3 and the trivial cases or 1:

In[48]:=
HamiltonianGraphQ[
 ResourceFunction["ChessTourGraph"]["Bishop", {3, 3}, "White"]]
Out[48]=
In[49]:=
HamiltonianGraphQ[
 ResourceFunction["ChessTourGraph"]["Bishop", {2, 2}, "White"]]
Out[49]=

In some board cases, the bishop graph can also be constructed using GraphData:

In[50]:=
GraphData[{"Bishop", {5, 10}}]
Out[50]=

However, not all board sizes are supported by GraphData:

In[51]:=
GraphData[{"Bishop", {50, 10}}]
Out[51]=

Possible Issues (2) 

The setting DirectedEdgesTrue will not work:

In[52]:=
ResourceFunction["ChessTourGraph"][#, 3, DirectedEdges -> True] & /@ {"King", "Queen", "Rook", "Knight", "Bishop"}
Out[52]=

ChessTourGraph can produce empty or null graphs when 0 or 1 is specified as the size:

In[53]:=
Table[ResourceFunction["ChessTourGraph"][#, i, DirectedEdges -> True], {i, 0, 1}] & /@ {"King", "Queen", "Rook", "Knight", "Bishop"}
Out[53]=

Neat Examples (3) 

Visualize the vertex coloring of the king graph:

In[54]:=
g = ResourceFunction["ChessTourGraph"]["King", 8, VertexSize -> Large];
In[55]:=
Annotate[g, {VertexStyle -> Thread[VertexList[g] -> FindVertexColoring[g, ColorData[106, "ColorList"]]]}]
Out[55]=

Visualize the bishop graph with vertices shown as colored squares matching their color on the chessboard:

In[56]:=
ResourceFunction["ChessTourGraph"]["Bishop", {10, 7}, VertexShapeFunction -> "Square", VertexSize -> 0.8, VertexStyle -> Join[Thread[
    VertexList[
      ResourceFunction["ChessTourGraph"]["Bishop", {10, 7}, "Black"]] -> RGBColor[0.682, 0.451, 0.294]], Thread[VertexList[
      ResourceFunction["ChessTourGraph"]["Bishop", {10, 7}, "White"]] -> RGBColor[0.945, 0.815, 0.631]]]]
Out[56]=

Find the shortest path for a knight to cross the board:

In[57]:=
g = ResourceFunction["ChessTourGraph"]["Knight", {10, 8}, VertexLabels -> Automatic];
In[58]:=
FindShortestPath[g, 1, 80]
Out[58]=

Publisher

Wolfram Discrete Computation

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 25 August 2025

Related Resources

License Information