Dynamic Programming (DP): What is the complexity of the best known algorithm for the 0-1 exact knapsack problem?
-
I am trying to understand if the *exact 0-1 knapsack problem* is also pseudo-polynomial time. Some clarifications: We are given n stones of varying and known weights, and some number W. We need to find any subset of the stones (if it exists) that sum *exactly* to W. Note: 0-1 implies that each weight can occur exactly zero or one times in the "bag" Is this pseudo-polynomial in W?
-
Answer:
This is just the subset sum problem, and there is an obvious O(N*W) algorithm. Let the weights of stones be stored in ar[N]. We use dynamic programming to check whether a sum of W can be obtained: bool poss[W+1]; // Initialised to false poss[0]=true; for(int i=0;i<N;i++) { for(int j=W;j>=ar[i];j--) if(poss[j-ar[i]])poss[j]=true; if(poss[W])return true; } return false; For a non-pseudopolynomial but usually much faster algorithm when W>2^(N/2), see .
Raziman T.V. at Quora Visit the source
Related Q & A:
- what is the Best known Upper bound on Twin Primes?Best solution by Mathoverflow
- What web scraping tool is the best to extract data?Best solution by Quora
- What do you think is the best American beer?Best solution by answers.yahoo.com
- On what material do sugar crystals grow best?Best solution by Yahoo! Answers
- What is the frequency of radiation that has a wavelength of 0.952 µm?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.