Knapsack algorithm with an additional property

笑着哭i 提交于 2019-12-03 02:24:27

You are trying to do something impossible - that's your problem.

When you are adding to the number of dimensions, your items are getting additional properties. So, instead of a left, column side of a table(prop1), you have rectangle side(prop1 x prop2) or block side (prop1 x prop2 x prop3) and so on. But the existing constraints, that define the upper, row side of the table, should have the same number of dimensions. Not only one dimension!. So, you will never be able to put two-property problem into a 3-dimensional block, you need 4D block instead. 6D block for 3 properties and so on.

dragonxlwang

Say you are using a three dimension table: A[x][y][z]=k, x: sum 1st property; y: sum 2nd property; z: sum 3rd property; k: minimal cost (maximal reward, which I prefer using reward)

So you iterate over items. Say current item is (p1, p2, p3, reward) (reward = - cost). for each (x,y,z,k), your update formula:

A[x+p1][y+p2][z+p3] = max(A[x+p1][y+p2][z+p3], A[x+p1][y+p2][z+p3] + reward)

If the 1st term on RHS is greater, on slot A[x+p1][y+p2][z+p3], the configuration of knapsack is remain still; otherwise, you update the knapsack by that of A[x+p1][y+p2][z+p3] plus the current item.

Hope this cut things clear.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!