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

The generation of the basic combinatorial objects of Knuth (section 7.2.1 of The Art of Computer Programming), beyond the book. More...

Files

file  ADS/combinatorial/Generation.h
 The generation of the basic combinatorial objects of Knuth (section 7.2.1 of The Art of Computer Programming), beyond the book [see Enumeration.h for the enumerations by backtrack of chapter 16]: each function calls visit() on every object, in a given order, and returns their number; a visit that returns true stops the generation, and one that returns void never does (as in FastGeneration.h).

Functions

template<class F>
long ADS::mixedRadix (const std::vector< int > &m, F &&visit)
 the tuples \( ( a_0 , \ldots , a_{n-1} ) \) with \( 0 \le a_j < m_j \), in lexicographic order, \( O( 1 ) \) amortized per tuple after \( O( n ) \)
template<class F>
long ADS::grayTuples (const std::vector< int > &m, F &&visit)
 the tuples with \( 0 \le a_j < m_j \) in the reflected Gray order, loopless
template<class F>
long ADS::permutationsLex (Tuple a, F &&visit)
 the permutations of the multiset \( a \), in lexicographic order from \( a \) sorted
template<class F>
long ADS::plainChanges (int n, F &&visit)
 the permutations of \( 0 , \ldots , n - 1 \) by swaps of adjacent items, \( O( 1 ) \) amortized per permutation, the same vector visited each time
template<class F>
long ADS::heapPermutations (int n, F &&visit)
 the permutations of \( 0 , \ldots , n - 1 \) by Heap's method, one swap each
template<class F>
long ADS::combinationsLex (int n, int t, F &&visit)
 the \( t \)-combinations of \( 0 , \ldots , n - 1 \) as increasing tuples, in lexicographic order
template<class F>
long ADS::revolvingDoor (int n, int t, F &&visit)
 the \( t \)-combinations of \( 0 , \ldots , n - 1 \) in the revolving-door order
template<class F>
long ADS::integerPartitions (int n, F &&visit)
 the partitions of \( n \), their parts nonincreasing, in reverse lexicographic order, \( O( 1 ) \) amortized per partition
template<class F>
long ADS::setPartitions (int n, F &&visit)
 the restricted growth strings of the partitions of a set of \( n \) items, in lexicographic order, \( O( 1 ) \) amortized per partition
template<class F>
long ADS::nestedParentheses (int n, F &&visit)
 the strings of \( n \) pairs of balanced parentheses, in lexicographic order

Detailed description

The generation of the basic combinatorial objects of Knuth (section 7.2.1 of The Art of Computer Programming), beyond the book.

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

Function documentation

◆ combinationsLex()

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

the \( t \)-combinations of \( 0 , \ldots , n - 1 \) as increasing tuples, in lexicographic order

In pseudocode (the lexicographic successor, the order of Knuth's Algorithm 7.2.1.3L, beyond the book): the last place that can grow grows by 1, and the places after it follow it by steps of 1.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{combinationsLex}( n , t ) \cr 2 & \quad c_j \gets j \text{ for } 0 \le j < t \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ \mathrm{visit}( c )\ \textbf{then return} \cr 5 & \qquad j \gets t - 1;\ \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 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 \)

◆ grayTuples()

template<class F>
long ADS::grayTuples ( const std::vector< int > & m,
F && visit )

the tuples with \( 0 \le a_j < m_j \) in the reflected Gray order, loopless

In pseudocode (Knuth's Algorithm 7.2.1.1H, beyond the book), the places counted from the right (\( a_j \) is a[ n - 1 - j ]), \( o_j \) the direction of the place \( j \) and \( f_j \) the focus pointers, which give the next place to change in \( O( 1 ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{grayTuples}( m ) \cr 2 & \quad a \gets ( 0 , \ldots , 0 );\ o \gets ( 1 , \ldots , 1 ); \ f_j \gets j \text{ for } 0 \le j \le n \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ \mathrm{visit}( a )\ \textbf{then return} \cr 5 & \qquad j \gets f_0;\ f_0 \gets 0 \cr 6 & \qquad \textbf{if}\ j = n\ \textbf{then return} \cr 7 & \qquad a_j \gets a_j + o_j \cr 8 & \qquad \textbf{if}\ a_j = 0\ \textbf{or}\ a_j = m_j - 1\ \textbf{then} \quad \triangleright\ \text{the end of its range} \cr 9 & \qquad\quad o_j \gets -o_j;\ f_j \gets f_{j+1};\ f_{j+1} \gets j + 1 \end{array}} \]

Precondition
each \( m_j \ge 2 \)

◆ heapPermutations()

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

the permutations of \( 0 , \ldots , n - 1 \) by Heap's method, one swap each

In pseudocode (Heap's method, the iterative form, beyond the book), \( c_i \) the counter of the place \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{heapPermutations}( n ) \cr 2 & \quad a \gets ( 0 , \ldots , n - 1 );\ c \gets ( 0 , \ldots , 0 ); \ \textbf{if}\ \mathrm{visit}( a )\ \textbf{then return} \cr 3 & \quad i \gets 1 \cr 4 & \quad \textbf{while}\ i < n\ \textbf{do} \cr 5 & \qquad \textbf{if}\ c_i < i\ \textbf{then} \cr 6 & \qquad\quad \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 7 & \qquad\quad \textbf{if}\ \mathrm{visit}( a )\ \textbf{then return} \cr 8 & \qquad\quad c_i \gets c_i + 1;\ i \gets 1 \cr 9 & \qquad \textbf{else}\ c_i \gets 0;\ i \gets i + 1 \end{array}} \]

Precondition
\( n \ge 0 \)

◆ integerPartitions()

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

the partitions of \( n \), their parts nonincreasing, in reverse lexicographic order, \( O( 1 ) \) amortized per partition

In pseudocode (Knuth's Algorithm 7.2.1.4P, beyond the book): the last part \( a_q \) larger than 1 drops by 1, and the 1s after it, with the unit taken from it, are split again into parts as large as it; the 1s are cut off at once, by the length \( m \) of the partition, never one by one, and a part 2 becomes \( 1 + 1 \) by one step, the case of half the partitions:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{integerPartitions}( n ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then}\ \mathrm{visit}( () ); \ \textbf{return} \cr 3 & \quad a_0 \gets 0;\ m \gets 0;\ r \gets n \cr 4 & \quad \textbf{loop} \cr 5 & \qquad m \gets m + 1;\ a_m \gets r;\ q \gets m \text{, or } m - 1 \text{ if } r = 1 \cr 6 & \qquad \textbf{loop} \cr 7 & \qquad\quad \textbf{if}\ \mathrm{visit}( a_1 , \ldots , a_m ) \ \textbf{then return} \cr 8 & \qquad\quad \textbf{if}\ a_q \ne 2\ \textbf{then break} \cr 9 & \qquad\quad a_q \gets 1;\ q \gets q - 1;\ m \gets m + 1;\ a_m \gets 1 \cr 10 & \qquad \textbf{if}\ q = 0\ \textbf{then return} \cr 11 & \qquad x \gets a_q - 1;\ a_q \gets x;\ r \gets m - q + 1;\ m \gets q \cr 12 & \qquad \textbf{while}\ r > x\ \textbf{do}\ m \gets m + 1;\ a_m \gets x;\ r \gets r - x \end{array}} \]

The work between two visits is the number of parts written, fewer than 3 per partition over the whole run (Lemma 3 of Generation of combinatorial objects).

Precondition
\( n \ge 0 \)

◆ mixedRadix()

template<class F>
long ADS::mixedRadix ( const std::vector< int > & m,
F && visit )

the tuples \( ( a_0 , \ldots , a_{n-1} ) \) with \( 0 \le a_j < m_j \), in lexicographic order, \( O( 1 ) \) amortized per tuple after \( O( n ) \)

In pseudocode (Knuth's Algorithm 7.2.1.1M, beyond the book), adding 1 with the carries over the places of radix larger than 1 only, \( b( j ) \) the last such place before \( j \) and \( \ell \) the last one, -1 if none (a place of radix 1 holds 0 for ever, and a carry that walked over it would cost \( \Theta( n ) \) per tuple for \( m = ( 2 , 1 , \ldots , 1 ) \)):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mixedRadix}( m ) \cr 2 & \quad a \gets ( 0 , \ldots , 0 ) \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ \mathrm{visit}( a )\ \textbf{then return} \cr 5 & \qquad j \gets \ell \cr 6 & \qquad \textbf{while}\ j \ge 0\ \textbf{and}\ a_j = m_j - 1 \ \textbf{do}\ a_j \gets 0;\ j \gets b( j ) \cr 7 & \qquad \textbf{if}\ j < 0\ \textbf{then return} \cr 8 & \qquad a_j \gets a_j + 1 \end{array}} \]

The carries are \( O( 1 ) \) amortized: a carry over the place \( j \) happens once in \( \prod_{i \ge j} m_i \ge 2^k \) tuples, \( k \) the places of radix larger than 1 from \( j \) on.

Precondition
each \( m_j \ge 1 \)

◆ nestedParentheses()

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

the strings of \( n \) pairs of balanced parentheses, in lexicographic order

In pseudocode (a backtrack in the order of Knuth's Algorithm 7.2.1.6P, beyond the book), \( o \) and \( c \) the parentheses opened and closed in \( s_0 , \ldots , s_{i-1} \), the opening one tried first:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{parentheses}( i , o , c )\quad \triangleright\ \text{called as } \mathrm{parentheses}( 0 , 0 , 0 ) \cr 2 & \quad \textbf{if}\ i = 2 n\ \textbf{then}\ \mathrm{visit}( s ); \ \textbf{return} \cr 3 & \quad \textbf{if}\ o < n\ \textbf{then}\ s_i \gets \text{(}; \ \mathrm{parentheses}( i + 1 , o + 1 , c ) \cr 4 & \quad \textbf{if}\ c < o\ \textbf{then}\ s_i \gets \text{)}; \ \mathrm{parentheses}( i + 1 , o , c + 1 ) \end{array}} \]

The generation stops as soon as a visit returns true.

Precondition
\( n \ge 0 \)

◆ permutationsLex()

template<class F>
long ADS::permutationsLex ( Tuple a,
F && visit )

the permutations of the multiset \( a \), in lexicographic order from \( a \) sorted

In pseudocode (Knuth's Algorithm 7.2.1.2L, beyond the book): the suffix after the last ascent is decreasing, and the next permutation raises the place of the ascent by the least larger item of the suffix and sorts the suffix.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{permutationsLex}( a ) \cr 2 & \quad \text{sort } a \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ \mathrm{visit}( a )\ \textbf{then return} \cr 5 & \qquad j \gets n - 2;\ \textbf{while}\ j \ge 0\ \textbf{and}\ a_j \ge a_{j+1}\ \textbf{do}\ j \gets j - 1 \cr 6 & \qquad \textbf{if}\ j < 0\ \textbf{then return} \cr 7 & \qquad l \gets n - 1;\ \textbf{while}\ a_j \ge a_l\ \textbf{do}\ l \gets l - 1 \cr 8 & \qquad \text{swap } a_j \text{ and } a_l;\ \text{reverse } a_{j+1} , \ldots , a_{n-1} \end{array}} \]

◆ plainChanges()

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

the permutations of \( 0 , \ldots , n - 1 \) by swaps of adjacent items, \( O( 1 ) \) amortized per permutation, the same vector visited each time

In pseudocode (Knuth's Algorithm 7.2.1.2P, beyond the book), the places from 1, \( c_j \) and \( o_j \) the counter and the direction of the item \( j \), and \( s \) the items above it already at an end (the vector of the code holds \( a_j \) at the index \( j - 1 \)):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{plainChanges}( n ) \cr 2 & \quad a_j \gets j - 1;\ c_j \gets 0;\ o_j \gets 1 \text{ for } 1 \le j \le n \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ \mathrm{visit}( a )\ \textbf{then return} \cr 5 & \qquad j \gets n;\ s \gets 0 \cr 6 & \qquad \textbf{loop} \cr 7 & \qquad\quad \textbf{if}\ j < 1\ \textbf{then return} \cr 8 & \qquad\quad q \gets c_j + o_j \cr 9 & \qquad\quad \textbf{if}\ q < 0\ \textbf{then}\ o_j \gets -o_j;\ j \gets j - 1 \cr 10 & \qquad\quad \textbf{else if}\ q = j\ \textbf{then} \cr 11 & \qquad\qquad \textbf{if}\ j = 1\ \textbf{then return} \cr 12 & \qquad\qquad s \gets s + 1;\ o_j \gets -o_j;\ j \gets j - 1 \cr 13 & \qquad\quad \textbf{else} \cr 14 & \qquad\qquad \text{swap } a_{j - c_j + s} \text{ and } a_{j - q + s}; \ c_j \gets q;\ \text{leave the inner loop} \end{array}} \]

Precondition
\( n \ge 0 \)

◆ revolvingDoor()

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

the \( t \)-combinations of \( 0 , \ldots , n - 1 \) in the revolving-door order

In pseudocode (the recursion behind Knuth's Algorithm 7.2.1.3R, beyond the book): the order \( R( n , t ) \) is \( R( n - 1 , t ) \) followed by \( R( n - 1 , t - 1 ) \) reversed, each with \( n - 1 \) added, so that the last combination of the first part and the first of the second differ by one item in and one out.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{door}( n , t , \mathit{rev} )\quad \triangleright\ \text{called as } \mathrm{door}( n , t , \textbf{false} ) \cr 2 & \quad \textbf{if}\ t = 0\ \textbf{then}\ \mathrm{visit}( c ); \ \textbf{return} \cr 3 & \quad \textbf{if}\ n = t\ \textbf{then}\ c \gets ( 0 , \ldots , t - 1 );\ \mathrm{visit}( c );\ \textbf{return} \cr 4 & \quad \textbf{if not}\ \mathit{rev}\ \textbf{then} \cr 5 & \qquad \mathrm{door}( n - 1 , t , \textbf{false} );\ c_{t-1} \gets n - 1;\ \mathrm{door}( n - 1 , t - 1 , \textbf{true} ) \cr 6 & \quad \textbf{else} \cr 7 & \qquad c_{t-1} \gets n - 1;\ \mathrm{door}( n - 1 , t - 1 , \textbf{false} );\ \mathrm{door}( n - 1 , t , \textbf{true} ) \end{array}} \]

The generation stops as soon as a visit returns true.

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

◆ setPartitions()

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

the restricted growth strings of the partitions of a set of \( n \) items, in lexicographic order, \( O( 1 ) \) amortized per partition

In pseudocode (the order of Knuth's Algorithm 7.2.1.5H, beyond the book), \( b_j = 1 + \max( a_0 , \ldots , a_{j-1} ) \) the largest value that \( a_j \) may take:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setPartitions}( n ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then}\ \mathrm{visit}( () ); \ \textbf{return} \cr 3 & \quad a \gets ( 0 , \ldots , 0 );\ b \gets ( 1 , \ldots , 1 ) \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \textbf{if}\ \mathrm{visit}( a )\ \textbf{then return} \cr 6 & \qquad j \gets n - 1;\ \textbf{while}\ j > 0\ \textbf{and}\ a_j = b_j \ \textbf{do}\ j \gets j - 1 \cr 7 & \qquad \textbf{if}\ j = 0\ \textbf{then return} \cr 8 & \qquad a_j \gets a_j + 1 \cr 9 & \qquad \textbf{for}\ k \gets j + 1\ \textbf{to}\ n - 1\ \textbf{do} \ a_k \gets 0;\ b_k \gets \max( b_{k-1} , a_{k-1} + 1 ) \end{array}} \]

The test of line 6 is evaluated \( \sum_{k=1}^{n} \varpi_k < 2 \varpi_n \) times in all, since the strings whose last \( r \) places open new blocks are \( \varpi_{n-r} \), \( 0 \le r \le n - 1 \), and \( \varpi_k \ge 2 \varpi_{k-1} \) for \( k \ge 2 \).

Precondition
\( n \ge 0 \)