Wiki
Download
Manual
Eggs
API
Tests
Bugs
show
edit
You can edit this page using
wiki syntax
for markup.
Article contents:
[[tags: egg]] == exact-cover Solver for exact cover problems based on Knuth's Algorithm X. [[toc:]] === 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 [[https://hackage.haskell.org/package/set-cover|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: * nothing is undone on backtracking; * a state can be searched more than once, or explored one step at a time; * partial assignments (such as the givens of a puzzle) are made by choosing options before the search. 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 * [[srfi-1]] * [[srfi-69]] * [[datatype]] * [[yasos]] === Library procedures ==== Problems and states <procedure>(make-exact-cover OPTIONS #!key primary branch-items)</procedure> 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)</procedure> Returns {{#t}} if {{X}} is a search state. <procedure>(exact-cover-choose STATE LABEL)</procedure> 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)</procedure> 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)</procedure> 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)</procedure> Returns the list of child states of {{STATE}}. <procedure>(exact-cover-chosen STATE)</procedure> Returns the labels of the chosen options, in the order they were chosen. <procedure>(exact-cover-uncovered STATE)</procedure> Returns the primary items not yet covered, in increasing order. <procedure>(exact-cover-available STATE)</procedure> Returns the labels of the options that can still be chosen, in their original order. <procedure>(exact-cover-complete? STATE)</procedure> Returns {{#t}} when every primary item is covered, so that the chosen options form a solution. <procedure>(exact-cover-item-count STATE)</procedure> Returns the number of items of the problem (primary and secondary). <procedure>(exact-cover-option-count STATE)</procedure> Returns the number of options still available in {{STATE}}. ==== Search <procedure>(exact-cover-fold STATE KONS KNIL #!key limit shuffle)</procedure> 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)</procedure> Returns two values: the list of solutions and the search statistics. <procedure>(exact-cover-count STATE #!key limit)</procedure> 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)</procedure> Returns {{#t}} if {{X}} is a search statistics record. <procedure>(search-stats-nodes STATS)</procedure> The number of states visited. <procedure>(search-stats-guesses STATS)</procedure> The number of states entered through a choice among two or more options. <procedure>(search-stats-updates STATS)</procedure> The number of options removed from the available sets by the choices made. <procedure>(search-stats->alist STATS)</procedure> Returns the statistics as an association list with the keys {{nodes}}, {{guesses}} and {{updates}}. === Examples <enscript highlight="scheme"> (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 </enscript> ==== 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. * Grid snapping: each node moves to one of its k nearest lattice points, no two nodes to the same point (nodes primary, lattice points secondary); k grows until a placement exists, and the placement with the least squared displacement is kept. * Orthogonal edge routing: each edge chooses one of its candidate grid paths (bounded detour and number of bends) so that no two edges share a grid segment or bend at the same point (edges primary, segments and bend points secondary); the limits grow until a routing exists, and the routing with the least length plus bends is kept. 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|https://github.com/iraikov/exact-cover]] === Author Ivan Raikov === Version history ; 1.0 : Initial release === License LGPL-3
Description of your changes:
I would like to authenticate
Authentication
Username:
Password:
Spam control
What do you get when you subtract 19 from 15?