Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
combinatorics_enumeration_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <iomanip>
38# include <iostream>
39
40# include <ah-comb.H>
41
42using namespace Aleph;
43
44namespace
45{
46 template <class Seq>
47 void print_seq(const Seq & s)
48 {
49 std::cout << "[";
50 for (size_t i = 0; i < s.size(); ++i)
51 {
52 if (i > 0)
53 std::cout << ", ";
54 std::cout << s(i);
55 }
56 std::cout << "]";
57 }
58
59 void rule()
60 {
61 std::cout << "------------------------------------------------------------\n";
62 }
63
65 {
66 std::cout << "[1] next_permutation with duplicates (multiset)\n";
67 rule();
68
69 Array<int> a = {1, 1, 2, 3};
70
71 size_t count = 1;
72 std::cout << std::setw(3) << count << " : ";
73 print_seq(a);
74 std::cout << "\n";
75
76 while (next_permutation(a))
77 {
78 ++count;
79 std::cout << std::setw(3) << count << " : ";
80 print_seq(a);
81 std::cout << "\n";
82 }
83
84 std::cout << "Total distinct lexicographic permutations: " << count << "\n";
85 std::cout << "After last step, reset to first: ";
86 print_seq(a);
87 std::cout << "\n\n";
88 }
89
91 {
92 std::cout << "[2] next_permutation with custom comparator (descending order)\n";
93 rule();
94
95 Array<int> a = {4, 3, 2, 1}; // first in Aleph::greater order
96
97 for (size_t step = 1; step <= 5; ++step)
98 {
99 std::cout << "Step " << step << " -> ";
100 print_seq(a);
101 std::cout << "\n";
103 }
104
105 std::cout << "\n";
106 }
107
109 {
110 std::cout << "[3] next_combination_indices (k-combinations over [0..n))\n";
111 rule();
112
113 const size_t n = 6;
114 Array<size_t> idx = {0, 1, 2};
115
116 size_t count = 0;
117 while (true)
118 {
119 ++count;
120 std::cout << std::setw(3) << count << " : ";
121 print_seq(idx);
122 std::cout << "\n";
123
124 if (not next_combination_indices(idx, n))
125 break;
126 }
127
128 std::cout << "Total combinations C(" << n << ", 3): " << count
129 << " (verified by combination_count = "
130 << combination_count(n, 3) << ")\n\n";
131 }
132
134 {
135 std::cout << "[4] next_combination_mask (fixed-popcount bitmask)\n";
136 rule();
137
138 const size_t n = 6;
139 uint64_t mask = first_combination_mask(3); // 000111
140
141 size_t count = 0;
142 while (true)
143 {
144 ++count;
145 std::cout << std::setw(3) << count << " : mask = ";
146 for (size_t b = n; b > 0; --b)
147 std::cout << (((mask >> (b - 1)) & 1ULL) ? '1' : '0');
148 std::cout << "\n";
149
150 if (not next_combination_mask(mask, n))
151 break;
152 }
153
154 std::cout << "Total bitmask combinations: " << count << "\n\n";
155 }
156
158 {
159 std::cout << "[5] for_each_combination / build_combinations\n";
160 rule();
161
163 "geom", "strings", "graphs", "dp", "net"
164 };
165
166 std::cout << "Feature triplets:\n";
168 [] (const Array<std::string> & c)
169 {
170 std::cout << " - ";
171 print_seq(c);
172 std::cout << "\n";
173 return true;
174 });
175
176 auto pairs = build_combinations(features, 2);
177 std::cout << "\nMaterialized pairs: " << pairs.size() << "\n";
178 std::cout << "First: ";
179 print_seq(pairs(0));
180 std::cout << " | Last: ";
181 print_seq(pairs(pairs.size() - 1));
182 std::cout << "\n\n";
183 }
184} // namespace
185
186int main()
187{
188 std::cout << "\n=== Combinatorics / Enumeration ===\n\n";
189
195
196 std::cout << "Done.\n";
197 return 0;
198}
Combinatorics utilities: permutations, combinations and matrix transposition.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool for_each_combination(const Array< T > &values, const size_t k, Op &&op)
Enumerate all k-combinations of an Array<T> in lexicographic index order.
Definition ah-comb.H:1064
Array< Array< T > > build_combinations(const Array< T > &values, const size_t k)
Materialize all k-combinations of Array<T>.
Definition ah-comb.H:1088
bool next_combination_indices(Array< size_t > &idx, const size_t n, const bool reset_on_last=true)
Advance an index-combination [i0 < i1 < ... < i(k-1)] to the next one.
Definition ah-comb.H:917
bool next_combination_mask(uint64_t &mask, const size_t n, const bool reset_on_last=true)
Advance a fixed-popcount bitmask to the next combination (Gosper hack).
Definition ah-comb.H:963
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
bool next_permutation(Array< T > &a, Compare cmp=Compare(), const bool reset_on_last=true)
Compute the next lexicographic permutation of an Array.
Definition ah-comb.H:834
uint64_t first_combination_mask(const size_t k)
Build the first k-of-64 combination mask (k low bits set).
Definition ah-comb.H:934
size_t combination_count(size_t n, size_t k)
Compute n choose k with overflow checks.
Definition ah-comb.H:861
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
void print_seq(const C< T > &c)