SUM OF SUBSETS PROBLEM
ABHISHEK KUMAR SINGH
This is a simple algorithm, but it demonstrates that sometimes
you need to return to a previous state and re-evaluate a
previous decision in order to solve a problem.
Backtracking is a general algorithmic technique that
considers searching every possible combination in order to
solve an optimization problem. Backtracking uses depth-first
search approach. By inserting more knowledge of the
problem, the search tree can be pruned to avoid considering
cases that don't look promising. While backtracking is useful
for hard problems to which we do not know more efficient
solutions, it is a poor solution for the everyday problems that
other techniques are much better at solving.
Sum of Subsets Using Backtracking
Subset sum problem is to find subset of elements
that are selected from a given set whose sum adds
up to a given number .
Ex: let A be a set
A={5,7,10,12,15,18,20}
and given sum m=35
so we have the following subsets that add
up to 35 are:
{15,20},{18,7,10},{5,10,20}, and
{18,12,5}
Assumption and considerations
set contains non-negative values.
input set is unique (no duplicates are
presented).
Solution Using NAÏVE APPROACH
2x2x2x2x2x……2=2n
x1 x2 x3 …..xn
So in totality we have 2
n
subsets
Let w1,w2,w3,………….wn
be the given set of n numbers
Let x1,x2,x3,…….xn belongs to {0,1}
If xi = 1 then wi is chosen
xi=0 then wi is not chosen
0
2
26
11 6 7 9 4 5 0
0
0
4
2
0
5
0
0
4
5
0
4
2
5 50 0 0
A={2,4,5} and m=9
Promising Conditions
where
s=∑wixi from i=1 to k-1
r=∑wi from i=k to n
s+r>=m
s+w(k+1)<=m
Solution Using BACKTRACKING
Algorithm:
sumofsubset(s,k,r)
{
X[k]=1;
if (s+W[k]=m) then write(X[1:k]);
else if (s+W[k]+W[k+1]<=m)
then sumofsubset(s+W[k], k+1,r- W[k]);
if ((s+ r-W[k]>=m)and(s+ W[k+1]<=m)) then
{
X[k]=0;
sumofsubset(s, k+1, r- W[k]);
}
}
thankyou

sum of subset problem using Backtracking

  • 1.
    SUM OF SUBSETSPROBLEM ABHISHEK KUMAR SINGH
  • 2.
    This is asimple algorithm, but it demonstrates that sometimes you need to return to a previous state and re-evaluate a previous decision in order to solve a problem. Backtracking is a general algorithmic technique that considers searching every possible combination in order to solve an optimization problem. Backtracking uses depth-first search approach. By inserting more knowledge of the problem, the search tree can be pruned to avoid considering cases that don't look promising. While backtracking is useful for hard problems to which we do not know more efficient solutions, it is a poor solution for the everyday problems that other techniques are much better at solving.
  • 3.
    Sum of SubsetsUsing Backtracking Subset sum problem is to find subset of elements that are selected from a given set whose sum adds up to a given number . Ex: let A be a set A={5,7,10,12,15,18,20} and given sum m=35 so we have the following subsets that add up to 35 are: {15,20},{18,7,10},{5,10,20}, and {18,12,5}
  • 4.
    Assumption and considerations setcontains non-negative values. input set is unique (no duplicates are presented).
  • 5.
    Solution Using NAÏVEAPPROACH 2x2x2x2x2x……2=2n x1 x2 x3 …..xn So in totality we have 2 n subsets Let w1,w2,w3,………….wn be the given set of n numbers Let x1,x2,x3,…….xn belongs to {0,1} If xi = 1 then wi is chosen xi=0 then wi is not chosen
  • 6.
    0 2 26 11 6 79 4 5 0 0 0 4 2 0 5 0 0 4 5 0 4 2 5 50 0 0 A={2,4,5} and m=9
  • 7.
    Promising Conditions where s=∑wixi fromi=1 to k-1 r=∑wi from i=k to n s+r>=m s+w(k+1)<=m Solution Using BACKTRACKING
  • 8.
    Algorithm: sumofsubset(s,k,r) { X[k]=1; if (s+W[k]=m) thenwrite(X[1:k]); else if (s+W[k]+W[k+1]<=m) then sumofsubset(s+W[k], k+1,r- W[k]); if ((s+ r-W[k]>=m)and(s+ W[k+1]<=m)) then { X[k]=0; sumofsubset(s, k+1, r- W[k]); } }
  • 9.