in reply to Re: knapsack problem solved by regex in thread knapsack problem solved by regex
I've just skimmed your code, but if I'm right then your algorithm is lucky to find the correct output for this particular input, but does not give the correct answer in general.
You're filling the knapsack with the items in the order of their relative value (v/w), so consider the following case when your knapsack is almost full (let's suppose there's still room for w=4):
nr w v v/w
1x 3 3.1 31/30 # case a
2x 2 2 1 # case b
Your algorithm will choose case 'a', increasing the full value by 3.1, however the correct choice is to choose case 'b' increasing the full value by 4. That's why you have to backtrack.
Re^3: knapsack problem solved by regex by BrowserUk (Pope) on Mar 14, 2010 at 18:05 UTC 
Cool. I knew (guessed) it was too easy, but it worked for several test sets. Thanks for the explanation.
Actually, scrap the above, because according to the wikipedia page,
The most common formulation of the problem is the 01 knapsack problem, which restricts the number xj of copies of each kind of item to zero or one.
So you can't have 2 of one item?
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] 

Think of the following: a solution to the 01 knapsack problem can be easily generalized to the bounded case, you just have to explicitly list the items with nr>1. Think of my previous example as this (just written abbreviated there):
w v v/w
3 3.1 31/30 # case a
2 2 1 # case b (together with the following line)
2 2 1 # case b
 [reply] [d/l] [select] 

 [reply] 
