The Dancing Links Algorithm - An explanation that is less explanatory but more on implementation?
Asked Answered
F

2

33

I've been working on a Sudoku Solver, my current solver uses the backtracking algorithm but it still takes too long.

I'm hoping to get it down to less than a second for most cases. As such, I've decided to rewrite it with the dancing links algorithm, understanding it is one of the better bruteforce methods that works well especially with a constraint problem such as the Sudoku Puzzle.

I have tried to read the Wiki and Knuth's paper on it, however both of them are kinda hard to comprehend and extremely verbose.

I also read Sudopedia's version on it, and it seems that once it got to the Sudoku's implementation, it got too abstract.

Can someone try to explain the Dancing Links algorithm not in terms of its derivation but its implementation? (would be great to use the Sudoku as an example)

Thanks!

Fluffy answered 5/10, 2009 at 5:3 Comment(5)
Well then I will guess that this should help you: A Sudoku Solver in Java implementing Knuth's Dancing Links AlgorithmGsuit
See also: stackoverflow.com/questions/1518346Maddie
The full source code for this link is not available anymore. And the code snippets in there have some bugs in them. Specifically the uncover(ColumnNode c) method. In that article that code is a copy of the cover method with some stuff in the for loop changed. The inner loop's iterator needs to go left not right, and the links need to be restored to the original, not repeat the cover operation. Example: leftNode.getUp().setDown( leftNode); instead of leftNode.getUp().setDown( leftNode.getDown() );Doldrums
I created this Sudoku Solver Visualizer that implements Dancing Links and several other algorithms including Greedy Best First Search and Backtracking. Maybe you'll find it helpful. The code can be found here, although it is quite messy. I recommend checking out the visualizer only.Metaphysics
I've also written 2 methods for Sudoku 1 using Dancing Links and the other using Hidden Singles. github.com/Elementrix08/SudokuCouteau
D
34

You might be interested in my implementation in javascript.


Firstly you have to understand Exact Cover. An exact cover problem is a problem where you're given a bunch of choices, and a set of constraints and your challenge is to select a bunch of the choices that will fill every constraint exactly once.

For example, consider the case of someone creating their ice dance routine. They have a number of tricks that they need to show the judges, and don't want to perform any trick more than once. They have a number of sequences which are groups of tricks that can be put together and they want to choose the ideal selection of sequences to cover all the tricks once. In this example, the constraints are that they must perform every trick. The choices are the possible sequences they could incorporate into their routine.

A nice way to represent problems of this sort is to draw out a table where the constraints are columns and the choices are rows, and you have a big X in cells where a particular choice fulfills that constraint.

As it turns out, given the right constraints and choices, sudoku can be described as an Exact Cover problem.


Ok, assuming you've got that, now you need to understand Algorithm X. Knuth said of it "Algorithm X is simply a statement of the obvious trial-and-error approach. (Indeed, I can’t think of any other reasonable way to do the job, in general.)". So here's my description of algorithm X:

  1. If your table has no columns, stop - you've solved it. If you've got a partial solution stored, then it's actually a real solution, return it.
  2. Select a column (representing a constraint).
  3. Find a row with a cross in that column (representing a choice that fulfills that constraint). Add it to some kind of structure where you're storing potential solutions. If you can't find a row, give up - there are no solutions.
  4. Assume that the row you found in 3 is in the solution, so remove all columns that it have an X in that row. While removing all those columns, also remove all rows that have an X in the columns you're removing (because you've already satisfied the constraint, so you're barred from choosing something that would satisfy it again).
  5. Now recursively try to solve the reduced table. If you can't, remove the row you tried from the potential solution structure, restore all the rows and columns you removed in steps 3 and 4 and try a different row. If you run out of rows, then give up - there are no solutions.

Now that you understand that, you can understand dancing links. Dancing Links is a way of implementing that algorithm efficiently. The key point of dancing links is that in a linked list, when you remove a node (which can be done efficently by modifying the pointers of its neighbours), the node that you've removed has all the information you need to add it back to the linked list (in the case that it turns out you were wrong when you guessed it was part of the solution). That plus the fact that if you make all your linked lists circular then suddenly you lose a lot of special cases is pretty much all dancing-links is.

Deimos answered 8/5, 2012 at 22:29 Comment(1)
I wonder if you can take a look to the puzzle I'm trying to solve and tell me if is possible solve it with dancing links. At first look similar to the Polyonimos puzzle but you can have overlaps. So can this puzzle also be considered exact cover and be solved with dancing links?Aduwa
W
26

Although this question is very old, I thought I'd add:

This page makes the algorithm very easy to understand: Zendoku writeup. Until I read about it at that link, I had thought this must be a super advanced algorithm, but really once you can visualize it, its just a really ingenious but simple solution.

Also my implementation in C# should be fairly easy to read... would be helpful to split the various classes out to different files I'm sure.
It is largely a direct implementation from Knuth's pdf, but with a few object orientated optimizations (actually since I did this a few months ago I don't quite remember how much I strayed from the pdf)

Woolfell answered 18/4, 2013 at 3:44 Comment(4)
link to your c# implementation is dead. any chance you can share that? I'm trying to get my head around how to visualise the list in memory.Extricate
Probably late, but it's a double linked list in a toroidal structures: from any cell you can move up, down, left, right and when you reach an edge you jump to the opposite edgeOtti
Just to update, both links work fine for me, I'm pretty sure I verified they still worked when that first comment was made as well (my implementation a public github gist, so should be as stable as anything open source these days)Woolfell
I wonder if you can take a look to the puzzle I'm trying to solve and tell me if is possible solve it with dancing links. At first look similar to the Polyonimos puzzle but you can have overlaps. So can this puzzle also be considered exact cover and be solved with dancing links? – Juan Carlos Oropeza 9 mins agoAduwa

© 2022 - 2024 — McMap. All rights reserved.