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

Implements [Sub-set sum problem] (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm, which tells whether a subset with target sum exists or not. More...

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

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.
 
namespace  subset_sum
 Functions for [Sub-set sum problem] (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm.
 

Functions

bool dynamic_programming::subset_sum::subset_sum_recursion (const std::vector< int > &arr, int targetSum, std::vector< std::unordered_map< int, bool > > *dp, int index=0)
 
bool dynamic_programming::subset_sum::subset_sum_problem (const std::vector< int > &arr, const int targetSum)
 
static void test ()
 Test Function.
 
int main ()
 Main function.
 

Detailed Description

Implements [Sub-set sum problem] (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm, which tells whether a subset with target sum exists or not.

In this problem, we use dynamic programming to find if we can pull out a subset from an array whose sum is equal to a given target sum. The overall time complexity of the problem is O(n * targetSum) where n is the size of the array. For example, array = [1, -10, 2, 31, -6], targetSum = -14. Output: true => We can pick subset [-10, 2, -6] with sum as (-10) + 2 + (-6) = -14.

Author
KillerAV

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
122 {
123 test(); // execute the test
124 return 0;
125}
static void test()
Test implementations.
Definition: subset_sum.cpp:57
Here is the call graph for this function:

◆ subset_sum_problem()

bool dynamic_programming::subset_sum::subset_sum_problem ( const std::vector< int > &  arr,
const int  targetSum 
)

Function implementing subset sum algorithm using top-down approach

Parameters
arrinput array
targetSumthe target sum of the subset
Returns
true/false based on if the target sum subset exists or not.
72 {
73 size_t n = arr.size();
75 return subset_sum_recursion(arr, targetSum, &dp);
76}
bool subset_sum_recursion(const std::vector< int > &arr, int targetSum, std::vector< std::unordered_map< int, bool > > *dp, int index=0)
Definition: subset_sum.cpp:43
for std::vector
Definition: partition_problem.cpp:39
T size(T... args)
Here is the call graph for this function:

◆ subset_sum_recursion()

bool dynamic_programming::subset_sum::subset_sum_recursion ( const std::vector< int > &  arr,
int  targetSum,
std::vector< std::unordered_map< int, bool > > *  dp,
int  index = 0 
)

Recursive function using dynamic programming to find if the required sum subset exists or not.

Parameters
arrinput array
targetSumthe target sum of the subset
dpthe map storing the results
Returns
true/false based on if the target sum subset exists or not.
47 {
48
49 if(targetSum == 0) { // Found a valid subset with required sum.
50 return true;
51 }
52 if(index == arr.size()) { // End of array
53 return false;
54 }
55
56 if ((*dp)[index].count(targetSum)) { // Answer already present in map
57 return (*dp)[index][targetSum];
58 }
59
60 bool ans = subset_sum_recursion(arr, targetSum - arr[index], dp, index + 1)
61 || subset_sum_recursion(arr, targetSum, dp, index + 1);
62 (*dp)[index][targetSum] = ans; // Save ans in dp map.
63 return ans;
64}
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test Function.

Returns
void
84 {
85 // custom input vector
86 std::vector<std::vector<int>> custom_input_arr(3);
87 custom_input_arr[0] = std::vector<int> {1, -10, 2, 31, -6};
88 custom_input_arr[1] = std::vector<int> {2, 3, 4};
89 custom_input_arr[2] = std::vector<int> {0, 1, 0, 1, 0};
90
91 std::vector<int> custom_input_target_sum(3);
92 custom_input_target_sum[0] = -14;
93 custom_input_target_sum[1] = 10;
94 custom_input_target_sum[2] = 2;
95
96 // calculated output vector by pal_part Function
97 std::vector<int> calculated_output(3);
98
99 for (int i = 0; i < 3; i++) {
100 calculated_output[i] =
102 custom_input_arr[i], custom_input_target_sum[i]);
103 }
104
105 // expected output vector
106 std::vector<bool> expected_output{true, false, true};
107
108 // Testing implementation via assert function
109 // It will throw error if any of the expected test fails
110 // Else it will give nothing
111 for (int i = 0; i < 3; i++) {
112 assert(expected_output[i] == calculated_output[i]);
113 }
114
115 std::cout << "All tests passed successfully!\n";
116}
bool subset_sum_problem(const std::vector< int > &arr, const int targetSum)
Definition: subset_sum.cpp:72