2

I've to implement a variation of the subset sum problem, my input will be positive and negative decimal, also I will need to know the subset, knowing that exists unfortunately it's not enough.

I've tried the algorithms found on wikipedia, but I can't make them work with negative numbers, and also I can't find the way to obtain the subset if it exists.

Could anyone point me where I could find some pseudo-code, documentation or implementation, for this algorithm.

nbro
  • 15,395
  • 32
  • 113
  • 196
user3633705
  • 31
  • 1
  • 2
  • Have you checked the references from the wikipedia article? Where else have you searched? – Bergi May 14 '14 at 09:33
  • Does the time matter or if it works is enough? – Khashayar May 14 '14 at 09:47
  • there is this C# version with backtracking which uses float numbers, you probably will have to adapt it to avoid floating precision issues http://kunuk.wordpress.com/2012/12/25/backtracking-subset-sum-with-c/ Beware subset sum can quickly run slow for large input as it is NP-complete – Kunukn May 14 '14 at 12:30
  • Yes @Khashayar time matters – user3633705 May 14 '14 at 16:45
  • By now i'm using @Kunukn blog post as a base (also i'm a C# developer) i've modified the code to take a generic input and to work with decimal types. Also i've added an stop by configuration to allow only a maxDepth, this way at least i can customize how it will perform. I know that the problem is NP Complete so i know that it's not possible to know every possible solution. If you want i can send you my mods but I now for sure you will now how to do it, – user3633705 May 14 '14 at 16:49

1 Answers1

1

I wrote the code in Java it checks all the possibilities

import java.util.*;

public class StackOverFlow {

    public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
        Set<Set<T>> sets = new HashSet<Set<T>>();
        if (originalSet.isEmpty()) {
            sets.add(new HashSet<T>());
            return sets;
        }
        List<T> list = new ArrayList<T>(originalSet);
        T head = list.get(0);
        Set<T> rest = new HashSet<T>(list.subList(1, list.size())); 
        for (Set<T> set : powerSet(rest)) {
            Set<T> newSet = new HashSet<T>();
            newSet.add(head);
            newSet.addAll(set);
            sets.add(newSet);
            sets.add(set);
        }       
        return sets;
    }

    public static int sumSet(Set<Integer> set){
        int sum =0;
        for (Integer s : set) {
            sum += s;
        }
        return sum;     
    }

    public static void main(String[] args) {
         Set<Integer> mySet = new HashSet<Integer>();
         mySet.add(-1);
         mySet.add(2);
         mySet.add(3);

         int mySum = 4;
         for (Set<Integer> s : powerSet(mySet)) {
             if(mySum == sumSet(s))
                 System.out.println(s + " = " + sumSet(s));
         }
    }
}

I hope it helps

Khashayar
  • 2,014
  • 3
  • 22
  • 31
  • Thanks a lot @Khashayar, finally i took Kunukn approach is the one that best fits my needs. I really appreciate your help. – user3633705 May 14 '14 at 16:51