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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:57:13+00:00 2026-05-26T20:57:13+00:00

Is the following 0-1 Knapsack problem solvable: ‘float’ positive values and ‘float’ weights (can

  • 0

Is the following 0-1 Knapsack problem solvable:

  • ‘float’ positive values and
  • ‘float’ weights (can be positive or negative)
  • ‘float’ capacity of the knapsack > 0

I have on average < 10 items, so I’m thinking of using a brute force implementation. However, I was wondering if there is a better way of doing it.

  • 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-05-26T20:57:14+00:00Added an answer on May 26, 2026 at 8:57 pm

    This is a relatively simple binary program.

    I’d suggest brute force with pruning. If at any time you exceed the allowable weight, you don’t need to try combinations of additional items, you can discard the whole tree.

    Oh wait, you have negative weights? Include all negative weights always, then proceed as above for the positive weights. Or do the negative weight items also have negative value?

    Include all negative weight items with positive value. Exclude all items with positive weight and negative value.

    For negative weight items with negative value, subtract their weight (increasing the knapsack capavity) and use a pseudo-item which represents not taking that item. The pseudo-item will have positive weight and value. Proceed by brute force with pruning.

    class Knapsack
    {
        double bestValue;
        bool[] bestItems;
        double[] itemValues;
        double[] itemWeights;
        double weightLimit;
    
        void SolveRecursive( bool[] chosen, int depth, double currentWeight, double currentValue, double remainingValue )
        {
            if (currentWeight > weightLimit) return;
            if (currentValue + remainingValue < bestValue) return;
            if (depth == chosen.Length) {
                bestValue = currentValue;
                System.Array.Copy(chosen, bestItems, chosen.Length);
                return;
            }
            remainingValue -= itemValues[depth];
            chosen[depth] = false;
            SolveRecursive(chosen, depth+1, currentWeight, currentValue, remainingValue);
            chosen[depth] = true;
            currentWeight += itemWeights[depth];
            currentValue += itemValues[depth];
            SolveRecursive(chosen, depth+1, currentWeight, currentValue, remainingValue);
        }
    
        public bool[] Solve()
        {
            var chosen = new bool[itemWeights.Length];
            bestItems = new bool[itemWeights.Length];
            bestValue = 0.0;
            double totalValue = 0.0;
            foreach (var v in itemValues) totalValue += v;
            SolveRecursive(chosen, 0, 0.0, 0.0, totalValue);
            return bestItems;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following example code for the knapsack problem in a file called
following extjs3 problem: i have an json store: dbStore = new Ext.data.JsonStore({ url: '/?[action]=getFormData',
So I have to solve the knapsack problem for class. So far, I've come
following problem: I have a simple paragraph with a lot of text where I
Following up from this question: How can I unlock a file that is locked
Following is my javascript code. The problem is when I call the send() function
Following on from another question I have asked I have created a new class
Following up from my last question does anyone know how I can use a
Following is a static struct in C++. How can this be represented in java.
Following suggestions on this site, I have adopted SimpleXML from org.simpleframework.xml. I use this

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.