openGPMP
Open Source Mathematics Package
combinatorics.cpp
Go to the documentation of this file.
1 #include <cstdint>
2 #include <iostream>
4 #include <vector>
5 
6 int main() {
7  gpmp::Comb comb_obj;
8  int n = 5;
9  int r = 3;
10  std::cout << "Permutations: " << comb_obj.permutation(n, r) << std::endl;
11  std::cout << "Combinations: " << comb_obj.combination(n, r) << std::endl;
12  std::cout << "Binomial Coefficient: " << comb_obj.binom_coeff(n, r)
13  << std::endl;
14  std::cout << "Factorial: " << comb_obj.factorial(n) << std::endl;
15 
16  std::vector<int> vec = {1, 2, 3};
17  std::vector<std::vector<int>> perms = comb_obj.permutations(vec);
18  std::cout << "Permutations of {1, 2, 3}:" << std::endl;
19  for (const auto &perm : perms) {
20  for (int x : perm) {
21  std::cout << x << " ";
22  }
23  std::cout << std::endl;
24  }
25 
26  std::vector<std::vector<int>> combs = comb_obj.combinations(vec, r);
27  std::cout << "Combinations of size " << r
28  << " from {1, 2, 3}:" << std::endl;
29  for (const auto &comb : combs) {
30  for (int x : comb) {
31  std::cout << x << " ";
32  }
33  std::cout << std::endl;
34  }
35 
36  std::cout << "Partitions of " << n << ":" << std::endl;
37  std::vector<std::vector<int>> partitions = comb_obj.partitions(n);
38  for (const auto &partition : partitions) {
39  for (int x : partition) {
40  std::cout << x << " ";
41  }
42  std::cout << std::endl;
43  }
44 
45  std::cout << "Bell numbers up to " << n << ":" << std::endl;
46  std::vector<int64_t> bell = comb_obj.bell_num(n);
47  for (int i = 0; i <= n; ++i) {
48  std::cout << "B(" << i << ") = " << bell[i] << std::endl;
49  }
50 
51  std::cout << "Stirling numbers of the second kind (S(5, 3)): "
52  << comb_obj.stirling_num(5, 3) << std::endl;
53 
54  std::cout << "Gray code of length 3:" << std::endl;
55  std::vector<int> gray = comb_obj.gray_code(3);
56  for (int x : gray) {
57  std::cout << x << " ";
58  }
59  std::cout << std::endl;
60 
61  std::cout << "Number of derangements for n=5: " << comb_obj.subfactorial(5)
62  << std::endl;
63 
64  std::cout << "Number of increasing subsequences of length 2 in a "
65  "permutation of length 5: "
66  << comb_obj.subsequences(5, 2) << std::endl;
67 
68  std::cout << "Partitions of " << n << " using distinct parts:" << std::endl;
69  std::vector<std::vector<int>> partition_distinct =
70  comb_obj.partition_distinct(n);
71  for (const auto &partition : partition_distinct) {
72  for (int x : partition) {
73  std::cout << x << " ";
74  }
75  std::cout << std::endl;
76  }
77 
78  std::cout << "Compositions of " << n << ":" << std::endl;
79  std::vector<std::vector<int>> compositions = comb_obj.compositions(n);
80  for (const auto &composition : compositions) {
81  for (int x : composition) {
82  std::cout << x << " ";
83  }
84  std::cout << std::endl;
85  }
86 
87  std::cout << "Number of Dyck words of length 10: " << comb_obj.dyck_words(5)
88  << std::endl;
89 
90  std::cout << "Dyck words of length 2 * 3:" << std::endl;
91  std::vector<std::string> dyck_words = comb_obj.gen_dyck_words_until(3);
92  for (const auto &word : dyck_words) {
93  std::cout << word << std::endl;
94  }
95 
96  std::cout << "Number of necklaces with 4 beads of 2 different colors: "
97  << comb_obj.necklaces(4, 2) << std::endl;
98 
99  std::cout << "Necklaces with 4 beads of 2 different colors:" << std::endl;
100  std::vector<std::vector<int>> necklaces = comb_obj.generateNecklaces(4, 2);
101  for (const auto &necklace : necklaces) {
102  for (int color : necklace) {
103  std::cout << color << " ";
104  }
105  std::cout << std::endl;
106  }
107 
108  std::cout << "Number of Lyndon words of length 3: "
109  << comb_obj.lyndonWords(3) << std::endl;
110 
111  std::cout << "Lyndon words of length 3:" << std::endl;
112  std::vector<std::string> lyndonWords = comb_obj.generateLyndonWords(3);
113  for (const auto &word : lyndonWords) {
114  std::cout << word << std::endl;
115  }
116 
117  return 0;
118 }
A class providing various combinatorial functions and algorithms.
Definition: comb.hpp:47
int64_t subfactorial(int n)
Calculate the subfactorial (!n) or derangement of n.
Definition: comb.cpp:131
int64_t necklaces(int n, int k)
Calculate the number of necklaces of length n with k colors.
Definition: comb.cpp:188
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
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
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
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
int main()