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

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

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/combinatorial/impl/Generation.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Typedefs

using ADS::Tuple = std::vector< int >
 a tuple, a permutation, a string

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

  • mixedRadix() (Algorithm 7.2.1.1M) the \( n \)-tuples with \( 0 \le a_j < m_j \) in lexicographic order, and grayTuples() (Algorithm 7.2.1.1H, loopless) in the reflected mixed-radix Gray order, where two consecutive tuples differ by 1 in one place;
  • permutationsLex() (Algorithm 7.2.1.2L, of Narayana Pandita) the permutations of a multiset in lexicographic order, plainChanges() (Algorithm 7.2.1.2P, of Steinhaus, Johnson and Trotter) those of \( n \) distinct items where two consecutive ones differ by a swap of adjacent items, and heapPermutations() (Heap's method, Algorithm 7.2.1.2G) by one swap each, with the least work per permutation;
  • combinationsLex() the \( t \)-combinations of \( n \) in lexicographic order (the order of Algorithm 7.2.1.3L), and revolvingDoor() in the order of Algorithm 7.2.1.3R, where two consecutive ones differ by one item in and one out;
  • integerPartitions() the partitions of \( n \) in reverse lexicographic order (that of Algorithm 7.2.1.4P), and setPartitions() the partitions of a set of \( n \) by their restricted growth strings \( a_1 = 0 \), \( a_{j+1} \le 1 + \max( a_1 , \ldots , a_j ) \) (as Algorithm 7.2.1.5H);
  • nestedParentheses() the strings of \( n \) pairs of balanced parentheses in lexicographic order (that of Algorithm 7.2.1.6P), i.e., the forests and the binary trees with \( n \) nodes.

The counts are \( \prod_j m_j \), the multinomial \( n! / \prod_v m_v! \) of the multiplicities, \( n! \), \( \binom{n}{t} \), \( p( n ) \), the Bell number \( \varpi_n \) and the Catalan number \( C_n \), and the tests check them. The contracts [see Contract.h]: the radices are positive, \( 0 \le t \le n \), \( n \ge 0 \). The functions are defined in Generation.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 4A, Addison-Wesley, 2011, section 7.2.1
H. F. Trotter, Algorithm 115: Perm, Communications of the ACM 5(8), 434-435, 1962, doi:10.1145/368637.368660
S. M. Johnson, Generation of permutations by adjacent transposition, Mathematics of Computation 17(83), 282-285, 1963, doi:10.1090/S0025-5718-1963-0159764-2
B. R. Heap, Permutations by interchanges, The Computer Journal 6(3), 293-298, 1963, doi:10.1093/comjnl/6.3.293
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa