|
ADS
Algorithms and Data Structures
|
The exact cover by the dancing links of Knuth (his Algorithm X), beyond the book [see Enumeration.h for the backtrack of chapter 16]: given the items and the options, each a set of items, find the sets of options that contain every primary item exactly once and every secondary item at most once. More...
#include "ADS/Contract.h"#include <algorithm>#include <cstddef>#include <utility>#include <vector>#include "ADS/backtrack/impl/DancingLinks.tpp"Go to the source code of this file.
Classes | |
| class | ADS::ExactCover |
| the exact cover of the primary items, with the secondary ones at most once, on dancing links More... | |
Namespaces | |
| namespace | ADS |
The exact cover by the dancing links of Knuth (his Algorithm X), beyond the book [see Enumeration.h for the backtrack of chapter 16]: given the items and the options, each a set of items, find the sets of options that contain every primary item exactly once and every secondary item at most once.
The items and the options are on circular doubly linked lists, a node per item of an option; covering an item takes it off the list of the items and the options that contain it off the lists of their other items, and since a removed node \( x \) keeps its own links, the uncovering puts it back by the two assignments \( L[ R[ x ] ] \gets x \), \( R[ L[ x ] ] \gets x \), in the reverse order. The search branches on the item with the fewest options left (the MRV heuristic), which detects the dead ends early.
Many puzzles are exact covers: the \( n \) queens (the rows and the columns primary, the diagonals secondary), the Sudoku (a cell, a row, a column and a box with each digit, 324 primary items), the pentominoes. The tests count the queens and solve the Sudoku of the backtrack chapter.
The contracts [see Contract.h]: the items of an option are distinct and in \( [ 0 , n ) \), and one of them is primary (an option of secondary items alone would never be chosen); every solution visited is an exact cover, an audit; the links after the search are as before it, an audit. The operations are defined in DancingLinks.tpp.