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

The Archive Base Latest Questions

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

I was thinking, I wanted to do a variation on the Knapsack Problem. Imagine

  • 0

I was thinking,

I wanted to do a variation on the Knapsack Problem.

Imagine the original problem, with items with various weights/value.

My version will, along with having the normal weights/values, contain a “group” value.

eg.
Item1[5kg, $600, electronic]
Item2[1kg, $50, food]

Now, having a set of items like this, how would I code up the knapsack problem to make sure that a maximum of 1 item from each “group” is selected.

Notes:

  1. You don’t need to choose an item from that group
  2. There are multiple items in each group
  3. You’re still minimizing weight, maximizing value
  4. The amount of groups are predefined, along with their values.

I’m just writing a draft of the code out at this stage, and I’ve chosen to use a dynamic approach. I understand the idea behind the dynamic solution for the regular knapsack problem, how do I alter this solution to incorporate these “groups”?

KnapSackVariation(v,w,g,n,W)
{
  for (w = 0 to W)
     V[0,w] = 0;
  for(i = 1 to n)
     for(w = 0 to W)
        if(w[i] <= w)
           V[i,w] = max{V[i-1, w], v[i] + V[i-1, w-w[i]]};
        else
           V[i,w] = V[i-1, w];
     return V[n,W];
}

That’s what I have so far, need to add it so that it will remove all corresponding items from the group it is in each time it solves this.

  • 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-26T00:57:40+00:00Added an answer on May 26, 2026 at 12:57 am

    Assume
    c[i] : The category of the ith element
    V[i,w,S] : Maximum value of the knapsack such that it contains at max one item from each category in S

    Recursive Formulation
    V[i,w,S] = max(V[i-1,w,S],V[i,w-w[i],S-{c[i]}] + v[i])
    Base Case
    V[0,w,S] = -`infinity if w!=0 or S != {}`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if this is a problem yet but wanted to start thinking
I was thinking that i wanted to gain some experience in the new asp.net
I wanted to implement the game Pacman. For the AI, I was thinking of
Thinking about a Windows-hosted build process that will periodically drop files to disk to
Thinking about my other problem , i decided I can't even create a regular
After thinking about This Question and giving an answer to it I wanted to
I am thinking this is impossible but I wanted to make sure. Is there
I've been doing some thinking about data redundancy, and just wanted to throw everything
Lately I've been thinking about writing my own language - something I've wanted to
I've been thinking about this idea and wanted to know if it's been implemented

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.