Syntactic Confectionery Delight PerlMonks

### Re^2: knapsack problem solved by regex

by rubasov (Friar)
 on Mar 14, 2010 at 17:37 UTC ( #828593=note: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
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.

The most common formulation of the problem is the 0-1 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.
Think of the following: a solution to the 0-1 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

I see. Two different items with the same values and weights. Gotcha.

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.

Create A New User
Node Status?
node history
Node Type: note [id://828593]
help
Chatterbox?
 [LanX]: all combinations with same amount of left and right? [Eily]: if you want to store in a structure with the coordinates as key, arrays might do, since the keys are going to be 0..n [LanX]: (Pascale path) [Discipulus]: yes Eily++ (very keen) I want to integrate my project with a 17th experiments. I want to colorize in sequence all paths [oiskuu]: Yeah, modifry the recursive func combinations() to return not the number, but the paths themselves. [Eily]: Discipulus I'd do that by starting from the bottom node I think. That way it can inherit the paths from the two nodes above (and so on, recursively) [LanX]: oh I meant fixed amount [LanX]: every path must have l left and r right edges and l and r are fixed and l+r is the height [LanX]: simple recursive function which goes left and right till l or r are exhausted

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2018-03-19 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (239 votes). Check out past polls.

Notices?