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

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. More...

#include <cstddef>
#include "ADS/Contract.h"
#include <algorithm>
#include <vector>
#include "ADS/backtrack/impl/Enumeration.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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): 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.

The processing returns true to stop at the first solution.

  • subsets(), the subsets of \( k \) of the \( n \) items, by the choices in or out, pruned when \( k \) items are in or when the items left are too few;
  • permutations() and derangements(), each item not chosen yet, the latter not in its own place (exercises 16.2 and 16.3);
  • subsetSum(), the subsets of positive items of sum \( k \), pruned when the sum exceeds \( k \) (exercise 16.4).

Each calls f( S ) on the solutions, in increasing order of the choices, and returns how many there are, or stops when f returns true. The subsets of \( k \) items are \( \binom{n}{k} = n! / ( k! ( n - k )! ) \) (the text writes \( n! / ( n - k )! \), the number of their orderings).

The contracts [see Contract.h]: \( n \) and \( k \) are not negative, and the items of subsetSum() are positive. The operations are defined in Enumeration.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 16.2 and exercises 16.2-16.4, https://algoritmicamente.it/materiali/libro/capitoli/cap16.pdf
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa