{"id":18964276,"url":"https://github.com/lorransutter/sudoku","last_synced_at":"2026-04-02T20:30:15.792Z","repository":{"id":114624709,"uuid":"101252961","full_name":"LorranSutter/Sudoku","owner":"LorranSutter","description":"Solve Sudoku using linear programming","archived":false,"fork":false,"pushed_at":"2022-05-20T01:23:06.000Z","size":1439,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-01T05:30:02.028Z","etag":null,"topics":["gurobi","gurobipy","linear-programming","problem-solving","puzzle","python","python3","sudoku"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LorranSutter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-24T04:09:00.000Z","updated_at":"2022-05-20T01:23:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"a7871224-0887-4aa2-a5e6-2e6ded1e65c1","html_url":"https://github.com/LorranSutter/Sudoku","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LorranSutter%2FSudoku","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LorranSutter%2FSudoku/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LorranSutter%2FSudoku/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LorranSutter%2FSudoku/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LorranSutter","download_url":"https://codeload.github.com/LorranSutter/Sudoku/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239959950,"owners_count":19725221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gurobi","gurobipy","linear-programming","problem-solving","puzzle","python","python3","sudoku"],"created_at":"2024-11-08T14:23:32.761Z","updated_at":"2026-04-02T20:30:15.744Z","avatar_url":"https://github.com/LorranSutter.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  Sudoku\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  Project created to solve sudoku game for any square dimension (9x9, 16x16, 25x25 ...) employing linear problem concepts.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  You can also check a linear solution to solve Radar Positioning problem \u003ca href='https://github.com/LorranSutter/Radar-positioning'\u003ehere\u003c/a\u003e.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"#runner-how-to-run\"\u003eHow to run\u003c/a\u003e\u0026nbsp;\u0026nbsp;|\u0026nbsp;\u0026nbsp;\n  \u003ca href=\"#pencil-dependencies\"\u003eDependencies\u003c/a\u003e\u0026nbsp;\u0026nbsp;|\u0026nbsp;\u0026nbsp;\n  \u003ca href=\"#game_die-what-is-sudoku\"\u003eWhat is sudoku?\u003c/a\u003e\u0026nbsp;\u0026nbsp;|\u0026nbsp;\u0026nbsp;\n  \u003ca href=\"#pencil2-what-is-linear-programming\"\u003eWhat is linear programming?\u003c/a\u003e\u0026nbsp;\u0026nbsp;|\u0026nbsp;\u0026nbsp;\n  \u003ca href=\"#triangular_ruler-sudoku-model\"\u003eSudoku model\u003c/a\u003e\u0026nbsp;\u0026nbsp;|\u0026nbsp;\u0026nbsp;\n  \u003ca href=\"#cookie-credits\"\u003eCredits\u003c/a\u003e\n\u003c/p\u003e\n\n## :runner: How to run\n\n```sh\npython3 LP_Sudoku.py sudoku_9x9.txt\n```\n\nInput example\n\n```\n9\n3 0 6 5 0 8 4 0 0\n5 2 0 0 0 0 0 0 0\n0 8 7 0 0 0 0 3 1\n0 0 3 0 1 0 0 8 0\n9 0 0 8 6 3 0 0 5\n0 5 0 0 9 0 6 0 0\n1 3 0 0 0 0 2 5 0\n0 0 0 0 0 0 0 7 4\n0 0 5 2 0 6 3 0 0\n```\n\nOutput example\n\n```\n3  1  6  | 5  7  8  | 4  9  2\n5  2  9  | 1  3  4  | 7  6  8\n4  8  7  | 6  2  9  | 5  3  1\n-------------------------------\n2  6  3  | 4  1  5  | 9  8  7\n9  7  4  | 8  6  3  | 1  2  5\n8  5  1  | 7  9  2  | 6  4  3\n-------------------------------\n1  3  8  | 9  4  7  | 2  5  6\n6  9  2  | 3  5  1  | 8  7  4\n7  4  5  | 2  8  6  | 3  1  9\n```\n\n## :pencil: Dependencies\n\ngurobipy library must be installed.\n\nInstallation instructions in [Gurobi](https://www.gurobi.com/documentation/9.0/quickstart_mac/py_python_interface.html).\n\n---\n\n## :game_die: What is sudoku?\n\nSudoku is a logic puzzle game with the objective to fill a 9x9 grid with digits from 1 to 9. It respects a set of rules to fill this grid:\n\n- Each row must have all digits from 1 to 9\n- Each column must have all digits from 1 to 9\n- Each of the nine 3x3 subgrid must have all digits from 1 to 9\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/e/e0/Sudoku_Puzzle_by_L2G-20050714_standardized_layout.svg\" alt=\"Sudoku unsolved\" width=\"300\"/\u003e\n\u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/1/12/Sudoku_Puzzle_by_L2G-20050714_solution_standardized_layout.svg\" alt=\"Sudoku solved\" width=\"300\"/\u003e\n\u003c/div\u003e\n\n## :pencil2: What is linear programming?\n\nLinear programming is a technique used to solve optimization problems where the elements have a linear relationship.\n\nLinear programs aims to maximize a **objective function** made of **decision variables** subject to **constraints** which ensures that all the elements have a linear relationship and all variables are non-negative.\n\n## :triangular_ruler: Sudoku model\n\nIn this case we just want to find a combination of variables that solves the puzzle. Therefore there will be no objetive function do be maximized, only linear constraints as follows:\n\n\u003cdiv align=\"center\"\u003e\n\n$$\\sum_{i=1}^nx_{ijk} \\ = \\ 1 \\ \\text{for} \\ j,k \\ \\in \\ [1,n]$$\n\n$$\\sum_{j=1}^nx_{ijk} \\ = \\ 1 \\ \\text{for} \\ i,k \\ \\in \\ [1,n]$$\n\n$$\\sum_{k=1}^nx_{ijk} \\ = \\ 1 \\ \\text{for} \\ i,j \\ \\in \\ [1,n]$$\n\n$$\\sum_{j=3p-2}^{3p} \\ \\sum_{i=3q-2}^{3q}x_{ijk} \\ = \\ 1 \\ \\text{for} \\ k \\ \\in \\ [1,n] \\ \\text{and} \\ p,q \\ \\in \\ \\left[ 1,\\sqrt n \\right]$$\n\n\u003c!-- Different way to right the formula above. But it is not quite right yet. --\u003e\n\u003c!-- $$\\sum_{j=1}^{\\sqrt n}\\sum_{i=1}^{\\sqrt n}x_{i + pj + qk} \\ = \\ 1 \\ for \\ k \\ \\in \\ [1,n] \\ and \\ p,q \\ \\in \\ \\left[ 1,\\sqrt n \\right]$$ --\u003e\n\u003c/div\u003e\n\nWe want to generalize the problem to solve a sudoku of any square dimension (9x9, 16x16, 25x25 ...). For that purpose, $n$ represents the dimension of the puzzle, $x_{ijk}$ are decision variables, $i$ represents the columns, $j$ represents the rows, $k$ represents all possible digitis depending on the puzzle dimension, and $p$ and $q$ represents an auxiliar variable to iterate in all subgrids.\n\nThe first and the second constraints ensures that all columns and all rows will be filled must have only one of the available digits. The third constraint ensures that each cell in the grid will have only one digit. The last constraint ensures that all subgrid will have only one of the available digits.\n\n---\n\n## :cookie: Credits\n\nThanks to [Harshit Sidhwa](https://github.com/harshitsidhwa?tab=repositories) in providing back tracking [code](https://repl.it/@rui1337/backtrack-sudoku-solver) as an inspiration to solve this problem.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florransutter%2Fsudoku","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florransutter%2Fsudoku","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florransutter%2Fsudoku/lists"}