Optimization of space for 0-1 knapsack problems that cainiao can understand

Source: Internet
Author: User

If you do not know what the 0-1 backpack problem is, the following is a brief description of the 0-1 backpack problem. Assume that there are n items, each of which has a size of w1, w2 ...... The value of wn is v1, v2 ....... Vn. 01 A backpack is a backpack with n items taken out and several items put in total_weight space, so that the total size of the backpack is the largest and there is no optimized version for the 0-1 backpack problem, the core code above is the following [cpp] for (int I = 1; I <= n; I ++) {for (int j = 1; j <= total_weight; j ++) {if (w [I]> j) {c [I] [j] = c [I-1] [j];} else {if (c [I-1] [j]> v [I] + c [I-1] [j-w [I]) {c [I] [j] = c [I-1] [j];} else {c [I] [j] = v [I] + c [I-1] [j-w [I] ;}}} note the state transition equation c [I] [j] = max {c [I-1] [j], c [I-1] [j-w [I] + v [I]} each c [I] [j] change value only with c [I-1] [x] {x: 1... J} The c [I-1] [x] is the value saved in the previous I loop, so, c can be reduced to one-dimensional array state transition equation to c [j] = max (c [j], c [j-w [I] + v [I]); furthermore, we note that the state transition equation, each derivation of c [I] [j] is derived through c [I-1] [j-w [I, instead of using c [I] [j-w [I], the scanning sequence of j should be changed from large to small. Otherwise, the I-th time is used to evaluate the c array, the value of c [j-w [I] (that is, c [I] [j-w [I]), then calculate the value of c [j] (that is, c [I] [j]) Because j increments, then the state equation is like c [I] [j] = max (c [I-1] [j], c [I] [j-w [I] + v [I]) obviously does not match the meaning of the question. Therefore, the above Code is changed to [cpp] for (int I = 1; I <= n; I ++) {for (int j = total_weight; j> = 1; j --) {if (w [I ]> J) {c [j] = c [j]; // indicates that the I th is equal to the I-1, here, because c [j] originally saved the previous value, this does not need to be changed} else {// meaning that the weight of the I-th item is smaller than that of the backpack, therefore, you can choose whether to place item I or not if (c [j]> v [I] + c [j-w [I]) {c [j] = c [j];} else {c [j] = v [I] + c [j-w [I] ;}} finally, we can optimize it by removing unnecessary statements to complete [cpp] for (int I = 1; I <= n; I ++) optimization) {for (int j = total_weight; j> = w [I]; j --) {if (c [j] <= v [I] + c [j-w [I]) c [j] = v [I] + c [j-w [I] ;}} this beautiful code is simply unimaginable! Note that the space optimization version cannot solve the optimal sequence, but can find the optimal solution, that is, the maximum value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.