wired knapsack...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

wired knapsack...

 
0
  #1
Sep 27th, 2008
ok, so this is my problem...
i have an array of items, which i want to sum to x or above in a most efficient way...
is there a way for doing this? cause all the values are 64 bit ints.
i tried recursion but it's slow cause i can have up to 200 items helppp!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: wired knapsack...

 
0
  #2
Sep 27th, 2008
why use recursion when a simple loop will do. Loops are always faster than recursion (and less memory/stack intensive too).
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: wired knapsack...

 
0
  #3
Sep 27th, 2008
a simple loop? could you please tell me what algorithm would that be? cause recursion would go O(n!), would have to check all the combinations, also i didn't say, there are no unlimited supplies of items like in usual knapsack.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: wired knapsack...

 
0
  #4
Sep 27th, 2008
Originally Posted by gregorynoob View Post
a simple loop? could you please tell me what algorithm would that be? cause recursion would go O(n!), would have to check all the combinations, also i didn't say, there are no unlimited supplies of items like in usual knapsack.
O (n!) when using recursion? I either don't understand the problem correctly or I think you are doing an extremely inefficent recursion. Post your recursion code. As for the simple loop, if I'm understanding correctly, you would just do something like this?

  1. int items[200];
  2. int x; // number of items to add.
  3.  
  4. // code to fill in above variables
  5. int sum = 0;
  6. for (int i = 0; i < x; i++)
  7. sum += items[i];

I have no idea if this is all you are looking for. This comment:

Originally Posted by gregorynoob View Post
would have to check all the combinations, also i didn't say, there are no unlimited supplies of items like in usual knapsack.
suggests there is more to the problem, but I don't know what you mean. Can you elaborate on the assignment?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: wired knapsack...

 
0
  #5
Sep 27th, 2008
well i guess it can really be done without DP, as it is not really limited and can be done by going through the sorted array of items from the end to the start, if an item is larger than limit, limit is set to zero and i break the loop, otherwise i subtract the item value from the limit and keep on going. i guess that was what Dragon meant.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: wired knapsack...

 
0
  #6
Sep 28th, 2008
the general knapsack problem and the subset sum problem are NP-hard, but not NP-incomplete. so there are no polynomial-time algorithms. but it is one of the easy NP-complete problems to solve.

it can be solved (in pseudo-polynomial time) using dynamic programming.

AFAIK, using a Branch and bound algorithm http://en.wikipedia.org/wiki/Branch_and_bound should yield a faster solution. perhaps, you should use a recursive (rather than iterative) Branch and bound. iteration won't give you any significant improvement in performance here.

knuth volume 4 http://www.amazon.com/Art-Computer-P.../dp/0321534964 would be the definitive reference.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: wired knapsack...

 
0
  #7
Sep 28th, 2008
Originally Posted by vijayan121 View Post
the general knapsack problem and the subset sum problem are NP-hard, but not NP-incomplete. so there are no polynomial-time algorithms. but it is one of the easy NP-complete problems to solve.

it can be solved (in pseudo-polynomial time) using dynamic programming.

AFAIK, using a Branch and bound algorithm http://en.wikipedia.org/wiki/Branch_and_bound should yield a faster solution. perhaps, you should use a recursive (rather than iterative) Branch and bound. iteration won't give you any significant improvement in performance here.

knuth volume 4 http://www.amazon.com/Art-Computer-P.../dp/0321534964 would be the definitive reference.

Oh, this is a well known problem? I had never heard of it before and was just going from the description in the first post so I was a little puzzled. I just looked it up on wikipedia and it makes more sense now.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC