exact-cover

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

  1. exact-cover
    1. Description
    2. Design
    3. Requirements
    4. Library procedures
      1. Problems and states
      2. Search
      3. Search statistics
    5. Examples
      1. Graph grid layout
    6. Repository
    7. Author
    8. Version history
    9. License

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

[procedure] (make-exact-cover OPTIONS #!key primary branch-items)

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.

[procedure] (exact-cover-state? X)

Returns #t if X is a search state.

[procedure] (exact-cover-choose STATE LABEL)

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.

[procedure] (exact-cover-choose* STATE LABELS)

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

[procedure] (exact-cover-branch STATE)

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).

[procedure] (exact-cover-step STATE)

Returns the list of child states of STATE.

[procedure] (exact-cover-chosen STATE)

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

[procedure] (exact-cover-uncovered STATE)

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

[procedure] (exact-cover-available STATE)

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

[procedure] (exact-cover-complete? STATE)

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

[procedure] (exact-cover-item-count STATE)

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

[procedure] (exact-cover-option-count STATE)

Returns the number of options still available in STATE.

[procedure] (exact-cover-fold STATE KONS KNIL #!key limit shuffle)

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.

[procedure] (exact-cover-solutions STATE #!key limit shuffle)

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

[procedure] (exact-cover-count STATE #!key limit)

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.

[procedure] (search-stats? X)

Returns #t if X is a search statistics record.

[procedure] (search-stats-nodes STATS)

The number of states visited.

[procedure] (search-stats-guesses STATS)

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

[procedure] (search-stats-updates STATS)

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

[procedure] (search-stats->alist STATS)

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