openGPMP
Open Source Mathematics Package
comb.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file.
25  *
26  *
27  *
28  * This software is distributed on an AS IS basis, WITHOUT
29  * WARRANTY OF ANY KIND, either express or implied.
30  *
31  ************************************************************************/
32 
33 #ifndef __COMB_HPP__
34 #define __COMB_HPP__
35 
36 #include <algorithm>
37 #include <cstdint>
38 #include <string>
39 #include <vector>
40 
41 namespace gpmp {
42 
47 class Comb {
48  public:
55  int64_t permutation(int n, int r);
56 
63  int64_t combination(int n, int r);
64 
71  int64_t binom_coeff(int n, int r);
72 
78  int64_t factorial(int n);
79 
85  std::vector<std::vector<int>> partitions(int n);
86 
92  std::vector<int64_t> bell_num(int n);
93 
100  int64_t stirling_num(int n, int k);
101 
107  std::vector<int> gray_code(int n);
108 
114  int64_t subfactorial(int n);
115 
122  int64_t subsequences(int n, int k);
123 
129  std::vector<std::vector<int>> partition_distinct(int n);
130 
136  std::vector<std::vector<int>> compositions(int n);
137 
143  int64_t dyck_words(int n);
144 
150  std::vector<std::string> gen_dyck_words_until(int n);
151 
158  int64_t necklaces(int n, int k);
159 
165  int phi(int n);
166 
173  std::vector<std::vector<int>> generateNecklaces(int n, int k);
174 
180  int64_t lyndonWords(int n);
181 
187  std::vector<std::string> generateLyndonWords(int n);
188 
196  void generatePartitions(int n,
197  int max,
198  std::vector<int> &partition,
199  std::vector<std::vector<int>> &result);
200 
208  void gen_partition_distinct(int n,
209  int max,
210  std::vector<int> &partition,
211  std::vector<std::vector<int>> &result);
212 
220  void gen_compositions(int n,
221  int max,
222  std::vector<int> &composition,
223  std::vector<std::vector<int>> &result);
224 
232  void gen_dyck_words(std::string prefix,
233  int open,
234  int close,
235  std::vector<std::string> &dyck_words);
236 
245  void gen_necklaces(std::vector<int> &necklace,
246  int index,
247  int n,
248  int k,
249  std::vector<std::vector<int>> &necklaces);
250 
258  void gen_lyndon_words(std::string prefix,
259  int n,
260  int max,
261  std::vector<std::string> &lyndonWords);
262 
268  std::vector<int> divisors(int n);
269 
276  template <typename T>
277  std::vector<std::vector<T>> permutations(const std::vector<T> &vec);
278 
286  template <typename T>
287  std::vector<std::vector<T>> combinations(const std::vector<T> &vec, int r);
288 };
289 
290 } // namespace gpmp
291 
292 #endif
A class providing various combinatorial functions and algorithms.
Definition: comb.hpp:47
void gen_dyck_words(std::string prefix, int open, int close, std::vector< std::string > &dyck_words)
Generate Dyck words.
Definition: comb.cpp:294
int64_t subfactorial(int n)
Calculate the subfactorial (!n) or derangement of n.
Definition: comb.cpp:131
int phi(int n)
Calculate Euler's totient function (φ)
Definition: comb.cpp:199
int64_t necklaces(int n, int k)
Calculate the number of necklaces of length n with k colors.
Definition: comb.cpp:188
void gen_compositions(int n, int max, std::vector< int > &composition, std::vector< std::vector< int >> &result)
Generate compositions of a positive integer.
Definition: comb.cpp:277
void gen_partition_distinct(int n, int max, std::vector< int > &partition, std::vector< std::vector< int >> &result)
Generate distinct partitions of a positive integer.
Definition: comb.cpp:257
void generatePartitions(int n, int max, std::vector< int > &partition, std::vector< std::vector< int >> &result)
Generate partitions of a positive integer.
Definition: comb.cpp:240
int64_t lyndonWords(int n)
Calculate the number of Lyndon words of length n.
Definition: comb.cpp:224
std::vector< int64_t > bell_num(int n)
Calculate the Bell numbers.
Definition: comb.cpp:99
int64_t dyck_words(int n)
Calculate the number of Dyck words of length 2n.
Definition: comb.cpp:175
int64_t binom_coeff(int n, int r)
Calculate the binomial coefficient (n choose r)
Definition: comb.cpp:65
void gen_necklaces(std::vector< int > &necklace, int index, int n, int k, std::vector< std::vector< int >> &necklaces)
Generate necklaces.
Definition: comb.cpp:311
int64_t factorial(int n)
Calculate the factorial of a number.
Definition: comb.cpp:80
std::vector< std::vector< int > > partitions(int n)
Generate all partitions of a positive integer n.
Definition: comb.cpp:91
std::vector< int > gray_code(int n)
Generate the Gray code sequence of length n.
Definition: comb.cpp:122
std::vector< std::vector< int > > partition_distinct(int n)
Generate all distinct partitions of a positive integer n.
Definition: comb.cpp:159
std::vector< int > divisors(int n)
Calculate the divisors of a positive integer.
Definition: comb.cpp:349
int64_t permutation(int n, int r)
Calculate the number of permutations (nPr)
Definition: comb.cpp:41
int64_t stirling_num(int n, int k)
Calculate the Stirling number of the second kind.
Definition: comb.cpp:111
int64_t combination(int n, int r)
Calculate the number of combinations (nCr)
Definition: comb.cpp:52
std::vector< std::vector< int > > compositions(int n)
Generate all compositions of a positive integer n.
Definition: comb.cpp:167
void gen_lyndon_words(std::string prefix, int n, int max, std::vector< std::string > &lyndonWords)
Generate Lyndon words.
Definition: comb.cpp:328
int64_t subsequences(int n, int k)
Calculate the number of subsequences of length k from n items.
Definition: comb.cpp:141
std::vector< std::vector< T > > combinations(const std::vector< T > &vec, int r)
Generate all combinations of a vector.
std::vector< std::string > generateLyndonWords(int n)
Generate all Lyndon words of length n.
Definition: comb.cpp:233
std::vector< std::vector< T > > permutations(const std::vector< T > &vec)
Generate all permutations of a vector.
std::vector< std::string > gen_dyck_words_until(int n)
Generate Dyck words up to length n.
Definition: comb.cpp:180
std::vector< std::vector< int > > generateNecklaces(int n, int k)
Generate all necklaces of length n with k colors.
Definition: comb.cpp:216
The source C++ openGPMP namespace.