ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Enumeration.h

The enumeration of the feasible solutions by backtrack (section 16.2). More...

Files

file  ADS/backtrack/Enumeration.h
 The enumeration of the feasible solutions by backtrack (section 16.2): a solution is a vector \( S[ 0 .. n - 1 ] \), each \( S[ i ] \) taken from the choices that \( S[ 0 .. i - 1 ] \) allows; each choice is tried, the solutions are processed, and the search goes on recursively from \( S[ 0 .. i ] \), i.e., a visit in depth of the tree of the choices.

Functions

template<class F>
long ADS::subsets (int n, int k, F &&f)
 the subsets of \( k \) of the items \( 0 , \ldots , n - 1 \), as vectors of \( n \) booleans, f( S ) on each and true from it to stop; the number visited
template<class T, class F>
long ADS::permutations (const std::vector< T > &A, F &&f)
 the permutations of the items of \( A \), f( S ) on each and true from it to stop; the items are told apart by their places, so that equal items give each permutation of the values more than once, \( n! \) in all
template<class T, class F>
long ADS::derangements (const std::vector< T > &A, F &&f)
 the derangements of the items of \( A \), no item in its own place, f( S ) on each and true from it to stop; the places are deranged, not the values, as in permutations()
template<class T, class F>
long ADS::subsetSum (const std::vector< T > &A, T k, F &&f)
 the subsets of the positive items of \( A \) of sum \( k \), as the places taken, f( S ) on each and true from it to stop; \( k = 0 \) gives the empty subset only, and equal items in different places give different subsets

Detailed description

The enumeration of the feasible solutions by backtrack (section 16.2).

The definitions, the invariants and the costs of the area are in Theory of backtracking.

Function documentation

◆ derangements()

template<class T, class F>
long ADS::derangements ( const std::vector< T > & A,
F && f )

the derangements of the items of \( A \), no item in its own place, f( S ) on each and true from it to stop; the places are deranged, not the values, as in permutations()

In pseudocode (exercise 16.3), the procedure of permutations() with the item of place \( |S| \) excluded from the place \( |S| \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{derangements}( A , S )\quad \triangleright\ \text{as } \mathrm{permutations} \text{, no item in its own place} \cr 2 & \quad \textbf{if}\ |S| = n\ \textbf{then return}\ f( S ) \cr 3 & \quad \textbf{foreach}\ c \in [ 0 , n ) \text{ not used and } \ne |S| \text{, in increasing order}\ \textbf{do} \cr 4 & \qquad \mathit{used}[ c ] \gets \textbf{true};\ \text{push } A[ c ] \text{ on } S \cr 5 & \qquad \textbf{if}\ \mathrm{derangements}( A , S ) \ \textbf{then return true} \cr 6 & \qquad \text{pop } S;\ \mathit{used}[ c ] \gets \textbf{false} \cr 7 & \quad \textbf{return false} \end{array}} \]

◆ permutations()

template<class T, class F>
long ADS::permutations ( const std::vector< T > & A,
F && f )

the permutations of the items of \( A \), f( S ) on each and true from it to stop; the items are told apart by their places, so that equal items give each permutation of the values more than once, \( n! \) in all

In pseudocode (exercise 16.2), \( S \) empty and no item used at the start:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{permutations}( A , S )\quad \triangleright\ S \text{ the prefix built, } \mathit{used} \text{ its places of } A \cr 2 & \quad \textbf{if}\ |S| = n\ \textbf{then return}\ f( S ) \cr 3 & \quad \textbf{foreach}\ c \in [ 0 , n ) \text{ not used, in increasing order}\ \textbf{do} \cr 4 & \qquad \mathit{used}[ c ] \gets \textbf{true};\ \text{push } A[ c ] \text{ on } S \cr 5 & \qquad \textbf{if}\ \mathrm{permutations}( A , S ) \ \textbf{then return true} \cr 6 & \qquad \text{pop } S;\ \mathit{used}[ c ] \gets \textbf{false}\quad \triangleright\ \text{undo, then the next choice} \cr 7 & \quad \textbf{return false} \end{array}} \]

◆ subsets()

template<class F>
long ADS::subsets ( int n,
int k,
F && f )

the subsets of \( k \) of the items \( 0 , \ldots , n - 1 \), as vectors of \( n \) booleans, f( S ) on each and true from it to stop; the number visited

In pseudocode (section 16.2), each item in or out of \( S \), the vector \( S[ 0 .. n - 1 ] \) false at the start:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{subsets}( S , i , c )\quad \triangleright \ c \text{ of the items before } i \text{ taken; true to stop} \cr 2 & \quad \textbf{if}\ c = k\ \textbf{then return}\ f( S ) \cr 3 & \quad \textbf{if}\ k - c > n - i\ \textbf{then return false}\quad \triangleright\ \text{too few items left, } i = n \text{ included} \cr 4 & \quad S[ i ] \gets \textbf{true};\ \textbf{if}\ \mathrm{subsets}( S , i + 1 , c + 1 )\ \textbf{then return true} \cr 5 & \quad S[ i ] \gets \textbf{false};\ \textbf{return} \ \mathrm{subsets}( S , i + 1 , c ) \end{array}} \]

Precondition
\( n \ge 0 \) and \( k \ge 0 \)

◆ subsetSum()

template<class T, class F>
long ADS::subsetSum ( const std::vector< T > & A,
T k,
F && f )

the subsets of the positive items of \( A \) of sum \( k \), as the places taken, f( S ) on each and true from it to stop; \( k = 0 \) gives the empty subset only, and equal items in different places give different subsets

In pseudocode (exercise 16.4): the items being positive, a sum that exceeds \( k \) cannot come back, and the branch is pruned.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{subsetSum}( A , S , i , s )\quad \triangleright\ s \text{ the sum of the places in } S \cr 2 & \quad \textbf{if}\ s = k\ \textbf{then return}\ f( S ) \cr 3 & \quad \textbf{foreach}\ j \in [ i , n ) \text{ with } s + A[ j ] \le k \text{, in increasing order}\ \textbf{do}\quad \triangleright \ \text{each subset once} \cr 4 & \qquad \text{push } j \text{ on } S;\ \textbf{if} \ \mathrm{subsetSum}( A , S , j + 1 , s + A[ j ] ) \ \textbf{then return true} \cr 5 & \qquad \text{pop } S \cr 6 & \quad \textbf{return false} \end{array}} \]

Precondition
the items of \( A \) are positive