|
ADS
Algorithms and Data Structures
|
the exact cover of the primary items, with the secondary ones at most once, on dancing links More...
#include <ADS/backtrack/DancingLinks.h>
Public member functions | |
| ExactCover (int primary, int secondary=0) | |
| the items \( 0 , \ldots , \mathit{primary} - 1 \) primary, the next \( \mathit{secondary} \) ones secondary, and no option | |
| int | addOption (const std::vector< int > &items) |
| a new option, made of items, and its index; a node at the bottom of the list of each of its items | |
| template<class F> | |
| long | solve (F &&visit) |
| calls visit( options ) on each solution, the indices of its options, until it returns true; the number of solutions visited | |
| long | updates (void) const |
| the nodes updated by the last solve(), Knuth's measure of the work | |
the exact cover of the primary items, with the secondary ones at most once, on dancing links
| ADS::ExactCover::ExactCover | ( | int | primary, |
| int | secondary = 0 ) |
the items \( 0 , \ldots , \mathit{primary} - 1 \) primary, the next \( \mathit{secondary} \) ones secondary, and no option
| int ADS::ExactCover::addOption | ( | const std::vector< int > & | items | ) |
a new option, made of items, and its index; a node at the bottom of the list of each of its items
| long ADS::ExactCover::solve | ( | F && | visit | ) |
calls visit( options ) on each solution, the indices of its options, until it returns true; the number of solutions visited
In pseudocode (Knuth's Algorithm X on dancing links, beyond the book), \( \mathit{len}( i ) \) the options of the item \( i \) left and \( \mathit{top}( p ) \) the item of the node \( p \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{search}() \cr 2 & \quad \textbf{if}\ \text{no primary item is left }\ \textbf{then} \ \mathrm{visit}( \mathit{chosen} );\ \textbf{return} \cr 3 & \quad i \gets \text{a primary item of least } \mathit{len}( i )\quad \triangleright\ \text{the MRV heuristic} \cr 4 & \quad \textbf{if}\ \mathit{len}( i ) = 0\ \textbf{then return} \cr 5 & \quad \mathrm{cover}( i ) \cr 6 & \quad \textbf{foreach}\ x \text{ in the list of } i \text{, top} \text{ down}\ \textbf{do} \cr 7 & \qquad \text{append the option of } x \text{ to } \mathit{chosen} \cr 8 & \qquad \textbf{foreach}\ p \ne x \text{ in the option of } x \ \textbf{do}\ \mathrm{cover}( \mathit{top}( p ) ) \cr 9 & \qquad \mathrm{search}() \cr 10 & \qquad \textbf{foreach}\ p \ne x \text{ in the option of } x \text{, in the reverse order}\ \textbf{do}\ \mathrm{uncover}( \mathit{top}( p ) ) \cr 11 & \qquad \text{remove the last option of } \mathit{chosen} \cr 12 & \quad \mathrm{uncover}( i ) \cr 13 & \textbf{procedure}\ \mathrm{cover}( i ) \cr 14 & \quad \textbf{foreach}\ p \text{ in the list of } i \text{, top} \text{ down}\ \textbf{do} \cr 15 & \qquad \textbf{foreach}\ q \ne p \text{ in the option of } p \ \textbf{do}\quad \triangleright\ \text{the option hidden} \cr 16 & \qquad\quad U[ D[ q ] ] \gets U[ q ];\ D[ U[ q ] ] \gets D[ q ]; \ \mathit{len}( \mathit{top}( q ) ) \gets \mathit{len}( \mathit{top}( q ) ) - 1 \cr 17 & \quad L[ R[ i ] ] \gets L[ i ];\ R[ L[ i ] ] \gets R[ i ]\quad \triangleright\ i \text{ off the list of the items} \cr 18 & \textbf{procedure}\ \mathrm{uncover}( i ) \cr 19 & \quad L[ R[ i ] ] \gets i;\ R[ L[ i ] ] \gets i \cr 20 & \quad \textbf{foreach}\ p \text{ in the list of } i \text{, bottom} \text{ up}\ \textbf{do} \cr 21 & \qquad \textbf{foreach}\ q \ne p \text{ in the option of } p \text{, in the reverse order}\ \textbf{do} \cr 22 & \qquad\quad U[ D[ q ] ] \gets q;\ D[ U[ q ] ] \gets q; \ \mathit{len}( \mathit{top}( q ) ) \gets \mathit{len}( \mathit{top}( q ) ) + 1 \end{array}} \]
The search stops as soon as visit returns true.
|
inlinenodiscard |
the nodes updated by the last solve(), Knuth's measure of the work