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 7724165
In Process

The Archive Base Latest Questions

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

This is a follow-up to my previous question . I still find it very

  • 0

This is a follow-up to my previous question. I still find it very interesting problem and as there is one algorithm which deserves more attention I’m posting it here.

From Wikipedia: For the case that each xi is positive and bounded by the same constant, Pisinger found a linear time algorithm.

There is a different paper which seems to describe the same algorithm but it is a bit difficult to read for me so please – does anyone know how to translate the pseudo-code from page 4 (balsub) into working implementation?

Here are couple of pointers I collected so far:

http://www.diku.dk/~pisinger/95-6.ps (the paper)
https://stackoverflow.com/a/9952759/1037407
http://www.diku.dk/hjemmesider/ansatte/pisinger/codes.html

PS: I don’t really insist on precisely this algorithm so if you know of any other similarly performant algorithm please feel free to suggest it bellow.

Edit

This is a Python version of the code posted bellow by oldboy:

class view(object):
    def __init__(self, sequence, start):
        self.sequence, self.start = sequence, start
    def __getitem__(self, index):
        return self.sequence[index + self.start]
    def __setitem__(self, index, value):
        self.sequence[index + self.start] = value

def balsub(w, c):
    '''A balanced algorithm for Subset-sum problem by David Pisinger
    w = weights, c = capacity of the knapsack'''
    n = len(w)
    assert n > 0
    sum_w = 0
    r = 0
    for wj in w:
        assert wj > 0
        sum_w += wj
        assert wj <= c
        r = max(r, wj)
    assert sum_w > c
    b = 0
    w_bar = 0
    while w_bar + w[b] <= c:
        w_bar += w[b]
        b += 1
    s = [[0] * 2 * r for i in range(n - b + 1)]
    s_b_1 = view(s[0], r - 1)
    for mu in range(-r + 1, 1):
        s_b_1[mu] = -1
    for mu in range(1, r + 1):
        s_b_1[mu] = 0
    s_b_1[w_bar - c] = b
    for t in range(b, n):
        s_t_1 = view(s[t - b], r - 1)
        s_t = view(s[t - b + 1], r - 1)
        for mu in range(-r + 1, r + 1):
            s_t[mu] = s_t_1[mu]
        for mu in range(-r + 1, 1):
            mu_prime = mu + w[t]
            s_t[mu_prime] = max(s_t[mu_prime], s_t_1[mu])
        for mu in range(w[t], 0, -1):
            for j in range(s_t[mu] - 1, s_t_1[mu] - 1, -1):
                mu_prime = mu - w[j]
                s_t[mu_prime] = max(s_t[mu_prime], j)
    solved = False
    z = 0
    s_n_1 = view(s[n - b], r - 1)
    while z >= -r + 1:
        if s_n_1[z] >= 0:
            solved = True
            break
        z -= 1
    if solved:
        print c + z
        print n
        x = [False] * n
        for j in range(0, b):
            x[j] = True
        for t in range(n - 1, b - 1, -1):
            s_t = view(s[t - b + 1], r - 1)
            s_t_1 = view(s[t - b], r - 1)
            while True:
                j = s_t[z]
                assert j >= 0
                z_unprime = z + w[j]
                if z_unprime > r or j >= s_t[z_unprime]:
                    break
                z = z_unprime
                x[j] = False
            z_unprime = z - w[t]
            if z_unprime >= -r + 1 and s_t_1[z_unprime] >= s_t[z]:
                z = z_unprime
                x[t] = True
        for j in range(n):
            print x[j], w[j]
  • 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:37:26+00:00Added an answer on June 1, 2026 at 4:37 am
    // Input:
    // c (capacity of the knapsack)
    // n (number of items)
    // w_1 (weight of item 1)
    // ...
    // w_n (weight of item n)
    //
    // Output:
    // z (optimal solution)
    // n
    // x_1 (indicator for item 1)
    // ...
    // x_n (indicator for item n)
    
    #include <algorithm>
    #include <cassert>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
      int c = 0;
      cin >> c;
      int n = 0;
      cin >> n;
      assert(n > 0);
      vector<int> w(n);
      int sum_w = 0;
      int r = 0;
      for (int j = 0; j < n; ++j) {
        cin >> w[j];
        assert(w[j] > 0);
        sum_w += w[j];
        assert(w[j] <= c);
        r = max(r, w[j]);
      }
      assert(sum_w > c);
      int b;
      int w_bar = 0;
      for (b = 0; w_bar + w[b] <= c; ++b) {
        w_bar += w[b];
      }
      vector<vector<int> > s(n - b + 1, vector<int>(2 * r));
      vector<int>::iterator s_b_1 = s[0].begin() + (r - 1);
      for (int mu = -r + 1; mu <= 0; ++mu) {
        s_b_1[mu] = -1;
      }
      for (int mu = 1; mu <= r; ++mu) {
        s_b_1[mu] = 0;
      }
      s_b_1[w_bar - c] = b;
      for (int t = b; t < n; ++t) {
        vector<int>::const_iterator s_t_1 = s[t - b].begin() + (r - 1);
        vector<int>::iterator s_t = s[t - b + 1].begin() + (r - 1);
        for (int mu = -r + 1; mu <= r; ++mu) {
          s_t[mu] = s_t_1[mu];
        }
        for (int mu = -r + 1; mu <= 0; ++mu) {
          int mu_prime = mu + w[t];
          s_t[mu_prime] = max(s_t[mu_prime], s_t_1[mu]);
        }
        for (int mu = w[t]; mu >= 1; --mu) {
          for (int j = s_t[mu] - 1; j >= s_t_1[mu]; --j) {
            int mu_prime = mu - w[j];
            s_t[mu_prime] = max(s_t[mu_prime], j);
          }
        }
      }
      bool solved = false;
      int z;
      vector<int>::const_iterator s_n_1 = s[n - b].begin() + (r - 1);
      for (z = 0; z >= -r + 1; --z) {
        if (s_n_1[z] >= 0) {
          solved = true;
          break;
        }
      }
      if (solved) {
        cout << c + z << '\n' << n << '\n';
        vector<bool> x(n, false);
        for (int j = 0; j < b; ++j) x[j] = true;
        for (int t = n - 1; t >= b; --t) {
          vector<int>::const_iterator s_t = s[t - b + 1].begin() + (r - 1);
          vector<int>::const_iterator s_t_1 = s[t - b].begin() + (r - 1);
          while (true) {
            int j = s_t[z];
            assert(j >= 0);
            int z_unprime = z + w[j];
            if (z_unprime > r || j >= s_t[z_unprime]) break;
            z = z_unprime;
            x[j] = false;
          }
          int z_unprime = z - w[t];
          if (z_unprime >= -r + 1 && s_t_1[z_unprime] >= s_t[z]) {
            z = z_unprime;
            x[t] = true;
          }
        }
        for (int j = 0; j < n; ++j) {
          cout << x[j] << '\n';
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a follow up question from my previous one. you can find it
This is a follow up to my previous question . My code is still
This is a follow-up to my previous question I would like to find a
This is a follow up to my previous question: Problem passing parameters via Iframe
This is a follow-up on my previous question . Once I got the problem
OK, this question is actually a follow-up question from my previous one: What would
This is a follow up question from my previous one found here I need
Note that this is a follow-up question from my previous one: How to memorize
This is a follow up question to a previous question I asked about calculating
This is a follow up from my previous question I have this code basically

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.