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

  • Home
  • SEARCH
  • 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 9266653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:22:10+00:00 2026-06-18T14:22:10+00:00

The dynamic programming algorithm to optimally fill a knapsack works well in the case

  • 0

The dynamic programming algorithm to optimally fill a knapsack works well in the case of one knapsack. But is there an efficient known algorithm that will optimally fill 2 knapsacks (capacities can be unequal)?

I have tried the following two approaches and neither of them is correct.

  1. First fill the first knapsack using the original DP algorithm to fill one knapsack and then fill the other knapsack.
  2. First fill a knapsack of size W1 + W2 and then split the solution into two solutions (where W1 and W2 are the capacities of the two knapsacks).

Problem statement (see also Knapsack Problem at Wikipedia):

  1. We have to fill the knapsack with a set of items (each item has a weight and a value) so as to maximize the value that we can get from the items while having a total weight less than or equal to the knapsack size.

  2. We cannot use an item multiple times.

  3. We cannot use a part of an item. We cannot take a fraction of an item. (Every item must be either fully included or not).
  • 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-18T14:22:11+00:00Added an answer on June 18, 2026 at 2:22 pm

    I will assume each of the n items can only be used once, and you must maximize your profit.

    Original knapsack is dp[i] = best profit you can obtain for weight i

    for i = 1 to n do
      for w = maxW down to a[i].weight do
        if dp[w] < dp[w - a[i].weight] + a[i].gain
          dp[w] = dp[w - a[i].weight] + a[i].gain
    

    Now, since we have two knapsacks, we can use dp[i, j] = best profit you can obtain for weight i in knapsack 1 and j in knapsack 2

    for i = 1 to n do
      for w1 = maxW1 down to a[i].weight do
        for w2 = maxW2 down to a[i].weight do
          dp[w1, w2] = max
                       {
                           dp[w1, w2], <- we already have the best choice for this pair
                           dp[w1 - a[i].weight, w2] + a[i].gain <- put in knapsack 1
                           dp[w1, w2 - a[i].weight] + a[i].gain <- put in knapsack 2
                       }
    

    Time complexity is O(n * maxW1 * maxW2), where maxW is the maximum weight the knapsack can carry. Note that this isn’t very efficient if the capacities are large.

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

Sidebar

Related Questions

I am implementing a dynamic programming algorithm for the knapsack problem in Java. I
Consider the 0/1 knapsack problem . The standard Dynamic Programming algorithm applies only when
There are many problems that can be solved using Dynamic programming e.g. Longest increasing
What is dynamic programming algorithm for finding a Hamiltonian cycle in an undirected graph?
For dynamic programming, what are some of the ways that I store a tree
I'm trying to parallelize a dynamic programming algorithm in C++11 using this approach: void
This problem comes out of the dynamic programming chapter in The Algorithm Deisgn Manual
I have a program that solves the weighted interval scheduling problem using dynamic programming
Consider a dynamic programming problem that asks how many distinct subsequences (not necessarily contiguous)
I have a dynamic programming algorithm (modified Needleman-Wunsch) which requires the same basic calculation

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.