Subset sum problem is to find subset of elements that are selected from a given set whose sum adds up to a given number K. We are considering the set contains non-negative values. It is assumed that the input set is unique (no duplicates are presented).
Exhaustive Search Algorithm for Subset Sum One way to find subsets that sum to K is to consider all possible subsets. A power set contains all those subsets generated from a given set. The size of such a power set is 2N.
Algorithm for sum of subset
Algorithm SUB_SET_PROBLEM, (i,sum, W, remSum)
// Description: Solve sub of subset problem using backtracking
//Input: W: Number for which subset is to be computed
i: Item index
sum: Sum of integers selected so far
remSum: Size of remaining problem i.e. (W - sum)
// Output: Solution tuple X
if FEASIBLE SUB_SET() == 1 then
if (sum == W) then
print X [1...i]
X [i + 1] <- 1
SUB SET PROBLEM(i +1. sum + w[i] + 1 ,W, remSum - w * [i] + 1 )
X[i+1] <- 0 //Exclude the " item
SUB_SET_PROBLEM( i + 1, sum, W, remSum- w[i] + 1 )
if (sum+remSum≥ W) AND (sum == W )
or ( sum + w[i] + 1 <= W) then
First recursive call represents the case when the current item is selected, and hence the problem size is reduced by w[i].
Second recursive call represents the case when we do not select the current item.
Complexity Analysis: It is intuitive to derive the complexity of sum of subset problem. In state space tree, at level i, the tree has 2′ nodes. So given n items, total number of nodes in tree would be 1 + 2 + 2 ^ 2 + 2 ^ 3 +..2^ n
Thus, sum of sub set problem runs in exponential order.
Backtracking Algorithm for Subset Sum Using exhaustive search we consider all subsets irrespective of whether they satisfy given constraints or not. Backtracking can be used to make a systematic consideration of the elements to be selected. Assume given set of 4 elements, say w … w. Tree diagrams can be used to design backtracking algorithms. The following tree diagram depicts approach of generating variable sized tuple.
In the above tree, a node represents function call and a branch represents candidate element. The root node contains 4 children. In other words, root considers every element of the set as different branch. The next level sub-trees correspond to the subsets that includes the parent node. The branches at each level represent tuple element to be considered. For example, if we are at level 1, tuple_vector can take any value of four branches generated. If we are at level 2 of left most node, tuple_vector can take any value of three branches generated, and so on…
For example the left most child of root generates all those subsets that include w. Similarly the second child of root generates all those subsets that includes w and excludes w.
As we go down along depth of tree we add elements so far, and if the added sum is satisfying explicit constraints, we will continue to generate child nodes further. Whenever the constraints are not met, we stop further generation of sub-trees of that node, and backtrack to previous node to explore the nodes not yet explored. In many scenarios, it saves considerable amount of processing time.
The tree should trigger a clue to implement the backtracking algorithm (try yourself). It prints all those subsets whose sum add up to given number. We need to explore the nodes along the breadth and depth of the tree. Generating nodes along breadth is controlled by loop and nodes along the depth are generated using recursion (post order traversal). Pseudo code given below,
if(subset is satisfying the constraint)
print the subset
exclude the current element and consider next element
generate the nodes of present level along breadth of tree and
recur for next levels
Following is the implementation of subset sum using variable size tuple vector. Note that the following program explores all possibilities similar to exhaustive search. It is to demonstrate how backtracking can be used. See next code to verify, how we can optimize the backtracking solution.
The power of backtracking appears when we combine explicit and implicit constraints, and we stop generating nodes when these checks fail. We can improve the above algorithm by strengthening the constraint checks and presorting the data. By sorting the initial array, we need not to consider rest of the array, once the sum so far is greater than target number. We can backtrack and check other possibilities.
Similarly, assume the array is presorted and we found one subset. We can generate next node excluding the present node only when inclusion of next node satisfies the constraints. Given below is optimized implementation (it prunes the subtree if it is not satisfying constraints).
Please Login to comment...