Talk:Knapsack problem/Unbounded: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 10: Line 10:


:: I'm new to this big-O notation stuff, but I think my algorithm would run in time proportional to: <pre>the product of ( the minimum of ( max_restriction(r)/restriction(r of i) for all restrictions r ) ) for each item i </pre> I can't see the exponential? --[[User:Paddy3118|Paddy3118]] 07:44, 3 December 2008 (UTC)
:: I'm new to this big-O notation stuff, but I think my algorithm would run in time proportional to: <pre>the product of ( the minimum of ( max_restriction(r)/restriction(r of i) for all restrictions r ) ) for each item i </pre> I can't see the exponential? --[[User:Paddy3118|Paddy3118]] 07:44, 3 December 2008 (UTC)

::: Let n = the number of types of items. So what I am saying is that you are taking the product of n things. So as n grows, you can think of it as something like (something)^n in the worst case. Or in other words, every time you add a new type of item, you always have to multiply it by some (potentially big) number. With the dynamic programming solution, it runs in time of (something)*n; so when n is already very big, adding a new type of item does not increase it by very much. Perhaps you can experiment with this and see what happens if you add a whole bunch of new items. --[[User:Spoon!|Spoon!]] 10:01, 3 December 2008 (UTC)

Revision as of 10:01, 3 December 2008

Dynamic Python solution

Is the underlying algorithm so different from what I called an exhaustive search solution? I don't think so. P.S: should the line w = sack.volume; v = sack.volume have the first volume replaced by weight?

It's great that you thought the problem worthy of your time though. I spent ages trying to find something beginning with K and then on formulating the problem :-)

--Paddy3118 05:40, 3 December 2008 (UTC)

Yes, in theory. Dynamic programming runs in time polynomial in the size of the sack weight, sack volume, and number of types of items; whereas the exhaustive search runs in time exponential in the number of types of items. But since the number of types of items here is so small (3), you won't see much of a difference. --Spoon! 07:04, 3 December 2008 (UTC)
I'm new to this big-O notation stuff, but I think my algorithm would run in time proportional to:
the product of ( the minimum of ( max_restriction(r)/restriction(r of i) for all restrictions r ) ) for each item i 
I can't see the exponential? --Paddy3118 07:44, 3 December 2008 (UTC)
Let n = the number of types of items. So what I am saying is that you are taking the product of n things. So as n grows, you can think of it as something like (something)^n in the worst case. Or in other words, every time you add a new type of item, you always have to multiply it by some (potentially big) number. With the dynamic programming solution, it runs in time of (something)*n; so when n is already very big, adding a new type of item does not increase it by very much. Perhaps you can experiment with this and see what happens if you add a whole bunch of new items. --Spoon! 10:01, 3 December 2008 (UTC)