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

The engineered generation of permutations and combinations, beyond the book. More...

Files

file  ADS/combinatorial/FastGeneration.h
 The engineered generation of permutations and combinations, beyond the book [see Generation.h for the algorithms of Knuth (section 7.2.1 of The Art of Computer Programming) as written there]: the same objects, with the work per object brought down to a few instructions, so that visit() is the larger part of the time.

Functions

template<class T, class F>
long ADS::forEachPermutation (std::vector< T > &a, F &&visit)
 the \( n! \) arrangements of \( a \) by Heap's method, visit( a ) on each
template<class T, class F>
long ADS::forEachPermutationLex (std::vector< T > &a, F &&visit)
 the distinct permutations of the multiset \( a \) in lexicographic order from \( a \) sorted, visit( a ) on each; \( a \) ends sorted
template<class F>
long ADS::forEachCombination (int n, int t, F &&visit)
 the increasing \( t \)-tuples of \( [ 0 , n ) \) in lexicographic order, visit( c ) on each, \( c \) a const std::vector< int > of size \( t \)
constexpr Word ADS::nextCombination (Word x)
 the next larger word with as many 1 bits as \( x \)
template<class F>
long ADS::forEachCombinationMask (int n, int t, F &&visit)
 the words of \( t \) 1 bits below \( 2^n \) in increasing order, visit( x ) on each

Detailed description

The engineered generation of permutations and combinations, beyond the book.

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

Function documentation

◆ forEachCombination()

template<class F>
long ADS::forEachCombination ( int n,
int t,
F && visit )

the increasing \( t \)-tuples of \( [ 0 , n ) \) in lexicographic order, visit( c ) on each, \( c \) a const std::vector< int > of size \( t \)

In pseudocode (the successor of Knuth's Algorithm 7.2.1.3L with a loop of its own for the last place, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{forEachCombination}( n , t ) \cr 2 & \quad c_j \gets j \text{ for } 0 \le j < t;\ \textbf{if}\ t = 0 \ \textbf{then}\ \mathrm{visit}( c );\ \textbf{return} \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{while}\ c_{t-1} < n\ \textbf{do}\ \mathrm{visit}( c ); \ c_{t-1} \gets c_{t-1} + 1 \cr 5 & \qquad j \gets t - 2;\ \textbf{while}\ j \ge 0\ \textbf{and}\ c_j = n - t + j\ \textbf{do}\ j \gets j - 1 \cr 6 & \qquad \textbf{if}\ j < 0\ \textbf{then}\ c_{t-1} \gets n - 1; \ \textbf{return} \cr 7 & \qquad c_j \gets c_j + 1;\ c_k \gets c_{k-1} + 1 \text{ for } j < k < t \end{array}} \]

Precondition
\( 0 \le t \le n \)

◆ forEachCombinationMask()

template<class F>
long ADS::forEachCombinationMask ( int n,
int t,
F && visit )

the words of \( t \) 1 bits below \( 2^n \) in increasing order, visit( x ) on each

In pseudocode (beyond the book), the lowest 1 moved up alone while it can, and nextCombination() only at the end of its run:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{forEachCombinationMask}( n , t ) \cr 2 & \quad \textbf{if}\ t = 0\ \textbf{then}\ \mathrm{visit}( 0 ); \ \textbf{return} \cr 3 & \quad x \gets 2^t - 1;\ \mathit{last} \gets x \ll ( n - t ); \ \mathit{top} \gets 2^{n-1} \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \mathrm{visit}( x );\ \ell \gets x \,\&\, -x \cr 6 & \qquad \textbf{while}\ \ell < \mathit{top}\ \textbf{and}\ x \,\&\, 2 \ell = 0\ \textbf{do}\ x \gets x + \ell;\ \ell \gets 2 \ell; \ \mathrm{visit}( x ) \cr 7 & \qquad \textbf{if}\ x = \mathit{last}\ \textbf{then return} \cr 8 & \qquad x \gets \mathrm{nextCombination}( x ) \end{array}} \]

Precondition
\( 0 \le t \le n \le 64 \)

◆ forEachPermutation()

template<class T, class F>
long ADS::forEachPermutation ( std::vector< T > & a,
F && visit )

the \( n! \) arrangements of \( a \) by Heap's method, visit( a ) on each

In pseudocode (Heap's method with its first 3 places unrolled, beyond the book), \( c_i \) the counter of the place \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{forEachPermutation}( a ) \cr 2 & \quad \textbf{if}\ n < 3\ \textbf{then}\ \text{visit } a \text{, and } a \text{ with } a_0 , a_1 \text{ swapped if } n = 2;\ \textbf{return} \cr 3 & \quad c \gets ( 0 , \ldots , 0 ) \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \text{visit } a \text{, then swap the places } 01 , 02 , 01 , 02 , 01 \text{ visiting } a \text{ after each}\quad \triangleright \ \text{the 6 of } a_0 a_1 a_2 \cr 6 & \qquad i \gets 3;\ \textbf{while}\ i < n\ \textbf{and}\ c_i \ge i \ \textbf{do}\ c_i \gets 0;\ i \gets i + 1 \cr 7 & \qquad \textbf{if}\ i \ge n\ \textbf{then return} \cr 8 & \qquad \textbf{if}\ i \text{ is odd }\ \textbf{then}\ \text{swap } a_{c_i} \text{ and } a_i\ \textbf{else}\ \text{swap } a_0 \text{ and } a_i \cr 9 & \qquad c_i \gets c_i + 1 \end{array}} \]

◆ forEachPermutationLex()

template<class T, class F>
long ADS::forEachPermutationLex ( std::vector< T > & a,
F && visit )

the distinct permutations of the multiset \( a \) in lexicographic order from \( a \) sorted, visit( a ) on each; \( a \) ends sorted

In pseudocode (Knuth's Algorithm 7.2.1.2L with its last 3 places unrolled, beyond the book): the last 3 items \( x \le y \le z \) are increasing after each step of L, which they never enter, since they end decreasing.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{forEachPermutationLex}( a ) \cr 2 & \quad \text{sort } a;\ \textbf{if}\ n < 4\ \textbf{then}\ \text{run} \text{ Algorithm L alone};\ \textbf{return} \cr 3 & \quad \textbf{loop} \cr 4 & \qquad ( x , y , z ) \gets ( a_{n-3} , a_{n-2} , a_{n-1} ) \cr 5 & \qquad \textbf{if}\ x < y < z\ \textbf{then}\ \text{visit } xyz , xzy , yxz , yzx , zxy , zyx \text{ in the last 3 places} \cr 6 & \qquad \textbf{else if}\ x = y < z\ \textbf{then}\ \text{visit } xxz , xzx , zxx \cr 7 & \qquad \textbf{else if}\ x < y = z\ \textbf{then}\ \text{visit } xyy , yxy , yyx \cr 8 & \qquad \textbf{else}\ \text{visit } xxx \cr 9 & \qquad j \gets n - 4;\ \textbf{while}\ j \ge 0\ \textbf{and not}\ a_j < a_{j+1}\ \textbf{do}\ j \gets j - 1 \cr 10 & \qquad \textbf{if}\ j < 0\ \textbf{then}\ \text{reverse } a; \ \textbf{return}\quad \triangleright\ \text{back to sorted} \cr 11 & \qquad l \gets n - 1;\ \textbf{while not}\ a_j < a_l\ \textbf{do}\ l \gets l - 1 \cr 12 & \qquad \text{swap } a_j \text{ and } a_l;\ \text{reverse } a_{j+1} , \ldots , a_{n-1} \end{array}} \]

◆ nextCombination()

Word ADS::nextCombination ( Word x)
nodiscardconstexpr

the next larger word with as many 1 bits as \( x \)

In pseudocode (the trick of Gosper as in Hacker's Delight, beyond the book), \( \rho \) the trailing zeros:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nextCombination}( x ) \cr 2 & \quad u \gets x \mid ( x - 1 )\quad \triangleright\ \text{the trailing} \text{ 0s set} \cr 3 & \quad \textbf{return}\ ( u + 1 ) \mid ( ( ( \lnot u \,\&\, ( u + 1 ) ) - 1 ) \gg ( \rho( x ) + 1 ) ) \end{array}} \]

Precondition
\( x \ne 0 \) and the result fits in 64 bits