views:

129

answers:

3

Hi,
I'm working on a sudoko solver (python). my method is using a game tree and explore possible permutations for each set of digits by DFS Algorithm.

in order to analyzing problem, i want to know what is the count of possible valid and invalid sudoko tables?

-> a 9*9 table that have 9 one, 9 two, ... , 9 nine.

(this isn't exact duplicate by this question)

my solution is:

1- First select 9 cells for 1s: (*)
alt text
2- and like (1) for other digits (each time, 9 cells will be deleted from remaining available cells): C(81-9,9) , C(81-9*2,9) .... =
alt text
3- finally multiply the result by 9! (permutation of 1s-2s-3s...-9s in (*))
alt text
this is not equal to accepted answer of this question but problems are equivalent. what did i do wrong?

+2  A: 

The number of valid Sudoku solution grids for the standard 9×9 grid was calculated by Bertram Felgenhauer and Frazer Jarvis in 2005 to be 6,670,903,752,021,072,936,960.

Mathematics of Sudoku | source

I think problem with your solution is that deleting 9 cells each time from available cells does not necessarily create a valid grid. What I mean is just deleting 9 cells won't suffice.

That is why 81! / (9!)^9 is much bigger number than actual valid solutions.

EDIT:

Permutations with repeated elements

Your solutions is almost correct if you want all the tables not just valid sudoku tables.

There is a formula:

(a+b+c+...)! / [a! b! c! ....]

Suppose there are 5 boys and 3 girls and we have 8 seats then number of different ways in which they can seat is

(5+3)! / (5! 3!)

Your problem is analogous to this one.

There are 9 1s , 9 2s ... 9 9s. and 81 places

so answer should be (9+9+...)! / (9!)^9

Now if you multiply again by 9! then this will add duplicate arrangements to the number by shuffling them.

TheMachineCharmer
@TheMachineCharmer: i don't want number of valid tables. do you think count of valid n*n sudoko tables filled by digits of n+1 radix is NP Hard? the solution you referred, solves problem by counting. (like 4-color) :-)
Sorush Rabiee
Your solution seems correct if you want all the tables not just valid ones :)
TheMachineCharmer
@ TheMachineCharmer : Hm... so is http://stackoverflow.com/questions/2512598/maths-question-number-of-different-permutations an incorrect answer?
Sorush Rabiee
No your answer should be 81!/(9!)^9 actually. I ll explain in a moment ;)
TheMachineCharmer
I can't see why are you multiplying by 9! ? could you explain please? It is not necessary.
TheMachineCharmer
Sorush Rabiee
I don't think counting number of valid n*n sudoku tables filed by digits of n+1 radix is NP Hard. Because we have analytical method of calculating number of valid tables without actually counting them one by one. See the links.
TheMachineCharmer
No! :) you have already accounted for that possibility of 9! modes while choosing 9 cell for each digit. Shuffling any more will only produce duplicates.
TheMachineCharmer
@TheMachineCharmer: Thanks a lot. :-)
Sorush Rabiee
+1  A: 

According to this Wikipedia article (or this OEIS sequence), there are roughly 6.6 * 10^21 different sudoku squares.

Vatine
+1  A: 

What you did wrong was the last step: you shouldn't multiply the answer by 9!. You have already counted all possible squares.

This doesn't help you much when counting the possible Sudoku-tables. One other thing you could do is to count the tables where the "row-condition" holds: that is just (9!)^9, because you just choose one permutation of 1..9 for every row.

Still closer to the Sudoku-problem is counting Latin squares. Latin square has to satisfy both the "row-condition" and "column condition". That is already a difficult problem and no closed form formula is known. Sudoku is a Latin square with the additional "subsquare-condition".

mattiast