|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of the [Subset |
| 4 | + * Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem. |
| 5 | + * @details |
| 6 | + * We are given an array and a sum value. The algorithm finds all |
| 7 | + * the subsets of that array with sum equal to the given sum and return such |
| 8 | + * subsets count. This approach will have exponential time complexity. |
| 9 | + * @author [Swastika Gupta](https://github.com/Swastyy) |
| 10 | + */ |
| 11 | + |
| 12 | +#include <cassert> /// for assert |
| 13 | +#include <iostream> /// for IO operations |
| 14 | +#include <vector> /// for std::vector |
| 15 | + |
| 16 | +/** |
| 17 | + * @namespace backtracking |
| 18 | + * @brief Backtracking algorithms |
| 19 | + */ |
| 20 | +namespace backtracking { |
| 21 | +/** |
| 22 | + * @namespace Subsets |
| 23 | + * @brief Functions for the [Subset |
| 24 | + * Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem. |
| 25 | + */ |
| 26 | +namespace subset_sum { |
| 27 | +/** |
| 28 | + * @brief The main function implements count of subsets |
| 29 | + * @param sum is the required sum of any subset |
| 30 | + * @param in_arr is the input array |
| 31 | + * @returns count of the number of subsets with required sum |
| 32 | + */ |
| 33 | +uint64_t number_of_subsets(int32_t sum, const std::vector<int32_t> &in_arr) { |
| 34 | + int32_t nelement = in_arr.size(); |
| 35 | + uint64_t count_of_subset = 0; |
| 36 | + |
| 37 | + for (int32_t i = 0; i < (1 << (nelement)); i++) { |
| 38 | + int32_t check = 0; |
| 39 | + for (int32_t j = 0; j < nelement; j++) { |
| 40 | + if (i & (1 << j)) { |
| 41 | + check += (in_arr[j]); |
| 42 | + } |
| 43 | + } |
| 44 | + if (check == sum) { |
| 45 | + count_of_subset++; |
| 46 | + } |
| 47 | + } |
| 48 | + return count_of_subset; |
| 49 | +} |
| 50 | +} // namespace subset_sum |
| 51 | +} // namespace backtracking |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief Test implementations |
| 55 | + * @returns void |
| 56 | + */ |
| 57 | +static void test() { |
| 58 | + // 1st test |
| 59 | + std::cout << "1st test "; |
| 60 | + std::vector<int32_t> array1 = {-7, -3, -2, 5, 8}; // input array |
| 61 | + assert(backtracking::subset_sum::number_of_subsets(0, array1) == |
| 62 | + 2); // first argument in subset_sum function is the required sum and |
| 63 | + // second is the input array |
| 64 | + std::cout << "passed" << std::endl; |
| 65 | + |
| 66 | + // 2nd test |
| 67 | + std::cout << "2nd test "; |
| 68 | + std::vector<int32_t> array2 = {1, 2, 3, 3}; |
| 69 | + assert(backtracking::subset_sum::number_of_subsets(6, array2) == |
| 70 | + 3); // here we are expecting 3 subsets which sum up to 6 i.e. |
| 71 | + // {(1,2,3),(1,2,3),(3,3)} |
| 72 | + std::cout << "passed" << std::endl; |
| 73 | + |
| 74 | + // 3rd test |
| 75 | + std::cout << "3rd test "; |
| 76 | + std::vector<int32_t> array3 = {1, 1, 1, 1}; |
| 77 | + assert(backtracking::subset_sum::number_of_subsets(1, array3) == |
| 78 | + 4); // here we are expecting 4 subsets which sum up to 1 i.e. |
| 79 | + // {(1),(1),(1),(1)} |
| 80 | + std::cout << "passed" << std::endl; |
| 81 | + |
| 82 | + // 4th test |
| 83 | + std::cout << "4th test "; |
| 84 | + std::vector<int32_t> array4 = {3, 3, 3, 3}; |
| 85 | + assert(backtracking::subset_sum::number_of_subsets(6, array4) == |
| 86 | + 6); // here we are expecting 6 subsets which sum up to 6 i.e. |
| 87 | + // {(3,3),(3,3),(3,3),(3,3),(3,3),(3,3)} |
| 88 | + std::cout << "passed" << std::endl; |
| 89 | + |
| 90 | + // Test 5 |
| 91 | + std::cout << "5th test "; |
| 92 | + std::vector<int32_t> array5 = {}; |
| 93 | + assert(backtracking::subset_sum::number_of_subsets(6, array5) == |
| 94 | + 0); // here we are expecting 0 subsets which sum up to 6 i.e. we |
| 95 | + // cannot select anything from an empty array |
| 96 | + std::cout << "passed" << std::endl; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Main function |
| 101 | + * @returns 0 on exit |
| 102 | + */ |
| 103 | +int main() { |
| 104 | + test(); // run self-test implementations |
| 105 | + return 0; |
| 106 | +} |
0 commit comments