Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
subset_sum_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 <iostream>
38# include <iomanip>
39
40# include <Subset_Sum.H>
41# include <print_rule.H>
42
43using namespace Aleph;
44
45namespace
46{
54 void print_values(const Array<int> & vals)
55 {
56 std::cout << "Values: [";
57 for (size_t i = 0; i < vals.size(); ++i)
58 {
59 if (i > 0)
60 std::cout << ", ";
61 std::cout << vals[i];
62 }
63 std::cout << "]\n";
64 }
65
74 void print_solution(const Array<int> & vals, const Array<size_t> & idx)
75 {
76 if (idx.is_empty())
77 {
78 std::cout << "Chosen subset: [empty]\n";
79 return;
80 }
81
82 int sum = 0;
83 std::cout << "Chosen subset: ";
84 for (size_t i = 0; i < idx.size(); ++i)
85 {
86 if (i > 0)
87 std::cout << " + ";
88 std::cout << vals[idx[i]];
89 sum += vals[idx[i]];
90 }
91 std::cout << " = " << sum << "\n";
92 }
93}
94
110int main()
111{
112 std::cout << "\n=== Subset Sum Toolkit ===\n\n";
113
114 // Example 1: DP with reconstruction
115 {
116 std::cout << "Scenario A: Classical DP reconstruction\n";
117 print_rule();
118 Array<int> vals = {3, 34, 4, 12, 5, 2};
119 const int target = 9;
120
122 std::cout << "Target: " << target << "\n";
123
124 const auto r = subset_sum(vals, target);
125 if (r.exists)
126 print_solution(vals, r.selected_indices);
127 else
128 std::cout << "No subset found.\n";
129 print_rule();
130 std::cout << "\n";
131 }
132
133 // Example 2: existence-only queries
134 {
135 std::cout << "Scenario B: Existence-only API\n";
136 print_rule();
137 Array<int> vals = {1, 5, 11, 5};
139 std::cout << "Sum to 11? " << (subset_sum_exists(vals, 11) ? "Yes" : "No")
140 << "\n";
141 std::cout << "Sum to 100? " << (subset_sum_exists(vals, 100) ? "Yes" : "No")
142 << "\n";
143 print_rule();
144 std::cout << "\n";
145 }
146
147 // Example 3: counting subsets
148 {
149 std::cout << "Scenario C: Counting all subsets by target\n";
150 print_rule();
151 Array<int> vals = {1, 2, 3, 4, 5};
153 for (int t = 3; t <= 10; ++t)
154 std::cout << " target=" << std::setw(2) << t << " -> "
155 << subset_sum_count(vals, t) << "\n";
156 print_rule();
157 std::cout << "\n";
158 }
159
160 // Example 4: MITM for larger n / signed values
161 {
162 std::cout << "Scenario D: Meet-in-the-middle with signed values\n";
163 print_rule();
164 Array<int> vals = {-7, -3, -2, 5, 8, 11, 13, 21};
165 const int target = 10;
167 std::cout << "Target: " << target << "\n";
168
169 const auto r = subset_sum_mitm(vals, target);
170 if (r.exists)
171 print_solution(vals, r.selected_indices);
172 else
173 std::cout << "No subset found.\n";
174 print_rule();
175 std::cout << "\n";
176 }
177
178 // Example 5: DP vs MITM consistency
179 {
180 std::cout << "Scenario E: DP vs MITM consistency table\n";
181 print_rule();
182 Array<int> vals = {2, 3, 7, 8, 10};
184 std::cout << std::setw(8) << std::left << "Target"
185 << std::setw(8) << "DP"
186 << std::setw(8) << "MITM" << "\n";
187 for (int t = 0; t <= 30; t += 5)
188 {
189 const bool dp_ans = subset_sum_exists(vals, t);
190 const auto mitm_r = subset_sum_mitm(vals, t);
191 std::cout << std::setw(8) << std::left << t
192 << std::setw(8) << (dp_ans ? "true" : "false")
193 << std::setw(8) << (mitm_r.exists ? "true" : "false") << "\n";
194 }
195 print_rule();
196 }
197
198 std::cout << "\nDone.\n";
199 return 0;
200}
Subset sum algorithms: classical DP and meet-in-the-middle.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:348
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool subset_sum_exists(const Array< T > &values, T target)
Subset sum existence check (space-optimized).
Definition Subset_Sum.H:223
size_t subset_sum_count(const Array< T > &values, T target)
Count the number of subsets summing to target.
Definition Subset_Sum.H:267
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.
Subset_Sum_Result< T > subset_sum_mitm(const Array< T > &values, T target)
Subset sum via meet-in-the-middle (MITM).
Definition Subset_Sum.H:313
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
Subset_Sum_Result< T > subset_sum(const Array< T > &values, T target)
Subset sum via classical DP with reconstruction.
Definition Subset_Sum.H:147
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
int main()
Demonstrates multiple subset-sum algorithms and queries using the Aleph-w library.
gsl_rng * r