Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:35:18+00:00 2026-06-01T04:35:18+00:00

Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =

  • 0

Consider this way of solving the Subset sum problem:

def subset_summing_to_zero (activities):
  subsets = {0: []}
  for (activity, cost) in activities.iteritems():
      old_subsets = subsets
      subsets = {}
      for (prev_sum, subset) in old_subsets.iteritems():
          subsets[prev_sum] = subset
          new_sum = prev_sum + cost
          new_subset = subset + [activity]
          if 0 == new_sum:
              new_subset.sort()
              return new_subset
          else:
              subsets[new_sum] = new_subset
  return []

I have it from here:

http://news.ycombinator.com/item?id=2267392

There is also a comment which says that it is possible to make it “more efficient”.

How?

Also, are there any other ways to solve the problem which are at least as fast as the one above?

Edit

I’m interested in any kind of idea which would lead to speed-up. I found:

https://en.wikipedia.org/wiki/Subset_sum_problem#cite_note-Pisinger09-2

which mentions a linear time algorithm. But I don’t have the paper, perhaps you, dear people, know how it works? An implementation perhaps? Completely different approach perhaps?

Edit 2

There is now a follow-up:
Fast solution to Subset sum algorithm by Pisinger

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:35:21+00:00Added an answer on June 1, 2026 at 4:35 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this related question: Deep cloning objects Is this really the best way to
Consider this piece of XML: <ListBox x:Name=pictureBox ItemsSource={Binding} MouseDoubleClick=item_DoubleClick > Is there any way
Consider this case class: case class IntPrinter(implicit val i: Int) { def print()(implicit i:
Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
Now I have my website built on PHP & Mysql. Consider this like a
Consider this code: template<typename T> class Base { template<typename U> friend void f(void *ptr)
Consider this package structure java.assignments Main.java java.assignments.lab1 Exe1.java java.assignments.lab2 Exe1.java Exe2.java Exe3.java java.assignments.lab3 Exe1.java
Consider this code sample: public abstract class Parent { public int val; public Parent()
Consider this query: SELECT table1.id, table1.review, table1.time, table2.author, table2.title FROM table1, table2 WHERE table1.id
Consider this: set A: 1 2 3 4 set B: 3 4 5 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.