Sudoku — A Logical Puzzle

Swapnil agrawal
3 min readFeb 9, 2021

Hello there! we all have heard about a very famous logical puzzle called “Sudoku”. Today i am going to tell you about this puzzle, one of the way for solving this puzzle and on which platform you can solve it. So let’s start.

So Basically Sudoku is a logical puzzle that is represented by a 9x9 Grid, and we have to fill a number range from 1 to 9 in each and every cell, but there is some rule for filling the numbers.

  1. Fill the number in a cell such that no other cell contains that number in the same row as well as the same column.
  2. Fill the number in a cell such that Every one of the nine 3x3 blocks contains all numbers from 1 to 9.

These are the two rules we have to follow and solve the sudoku puzzle. This puzzle is a very famous puzzle based on Propositional Logic in Discrete Mathematics.

Now Question comes in mind that how to solve this puzzle?

So For Solving this question one of the techniques used in programming is called backtracking. Backtracking is basically a recursive way of solving any problem, but it is different from recursion. So firstly you will have to know about backtracking.

In General, if I tell you the idea for solving this puzzle is that there are a total of 9x9 = 81 cells. You just try each and every cell (which are empty) and put all numbers between 1 to 9 in that cell and check whether it is valid or not to put the number in that cell, if valid then put the number in the cell otherwise find some another number, try this approach for each and every cell.

Let me show you the mathematical steps for solving this puzzle which I found in the Discrete Mathematics book written by Kenneth H. Rosen. Below is the picture of that constraint and you can also check it in the Book. (on page 53 of pdf). (Link is provided.)

Steps for solving Sudoku puzzle written in Discrete Mathematics book written by Keneth H. Rosen

So In above picture Mathematical Form is given For Solving Sudoku Puzzle. Let me Tell you all the steps in simple terms.

Steps:

  1. Check First Empty Location in Given Grid. (in the above picture point 1)
  2. If You do not find any empty cell then, no need to go further, all cell in the grid is filled with valid numbers.
  3. Now start looping from numbers 1 to 9 in each cell and the number which satisfies fill that number.

3. a. Take the number and check if that number is already in the same row or not. (in the above picture point 2.)

3. b. Take the number and check if that number is already in the same column or not. (in the above picture point 3).

3.c. Take the number then check if that number is already in the same grid of 3x3.(in the above picture point 4).

4. Now the last point in the above picture is saying that if all the above conditions (3.a, 3.b,3.c) satisfies that number is not present in the same row, the same column, and the same grid then we assign that number to that cell and go for next empty cell.

5. If somewhere our previous filled cell is not filled with the valid number then remove the number from that cell and again backtrack for another number.

These all are the basic steps to solve a Sudoku puzzle, the above Algorithm take exponetial time to solve sudoku puzzle, but you can further optimize this. I can’t provide my code here for this puzzle, you just solve it with given steps.

For getting more about this puzzle you can follow the below resources.

  1. Discrete Mathematics Book, written by Keneth H. Rosen.
  2. MIT Lecture on Sudoku Puzzle. (here you can find more optimize way to solve this puzzle)
  3. For Solving this puzzle, You can go on Leetcode.

Thank you all. If you find something wrong in this article. Please tell me.

--

--

Swapnil agrawal

A passionate Computer programmer, making my path towars development field. You can follow me here https://bitmoreengineering.hashnode.dev/