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

Hmmm. Consider this program, whose goal is to figure out the best way to
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 code: new Ajax.Request('?service=example', { parameters : {tags : 'exceptions'}, onSuccess : this.dataReceived.bind(this)
Consider this scenario: I've an XML file called person.xml with the following data in
Consider this scenario. I have my own website, that I use as my identifier,
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Consider this - a base class A, class B inheriting from A, class C
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
Consider this python program: import sys lc = 0 for line in open(sys.argv[1]): lc

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.