Function Repository Resource:

SudokuGraph

Source Notebook

Generate an n-Sudoku graph

Contributed by: Alejandra Ortiz

ResourceFunction["SudokuGraph"][n]

generates an undirected graph with n4 vertices corresponding to the cells of an n2x n2 sudoku board and with edges corresponding to all pairs of cells that belong to the same row, column, or block of the puzzle.

Details

ResourceFunction["SudokuGraph"] takes the same options as Graph.

Examples

Basic Examples (1) 

The first few sudoku graphs on differently sized sudoku boards:

In[1]:=
Table[ResourceFunction["SudokuGraph"][i, PlotLabel -> Subscript[S, i]], {i, 2, 3}]
Out[1]=

Scope (1) 

The Sudoku graph is automatically laid out in the order of the cell numeration on the Sudoku board:

In[2]:=
ResourceFunction["SudokuGraph"][3, VertexSize -> Large, VertexLabels -> Placed[Automatic, Center]]
Out[2]=

Options (6) 

GraphHighlight (2) 

Highlight the vertex 1:

In[3]:=
HighlightGraph[
 ResourceFunction["SudokuGraph"][2, VertexSize -> Medium], {1}]
Out[3]=

Highlight the edge 16:

In[4]:=
HighlightGraph[
 ResourceFunction["SudokuGraph"][2, VertexSize -> Medium], {1 \[UndirectedEdge] 6}]
Out[4]=

VertexLabels (2) 

Label individual vertices:

In[5]:=
ResourceFunction["SudokuGraph"][2, VertexSize -> Large, VertexLabels -> {2 -> Placed["A", Center]}]
Out[5]=

Label all vertices:

In[6]:=
ResourceFunction["SudokuGraph"][2, VertexSize -> Large, VertexLabels -> Placed[Automatic, Center]]
Out[6]=

VertexSize (2) 

By default, the size of vertices is computed automatically:

In[7]:=
ResourceFunction["SudokuGraph"][2, VertexSize -> Automatic]
Out[7]=

Specify the size of all vertices using symbolic vertex size:

In[8]:=
Table[ResourceFunction["SudokuGraph"][2, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]
Out[8]=

Applications (1) 

Find a vertex coloring of a sudoku graph:

In[9]:=
g = ResourceFunction["SudokuGraph"][3];
In[10]:=
Annotate[g, {VertexStyle -> Thread[VertexList[g] -> FindVertexColoring[g, ColorData[106, "ColorList"]]]}]
Out[10]=

Publisher

Wolfram Summer School

Version History

  • 1.0.0 – 14 June 2023

Related Resources

License Information