R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

I am intrigued to solving Sudoku puzzles, and of course there is the hard way of analyzing each cell, then the grid, etc. But I am sure there must be a math to solving such a puzzle. Does anyone know about it. Thank u.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here is one of the classic essays on solving Soduku puzzles.

http://norvig.com/sudoku.html

http://norvig.com/sudoku.html

The general problem is NP-complete, but Dancing Links is fast for 9x9

There is going to be many different mathematical ways to get a sudoku solution.

The one you may first try to set up would be to formalise the human solution technique of listing for each cell all the current possible values that aren't ruled out by horizontal, vertical or sub 3x3 grids. Then go through and actually assign each cell that has only one possibility and repeat iteratively. The rules for determining what is possible needs to include more than just horizontal, vertical and 3x3 cell remainders. For example if any 2 cells have the same two choices, then these values must be removed as a choice for other cells in the corresponding horizontal, vertical and 3x3 grid. The same applies for 3 cells that have the same 3 choices, etc.

However I imagine that that "procedure" is not what you are thinking about.

Another method would involve setting up a system of equations and solving them.

To explain first check out linear programming http://en.wikipedia.org/wi

Using integer programming it is possible to specify any suduko problem. The easiest formulation will be a big problem - 243 equations with 324 variables - less 9 variables for each of the given numbers already in the shown problem. Then you can derive a solution to it using the integer programming techniques.

Such a formulation would be -

for each of the 81 individual grid positions establish 9 variables. All there variables are restricted to integer values with a minimum value of 0 and a maximum value of 1. (That is they all are what is termed 0/1 binary variables). Number the grid positions (1, 1) up to (9, 9) where (i, j) denotes the grid for row i, column j where both i and j range from 1 - 9. I will call the variable created Xijk. The first 9 variables X11k are all concerned with grid position (1, 1), X111 is 1 if and only if grid(1, 1) has 1 as its value when solved. If no then its value will be 0. likewise X112 is 1 if grid (1, 1) has a 2 as its value when solved, 0 otherwise.

In general Xijk is 1 if grid position (i, j) has "k" as the solution value, 0 otherwise.

The to specify the equations, notice that each grid position can only have one number in it, so there will be 81 equations of the form

Xij1 + Xij2 + Xij3 + ... + Xij9 = 1

Or compactly Sum (over the 1-9 k values) Xijk = 1.

Those 81 equations ensure that the solution has exactly 1 value in each cell.

Now for each row (and also each column and each 3x3 sub grid) we must have exactly one "1", exactly one "2", ... exactly one "9".

X111 + X121 + X131 + ... X191 = 1

or Sum(j=1..9) X1j1 = 1

and in general for each of the 9 digits for each of the 9 rows we have 81 equations

Sum (j=1 ..9) Xijk = 1

and in general for each of the 9 digits for each of the 9 columns we have 81 equations

Sum (i=1 ..9) Xijk = 1

and in general for each of the 9 digits for each of the 3x3 sub grids 81 we have 81 equations

Sum (i,j range over the 9 values in 3x3 grid) Xijk = 1

====

The initial clues can either be put in as additional equations like

Eg X264 = 1 being In row 2 column 6 there is a 4. This will be conceptually the easiest way to go, but to improve efficiency it would be possible to remove the columns from the above formulation corresponding to disallowed values. In the example we would remove X261, X262, X263 (leave for the time being X264) and remove X265, X266, ...X269. Next for all equations that mention X264, the value of X264 is given as 1 so the X264 variable can be removed as long as we also subtract 1 from the right hand side of each equation that it is in.

====

This method will set up the equations that specify the suduko problem and will lead to its solution.

=====

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialYou can use schemes like I specified above to describe the sudoku problem and then determine the solution by solving the associated problem with integer programming. Such a scheme can exactly specify the problem so that solving the suduko is equivalent to solving the associated integer programming problem.

With further careful study I'm sure that it may be possible to reduce the size of the associated system of equations, but hey with the mathematics it is not the quick solving but the ability to solve that is interesting.

In fact if you wanted to turn the whole problem on its head, given an integer programming system that has the form given above, a solution to the integer programming set of equations could be done using a compact 9x9 sudoku grid!!

If compute power is of no concern; if you can set up the matrix corresponding to the system of equations; if you have access to integer programming codes then go ahead.

From the integer programming processing you should be able to determine one of -

1/ There is no feasiable solution

2/ There is a feasiable solution (and what that solution was).

However if there was more than one feasiable solution then most integer programming codes would not tell you that another solution existed and was possible.

These results and the speed of doing so is dependent on the integer programming code, though the size of the problem is specified up front and hence this is not a problem that will grow without bounds.

If you know beforehand that there is (at least one) feasiable solution then the code should be able to make use of that fact in its method to determine the first feasiable solution (by minimising the infeasibilities).

Math / Science

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

You can automate the sort of analysis you use to solve a Soduku manually in a fairly simple computer program.