Initial contact with dynamic planning, simple analysis

Source: Internet
Author: User

I. Simple backpack Problems

1. Question Description: There are n items with weight and value of WI and VI respectively. Select items with a total weight of no more than W from these items and find the maximum value of the total value in the selected solution, you can select a room or not ).


[Analysis]: for such problems, we can first use the simplest and easiest way to illustrate all possibilities and find the most appropriate one.


For the function Rec (int I, Int J) // here I represents the first few items, while j Represents the size of the backpack at this time.

Since it is the simplest and simplest idea, let's think about whether we want to put this item in our backpack. 1. If we do not put a backpack, We will directly consider the next item, that is, Rec (I + 1, J) // at this time, I + 1 indicates the next item, and the size of the backpack has not changed, still J; 2. If the current item is included in the backpack, that is, Rec (I + 1, J-W [I]) + V [I] // This indicates the value we get.


For details, see the code:

# Include <iostream> using namespace STD; // n indicates the number of items, W indicates the size of the backpack int N, W; // array W indicates the size of each item, array V indicates the value of each item int W [100], V [100]; int Rec (int I, Int J) {int res; // no remaining item if (I = N) {res = 0; // This item cannot be selected, because it is larger than the size of the backpack} else if (j <W [I]) {res = Rec (I + 1, J );} // else {res = max (Rec (I + 1, J), REC (I + 1, J-W [I]) + V [I]);} return res;} int main () {CIN> N; For (INT I = 0; I <n; I ++) {CIN> W [I]> V [I];} CIN> W; cout <Rec (0, W) <Endl; return 0 ;}

As a result, the simplest and simplest method solves this backpack problem, but the above method may waste a lot of time. The following is an optimization method.


Imagine that there are two branches in each layer of the above search, which requires O (2 ^ N) at the worst. But in fact, some of these computation results have already been obtained, the next time we search again, we calculate again, that is, we repeat twice, so we can store the first calculation results, we can use it directly when we try again. (Accumulate memory-based search here !)

For details, see the code:


 
# Include <iostream> # include <cstring> using namespace STD; // n indicates the number of items, W indicates the size of the backpack int N, W; // array W indicates the capacity of each item, array V indicates the value of each item int W [100], V [100]; /// Add a "memory array" int DP [100] [100] here; int Rec (int I, Int J) {// if the previous calculation result is used directly if (DP [I] [J]> = 0) {return DP [I] [J];} int res; // result resortif (I = N) {res = 0;} else if (j <W [I]) {res = Rec (I + 1, J );} else {res = max (Rec (I + 1, J), REC (I + 1, J-W [I]) + V [I]);} // record the results in the array. Return DP [I] [J] = res;} int main () {CIN> N; For (INT I = 0; I <n; I ++) {CIN> W [I]> V [I];} CIN> W; memset (DP,-1, sizeof (DP )); cout <Rec (0, W) <Endl; return 0 ;}

At this point, the prototype of dynamic planning is basically formed, but the code above is all performed using recursion. Below we replace the recursive formula with the recursive formula using arrays, that is, the recurrence relationship of dynamic planning that we often use:

DP [I + 1] [J] // the maximum value of the total value when the total weight of an item from the previous item cannot exceed J.

DP [0] [J] = 0

DP [I + 1] [J] = max (DP [I] [J], DP [I] [J-W [I] + V [I])


That is:

 
Void solve () {for (INT I = 0; I <n; I ++) {for (Int J = 0; j <= W; j ++) {If (j <W [I]) {DP [I + 1] [J] = DP [I] [J];} else {DP [I + 1] [J] = max (DP [I] [J], DP [I] [J-W [I] + V [I]) ;}}} cout <DP [N] [W] <Endl ;}


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.