2

I was wondering what the Big-O of a simple brute force backtracking Sudoku solving algorithm was.

Sudoku has 4 constraints:

  1. cell - one cell can contain one number max
  2. region - numbers in the region must be all different
  3. row - numbers on the same row must be all different
  4. column - numbers on the same column must be all different

Given the 9x9 grid:

3|___|___________
_|_ _|_ _ _ _ _ _
_|___|_ _ _ _ _ _
_|_ _ _ _ _ _ _ _
_|_ _ _ _ _ _ _ _
_|_ _ _ _ _ _ _ _
_|_ _ _ _ _ _ _ _
_|_ _ _ _ _ _ _ _
_|_ _ _ _ _ _ _ _

I think the row, column and region constraints are all O(n!) since (n)(n-1)(n-2)(n-3)(n-4)(n-5)(n-6)(n-7)(n-8) for each row, column and region as they get filled up.

But given that there must be a minimum of 17 given solutions for a unique 9x9 Sudoku solution to exist, I'm not sure now. The number of permutations are O(n^(n^2 - k)) where k = 17 for pure brute force but this does not include constraint satisfaction, which I'm sure is either exponential O(c^n) or factorial O(n!) at least.

So the question again is what is the Big-O of sudoku using brute force backtracking with constraint satisfaction and why? O(log n!)?

Tai
  • 101
  • 1
  • 1
  • 10

1 Answers1

0

Although this answer might considered a bit picky, the complexity is O(1); as Sudoku is played on a grid of fixed size of 9*9 cells and each cell admits exactly one of 9 different states, namely an assignment of one of the values in {1,...,9}, using a brute force approach to extend a given partial assignment takes constant time. Otherwise, it would have to be clarified what exactly the n mentioned in the question would be.

That being said, if n is supposed to be the board size, i.e. there are n*n,m is the number of possible states per cell, b1 is the number of regions and b2 is the region size (i.e. each of the b1 regions has b2*b2 cells) the runtime complexity can be roughly estimated by the following reasoning. There are m^(n*n) states of the board; checking if a state of the board is legal with respect to the sudou rules takes (n^2)*b1*(b2^2) operations, namely checking uniqueness of each cell state in each row, column and block. In total, a feasible assignment can be determied

O(m^{n*n}*(n^2)*b1*(b2^2))

steps.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • 2
    Note: there are 4*4 and 16*16 sudokus. – joop Oct 18 '16 at 10:15
  • n is the dimensions of the Sudoku board e.g. 4x4, 9x9, 16x16, etc. – Tai Oct 18 '16 at 10:20
  • @Codor that's not right because O(m^(n*n)) would be the total number of permutations the board can have so anything over that would be wrong. – Tai Oct 19 '16 at 12:36
  • @Codor perhaps it's O(m^(n/c)) where c is the row, column and regional constraints – Tai Oct 19 '16 at 12:47