Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.78/5 (8 votes)
See more:
Hi everybody!
I need some help solving an algorithm in C#.
The knapsack algorithm (also called Rucksack algorithm) is a problem in which
a rucksack has to be packed with a certain weight W using only a certain number of items N that can be choosen from a list of items S.
For simplification the list S only contains the weights of the items.

The values are:

W = 15
N = 17
S = [1, 2, 4, 9]

I need help in how to go about solving this problem in C#.

Thanks in advance!
Updated 24-Dec-10 3:41am
Smithers-Jones 24-Dec-10 9:28am    
Wow, how demanding and rude. You know, just because it's Christmas, that doesn't mean, people are giving code away for free. Either show some effort and come back, when you are stuck and need real help, or pay for it.

modified: The OP's original question was a rude and demanding one-liner, thus my comment.
Manfred Rudolf Bihy 24-Dec-10 9:32am    
And I want a bit more explanation, please. From what you wrote I'm pretty sure you're talking about the famous Rucksack or Knapsack algorithm, but it would serve you well if you did a little bit more explaining.
Just plainly stating that you want a prefabricated solution from us without any sign of engagement from you, there isn't much hope for you that anybody will help at all.
If you need help re-formulating the question you can aslo ask for that, and more likely than not somebody will do just that.
Manfred Rudolf Bihy 24-Dec-10 9:43am    
I was feeling a bit christmassy, so I just reformulated your question a bit :).
Smithers-Jones 24-Dec-10 9:44am    
Manfred, you are too good. Rewriting this guys one-line demanding "question"... Cheers.
Manfred Rudolf Bihy 24-Dec-10 9:49am    
@Smithers: Well looking at the other question he posted, I didn't even get it right. :-D
No wonder as there was not much to go on anyhow, Cheers!
Happy Xmas, solstice or whatever you're celebrating this time of the year :)

1 solution

Ok, this is a funny problem.

In first place, I suspected this could be a NP-complete problem but later I suspected I was mistaken because some "backdoor" algorithm might exist. However, this article on the problem states that it is indeed a NP-complete one. But only when I learned from the same article that there are different Knapsack and Knapsack-like problems, only then I paid closer attention to the formulation of the present Question. Silly me!

Well, this is neither basic nor generalized Knapsack problem, so let's consider this is a Knapsack-like problem, rather simplified. The formulation is contradictory: the words "I need some help solving an algorithm in C#" hint that general algorithm is required, but is this case, why giving the following constraints: W = 15, N = 17, S = [1, 2, 4, 9]? With this figures, formally speaking, algorithm is not required, proven solution would be the answer.

As my mood of relaxing after the Christmas Eve party does not inspire much enthusiasm for working at the general algorithm, let' first think about the formal solution with constraints.

Ok, we can immediately see that there is no solution.
The proof: as minimum S=1, minimum sum of N weights is equal to N*1 = N = 17, which is more than W = 15. Now, follow the most delicate and hard-to-understand inference: if minimum sum of N weights is less then W, any other combination of N weights is more than W => no combination of N weights is equal to W => :thumbsdown: ---- ;)

Aha, now we can see that the inquirer is locked in the corner. I do understand that shaden2009 could have done a simple mistake; and the figures could be different. Who knows? Slip of a hand. May be W and N should be swapped; then the problem is solvable... -- too late! The proof is presented, and shaden2009 assumed sole responsibility for the formulation of the problem.

In this way, shaden2009, being a undoubtedly, honest person (we all are supposed to be honest aren't we?) has nothing to do but accept my answer now.

If there is more accurate formulation of the problem which would make it more sensible, we would certainly enjoy considering it (shall we?), but that should be another question. The present answer should be accepted; and the present question closed.

Thank you for attention!
Take it easy! ;)
Best wishes in New Year, everyone!
Share this answer
Sandeep Mewara 25-Dec-10 1:41am    
Now, since you have replied this with something really relevant and given OP an exception for this time... I will delete my answer.

BTW, good work. A 5! again...
Espen Harlinn 26-Feb-11 11:22am    
Very good reply - my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900