Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
subset_sum.cpp File Reference

Implementation of the Subset Sum problem. More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for subset_sum.cpp:

Namespaces

namespace  backtracking
 for vector container
 
namespace  Subsets
 Functions for the Subset Sum problem.
 

Functions

uint64_t backtracking::subset_sum::number_of_subsets (int32_t sum, const std::vector< int32_t > &in_arr)
 The main function implements count of subsets.
 
static void test ()
 Test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of the Subset Sum problem.

We are given an array and a sum value. The algorithm finds all the subsets of that array with sum equal to the given sum and return such subsets count. This approach will have exponential time complexity.

Author
Swastika Gupta

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
103 {
104 test(); // run self-test implementations
105 return 0;
106}
static void test()
Test implementations.
Definition subset_sum.cpp:57
Here is the call graph for this function:

◆ number_of_subsets()

uint64_t backtracking::subset_sum::number_of_subsets ( int32_t sum,
const std::vector< int32_t > & in_arr )

The main function implements count of subsets.

Parameters
sumis the required sum of any subset
in_arris the input array
Returns
count of the number of subsets with required sum
33 {
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}
T size(T... args)
bool check(const std::string &s, const std::unordered_set< std::string > &strSet, int pos, std::vector< int > *dp)
Function that checks if the string passed in param can be segmented from position 'pos',...
Definition word_break.cpp:80
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test implementations.

Returns
void
57 {
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}
T endl(T... args)
Here is the call graph for this function: