Reducing time complexity of Knapsack 0~1 while using DP algorithm

不羁岁月 提交于 2019-12-11 08:05:46

问题


I'm using DP algorithm, i.e. storing sub-problem values in 2D array where one axis

means n items and other - w values from 0 to W where W is the maximum

capacity of knapsack. Therefore T[n-1][W] value is the optimum I need to

calculate. I've read in other sources that time complexity of this algorithm is

O(nW). My quesiton would be: is it possible to reduce this time complexity even more?

I found other answer which talks about pretty much same thing but I can't understant it without example: how to understand about reducing time complexity on 0~1 knapsack

I tells that we de not need to to calculate T[i][w] with small w values as they are not used in the optimum, but I can't get this properly, could anyone give detailed and visual example? This would benefit me a lot.


回答1:


The 2D array you're trying to fill is of size n by W (actually, W+1 since the values go from 0..W, but off-by-one doesn't affect the asymptotic complexity here). Therefore, to fill that array, you would need to do at least n*W work (even if you just initialize the array to all zeroes!).

Therefore, Θ(nW) (tightly bound, which is both O(nW) and Ω(nW)) is the best you can do in terms of asymptotic algorithmic time complexity.

This is what makes the dynamic programming solution so cool, is that you spend constant time on each element of the solution array (in this case, 2D) doing some constant work, from the bottom up (contrast this to the complexity of the top-down recursive solution!).



来源:https://stackoverflow.com/questions/42879631/reducing-time-complexity-of-knapsack-01-while-using-dp-algorithm

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