A Go library for exact cover problems. It uses Knuth's dancing cells, which are
based on sparse sets, instead of dancing links. Its API is that of
sjnam/dlx.
go get github.com/sjnam/dancing-cellspackage main
import (
"fmt"
"strings"
cells "github.com/sjnam/dancing-cells"
)
func main() {
input := `a b c d e f g
c e
a d g
b c f
a d f
b g
d e g
`
res := cells.NewXCC().Dance(strings.NewReader(input))
for sol := range res.Solutions {
fmt.Println(sol) // [[a d f] [c e] [b g]]
}
}The first line of the input names the items; each line after it is an option.
- Items after a
|on the first line are secondary. They are covered at most once, except that options giving one the same color, as inname:color, can be used together. - A line that begins with
|is a comment.
To stop before all solutions have arrived, pass a context with WithContext
and cancel it.
| Engine | Use it for | Source |
|---|---|---|
NewXCC() |
ordinary exact covers | ssxcc.gw |
NewMCC() |
items that must be covered several times | ssmcc.gw |
NewXCCDC() |
searches with too many nodes | xccdc.gw |
zdd.New() |
too many solutions to list | zdd/zdd.gw |
With NewMCC, put the number of times before the item on the first line: 2|a
for exactly twice, 1:3|a for one to three times.
NewXCCDC maintains domain consistency at every node, discarding hopeless
options early. There are fewer nodes, but each costs more, so whether it pays
depends on the problem.
| Problem | NewXCC nodes |
NewXCCDC nodes |
Time with NewXCCDC |
|---|---|---|---|
| 15×15 fillomino | 133,639 | 82 | about 200 times faster |
| 8×8 pentominoes | 93,833 | 12,295 | 6 to 7 times slower |
The zdd package returns all solutions as one decision diagram (ZDD). It
counts the 258,584,046,368 domino tilings of a 10×10 board with 13,560 search
nodes. It does not help when subproblems seldom recur, as with pentominoes.
Only this package depends on sjnam/bdd.
d := zdd.New().Dance(strings.NewReader(input))
d.Count() // number of solutions
d.Random(rnd) // one solution, uniformly at random
d.MaxWeight(w) // the heaviest solutionNewXCC and NewMCC have Minimize. Give each option a cost, and it delivers
cheaper and cheaper covers; the last one is optimum.
res := cells.NewXCC().Minimize(strings.NewReader(input),
func(o int, opt cells.Option) int { return price(opt) })- Set
Bestto k to get the k cheapest covers. They are the k cheapest of those delivered. - Set
Boundto a function that gives a lower bound on the remaining cost, to prune earlier. - With
NewMCC, an option can have a negative cost only if it contains an item whose number of times is fixed.
The examples' .go files are not in the repository;
GWEB makes them. Run make -C examples first.
Their documents and messages are in Korean.
| Example | Run |
|---|---|
| N queens | go run ./examples/queen 8 |
| Langford pairs | go run ./examples/langford 4 |
| Pentominoes | go run ./examples/pentominoes examples/pentominoes/6x10.dlx |
| Sudoku | go run ./examples/sudoku examples/sudoku/puzzles.txt |
| Fillomino | go run ./examples/filomino examples/filomino/10x10.filomino.dlx |
| Zebra puzzle | go run ./examples/zebra |
| Word search | go run ./examples/wordsearch examples/wordsearch/mathematicians.txt 15 15 |
| Five words covering 24 letters | go run ./examples/words examples/words/sgb-words.txt 5 |
Partridge puzzle (NewMCC) |
go run ./examples/partridge 8 |
Domino tilings (zdd) |
go run ./examples/domino -aztec 8 |
Cheapest Latin square transversal (Minimize) |
go run ./examples/transversal -plain 11 |
Partridge puzzle with a hollow center (Minimize) |
go run ./examples/hollow -z 16 |
| Exact cover preprocessor | go run ./examples/dlx-pre < examples/pentominoes/6x10.dlx |
| Symmetric word cubes | cd examples/word-cube-dlx && go run . sgb-words.txt > wordcube.dlx |
Careful readings of exercises in TAOCP 7.2.2.1, checked by program, are in
taocp-7.2.2.1-exercises.
The engines and examples are literate programs, written in Korean. The .gw
files are the source; the .go files are generated from them, so don't edit
those by hand. Start with dcells.gw, which explains sparse sets.
make # make the .go files from the .gw files, and build
make pdf # typeset the documents