openGPMP
Open Source Mathematics Package
Public Member Functions | List of all members
gpmp::Comb Class Reference

A class providing various combinatorial functions and algorithms. More...

#include <comb.hpp>

Public Member Functions

int64_t permutation (int n, int r)
 Calculate the number of permutations (nPr) More...
 
int64_t combination (int n, int r)
 Calculate the number of combinations (nCr) More...
 
int64_t binom_coeff (int n, int r)
 Calculate the binomial coefficient (n choose r) More...
 
int64_t factorial (int n)
 Calculate the factorial of a number. More...
 
std::vector< std::vector< int > > partitions (int n)
 Generate all partitions of a positive integer n. More...
 
std::vector< int64_t > bell_num (int n)
 Calculate the Bell numbers. More...
 
int64_t stirling_num (int n, int k)
 Calculate the Stirling number of the second kind. More...
 
std::vector< int > gray_code (int n)
 Generate the Gray code sequence of length n. More...
 
int64_t subfactorial (int n)
 Calculate the subfactorial (!n) or derangement of n. More...
 
int64_t subsequences (int n, int k)
 Calculate the number of subsequences of length k from n items. More...
 
std::vector< std::vector< int > > partition_distinct (int n)
 Generate all distinct partitions of a positive integer n. More...
 
std::vector< std::vector< int > > compositions (int n)
 Generate all compositions of a positive integer n. More...
 
int64_t dyck_words (int n)
 Calculate the number of Dyck words of length 2n. More...
 
std::vector< std::string > gen_dyck_words_until (int n)
 Generate Dyck words up to length n. More...
 
int64_t necklaces (int n, int k)
 Calculate the number of necklaces of length n with k colors. More...
 
int phi (int n)
 Calculate Euler's totient function (φ) More...
 
std::vector< std::vector< int > > generateNecklaces (int n, int k)
 Generate all necklaces of length n with k colors. More...
 
int64_t lyndonWords (int n)
 Calculate the number of Lyndon words of length n. More...
 
std::vector< std::string > generateLyndonWords (int n)
 Generate all Lyndon words of length n. More...
 
void generatePartitions (int n, int max, std::vector< int > &partition, std::vector< std::vector< int >> &result)
 Generate partitions of a positive integer. More...
 
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. More...
 
void gen_compositions (int n, int max, std::vector< int > &composition, std::vector< std::vector< int >> &result)
 Generate compositions of a positive integer. More...
 
void gen_dyck_words (std::string prefix, int open, int close, std::vector< std::string > &dyck_words)
 Generate Dyck words. More...
 
void gen_necklaces (std::vector< int > &necklace, int index, int n, int k, std::vector< std::vector< int >> &necklaces)
 Generate necklaces. More...
 
void gen_lyndon_words (std::string prefix, int n, int max, std::vector< std::string > &lyndonWords)
 Generate Lyndon words. More...
 
std::vector< int > divisors (int n)
 Calculate the divisors of a positive integer. More...
 
template<typename T >
std::vector< std::vector< T > > permutations (const std::vector< T > &vec)
 Generate all permutations of a vector. More...
 
template<typename T >
std::vector< std::vector< T > > combinations (const std::vector< T > &vec, int r)
 Generate all combinations of a vector. More...
 

Detailed Description

A class providing various combinatorial functions and algorithms.

Definition at line 47 of file comb.hpp.

Member Function Documentation

◆ bell_num()

std::vector< int64_t > gpmp::Comb::bell_num ( int  n)

Calculate the Bell numbers.

Parameters
nThe number of Bell numbers to calculate
Returns
A vector of Bell numbers

Definition at line 99 of file comb.cpp.

99  {
100  std::vector<int64_t> bell(n + 1, 0);
101  bell[0] = 1;
102  for (int i = 1; i <= n; ++i) {
103  for (int j = 0; j < i; ++j) {
104  bell[i] += binom_coeff(i - 1, j) * bell[j];
105  }
106  }
107  return bell;
108 }
int64_t binom_coeff(int n, int r)
Calculate the binomial coefficient (n choose r)
Definition: comb.cpp:65

Referenced by main().

◆ binom_coeff()

int64_t gpmp::Comb::binom_coeff ( int  n,
int  r 
)

Calculate the binomial coefficient (n choose r)

Parameters
nThe total number of items
rThe number of items to choose
Returns
The binomial coefficient

Definition at line 65 of file comb.cpp.

65  {
66  if (n < r)
67  return 0; // Invalid input
68  r = std::min(r, n - r); // Optimization to reduce calculations
69  std::vector<int64_t> dp(r + 1, 0);
70  dp[0] = 1;
71  for (int i = 1; i <= n; ++i) {
72  for (int j = std::min(i, r); j > 0; --j) {
73  dp[j] = dp[j] + dp[j - 1];
74  }
75  }
76  return dp[r];
77 }

Referenced by main().

◆ combination()

int64_t gpmp::Comb::combination ( int  n,
int  r 
)

Calculate the number of combinations (nCr)

Parameters
nThe total number of items
rThe number of items to choose
Returns
The number of combinations

Definition at line 52 of file comb.cpp.

52  {
53  if (n < r)
54  return 0; // Invalid input
55  r = std::min(r, n - r); // Optimization to reduce calculations
56  int64_t result = 1;
57  for (int i = 0; i < r; ++i) {
58  result *= (n - i);
59  result /= (i + 1);
60  }
61  return result;
62 }

Referenced by main().

◆ combinations()

template<typename T >
std::vector<std::vector<T> > gpmp::Comb::combinations ( const std::vector< T > &  vec,
int  r 
)

Generate all combinations of a vector.

Template Parameters
TThe type of elements in the vector
Parameters
vecThe vector
rThe size of combinations
Returns
A vector of all combinations

Referenced by main().

◆ compositions()

std::vector< std::vector< int > > gpmp::Comb::compositions ( int  n)

Generate all compositions of a positive integer n.

Parameters
nThe integer to compose
Returns
A vector of compositions

Definition at line 167 of file comb.cpp.

167  {
168  std::vector<std::vector<int>> result;
169  std::vector<int> composition;
170  gen_compositions(n, n, composition, result);
171  return result;
172 }
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

Referenced by main().

◆ divisors()

std::vector< int > gpmp::Comb::divisors ( int  n)

Calculate the divisors of a positive integer.

Parameters
nThe integer
Returns
A vector of divisors

Definition at line 349 of file comb.cpp.

349  {
350  std::vector<int> divs;
351  for (int i = 1; i * i <= n; ++i) {
352  if (n % i == 0) {
353  divs.push_back(i);
354  if (i != n / i) {
355  divs.push_back(n / i);
356  }
357  }
358  }
359  return divs;
360 }

◆ dyck_words()

int64_t gpmp::Comb::dyck_words ( int  n)

Calculate the number of Dyck words of length 2n.

Parameters
nThe length parameter
Returns
The number of Dyck words

Definition at line 175 of file comb.cpp.

175  {
176  return binom_coeff(2 * n, n) / (n + 1);
177 }

Referenced by main().

◆ factorial()

int64_t gpmp::Comb::factorial ( int  n)

Calculate the factorial of a number.

Parameters
nThe number
Returns
The factorial of n

Definition at line 80 of file comb.cpp.

80  {
81  if (n < 0)
82  return 0; // Invalid input
83  int64_t result = 1;
84  for (int i = 1; i <= n; ++i) {
85  result *= i;
86  }
87  return result;
88 }

Referenced by main().

◆ gen_compositions()

void gpmp::Comb::gen_compositions ( int  n,
int  max,
std::vector< int > &  composition,
std::vector< std::vector< int >> &  result 
)

Generate compositions of a positive integer.

Parameters
nThe integer to compose
maxThe maximum value in each composition
compositionThe current composition being generated
resultThe resulting vector of compositions

Definition at line 277 of file comb.cpp.

280  {
281  if (n == 0) {
282  result.push_back(composition);
283  return;
284  }
285 
286  for (int i = std::min(n, max); i >= 1; --i) {
287  composition.push_back(i);
288  gen_compositions(n - i, i, composition, result);
289  composition.pop_back();
290  }
291 }

◆ gen_dyck_words()

void gpmp::Comb::gen_dyck_words ( std::string  prefix,
int  open,
int  close,
std::vector< std::string > &  dyck_words 
)

Generate Dyck words.

Parameters
prefixThe current Dyck word prefix
openThe number of open parentheses remaining
closeThe number of close parentheses remaining
dyck_wordsThe resulting vector of Dyck words

Definition at line 294 of file comb.cpp.

297  {
298  if (open == 0 && close == 0) {
299  dyck_words.push_back(prefix);
300  return;
301  }
302  if (open > 0) {
303  gen_dyck_words(prefix + "(", open - 1, close, dyck_words);
304  }
305  if (close > open) {
306  gen_dyck_words(prefix + ")", open, close - 1, dyck_words);
307  }
308 }
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 dyck_words(int n)
Calculate the number of Dyck words of length 2n.
Definition: comb.cpp:175

◆ gen_dyck_words_until()

std::vector< std::string > gpmp::Comb::gen_dyck_words_until ( int  n)

Generate Dyck words up to length n.

Parameters
nThe maximum length of Dyck words
Returns
A vector of Dyck words

Definition at line 180 of file comb.cpp.

180  {
181  std::vector<std::string> dyck_words;
182  gen_dyck_words("", n, n, dyck_words);
183  return dyck_words;
184 }

Referenced by main().

◆ gen_lyndon_words()

void gpmp::Comb::gen_lyndon_words ( std::string  prefix,
int  n,
int  max,
std::vector< std::string > &  lyndonWords 
)

Generate Lyndon words.

Parameters
prefixThe current Lyndon word prefix
nThe length of the Lyndon words
maxThe maximum value in each Lyndon word
lyndonWordsThe resulting vector of Lyndon words

Definition at line 328 of file comb.cpp.

331  {
332  if (static_cast<int>(prefix.size()) == n) {
333  lyndonWords.push_back(prefix);
334  return;
335  }
336 
337  for (char c = (prefix.empty() ? 'a' : prefix.back() + 1);
338  c <= 'z' && static_cast<int>(prefix.size()) < n;
339  ++c) {
340  if (n % (static_cast<int>(prefix.size()) + 1) == 0) {
341  gen_lyndon_words(prefix + c, n, prefix.size() + 1, lyndonWords);
342  } else {
343  gen_lyndon_words(prefix + c, n, max, lyndonWords);
344  }
345  }
346 }
int64_t lyndonWords(int n)
Calculate the number of Lyndon words of length n.
Definition: comb.cpp:224
void gen_lyndon_words(std::string prefix, int n, int max, std::vector< std::string > &lyndonWords)
Generate Lyndon words.
Definition: comb.cpp:328

◆ gen_necklaces()

void gpmp::Comb::gen_necklaces ( std::vector< int > &  necklace,
int  index,
int  n,
int  k,
std::vector< std::vector< int >> &  necklaces 
)

Generate necklaces.

Parameters
necklaceThe current necklace being generated
indexThe current index in the necklace
nThe length of the necklace
kThe number of colors
necklacesThe resulting vector of necklaces

Definition at line 311 of file comb.cpp.

315  {
316  if (index == n) {
317  necklaces.push_back(necklace);
318  return;
319  }
320 
321  for (int color = 1; color <= k; ++color) {
322  necklace[index] = color;
323  gen_necklaces(necklace, index + 1, n, k, necklaces);
324  }
325 }
int64_t necklaces(int n, int k)
Calculate the number of necklaces of length n with k colors.
Definition: comb.cpp:188
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

◆ gen_partition_distinct()

void gpmp::Comb::gen_partition_distinct ( int  n,
int  max,
std::vector< int > &  partition,
std::vector< std::vector< int >> &  result 
)

Generate distinct partitions of a positive integer.

Parameters
nThe integer to partition
maxThe maximum value in each partition
partitionThe current partition being generated
resultThe resulting vector of partitions

Definition at line 257 of file comb.cpp.

260  {
261  if (n == 0) {
262  result.push_back(partition);
263  return;
264  }
265 
266  for (int i = std::min(n, max); i >= 1; --i) {
267  if (std::find(partition.begin(), partition.end(), i) ==
268  partition.end()) {
269  partition.push_back(i);
270  gen_partition_distinct(n - i, i, partition, result);
271  partition.pop_back();
272  }
273  }
274 }
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

◆ generateLyndonWords()

std::vector< std::string > gpmp::Comb::generateLyndonWords ( int  n)

Generate all Lyndon words of length n.

Parameters
nThe length of the Lyndon words
Returns
A vector of Lyndon words

Definition at line 233 of file comb.cpp.

233  {
234  std::vector<std::string> lyndonWords;
235  gen_lyndon_words("", n, n, lyndonWords);
236  return lyndonWords;
237 }

Referenced by main().

◆ generateNecklaces()

std::vector< std::vector< int > > gpmp::Comb::generateNecklaces ( int  n,
int  k 
)

Generate all necklaces of length n with k colors.

Parameters
nThe length of the necklace
kThe number of colors
Returns
A vector of necklaces

Definition at line 216 of file comb.cpp.

216  {
217  std::vector<std::vector<int>> necklaces;
218  std::vector<int> necklace(n, 0);
219  gen_necklaces(necklace, 0, n, k, necklaces);
220  return necklaces;
221 }

Referenced by main().

◆ generatePartitions()

void gpmp::Comb::generatePartitions ( int  n,
int  max,
std::vector< int > &  partition,
std::vector< std::vector< int >> &  result 
)

Generate partitions of a positive integer.

Parameters
nThe integer to partition
maxThe maximum value in each partition
partitionThe current partition being generated
resultThe resulting vector of partitions

Definition at line 240 of file comb.cpp.

243  {
244  if (n == 0) {
245  result.push_back(partition);
246  return;
247  }
248 
249  for (int i = std::min(n, max); i >= 1; --i) {
250  partition.push_back(i);
251  generatePartitions(n - i, i, partition, result);
252  partition.pop_back();
253  }
254 }
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

◆ gray_code()

std::vector< int > gpmp::Comb::gray_code ( int  n)

Generate the Gray code sequence of length n.

Parameters
nThe number of bits in the sequence
Returns
A vector representing the Gray code

Definition at line 122 of file comb.cpp.

122  {
123  std::vector<int> gray;
124  for (int i = 0; i < (1 << n); ++i) {
125  gray.push_back(i ^ (i >> 1));
126  }
127  return gray;
128 }

Referenced by main().

◆ lyndonWords()

int64_t gpmp::Comb::lyndonWords ( int  n)

Calculate the number of Lyndon words of length n.

Parameters
nThe length of the Lyndon word
Returns
The number of Lyndon words

Definition at line 224 of file comb.cpp.

224  {
225  int64_t result = 0;
226  for (int d : divisors(n)) {
227  result += phi(n / d);
228  }
229  return result;
230 }
int phi(int n)
Calculate Euler's totient function (φ)
Definition: comb.cpp:199
std::vector< int > divisors(int n)
Calculate the divisors of a positive integer.
Definition: comb.cpp:349

Referenced by main().

◆ necklaces()

int64_t gpmp::Comb::necklaces ( int  n,
int  k 
)

Calculate the number of necklaces of length n with k colors.

Parameters
nThe length of the necklace
kThe number of colors
Returns
The number of necklaces

Definition at line 188 of file comb.cpp.

188  {
189  if (n == 0)
190  return 0;
191  int64_t result = 0;
192  for (int d : divisors(n)) {
193  result += phi(n / d) * pow(k, d);
194  }
195  return result / n;
196 }

Referenced by main().

◆ partition_distinct()

std::vector< std::vector< int > > gpmp::Comb::partition_distinct ( int  n)

Generate all distinct partitions of a positive integer n.

Parameters
nThe integer to partition
Returns
A vector of distinct partitions

Definition at line 159 of file comb.cpp.

159  {
160  std::vector<std::vector<int>> result;
161  std::vector<int> partition;
162  gen_partition_distinct(n, n, partition, result);
163  return result;
164 }

Referenced by main().

◆ partitions()

std::vector< std::vector< int > > gpmp::Comb::partitions ( int  n)

Generate all partitions of a positive integer n.

Parameters
nThe integer to partition
Returns
A vector of partitions

Definition at line 91 of file comb.cpp.

91  {
92  std::vector<std::vector<int>> result;
93  std::vector<int> partition;
94  generatePartitions(n, n, partition, result);
95  return result;
96 }

Referenced by main().

◆ permutation()

int64_t gpmp::Comb::permutation ( int  n,
int  r 
)

Calculate the number of permutations (nPr)

Parameters
nThe total number of items
rThe number of items to choose
Returns
The number of permutations

Definition at line 41 of file comb.cpp.

41  {
42  if (n < r)
43  return 0; // Invalid input
44  int64_t result = 1;
45  for (int i = 0; i < r; ++i) {
46  result *= (n - i);
47  }
48  return result;
49 }

Referenced by main().

◆ permutations()

template<typename T >
std::vector<std::vector<T> > gpmp::Comb::permutations ( const std::vector< T > &  vec)

Generate all permutations of a vector.

Template Parameters
TThe type of elements in the vector
Parameters
vecThe vector
Returns
A vector of all permutations

Referenced by main().

◆ phi()

int gpmp::Comb::phi ( int  n)

Calculate Euler's totient function (φ)

Parameters
nThe integer
Returns
The value of φ(n)

Definition at line 199 of file comb.cpp.

199  {
200  int result = n;
201  for (int p = 2; p * p <= n; ++p) {
202  if (n % p == 0) {
203  while (n % p == 0) {
204  n /= p;
205  }
206  result -= result / p;
207  }
208  }
209  if (n > 1) {
210  result -= result / n;
211  }
212  return result;
213 }

◆ stirling_num()

int64_t gpmp::Comb::stirling_num ( int  n,
int  k 
)

Calculate the Stirling number of the second kind.

Parameters
nThe total number of items
kThe number of non-empty subsets
Returns
The Stirling number

Definition at line 111 of file comb.cpp.

111  {
112  if (n < k)
113  return 0;
114  if (n == 0 && k == 0)
115  return 1;
116  if (k == 0 || k == n)
117  return 1;
118  return k * stirling_num(n - 1, k) + stirling_num(n - 1, k - 1);
119 }
int64_t stirling_num(int n, int k)
Calculate the Stirling number of the second kind.
Definition: comb.cpp:111

Referenced by main().

◆ subfactorial()

int64_t gpmp::Comb::subfactorial ( int  n)

Calculate the subfactorial (!n) or derangement of n.

Parameters
nThe number
Returns
The subfactorial of n

Definition at line 131 of file comb.cpp.

131  {
132  if (n == 0)
133  return 1;
134  if (n == 1)
135  return 0;
136  return (n - 1) * (subfactorial(n - 1) + subfactorial(n - 2));
137 }
int64_t subfactorial(int n)
Calculate the subfactorial (!n) or derangement of n.
Definition: comb.cpp:131

Referenced by main().

◆ subsequences()

int64_t gpmp::Comb::subsequences ( int  n,
int  k 
)

Calculate the number of subsequences of length k from n items.

Parameters
nThe total number of items
kThe length of subsequences
Returns
The number of subsequences

Definition at line 141 of file comb.cpp.

141  {
142  if (k > n)
143  return 0;
144  std::vector<int64_t> dp(n + 1, 0);
145  dp[0] = 1;
146  for (int i = 1; i <= n; ++i) {
147  for (int j = i - 1; j >= 0; --j) {
148  dp[i] += dp[j];
149  }
150  }
151  int64_t result = 0;
152  for (int i = n - k; i <= n; ++i) {
153  result += dp[i];
154  }
155  return result;
156 }

Referenced by main().


The documentation for this class was generated from the following files: