Fast solution to Subset sum

后端 未结 6 1454
不思量自难忘°
不思量自难忘° 2020-11-27 19:09

Consider this way of solving the Subset sum problem:

def subset_summing_to_zero (activities):
  subsets = {0: []}
  for (activity, cost) in activities.iterit         


        
6条回答
  •  不知归路
    2020-11-27 20:02

    While my previous answer describes the polytime approximate algorithm to this problem, a request was specifically made for an implementation of Pisinger's polytime dynamic programming solution when all xi in x are positive:

    from bisect import bisect
    
    def balsub(X,c):
        """ Simple impl. of Pisinger's generalization of KP for subset sum problems
        satisfying xi >= 0, for all xi in X. Returns the state array "st", which may
        be used to determine if an optimal solution exists to this subproblem of SSP.
        """
        if not X:
            return False
        X = sorted(X)
        n = len(X)
        b = bisect(X,c)
        r = X[-1]
        w_sum = sum(X[:b])
        stm1 = {}
        st = {}
        for u in range(c-r+1,c+1):
            stm1[u] = 0
        for u in range(c+1,c+r+1):
            stm1[u] = 1
        stm1[w_sum] = b
        for t in range(b,n+1):
            for u in range(c-r+1,c+r+1):
                st[u] = stm1[u]
            for u in range(c-r+1,c+1):
                u_tick = u + X[t-1]
                st[u_tick] = max(st[u_tick],stm1[u])
            for u in reversed(range(c+1,c+X[t-1]+1)):
                for j in reversed(range(stm1[u],st[u])):
                    u_tick = u - X[j-1]
                    st[u_tick] = max(st[u_tick],j)
        return st
    

    Wow, that was headache-inducing. This needs proofreading, because, while it implements balsub, I can't define the right comparator to determine if the optimal solution to this subproblem of SSP exists.

提交回复
热议问题