 Hello and welcome to this video lecture on backtracking approach. I am Vinit Thribuwan from Department of Computer Science and Engineering from Walchand Institute of Technology, Solapur and today we shall see how to solve the sum of subsets problem by applying the backtracking approach. So the learning outcomes of this video lecture is at the end of this session you will be able to apply backtracking approach to solve the sum of subsets problem. So what exactly is the sum of subsets problem? We are given n distinct positive numbers, we may also call it as weights. The objective of the problem is to find all combination of weights whose sum is equal to a given weight m. Now we know that whenever we apply backtracking approach we generate a state space tree. So for this problem also state space tree will be generated to check the possibilities of the subset where the total weight of the subset will be equal to m and we will generate the tree by keeping the weight less than equal to m. When we start adding the nodes to the tree to check the weight of the subset we will keep the weight of the subset as you can see here less than equal to m. And if any path in the tree is crossing over m the weight is increasing and going beyond m we will cancel that path and then backtrack the solution to the previous node. We shall see how to solve this problem using the backtracking approach. Now consider a simple example we have four positive distinct weights 5, 10, 15 and 20 and m that is the subset that we have to find is equivalent to 25. Now we have to find the sum of subsets which is equal to m. Now we shall see how to apply backtracking approach to solve this particular solution. Initially we start with this node now here two values are given one is the value of the subset and the other value is the summation of all the values given as weights. Now 50 is the summation of 5 plus 10 that is 15 plus 15 that is 30 plus 20 that is 50. So initially in the initial node we write 0, 50, 0 standing for the value of the subset initially there is no weight added to the subset that is why we write it as 0. And 50 is the summation of all the weights. Now we start with our procedure we add the first node to the tree and the first weight is added in the subset. Now when we add weight 5 to the subset so 0 plus 5 becomes 5 and we reduce the weight of the first object or the first weight from the total weight here. So 5 is been added in the weight of the subset and the weight of the first object or the first given value is reduced from the total summation. So we have the first node as 5 comma 45 where we have included the first weight in the subset. Now we want to see whether we can reach to 25 by adding the second weight. So we have added the second weight as well where we denote it as x2 equals 1. So after adding the second weight to the subset our value of the subset becomes 5 plus 10 that is 15 and we reduce the weight from the total summation that is 10 is reduced from 45 giving us weight 35. So this is the node when we add the first two weights that is 5 and 10 to the subset. Now we want to check what the subset will be when we add the next node. So adding the next node denoting it as x3 equals 1 will give us 15 plus the weight of the third node that is 15 giving us 30 and reducing 50 from 35 gives us 20. So this is the third node that we have added. But if you carefully see we have to find out the subsets of the value 25 and not greater than 25. So this particular node is infeasible and we want to bound this particular path and we don't want to continue it. That is why we kill this particular node and do not continue with the solution path any further. Now since we cannot continue with this path any further since the value of the subset is greater than 25 we backtrack to the solution and do not include the third node. So if we do not include the third node or the third weight that is 15 what value will we get? So x3 equals 0 that means we are not including the third weight. So the weight of the subset will remain 15 and we are reducing the weight of the third value, the value of the third weight that is 15 giving us remaining value as 20. So further we again continue by including the fourth weight now. So if we include the fourth weight that equals x4 equals 1 we see that the weight of the subset becomes 35 and the total weight is reduced to 0 deducting the weight of the fourth object. Now if we see that we have reached to the value 35 of the subset which is again infeasible because the subsets that need to be found should be of the value 25. So this particular solution is again unacceptable because the value that we have obtained is greater than 25. So we again backtrack and find the solution when the fourth value, the weight of the fourth value is not included in the solution and then we come to the final solution that is the weight of the subset is 15 and no other values, no other weights are remaining so we come to this solution but we have to find the sum of subsets to be equal to 25. So we will again backtrack and then find out the other possibilities. So now we backtrack to this particular first node and now we will not be, we include the first node but we will not include the second node. So if we do not include the second node the sum of subset will be 5 but we are reducing the value of the second weight that is 10 from 45 giving us 5 and 35, 5 comma 35. So now we can check and include whether we should include the third weight or not but I can see that if I include the third weight here so that will be 5 plus 15 and again if I include the fourth weight that will be 20 plus 20 which is equal to 40 which is again greater than 25. So here for convenience I will not include the third weight again of course you can add it and see for yourself that that will not lead you to the correct solution but I am again not including the third weight and my weight of the subset is 5 and the remaining weight is 20. Now if I check clearly that when I include the fourth weight so x4 equals 1 gives me the weight of the subset as 25 and the remaining weight is 0. So here I come to the conclusion that when I include the first object or the first weight and the fourth weight so the solution that I get is equivalent to 25 which is the expected solution. So the solution that I get is x1 equals 1 that is I consider the first weight, x2 equals 0 that is I do not include the second weight, x3 equals 0 that is I do not consider the third weight and x4 equals 1 that means I consider the fourth value in the solution and that gives me a solution to the sum of subsets problem. If you continue with the same pattern you will find out all the available solutions which give us the whose sum of subsets is equal to 25 among the given weights. So at this point in time I want you to find out another subset which is equal to m where the value of m is 25 and the weights are as given above. So we shall see the solution to this problem we start with 0, 50 where 0 is the sum of subsets and 50 is the total summation of the weights. So I will show the portion of the state space tree here we do not include the first weight that is why x1 equals 0 and the sum of subsets is 0 and we have reduced the value of the first weight from the total summation. Now we include the first weight so the sum of subset is equal to 0 plus 10 that is 10 and we reduce the weight of the second that is the value of the second weight from the total summation. So now we have 10 comma 35 after including the second weight. Similarly we also include the third weight and the sum of subset becomes 25 and the remaining value is 20 and thereafter we do not include the fourth weight. So the sum of subsets remains 25 and the remaining value is 0. So our solution is x1 equals 0, x2 equals 1, x3 equals 1 and x4 equals 0. So these are the references that I have used.