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

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

#include "ADS/Contract.h"
#include "ADS/combinatorial/Bitwise.h"
#include "ADS/combinatorial/Generation.h"
#include <algorithm>
#include <bit>
#include <cstdint>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/combinatorial/impl/FastGeneration.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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

Each function calls visit() on every object and returns their number; if visit() returns bool, true stops the generation, and if it returns void there is no test at all.

  • forEachPermutation( a , visit ) visits the \( n! \) arrangements of \( a \) (any items, the duplicates not merged) by Heap's method, one swap each: the arrangements of the first 3 items are visited by straight-line code (the 5 swaps of the places 01, 02, 01, 02, 01 of Heap's method on 3 items), and the counters \( c_i \) of the iterative method drive only the places \( i \ge 3 \), i.e., once every 6 objects; the cost is one swap per object, as in any form of Heap's method, and the steps of the counters fall from \( \sum_{k \ge 1} 1 / k! = e - 1 \) per object to \( \sum_{k \ge 3} 1 / k! = e - 5 / 2 < 0.22 \).
  • forEachPermutationLex( a , visit ) visits the distinct permutations of the multiset \( a \) in lexicographic order (as std::next_permutation from \( a \) sorted): Knuth's Algorithm 7.2.1.2L for the places up to \( n - 4 \), and the permutations of the last 3 items written directly, 6 of them if they are distinct and 3 or 1 otherwise; the loops of L then run once every 6 objects instead of at each.
  • forEachCombination( n , t , visit ) visits the increasing \( t \)-tuples of \( [ 0 , n ) \) in lexicographic order: the last place runs through its values by a loop of its own, and the successor of Algorithm 7.2.1.3L moves the others only when it is over, once every \( n - c_{t-2} - 1 \) objects.
  • forEachCombinationMask( n , t , visit ) visits the words of \( t \) 1 bits below \( 2^n \), \( n \le 64 \), in increasing order, by nextCombination(), the trick of Gosper with the trailing zeros of the processor in place of the division of nextSameWeight() in Bitwise.h:

    \[ u = x \mid ( x - 1 ) , \quad x' = ( u + 1 ) \mid \big( ( ( \lnot u \,\&\, ( u + 1 ) ) - 1 ) \gg ( \rho( x ) + 1 ) \big) , \]

    \( \rho \) the trailing zeros; while the lowest 1 of \( x \) has a 0 above it, below the place \( n - 1 \), the next word is \( x + ( x \,\&\, -x ) \), which a loop of its own does in two operations, so that the step above runs once per run of the lowest 1, i.e., for a fraction of about \( t / n \) of the words.

The counts are \( n! \), the multinomial \( n! / \prod_v m_v! \) of the multiplicities and \( \binom{n}{t} \), as for Generation.h. The contracts [see Contract.h]: \( 0 \le t \le n \), \( n \le 64 \) for the words. The functions are defined in FastGeneration.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 4A, Addison-Wesley, 2011, sections 7.2.1.2 and 7.2.1.3
B. R. Heap, Permutations by interchanges, The Computer Journal 6(3), 293-298, 1963, doi:10.1093/comjnl/6.3.293
R. Sedgewick, Permutation generation methods, ACM Computing Surveys 9(2), 137-164, 1977, doi:10.1145/356689.356692
H. S. Warren, Hacker's Delight, 2nd ed., Addison-Wesley, 2012, section 2-1 (the next word with as many 1 bits)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa