5

I've found here a statement that Algorithm X for sudoku has O(N^3) time complexity where N is a board size.

That's maybe logical, since for sudoku the binary matrix to compute has N^3 rows. But that makes sudoku problem solvable in a polynomial time, and sudoku is known to be NP problem, that means (as I understand it)

  • not possible to always solve in a polynomial time

  • possible to verify a solution in a polynomial time

So what is the time complexity of Algorithm X for sudoku, and is it possible to solve a sudoku in a polynomial time or not ?

Thank you!

tkrishtop
  • 734
  • 1
  • 8
  • 18

2 Answers2

8

Mathematics of Sudoku explains this pretty well:

The general problem of solving Sudoku puzzles on n^2×n^2 grids of n×n blocks is known to be NP-complete.

The runtime complexity of any algorithm solving Sudoku is thus at least exponential in n. For a normal Sudoku (n = 3) this means O(N^3) is perfectly reasonable.

SaiBot
  • 3,595
  • 1
  • 13
  • 19
  • If `n` is 3, what does `N` represent? And why isn't it a constant as well? – rici Jan 14 '19 at 16:51
  • 1
    @rici, there are 2 constants here: `n = 3` is a size of block, and `N = n^2 = 9` is a size of row/column. Even if it is a constant, people are still used to speak about time complexity in terms like "O(N^3) where N = 9". [For example, in this article](http://www.enggjournals.com/ijet/docs/IJET17-09-03-043.pdf) – tkrishtop Jan 14 '19 at 17:08
  • 3
    @tkrishtop: sometimes people use terms incorrectly. Sometimes these errors are innocuous, other times (as in the cited article) they make the text meaningless. In no case should they be emulated. The claim in that paper, to the extent that it is meaningful, is wrong or misleading. Dancing links has many virtues but it does not make polynomial-time algorithms out of exponential-time algorithms. – rici Jan 14 '19 at 20:57
  • @rici sorry, I didn’t get your point. Do you want just to mention that “O(N^3) where N = 9” is not elegant and better to say “9^3 operations” ? Or you’re not agree that it’s 9^3 ? – tkrishtop Jan 16 '19 at 10:41
  • 1
    @tkrishtop: it is certainly not 9^3 operations, for any meaningful definition of "operation". So there is that. The statement that DLX takes cubic time is just plain wrong, and attempting to say that it applies to some fixed `n` shows a complete lack of understanding of what Big-O notation means. Forget that paper and read a good textbook. – rici Jan 16 '19 at 12:23
  • @rici, so what is DLX time complexity equal to? – tkrishtop Jan 16 '19 at 21:38
  • @tkrishtop: as this answer says, it is worst-case exponential. I don't believe a tight-limit (θ()) is known; in theory DLX could be O(2^N) where N is the number of sets, but I think Sudoku can be solved with a smaller (though still exponential) time-sensitive complexity – rici Jan 17 '19 at 21:57
  • A standard, 9x9 sudoku, is *not* NP-complete. Only a NxN one. So O(N^3) seems reasonable. We could try to generalise this to NxN, (here lowercase n means the size of a block), this would result in O((n*n)^n) or more elegantly O(n^(2n)). This is still exponential/nonpolynomial – Misty Jun 20 '21 at 16:55
-1

For a complete analysis of running time see: https://11011110.github.io/blog/2008/01/10/analyzing-algorithm-x.html

There it is stated that

even the stupidest version of Algorithm X, one that avoids any chance to backtrack and always chooses its pivots in such a way as to maximize its running time, takes at most O(3n/3)

which places the algorithm in exponential running time (EXPTIME).

Brian Minton
  • 3,377
  • 3
  • 35
  • 41