Talking about one-dimension array of DP algorithm (I) solving the 01 knapsack problem and the dp dimension

Source: Internet
Author: User

Talking about one-dimension array of DP algorithm (I) solving the 01 knapsack problem and the dp dimension

Introduction to DP algorithm (I)

-- How to use a one-dimensional array to solve the 01 knapsack problem

 

Dynamic Programming (also known as Dynamic planning) is one of the most classic algorithms. This document introduces the familiar number tower problem and discusses in depth the solution of the 001 backpack.

First, as shown in, it is required to go from the top layer to the bottom layer. If each step can only go to adjacent nodes, what is the maximum sum of numbers of the nodes that pass through?

In this case, it is obviously impossible to directly select a subnode with a large number for any node. take 9 as an example. If 15, current, and 24> 21 are selected, but the two subnodes of 15 are too small, 24 + 6 + 18 <21 + 10 + 18. it can be seen that finding the partial optimal solution of each stage is not a global optimal solution. in addition, if you use the brute force algorithm to traverse each path once, when the number of layers is n, the total number of paths increases exponentially:

Obviously, this method requires a large amount of computing and cannot be used. in this case, the DP algorithm is effective. the specific idea is: From the penultimate layer, a layer traverses up. the first node of the second-to-last layer is 2. If the path goes through 2, a large value is selected for the left subnode 19. 19 + 2 = 21 is used to replace the original 2. similarly, 18 is changed to 18 + 10 = 28, 9 is changed to 19,5 is changed to 21. in this way, the second-to-last layer becomes the four nodes 21 28 19 21, and the last layer is discarded. in this way, the layer goes up until the first layer selects the larger node on the second layer and adds it to the 9 above to obtain the global optimal solution.

Code Implementation: If the number tower is n layers, it is very simple to open a two-dimensional array of n * n, Which is omitted here.

After having a basic understanding of the concept of dynamic planning, we have summarized the basic concept of dynamic planning. Before that, we will first explain what is a multi-stage decision-making problem and what is a state.

Multi-stage decision-making: a problem can be divided into several stages, and decisions should be made at each stage;

Status: The natural or objective condition at the beginning of each stage. In the preceding example, the state of each stage is the choice of the two subnodes that reach the current node.

Dynamic Planning is a multi-stage decision-making problem in which decisions taken at various stages depend on the current state and result in state transfer. A decision sequence is generated in a changed state, therefore, there is a "dynamic" meaning that this method to solve the problem of multi-stage decision optimization is a dynamic planning method.

The problem of applying the DP algorithm generally has the following characteristics:

(1) Optimization Principle (optimal sub-structure)

The sub-policies of an optimization strategy are also optimal. for example, the second layer to the bottom layer and the maximum path must be the subset from the top layer to the bottom layer and the largest path;

(2) No backend (no backend effect)

In general, once the status of a stage is determined, it will not be affected by the decision after the status;

(3) subproblems overlap

That is, sub-problems are not independent. different from the first two, this feature is not necessary. If it is not satisfied, the DP algorithm is not advantageous. if the policy is independent, it is easier and more convenient.

The following describes the typical 01 backpack problem:

Put N items in a V-sized backpack. the space required for the I-th item is need [I], and its value is value [I]. how can we maximize the value of items in a backpack?

  Analysis: For any item, there is only a choice to put it or not, so it is called a 01 backpack. use best (N, V) * to indicate the maximum value of N items in a backpack with a capacity of V. for the nth item, unless need [N]> V has two options:

(1) If the nth item is placed in the backpack

Best (N, V) = best (N-1, V-need [N]) + value [N]; // that is equal to the value of N items plus the maximum value of placing N-1 items in a backpack with a capacity of V-need [N;

(2) If you do not release

Best (N, V) = best (N-1, V-need [N]) + value [N]; // that is equal to the value of N items plus the maximum value of placing N-1 items in a backpack with a capacity of V-need [N;

In this way, we repeat the above steps until N and V are reduced to 0, and we can obtain the I (1 <= I <= N) for any of the items ), the remaining size of the current backpack is j (0 <= j <= V ).

if(j<need[i])    best(i,j)=best(i-1,j);else    best(i,j)=MAX{best(i-1,j-need[i])+value[i],best(i-1,j)};

This is the State Transfer Law from the I stage to the I-1 stage, the programmer in order to force increase, a term-State transfer equation.

When I = 0 **, you only need to set the boundary value best (0, j) = 0. in this way, solving the problem of best (N, V), which looks complicated and cannot be started, is changed from best (0, j) When I = 0) = 0 to the best (N, j) When I = N ).

 

* Best (N, V) is only a representation of the relationship between items, backpack capacity, and value. Do not worry about why it means so. What exactly does it mean? How is it based on N, V to get value;

** Originally, I = 0 is meaningless, because it is derived from the N th item to the 1st item gradually, and set best (I, j) When I = 0) just to satisfy the mathematical computation;

 

  Code Implementation: An example helps you understand that there are four items: 1, 2, 3, and 4; the space they need is 2, 3, 4, and 1, respectively, and their respective values are 2, 5, 3, and 2, and the total space of the backpack is 5. How can we put them to maximize the value of the items in the backpack? According to the state transition equation, the list is as follows:

 

I Need Value J
1 2 3 4 5
0     0 0 0 0 0
1 2 2          
2 3 5          
3 4 3          
4 1 2          

 

J increments from 1 to 5, best () = 0, returns best (I, j) in sequence, and fills in the table:

 

I Need Value J
1 2 3 4 5
1 2 2 0 2 2 2 2

 

Enter a complete table from 1 to 4:

 

I Need Value J
1 2 3 4 5
0     0 0 0 0 0
1 2 2 0 2 2 2 2
2 3 5 0 2 5 5 7
3 4 3 0 2 5 5 7
4 1 2 2 2 5 7 7

 

The observation table is also a two-dimensional array. intuitively, the number tower is recursive from the bottom up, and the 01 backpack is a row of rows to the bottom right corner of the array. defines the two-dimensional array best [N + 1] [V]. best [N + 1] [V] is the global optimal solution. the core code is as follows:

 

for(j=1;j<=V;j++)    best[0][j]=0;for(i=1;i<=N+1;i++)    for(j=1;j<=V;j++)    {        if(j<need[i])            best[i][j]=best[i-1][j];        else            best[i][j]=MAX{best[i-1][j-need[i]]+value[i],best[i-1][j]};    }        

  Code optimization: If the problem is solved in this way, it will be boring. in the above table, we can find that applying for a large (N + 1) * V array is too extravagance and waste to get the last element, in ACMer, the storage space is too large. it is not difficult to find, to solve the I stage is actually only the value of the I-1 stage, that is to say, no matter how big N, only a 2 * V array can solve the problem, this saves a lot of space. if I is an even number, it is saved to best [0] []. If I is an odd number, it is saved to best [1] [].

 

Best (I-1, j-x)

...

Good (I-1, j)

 

...

Best (I, j)

 

for(j=1;j<=V;j++)    best[0][j]=0;for(i=1;i<=N+1;i++){    if(i%2){        if(j<need[i])            best[1][j]=best[0][j];        else            best[1][j]=MAX{best[0,j-need[i]]+value[i],best[0,j]};    }    else{        if(j<need[i])            best[0][j]=best[1][j];        else                best[0][j]=MAX{best[1][j-need[i]]+value[i],best[1][j]};    }}    

  Continue Optimization: The title will be guessed, and the problem can be further simplified to a one-dimensional array. As for how to implement it, read the table above and continue to think (drawing a table is not used to occupy space.

 

...

Best (J-2)

Best (J-1)

Best (j)

...

First, set the one-dimensional array best [] = {0}. Pay attention to the following two details:

(1) The original data must be computed and overwritten from best [V], best [V-1;

(2) only need to overwrite j = need [I].

As for the reason, it is not difficult to understand. The core code is provided here:

 

__int64 best[]={0};        //long longfor(i=1; i<=n; i++)    for(j=m; j>=needed[i]; j--)        best[j]=MAX{best[j],best[j-need[i]]+value[i]};

  

Optimization to the present, that is to say, the entire 01 backpack problem, only three lines of code are required! Whether it is time, space, or code simplicity, it is the best!

Related Article

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.