Knowledge Points: 01 Backpack (Multiple posture: two-dimensional implementation + one-dimensional implementation + rolling array implementation + backpack full + output optimal scheme)

Source: Internet
Author: User
0-1 BackpackApril 30, 2012 yx.ac Comment Read review article author: yx.ac article source: Yong-lucky | Thinking (http://www.ahathinking.com) Reprint Please specify, thank you for your cooperation.

---

April has not written, can not so wasted ah, hurriedly water an article, haha. Review some of the basics of DP a few days ago, just do it and start with 0-1 backpacks.

This section reviews the basic model of the 0-1 backpack, about its implementation there are many kinds of writing, here to do a simple enumeration of different implementations, mainly write code practiced hand, mainly has the following aspects:

==0-1 knapsack Problem Definition & BASIC implementation

==0-1 backpack using a scrolling array to compress space

==0-1 backpack using one-dimensional arrays

==0-1 Backpack is just full

==0-1 Backpack output Optimal scheme

========================================

0-1 knapsack Problem definition & Basic implementation

Problem: There is a V-size backpack, there are many different weights weight[i] (I=1..N) different value value[i] (I=1..N) items, each item only one, want to calculate the maximum value of goods to put.

The key to DP is to find the optimal substructure and overlap sub-problem, and then find the state transition equation, coding is relatively easy. The optimal substructure guarantees that each state is optimal, and the overlapping sub-problem is the same as the solution of the N state and the n-1 state; DP is generally implemented on the basis of the state transfer equation from the bottom up iteration to obtain the optimal solutions (can also use recursive top-down solution).

Back to 0-1 backpacks, each object I, corresponds to two states: Put & not put in the backpack. The best solution for a backpack is to choose a state that maximizes the value of the backpack when confronted with each object. 0-1 the state transfer equation of the backpack is

1 F (i,v) = max{f (i-1,v), F (I-1,v-c[i]) +w[i]}

F (i,v) indicates the maximum value of the first I object to the backpack at a capacity of V, C[i] represents the cost (i.e. weight) of the object I, W[i] represents the value of the object I, and if the first object is not placed in the backpack, the maximum value of the backpack is equal to the maximum value of the previous I-1 object facing the capacity V If the first object is selected, the maximum value of the backpack is equal to the maximum value of the first I-1 object facing the capacity V-cost[i] plus the value of the object I w[i].

For implementation, a two-dimensional array (state transition matrix) Dp[i][j] is generally used to record the optimal state of each sub-problem, where dp[i][j] represents the maximum value of the first I object facing the capacity J backpack.

The following gives the basic implementation of the 0-1 knapsack, the time complexity is O (n*v), the space complexity is also O (n*v), the initialization of the legal state is very important, for the first object is f[0][j], if the capacity j is less than the first object (number 0) weight, the maximum value of the backpack is 0, If the capacity j is greater than the weight of the first object, the maximum value of the backpack is the value of the object. In order to be able to verify the optimal solution of each state, the program finally outputs the valid part of the state transition matrix to the file.

The code is as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

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.