Sudoku Solver

Introduction

A Sudoku puzzle is typically a grid that contains 81 cells, with nine rows and nine columns. The grid can be divided into subgrids or locals grids of three by three (total of nine cells). The goal is to fill all the empty cells from 1 to 9 so that no number is repeated in either a single column, row or subgrid.

In the code below a backtracking recursive algorithm is used to solve sudoku puzzles. The overall premise how this works is as follows: The first empty cell is filled with a number 1 if this choice is compatible with all the existing number then it moves on to the second empty cell, where it places a 1. If a conflict is encounterd then the 1 is earesed and a 2 is palced, after placing the the first "allowed" number in that cell it moves to the next empty cell and starts again. It can be possible that the program backtracks several times before being able to move on (DELAHAYE, 2006).

Backtracking Recursion Solver

When running the sudoku the following code is exceted first:

This function takes the initial state with the clues and makes a copy (to not change the origional) and then calles the solve function which executes the sudoku solver. If it checks that either the ouput of the solver is allowed or that it is not the same as the origional state (i.e. no valid solution is avialible) then it outputs a 9 by 9 grid of -1. Otherwise it outputs the solution.

The is_allowed function below, in this case is used to verify that the initial solution is an allowed problem i.e. ensuring that no number is repeated in either its row, column or local grid.

Then the actual solve function below, is used to solve the sudoku puzzle. It calls a search function that returns two possible results it either finds an empty cell and returns that index or if the entire grid has been filled then nothing is return if that is the case the loop ends and the puzzle has been solved.

In the solve function it calls two functions the search and the verfiy functions below:

The search function simply searches for an empty cell and returns the index of that cell. The indexes given by the seach function are then used in the verfiy function which iterates i (the proposed number) from 1 to 9 to verify if that number is a valid solution.

The if solve(grid) part is the recursive section of the algorithm a brute force method that will run continuously until the puzzle is solved. Each time the function is called it adds a number however, sometimes before the solution is reached the algorithm may add incorrect numbers to the grid.

In this case the backtracking takes place and then it runs into an issue since at one point all the proposed values of i are not valid solutions. In this case, it returns false. If a false is returned it exits out and goes back to the last value that was changed and then runs an iteration on the next value of i. The grid[row][col] = 0 resets the index, so when a "dead end" is reached it will backtrack and attempt to resolve any incorrect values it previously entered. The repeat process of this will allow it to eventually provide a valid solution to the Sudoku puzzle.

Refrences

DELAHAYE, J., 2006. The Science Behind Sudoku. [online] Cs.virginia.edu. Available at: http://www.cs.virginia.edu/~robins/The_Science_Behind_SudoKu.pdf [Accessed 14 May 2021].