Tinker for a long time finally understood some
0-1 knapsack Problem Description
There was a thief stealing a shop, found that there are n items, the first item value is VI, the weight of WI, assuming that VI and WI are integers. He hopes to take away the goods the more valuable the better, but his backpack can only be loaded with W-pounds of Things, W is a whole number. What kind of things should he take?
"Note" 0-1 knapsack problem: Each item is either taken away or left, (0-1 choices are required). A thief cannot take only one part of an item or take more than two times the same item.
Using dynamic programming algorithm to solve 0-1 knapsack problem,
Define the state with sub-problem: f[i][J] means that the first I item is put into a backpack with a capacity of J to get maximum value. The core State transfer equation is:
f[i][j]=max{f[i-1][j],f[i-1][J-a[i]]+c[i]}
That is, "put the first I item into a backpack with a capacity of J" This sub-problem, if only consider the article I item strategy (put or not), can be converted into a only 1 items of the first I-item problem. If I do not put the article I, then the problem is converted to "the first I-1 items into the capacity of the backpack", the value of f[i-1][j]; If you put the item I, then the problem translates into "The first I-1 items into the remaining capacity of j-a[i] backpack", the maximum value can be obtained is f[ i-1][J-a[i]] plus the value obtained by placing the article I item c[i]
Specific implementation procedures are described in https://github.com/Wuyanan520/boruishangge/, the source code for the. Ipython file, to facilitate your review, dump a copy of the HTML file.
0-1 knapsack Problem 1