|
ADS
Algorithms and Data Structures
|
the exact cover with colors on sparse sets, the dancing cells More...
#include <ADS/backtrack/FastExactCover.h>
Public member functions | |
| FastExactCover (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, const std::vector< int > &colors={}) |
| a new option, made of items, and its index; colors[ k ] \( > 0 \) is the color of items[ k ], and 0, or no colors, is no color | |
| template<class F> | |
| long | solve (F &&visit) |
| calls visit( options ) on each solution, the indices of its options in the order chosen, until it returns true (if it returns bool); the number visited | |
| long | updates (void) const |
| the options taken out of a set by the last solve() | |
| long | nodes (void) const |
| the nodes of the tree of the search of the last solve() | |
the exact cover with colors on sparse sets, the dancing cells
| ADS::FastExactCover::FastExactCover | ( | int | primary, |
| int | secondary = 0 ) |
the items \( 0 , \ldots , \mathit{primary} - 1 \) primary, the next \( \mathit{secondary} \) ones secondary, and no option
| int ADS::FastExactCover::addOption | ( | const std::vector< int > & | items, |
| const std::vector< int > & | colors = {} ) |
a new option, made of items, and its index; colors[ k ] \( > 0 \) is the color of items[ k ], and 0, or no colors, is no color
|
inlinenodiscard |
the nodes of the tree of the search of the last solve()
| long ADS::FastExactCover::solve | ( | F && | visit | ) |
calls visit( options ) on each solution, the indices of its options in the order chosen, until it returns true (if it returns bool); the number visited
In pseudocode (Knuth's Algorithm C on sparse sets, beyond the book), recursive here while the code keeps the levels on a stack of its own:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{search}() \cr 2 & \quad \textbf{if}\ \text{a forced item } x \text{ is pending } \ \textbf{then}\ \text{branch on } x \text{ without saving the sizes} \cr 3 & \quad \textbf{if}\ \text{no primary item is active } \ \textbf{then}\ \mathrm{visit}( \mathit{chosen} );\ \textbf{return} \cr 4 & \quad x \gets \text{the active primary item of least } \mathrm{size}( x ) \text{, the least index on ties} \cr 5 & \quad \textbf{if}\ \mathrm{size}( x ) = 0\ \textbf{then return} \cr 6 & \quad \text{take } x \text{ out of the active items}; \ \mathrm{hide}( x , 0 )\quad \triangleright\ \text{without the tests} \text{ of lines 17 and 18} \cr 7 & \quad \text{push the sizes of the active items} \cr 8 & \quad \textbf{foreach}\ o \text{ among the first } \mathrm{size}( x ) \text{ of } \mathit{set}( x )\ \textbf{do} \cr 9 & \qquad \text{take the other items of } o \text{ out of the active} \text{ ones} \cr 10 & \qquad \textbf{if}\ \mathrm{hide}( c , \text{the color of } c \text{ in } o ) \text{ for each other item } c \text{ of } o \ \textbf{then} \cr 11 & \qquad\quad \text{append } o \text{ to } \mathit{chosen}; \ \mathrm{search}();\ \text{remove } o \text{ from } \mathit{chosen} \cr 12 & \qquad \text{write the sizes pushed back}\quad \triangleright \ \text{no swap undone} \cr 13 & \textbf{function}\ \mathrm{hide}( c , k ) \cr 14 & \quad \textbf{foreach}\ o \text{ among the first } \mathrm{size}( c ) \text{ of } \mathit{set}( c ) \text{, of a color other than } k \text{ if } k \ne 0\ \textbf{do} \cr 15 & \qquad \textbf{foreach}\ \text{active item } u \ne c \text{ of } o \ \textbf{do} \cr 16 & \qquad\quad \text{swap } o \text{ with the last active option of } \mathit{set}( u );\ \mathrm{size}( u ) \gets \mathrm{size}( u ) - 1 \cr 17 & \qquad\quad \textbf{if}\ u \text{ is primary and } \mathrm{size}( u ) = 0\ \textbf{then return false}\quad \triangleright \ \text{uncoverable} \cr 18 & \qquad\quad \textbf{if}\ u \text{ is primary and } \mathrm{size}( u ) = 1\ \textbf{then}\ \text{mark } u \text{ as forced} \cr 19 & \quad \textbf{return true} \end{array}} \]
|
inlinenodiscard |
the options taken out of a set by the last solve()