chickadee » exact-cover

exact-cover

Solver for exact cover problems based on Knuth's Algorithm X.

Description

An exact cover problem has a set of items and a list of options, where each option is a set of items. A solution is a choice of options such that every primary item lies in exactly one chosen option and every secondary item in at most one. Sudoku, the n queens problem, polyomino tilings and Latin squares all take this form.

Design

The search picks an uncovered primary item contained in the fewest available options, branches on each of those options, and in each branch removes the options that share an item with the chosen one. An item with one option is therefore covered at once (a forced move), and an item with none ends the branch.

Instead of the dancing-links data structure, which unlinks and relinks options in place, the search works on immutable states, following the design of the Haskell library set-cover by Henning Thielemann.

A state holds the set of available options, the set of uncovered primary items, the number of available options containing each item, and the labels of the chosen options. Choosing an option returns a new state and leaves the old one intact, so that:

Item sets and option sets are bit sets (vectors of fixnum words). Each option carries the set of options it conflicts with, computed once per problem, so a choice is one set difference plus an update of the counts of the items of the removed options.

Requirements

Library procedures

Problems and states

make-exact-cover OPTIONS #!key primary branch-itemsprocedure

Returns the initial search state of an exact cover problem.

OPTIONS is a list of (LABEL ITEM ...): a label (any value, distinct under equal?) and one or more items (non-negative integers).

Items below primary must be covered exactly once, the others at most once. The default is that all items are primary.

branch-items is a list of primary items to branch on in preference to the others.

exact-cover-state? Xprocedure

Returns #t if X is a search state.

exact-cover-choose STATE LABELprocedure

Returns the state after choosing the option labelled LABEL, or #f if that option conflicts with an option already chosen. STATE itself is unchanged. Signals an error if there is no option with this label.

exact-cover-choose* STATE LABELSprocedure

Chooses the options labelled LABELS in turn. Returns the resulting state, or #f if one of them cannot be chosen.

exact-cover-branch STATEprocedure

Returns two values: the item the search would branch on (#f when the state is complete) and the labels of its options (empty at a dead end).

exact-cover-step STATEprocedure

Returns the list of child states of STATE.

exact-cover-chosen STATEprocedure

Returns the labels of the chosen options, in the order they were chosen.

exact-cover-uncovered STATEprocedure

Returns the primary items not yet covered, in increasing order.

exact-cover-available STATEprocedure

Returns the labels of the options that can still be chosen, in their original order.

exact-cover-complete? STATEprocedure

Returns #t when every primary item is covered, so that the chosen options form a solution.

exact-cover-item-count STATEprocedure

Returns the number of items of the problem (primary and secondary).

exact-cover-option-count STATEprocedure

Returns the number of options still available in STATE.

Search

exact-cover-fold STATE KONS KNIL #!key limit shuffleprocedure

Folds (KONS labels acc) over the solutions, starting from KNIL. The labels of each solution are in the order chosen. The search stops after limit solutions.

shuffle, a procedure reordering a list, sets the order in which the options of each branch are tried (for example a random order).

Returns two values: the result and the search statistics.

exact-cover-solutions STATE #!key limit shuffleprocedure

Returns two values: the list of solutions and the search statistics.

exact-cover-count STATE #!key limitprocedure

Returns two values: the number of solutions and the search statistics.

Search statistics

The statistics count the states visited, the states entered through a choice among two or more options, and the options removed by the choices. They serve as measures of difficulty.

search-stats? Xprocedure

Returns #t if X is a search statistics record.

search-stats-nodes STATSprocedure

The number of states visited.

search-stats-guesses STATSprocedure

The number of states entered through a choice among two or more options.

search-stats-updates STATSprocedure

The number of options removed from the available sets by the choices made.

search-stats->alist STATSprocedure

Returns the statistics as an association list with the keys nodes, guesses and updates.

Examples

(import exact-cover (srfi 1))

;; Knuth's example: items A .. G are 0 .. 6.
(define st (make-exact-cover '((1 2 4 5) (2 0 3 6) (3 1 2 5)
                               (4 0 3) (5 1 6) (6 3 4 6))))
(exact-cover-solutions st)     ; => ((4 1 5)), stats

;; Eight queens: rows and columns primary, diagonals secondary.
(define (queens n)
  (make-exact-cover
   (append-map (lambda (r)
                 (map (lambda (c)
                        (list (cons r c) r (+ n c) (+ (* 2 n) r c)
                              (+ (* 4 n) -1 r (- c) n -1)))
                      (iota n)))
               (iota n))
   primary: (* 2 n)))
(exact-cover-count (queens 8))  ; => 92, stats

Graph grid layout

The file examples/graph-grid-layout.scm in the source repository draws a small graph in three stages: a force-directed layout (Fruchterman-Reingold), then two exact cover problems.

The result is printed as text and checked against these rules.

csi -s examples/graph-grid-layout.scm

Repository

https://github.com/iraikov/exact-cover

Author

Ivan Raikov

Version history

1.0
Initial release

License

LGPL-3

Contents »