SubsetSum ProblemThe SubsetSum Problem is to find a subset's' of the given set S = (S_{1} S_{2} S_{3}...S_{n}) where the elements of the set S are n positive integers in such a manner that s'∈S and sum of the elements of subset's' is equal to some positive integer 'X.' The SubsetSum Problem can be solved by using the backtracking approach. In this implicit tree is a binary tree. The root of the tree is selected in such a way that represents that no decision is yet taken on any input. We assume that the elements of the given set are arranged in increasing order: S_{1} ≤ S_{2} ≤ S_{3}... ≤ S_{n} The left child of the root node indicated that we have to include 'S_{1}' from the set 'S' and the right child of the root indicates that we have to execute 'S_{1}'. Each node stores the total of the partial solution elements. If at any stage the sum equals to 'X' then the search is successful and terminates. The dead end in the tree appears only when either of the two inequalities exists:
s'+ S_{i} + 1 > X
Example: Given a set S = (3, 4, 5, 6) and X =9. Obtain the subset sum using Backtracking approach. Solution: The implicit binary tree for the subset sum problem is shown as fig: The number inside a node is the sum of the partial solution elements at a particular level. Thus, if our partial solution elements sum is equal to the positive integer 'X' then at that time search will terminate, or it continues if all the possible solution needs to be obtained.
Next TopicN Queens Problems
