-1

This chunk of code is the function that solves a variant of a subset sum problem, but I dont really understand how it gets the answer.

The function returns the subset sum that's closest to the value, if its the same as the value then it just return the value, if two subsets sums are equally close to the value then return the greater sum.

This is it

public static List<int> SolveSubsetSum(int value, IEnumerable<int> xs) 
{
    int b = 2*value+1;
    List<int>[] cols = new List<int>[b];
    cols[0] = new List<int>();
    foreach(int xi in xs) {
        for(int s = b-xi-1; s >= 0; s--) {
            if(cols[s+xi] == null && cols[s] != null) {
               List<int> cln = new List<int>(cols[s]);
               cln.Add(xi);
               cols[s+xi] = cln;
            }
        }
    }
    for(int d = 0; d <= value; d++) {
        if(cols[value+d] != null) {
            return cols[value+d];
        }
        else if(cols[value-d] != null) {
            return cols[value-d];
        } 
    }
    return cols[0];
}

I understand the standard stuff, but I have no clue whats going on in the loops.

My main questions are

  • Why does it need an array of 2*value+1 collections ?

  • Whats going on in the loops ?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
user3621898
  • 589
  • 5
  • 24
  • I think the best way you can understand what is going on is to debug step by step, and look at what this code is doing, thats how you learn to code – bto.rdz Dec 14 '15 at 20:06
  • While not linking is certainly rude, I don't think it's reasonable to claim that the user is passing off this code as their own, considering they clearly state they don't understand it. For it to be plagiarism, the question would have to state or strongly imply that the code was the author's own. http://dictionary.reference.com/browse/plagiarism – Michael Blackburn Dec 14 '15 at 20:14
  • @MichaelBlackburn: ok no problem, but a quick read of both question and answer should solve the issue don't you think? – Willem Van Onsem Dec 14 '15 at 20:15

1 Answers1

3

Somehow, the code in your question is an exact copy of this (my1) answer that implemented this algorithm. I agree that the algorithm itself is not new, but both the structure of the code as well as the variables indicate copying. If you had read the question (and answer) carefully, you could have found out that the problem being solved is the closest subset sum problem. This means that in case no such set can be constructed, you return the set with the smallest difference.

Now since such set can be larger that the requested sum, you need at most 2 K+1 collections with K the requested number, because it is possible that the smallest number in your collection is K-1. Say for instance that the given numbers are {2,5,8,10} and you wish to construct a subset of 6, than the nearest subset sum is {2,5}. You need enough "collections" to store subsets that can be larger.

What's going on in the loops is explain in the linked answer.

1 Who wrote the answer is irrelevant, nevertheless is it easier to detect your own work.

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I actually had no idea, I'm just looking through a friends code – user3621898 Dec 14 '15 at 20:16
  • @user3621898: ok. I've modified the statement making it a bit softer. Simply copying from SO however is at least rather *rude*. I didn't dv your question btw. – Willem Van Onsem Dec 14 '15 at 20:18
  • @CommuSoft you may consider updating your original explanation (if you feel that there some more information needed) - I've made this post duplicate of your original answer. – Alexei Levenkov Dec 14 '15 at 20:20
  • @AlexeiLevenkov: yes, I will update it, will take some time. – Willem Van Onsem Dec 14 '15 at 20:23
  • @user3621898 your friend clearly violated license used by SO by not giving proper attribution (http://creativecommons.org/licenses/by-sa/3.0/), which in turn could have significantly help you to understand the code... – Alexei Levenkov Dec 14 '15 at 20:24
  • @CommuSoft for(int s = b-xi-1; s >= 0; s--) This part I really dont understand, why b-xi-1 and why are you iterating backwards ? – user3621898 Dec 14 '15 at 20:43
  • @user3621898: I'm working on a more in depth explanation. Give me an hour (or something like that). – Willem Van Onsem Dec 14 '15 at 20:46
  • @user3621898: explanation updated, please post comments there, because both questions redirect to that answer. – Willem Van Onsem Dec 14 '15 at 21:37