Chapter 2 dynamic planning-steel strip cutting

Source: Internet
Author: User

Concept of Dynamic Planning

Dynamic Programming solves the entire problem by combining sub-problems. The splitting algorithm divides the problem into several independent subproblems, recursively solves each problem, and then merges the subproblems to obtain the solution of the original problem. For example, Merge Sorting and quick sorting adopt the splitting algorithm. This book introduces the operation steps of the merge Sort Algorithm in Chapter 2. For details, see http://www.cnblogs.com/anker/archive/2013/01/22/2874252.html. Different from dynamic planning, it is suitable for subproblems that are not independent. That is to say, each subproblem contains a public subproblem. In this case, the use of the divide and conquer algorithm repeats unnecessary work. The dynamic programming algorithm is used to solve each subproblem only once, and the results are stored in a table for reference. This avoids re-calculation of the answers to each subproblem.

The differences between dynamic planning and divide and conquer:
(1) divide and conquer refers to dividing a problem into several independent subproblems and solving each subproblem recursively.
(2) Dynamic Planning is applicable to situations where these subproblems are not independent, that is, various subproblems include public subproblems.

Dynamic Planning is usually used for optimization problems (such problems generally have many feasible solutions and we want to find an optimal (maximum or minimum) Value Solution from these solutions ). The design of a dynamic planning algorithm consists of the following four steps:

(1) describe the structure of the Optimal Solution

(2) recursively define the value of the Optimal Solution

(3) calculate the value of the optimal solution in an ascending mode.

(4) construct an optimal solution from the calculated results

The most important part of dynamic planning is to find the sub-structure of the optimal solution.

Cutting a steel strip

Steel strip cuttingDescription: A steel strip with a length of N inches and a price list Pi (I = 1, 2, 3,..., n) are given, and the steel cutting scheme is obtained to maximize the sales revenue of RN. Note that if the price of the steel strip with a length of N inches is pn large enough, the optimal solution may be that no cutting is required at all.

If an optimal solution is used to cut the steel strip into k segments (for a certain 1 <= k <= N), then the optimal cutting scheme

N = I1 + I2 +... + ik

The maximum benefit is obtained when the length of the steel strip is I1, I2,... IK.

Rn = PI1 + Pi2 +... + Pik

Generally, for RN (n> = 1), we can describe it with a shorter optimal cut benefit:

Rn = max (Pn, R1 + R (n-1), r2 + R (n-2),..., R (n-1) + R1)

The first parameter PN corresponds to the scheme of directly selling steel strip with a length of N inches without cutting. The other n-1 parameters correspond to the other n-1 schemes: For each I = ,... n-1: First, cut the steel strip into the ends of the I and n-I segments, and then solve the optimal cutting benefits RI and R (n-I) of the two segments) (The optimal benefit of each solution is the sum of the optimal benefits of the two segments ). Because we cannot predict which scheme will obtain the maximum benefit, we must examine all possible I, and select the benefit winner. If the original steel strip is sold directly, we can certainly choose not to cut it.

Top-down Recursion

The following is a direct top-down recursive method.

CUT-ROD(p,n)if n==0    return 0q=-∞for i=1 to n    q=max(q,p[i]+CUT-ROD(p,n-i))return q

C ++ implementation code:

#include<iostream>using namespace std;int cut_rod(int p[],int n){    if(n==1)        return 0;    int q=-1;    int i;    for(i=1;i<n;i++)        q=max(q,p[i]+cut_rod(p,n-i));    return q;}int main(){    int p[11]={0,1,5,8,9,10,17,17,20,24,30};    int i;    for(i=0;i<11;i++)        cout<<cut_rod(p,i+1)<<endl;}

Running result:

Use the dynamic planning method to solve the problem of optimal steel strip cutting

There are two equivalent implementation methods for dynamic planning. The following uses the steel strip cutting problem as an example to describe these two methods.

The first method is the top-down method with Memo. This method is still written in a natural recursive form, but the process saves the solution of each subproblem (usually stored in an array or a hash ). When you need a subproblem solution, the process first checks whether the solution has been saved. If yes, the saved value is directly returned, saving the computing time. Otherwise, this subproblem is calculated in the usual way. We call this recursive process a memo because it "remembers" the previously calculated results.

The second method is called the bottom-up method. In general, this method needs to properly define the subproblem "scale" so that the solution of any subproblem only depends on the solution of the "smaller" subproblem. Therefore, we can sort sub-problems by scale and solve them in a descending order. When a subproblem is solved, the smaller subproblems it depends on have been solved and the results have been saved. Each subproblem only needs to be solved once. When we solve it (also the first time we encounter it), all its pre-mentioned subproblems have been solved.

The following is the pseudo code of the top-down cut-rod process, and the memo mechanism is added:

MEMOIZED-CUT-ROD(p,n)let r[0...n] be a new arrayfor i=0 to n    r[i]=-∞return MEMOIZED-CUT-ROD-AUX(p,n,r)MEMOIZED-CUT-ROD-AUX(p,n,r)if r[n]>=0    return r[i]if n==0    q=0else    q=-∞    for i=1 to n        q=max(q,p[i]+MEMOIZED-CUT-ROD-AUX(p,n-i,r))r[n]=qreturn q

Bottom-up version:

BOTTOM-UP-CUT-ROD(p,n)let r[0..n] be a new arrayr[0]=0for j=1 to n    q=-∞    for i=1 to j        q=max(q,p[i]+r[j-i])    r[j]=qreturn r[n]

 

Chapter 2 dynamic planning-steel strip cutting

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.