{"slug": "grobner-basis-representations-of-sudoku", "title": "Grobner Basis Representations of Sudoku", "summary": "A developer demonstrated three polynomial encodings of Shidoku (4x4 Sudoku) for a Grobner basis constraint solver, drawing on Arnold et al. (2009): the sum-product representation, the roots-of-unity representation, and the boolean Shidoku system. The sum-product form expresses the puzzle as 40 polynomial equations—10 for unknown cells, 6 for given cells, and 24 for the 12 units—and the approach was implemented in Python with SymPy, with code shared on Google Colab and GitHub.", "body_md": "This is part of our series on *Alternatives to Backprop for Reasoning Model*s:\n\n- [Part 1: Belief Propagation](https://leetarxiv.substack.com/p/sinkhorn-solves-sudoku) : Obscure Backprop Alternative That Solves Sudoku.\n- [Part 2: Target Propagation](https://leetarxiv.substack.com/p/target-propagation) :  Bengio’s Biologically Plausible Backprop Alternative.\n- [Part 3: Forward Forward](https://leetarxiv.substack.com/p/forward-forward-algorithm-hinton) : Hinton’s New Alternative to Backpropagation.\n- Part 4 (we are here): Grobner Constraint Solvers for Sudoku.\n\n# 1.0 Introduction\n\n*Grobner Basis Representations of Sudoku* (Arnold et al., 2009)[1](#footnote-1) demonstrates practical ways to encode Sudoku tables as polynomial constraints for a [Grobner basis solver.](https://leetarxiv.substack.com/p/resultants-grobner-basis-semaev)\n\nIf you know nothing about Grobner bases then we recommend this super friendly intro:\n\nWe introduced Sudoku in [Part 1](https://leetarxiv.substack.com/p/sinkhorn-solves-sudoku?utm_source=publication-search) when we coded the [belief propagation](https://leetarxiv.substack.com/p/sinkhorn-solves-sudoku?utm_source=publication-search) paper.\n\n[Belief propagation](https://leetarxiv.substack.com/p/sinkhorn-solves-sudoku?utm_source=publication-search) algorithm is a gradient-descent alternative that uses sinkhorn-knopp to solve sudoku with machine learning.\n\nThis paper demonstrates 3 different ways to encode constraints for a polynomial Sudoku solver. (Arnold et al., 2009) however works with Shidoku boards.\n\n## 1.1 Shidoku Primer\n\n*Shidoku* is a Sudoku variant where regions each contain the integers 1-4 exactly once. \n\nShidoku has 4 rows, 4 columns and 4 [2*2] boxes, so 12 units. Typically one is provided 6 known cells and must fill 10 unknown cells.\n\nThe rules are exactly similar to Sudoku and we went into extreme detail in [part 1](https://leetarxiv.substack.com/p/sinkhorn-solves-sudoku?utm_source=publication-search). \n\nThe remainder of this article solves the left-most board in Figure 2 above.\n\n# 2.0 Polynomial Representations of Shidoku\n\nCode is available on [Google Colab](https://colab.research.google.com/drive/12ZFuo_REtD6TwI8-K2rcex8SM5_agjZB?usp=sharing) and GitHub.\n\n(Arnold et al., 2009) introduces three different polynomial representation of Shidoku: the *sum-product* representation, the *roots-of-unity* representation and the *boolean shidoku* system.\n\nEach representation has its advantages and we’ll cover each. If you know nothing about Grobner bases then we recommend this super friendly intro:\n\n## 2.1 Sum-Product Representation\n\nThe *Sum-Product representation* encodes the 4 possible cell values as a polynomial product:\n\nFurthermore, one encodes the fact that one must choose 4 numbers that sum to 10 and multiply to 24 exactly once as the polynomial pair:\n\nNote that the equation 2 pair above can be replaced by the single equation:\n\nThe sum-product form encodes Shidoku rules as 40 polynomial equations. Then to encode provided cells one simply includes more equations. For instance, the left Figure 2 board needs these equations:\n\nLet’s break down the 40 equations among the cells and units (we showed why there are 12 units in Section 1.1):\n\n1. 10 unknown cells. So 10 equations representing (xi−1)(xi−2)(xi−3)(xi−4)=0.\n2. 6 known cells. So 6 equations where k is a provided constant in x-k = 0.\n3. 12 units. So 24 eqs since each unit has x1+x2+x3+x4−10=0 and x1x2x3x4−24=0.\n\n10+6+24= 40 equations.\n\nIn Python, this resembles:\n\n``` python\nimport sympy as sp\n\nboard = [[0,0,0,4],\n         [4,0,2,0],\n         [0,3,0,1],\n         [1,0,0,0]]\n\nx = sp.Matrix(4,4,lambda i,j: sp.Symbol(f\"x{i+1}{j+1}\"))\nequations = []\n\nfor i in range(4):\n    for j in range(4):\n        v = x[i,j]\n        equations.append((v-1)*(v-2)*(v-3)*(v-4) if board[i][j] == 0 else v-board[i][j])\n\nunits = [[x[i,j] for j in range(4)] for i in range(4)]\nunits += [[x[i,j] for i in range(4)] for j in range(4)]\nunits += [[x[i,j] for i in range(bi,bi+2) for j in range(bj,bj+2)] for bi in range(0,4,2) for bj in range(0,4,2)]\n\nfor unit in units:\n    equations += [sum(unit)-10, sp.prod(unit)-24]\n\nG = sp.groebner(equations, *list(x), order=\"lex\")\n\nprint(\"Variables:\", len(list(x)))\nprint(\"Equations:\", len(equations))\nsolutions = sp.solve(equations, list(x), dict=True)\n\nprint(\"\\nSolutions:\")\nfor sol in solutions:\n    for i in range(4):\n        print([sol[x[i,j]] for j in range(4)])\n```\n\nRunning this code yields our expected solution!\n\n## 2.2 Roots-of-Unity Representation \n\nThe *Roots-of-Unity representation* encodes the 4 possible cell values as a complex polynomial product:\n\nFor each cell pair, say w and x, one forces w and x to admit different values using the polynomial product:\n\nThis form need 72 equations to encode Shidoku’s structure and the entries (`1,2,3,4`) are replaced by the fourth roots of unity {`±1,±i`}.\n\nLet’s break down the 72 equations:\n\n1. 10 unknown cells. So 10 equations representing (x <sup>4</sup><sub>i</sub> − 1) = 0.\n2. 6 known cells. So 6 equations where k is a provided constant in x-k = 0.\n3. 56 unique pairs because: \n  - Each row has (4 choose 2 = 6) ways to pick pairs. There are 4 rows so 4*6 = 24 unique pairs in the rows.\n  - Each col has (4 choose 2 = 6) ways to pick pairs. There are 4 cols so 4*6 = 24 unique pairs in the columns.\n  - These 8 uncounted pairs in the diagonal:\n\n10+6+56= 72 equations.\n\nIn Python, this resembles:\n\n``` python\nimport sympy as sp\n\nboard = [[0,0,0,4],\n         [4,0,2,0],\n         [0,3,0,1],\n         [1,0,0,0]]\nx = sp.Matrix(4,4,lambda i,j: sp.Symbol(f\"x{i+1}{j+1}\"))\nroots = {1:1, 2:sp.I, 3:-1, 4:-sp.I}\nequations = []\n\n#16 cell equations\nfor i in range(4):\n    for j in range(4):\n        v = x[i,j]\n        equations.append(v**4-1 if board[i][j] == 0 else v-roots[board[i][j]])\n\n#56 unique row/column/block pair equations\npairs = set()\nfor i in range(4):\n    for j in range(4):\n        for k in range(4):\n            for l in range(4):\n                if (i,j) != (k,l):\n                    if i == k or j == l or (i//2 == k//2 and j//2 == l//2):\n                        pairs.add(tuple(sorted([(i,j),(k,l)])))\n\nfor (i,j),(k,l) in pairs:\n    w,v = x[i,j],x[k,l]\n    equations.append((w+v)*(w**2+v** 2))\n\nprint(\"Cell equations:\", 16)\nprint(\"Pair equations:\", len(pairs))\nprint(\"Total equations:\", len(equations))\n\nG = sp.groebner(equations, *list(x), order=\"lex\")\n\n# Extract solution from the Groebner basis\nsolution = sp.solve(G.polys, list(x), dict=True)\n\ndecode = {1:1, sp.I:2, -1:3, -sp.I:4}\n\nprint(\"\\nSolution:\")\nfor sol in solution:\n    for i in range(4):\n        print([decode[sp.simplify(sol[x[i,j]])] for j in range(4)])\n```\n\nRunning the code alongside the complex to real map `(1→1, 2→i, 3→−1, 4→−i)` yields:\n\n## 2.3 Boolean Shidoku Representation\n\nThe *Boolean Shidoku* representation introduces four boolean variables for each Shidoku board cell w<sub>1</sub>,w<sub>2</sub>,w<sub>3</sub>,w4 and encodes each cell as a boolean variable like:\n\nThe boolean form uses a *one-hot encoding* approach, where the correct value equals 1 while the incorrect are zero. This can be encoded as:\n\nFor each cell pair, say w and x, one forces w and x to admit different values using the boolean product:\n\nThe boolean representation requires 136 polynomial equations to encode Shidoku rules.\n\nHowever, one experiences a significant speedup because any power of w<sub>k</sub> can be replaced by itself ie. w<sub>2</sub> = w.\n\nOnce more, we breakdown the 136 eqs:\n\n1. 64 boolean equations. One for each of the 16 * 4 boolean variables b <sup>2</sup> i<sub>j,k</sub> −b<sub>ij,k</sub> =0\n2. 16 one hot encoding equations. b <sub>ij,1</sub> +b<sub>ij,2</sub> +b<sub>ij,3</sub> +b<sub>ij,4</sub> −1=0.\n3. 56 pairwise equations derived similar to Section 2.2.\n\nTotal is 64+16+56=136 equations.\n\nIn Python, we write\n\n``` python\nimport sympy as sp\n\nboard = [[0,0,0,4],\n         [4,0,2,0],\n         [0,3,0,1],\n         [1,0,0,0]]\nb = sp.MutableDenseNDimArray.zeros(4,4,4)\n\nfor i in range(4):\n    for j in range(4):\n        for k in range(4):\n            b[i,j,k] = sp.Symbol(f\"b{i+1}{j+1}{k+1}\")\n\nequations = []\n\n#Boolean constraints and exactly one value per cell\nfor i in range(4):\n    for j in range(4):\n        equations += [b[i,j,k]**2 - b[i,j,k] for k in range(4)]\n        equations.append(sum(b[i,j,k] for k in range(4)) - 1)\n\n#Given cells\nfor i in range(4):\n    for j in range(4):\n        if board[i][j]:\n            equations.append(b[i,j,board[i][j]-1] - 1)\n\n#56 unique pairs sharing a row, column, or block\npairs = set()\nfor i in range(4):\n    for j in range(4):\n        for k in range(4):\n            for l in range(4):\n                if (i,j) != (k,l):\n                    if i == k or j == l or (i//2 == k//2 and j//2 == l//2):\n                        pairs.add(tuple(sorted([(i,j),(k,l)])))\n\nfor (i,j),(k,l) in pairs:\n    equations.append(sum(b[i,j,d]*b[k,l,d] for d in range(4)))\n\nvariables = [b[i,j,k] for i in range(4) for j in range(4) for k in range(4)]\n\nG = sp.groebner(equations, *variables, order=\"lex\")\n\n#Decode solution\nsolutions = sp.solve(G.polys, variables, dict=True)\n\nprint(\"Variables:\", len(variables))\nprint(\"Equations:\", len(equations))\nprint(\"\\nSolution:\")\n\nfor sol in solutions:\n    solved_board = []\n    for i in range(4):\n        row = []\n        for j in range(4):\n            value = next(\n                k + 1 for k in range(4)\n                if sol.get(b[i,j,k], b[i,j,k]) == 1\n            )\n            row.append(value)\n        solved_board.append(row)\n\n    for row in solved_board:\n        print(row)\n```\n\nIt takes a while but once its done. We get the correct solution!\n\n# 3.0 Conclusion\n\nWe saw three different approaches to encoding Sudoku as Grobner basis ideals. Then we coded it all. Pretty cool stuff.\n\n# References\n\n[1](#footnote-anchor-1)\n\nArnold, E., Lucas, S., & Taalman, L. (2010). Gröbner basis representations of Sudoku. *The College Mathematics Journal, 41*(2), 101–112. [https://doi.org/10.4169/074683410x480203](https://doi.org/10.4169/074683410x480203)", "url": "https://wpnews.pro/news/grobner-basis-representations-of-sudoku", "canonical_source": "https://leetarxiv.substack.com/p/grobner-basis-representations-of", "published_at": "2026-12-03 13:29:27+00:00", "updated_at": "2026-12-03 13:47:28.895631+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "ai-research"], "entities": ["Arnold et al.", "SymPy", "Google Colab", "GitHub"], "also_reported_by": [], "takeaway": "Encoding a Shidoku board as 40 polynomial equations—one quartic per unknown cell, one linear equation per given cell, and sum/product constraints per unit—lets a Grobner basis solver handle the puzzle.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/grobner-basis-representations-of-sudoku", "markdown": "https://wpnews.pro/news/grobner-basis-representations-of-sudoku.md", "text": "https://wpnews.pro/news/grobner-basis-representations-of-sudoku.txt", "jsonld": "https://wpnews.pro/news/grobner-basis-representations-of-sudoku.jsonld"}}