40#include <gtest/gtest.h>
47size_t brute_subset_count(
const Array<int> &vals,
const int target)
49 const size_t n = vals.
size();
50 const uint64_t total_masks =
static_cast<uint64_t
>(1) << n;
52 for (uint64_t mask = 0; mask < total_masks; ++mask)
55 for (
size_t i = 0; i < n; ++i)
56 if (mask & (uint64_t(1) << i))
68 for (
size_t idx : selected)
70 if (idx >= vals.
size() or seen[idx])
83 EXPECT_TRUE(
r.exists);
84 EXPECT_EQ(
r.selected_indices.size(), 0u);
87 EXPECT_FALSE(
r.exists);
94 EXPECT_TRUE(
r.exists);
95 EXPECT_EQ(
r.selected_indices.size(), 0u);
102 EXPECT_TRUE(
r.exists);
106 for (
size_t selected_indice :
r.selected_indices)
107 total += vals[selected_indice];
115 EXPECT_FALSE(
r.exists);
132TEST(SubsetSum, CountZeroTarget)
142 EXPECT_THROW(
subset_sum(vals, -1), std::domain_error);
147TEST(SubsetSum, NegativeValuesRejected)
150 EXPECT_THROW(
subset_sum(vals, 10), std::domain_error);
155TEST(SubsetSum, ZeroValueMultiplicityInCount)
161 EXPECT_TRUE(
r.exists);
162 EXPECT_TRUE(valid_selection(vals,
r.selected_indices, 1));
171 EXPECT_TRUE(
r.exists);
174 EXPECT_FALSE(
r.exists);
181 EXPECT_TRUE(
r.exists);
184 for (
size_t k = 0;
k <
r.selected_indices.size(); ++
k)
185 total += vals[
r.selected_indices[
k]];
193 EXPECT_FALSE(
r.exists);
200 for (
int i = 1; i <= 20; ++i)
205 EXPECT_TRUE(
r.exists);
209 EXPECT_TRUE(r2.exists);
212 for (
size_t k = 0;
k < r2.selected_indices.size(); ++
k)
213 total += vals[r2.selected_indices[
k]];
214 EXPECT_EQ(total, 30);
217TEST(SubsetSumMITM, SingleElement)
221 EXPECT_TRUE(
r.exists);
222 EXPECT_EQ(
r.selected_indices.size(), 1u);
223 EXPECT_EQ(
r.selected_indices[0], 0u);
226 EXPECT_FALSE(
r.exists);
229TEST(SubsetSum, StressVsBruteForce)
234 for (
int target = 0; target <= 30; ++target)
239 EXPECT_EQ(dp_ans, mitm_r.exists) <<
"Disagreement at target=" << target;
243TEST(SubsetSum, RandomExistsAndCountVsBruteForce)
245 std::mt19937
rng(4242);
246 for (
int trial = 0; trial < 100; ++trial)
248 const size_t n = 1 +
rng() % 18;
251 for (
size_t i = 0; i < n; ++i)
252 vals.
append(
static_cast<int>(
rng() % 11));
254 const int target =
static_cast<int>(
rng() % 45);
258 const size_t brute_count = brute_subset_count(vals, target);
260 EXPECT_EQ(
count, brute_count);
261 EXPECT_EQ(
exists, brute_count > 0);
264 EXPECT_EQ(
r.exists, brute_count > 0);
266 EXPECT_TRUE(valid_selection(vals,
r.selected_indices, target));
270TEST(SubsetSumMITM, RandomVsBruteForce)
272 std::mt19937
rng(2024);
273 for (
int trial = 0; trial < 80; ++trial)
275 const size_t n = 1 +
rng() % 22;
278 for (
size_t i = 0; i < n; ++i)
279 vals.
append(
static_cast<int>(
rng() % 41) - 20);
281 const int target =
static_cast<int>(
rng() % 61) - 30;
284 const bool brute_exists = brute_subset_count(vals, target) > 0;
285 EXPECT_EQ(mitm.exists, brute_exists);
287 EXPECT_TRUE(valid_selection(vals, mitm.selected_indices, target));
291TEST(SubsetSumMITM, TooManyElementsThrows)
294 for (
int i = 0; i < 128; ++i)
Subset sum algorithms: classical DP and meet-in-the-middle.
Space-efficient bit array implementation.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Contiguous array of bits.
Main namespace for Aleph-w library functions.
bool subset_sum_exists(const Array< T > &values, T target)
Check if a subset summing to target exists (space-optimized).
size_t subset_sum_count(const Array< T > &values, T target)
Count the number of subsets that sum to target.
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
Subset_Sum_Result< T > subset_sum_mitm(const Array< T > &values, T target)
Solve the subset sum problem via meet-in-the-middle (MITM).
Subset_Sum_Result< T > subset_sum(const Array< T > &values, T target)
Solve the subset sum problem via classical DP with reconstruction.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.