2

I'm writing a sudoku solver and thinking about an algorithm to implement it in. I know backtracking has a time complexity of O(n^m) where n is the number of possibilities for each square and m is the number of spaces that are blank. But I couldn't get an exact analysis on dancing links. Can someone explain what it is?

OmG
  • 18,337
  • 10
  • 57
  • 90
user3666471
  • 907
  • 11
  • 24
  • Too broad. Read the rules. – JK. Jun 09 '16 at 02:56
  • 1
    "What is the big-O complexity of a specific algorithm" seems like a reasonably narrow and acceptable question to me. I don't know the answer, though! – librik Jun 09 '16 at 03:02
  • 2
    Dancing links implements depth-first backtracking (usually with prioritization of which square to explore next based on the number of legal choices for that square). The interesting part of dancing links is the way it manages the constraints and search space, but that doesn't fundamentally make it different from "backtracking". – Paul Hankin Jun 09 '16 at 04:33

1 Answers1

0

Dancing Links (Algorithm X) designed my Donald Knuth is also worse case O(N^N^2) kinda. It is in reality much less than this.

I figured this out when I wrote two sudoku solvers, one with and one without Dancing Links.

If you introduce forward checking (check to make sure the number is a valid play before you attempt to continue your depth first search (This is also known as pruning in the searching world) then you can save your algorithm from wasting time. If this is the only improvement you do then you could still hit the worse case scenario (ie. first number is the max number).

Dancing Links, if you would like to think of it like this Dancing Links is a Forward Checking - Priority Queue - Depth First Search. The real speed comes from the cover grid This is way that you don't need to recalculate what possibilities are left (if you implement it correctly), which will be the more time consuming process of your sudoku solver. Also, you can set up the algorithm to pick the (spot, row, column, or box) with the least possibilities left to reduce on branching size.

Here are a few links that really helped me make my solver:

https://www.ocf.berkeley.edu/~jchu/publicportal/sudoku/0011047.pdf https://www.ocf.berkeley.edu/~jchu/publicportal/sudoku/sudoku.paper.html

The Big O complexity of this problem would still be O(n * n * n) since we are still going from spot to spot and trying up to n values for that spot. It just happens that the Ω(x) where x is the number of empty spaces for both implementations, but the computation time per step is a lot less for dancing links.

This looks like Dancing Links is just a DFS implementation, since it is. The worse case for a 4 by 4 would be 4 * 3 * 2 * 3 * 2 * 2 * 2 * 2 * 2 Which is due to that column Heuristic where we pick the column of the cover grid with the least number of ones to prevent massive branching.

DRSisco
  • 31
  • 4