Introduction to algorithms-Dynamic Planning-steel strip cutting and introduction to Steel Strip

Source: Internet
Author: User

Introduction to algorithms-Dynamic Planning-steel strip cutting and introduction to Steel Strip

Dynamic Planning is usually used to solve optimization problems. In such problems, a group of choices are made to achieve the optimal solution. When making each choice, a subproblem is usually generated in the same form as the original problem. When more than one selection subset generates the same subproblem, the dynamic planning technology is usually very effective. The key technology is to save the solution for each of these subproblems, when it appears again, you can avoid repeated solutions.

 

Steel strip cutting

Serling bought long steel bars and cut them into short steel bars for sale. There is no cost for the cutting process itself. The company's management wants to know the best cutting solution. Suppose we know that the price for selling a steel strip with a length of I inches is pi (I = ,..., In USD ). The length of the steel strip is full inches. Figure 15-1 shows an example of a price list.

The steel strip cutting problem is as follows: Given a steel strip with a length of n inches and a price list pi (I = 1, 2 ,... N), to find the cutting steel strip solution, making the sales revenue rn the largest. 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.

 

I. Problem Analysis

Steel Strip with n inches in length2n-1Different Cutting schemes, Because I (I = 1, 2 ,... N) at an inch, you can always choose to cut or not cut.

If an optimal solution is used to cut the steel strip into k segments (for a certain 1, i2... the maximum benefit of short ik segments is that pn corresponds to no cutting, for each I = ,..., N-1: First, cut the steel strip into two segments with the length of I and n-I, then the optimal cut-profit ri and rn-I of the two segments are solved (the optimal benefit of each scheme is the sum of the optimal benefits of the two segments ). After the first cutting, we regard the two steel bars as two independent steel strip cutting problems. The optimal solution of the original problem is formed by combining the optimal solutions of two related sub-problems and selecting the combined benefit winner among all possible two-segment cutting solutions.

 

There is still a similar but simpler recursive solution to the steel strip cutting problem: cutting the steel strip from the left side of the Section with the length of I, only the section with n-I length left on the right is cut, and the section on the left is not cut. The formula is as follows: 2. Algorithm Implementation

1,Simple Recursive Algorithm

Reference code for top-down recursion:

int CutRod(const int *p, int n){    if (n == 0)    {        return 0;    }    int q = -1;    for (int i = 1; i <= n; ++i)    {        int tmp = CutRod(p, n - i);        if (q < tmp)        {            q = tmp;        }    }    return q;}

Analysis: The CutRod Implementation of top-down recursion is inefficient because CutRod repeatedly uses the same parameter value to call itself recursively, that is, it solves the same subproblem repeatedly. Its running time is for Steel Strip CutRod whose length is n to investigate all possible cutting schemes of 2n-1. There are 2-1 leaf nodes in the recursive call tree, and each leaf node corresponds to a possible cutting scheme.

 

2,Dynamic Planning Algorithm

Simple recursive algorithms are inefficient because they repeatedly solve the same subproblem. Therefore, the dynamic planning method carefully arranges the order of solution, solves each subproblem only once, and saves the results. If you need to solve this subproblem again later, you only need to find the saved result, instead of re-computing it. Therefore, the dynamic planning method requires additional memory space to save computing space.

Dynamic Planning has two equivalent implementation methods.

(1)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.

(2)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 solve sub-Problems in ascending order of scale. When a subproblem is solved, the smaller subproblems it depends on have been solved and the results have been saved. Each sub-problem only needs to be solved once. When we solve it, all its pre-mentioned sub-problems have been solved.

Note:The algorithms obtained by the two methods have the same progressive running time. The only difference is that in some special cases, the top-down method does not actually recursively examine all possible subproblems. Because there is no overhead for frequent recursive function calls, the time complexity function of the bottom-up method usually has a smaller coefficient.

 

The following is a reference code for the top-down process of dynamic planning-with Memo:

1 int MemoizedCutRodAux (const int * p, int n, int * r) 2 {3 if (r [n]> = 0) 4 {5 return r [n]; // first check whether the required value exists 6} 7 8 int q =-1; 9 if (n = 0) 10 {11 q = 0; 12} 13 else14 {15 for (int I = 1; I <= n; ++ I) 16 {17 int tmp = p [I] + MemoizedCutRodAux (p, n-I, r); 18 if (q <tmp) 19 {20 q = tmp; 21} 22} 23} 24 r [n] = q; 25 26 return q; 27} 28 29 int MemoizedCutRod (const int * p, int n) 30 {31 int * r = new int [n + 1]; 32 for (int I = 0; I <= n; ++ I) 33 {34 r [I] =-1; 35} 36 37 return MemoizedCutRodAux (p, n, r); 38}

 

The following is a reference code for the dynamic planning-bottom-up process:

int BottomUpCutRod(const int *p, int n){    int *r = new int[n + 1];    r[0] = 0;    for (int i = 1; i <= n; ++i)    {        int q = -1;        for (int j = 1; j <= i; ++j)        {            int tmp = p[j] + r[i - j];            q = q > tmp ? q : tmp;        }        r[i] = q;    }    return r[n];}

Note:The top-down and bottom-up algorithms share the same incremental running time. Finally, the reconstruction solution is provided. Reference code:

1 # include <iostream> 2 using namespace std; 3 4 void ExtendedBUCutRod (const int * p, int n, int * r, int * s) 5 {6 r [0] = 0; 7 for (int I = 1; I <= n; ++ I) 8 {9 int q =-1; 10 for (int j = 1; j <= I; ++ j) 11 {12 int tmp = p [j-1] + r [I-j]; 13 if (q <tmp) 14 {15 q = tmp; 16 s [I] = j; 17} 18} 19 r [I] = q; 20} 21} 22 23 // r [] maximum benefit of steel bars whose retention length is I, s [] Save the cut length 24 void PrintBUCutRod (const int * p, int n, int * r, int * s) 25 {26 ExtendedBUCutRod (p, n, r, s); 27 cout <"length is" <n <"maximum benefit of steel bars:" <r [n] <endl; 28 29 cout <"the maximum steel bar length is:"; 30 while (n> 0) 31 {32 cout <s [n] <""; 33 n = n-s [n]; 34} 35 cout <endl; 36} 37 38 // Test39 int main () 40 {41 int n; 42 while (cin> n) 43 {44 int * p = new int [n]; 45 for (int I = 0; I <n; ++ I) 46 {47 cin> p [I]; 48} 49 int * r = new int [n + 1]; 50 int * s = new int [n + 1]; 51 52 PrintBUCutRod (p, n, r, s); 53} 54 55 return 0; 56}

A test case is:

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.