Introduction to algorithms-Dynamic Planning (steel strip cutting)

Source: Internet
Author: User

Introduction to algorithms-Dynamic Planning (steel strip cutting)
Steel strip cutting

An existing steel strip with a length of n inches and a price list Pi , Seeking for cutting solutions to maximize sales benefits Rn Max

Steel Strip with n inches in length 2n? 1 Different Cutting schemes, because each inch position can decide to cut or not cut.

To get Rn Maximum: You can divide the problem into subproblems for solving. First, you must cut the knife and then consider the biggest benefit of the remaining parts.
Rn = Max { Pk + rn? K } (K = 1, 2, 3... N-1 ),
Pk Parts are sold as a whole without further cutting;
Rn? K Part continue cutting, consider all the circumstances, into sub-problems.
Find the income owner corresponding to all K values Rn <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/ycTcsru9 + NDQyM66zsfQuO7A + 9Lm1 + 6087y0PG5vYnI + cm49cG48L25vYnI + PC9wPg0KPHA + release = "dynamic planning solution"> Dynamic Planning and solving

Consider the above Algorithm Implementation: Rn = max (pi + rn? I) (I = 1, 2, 3... N, R0 = 0 ) To calculate Rn The cut method in all cases must be calculated, that is, there are n branches, and the I branch corresponds to the I Subbranch. Therefore, if recursive call is used to calculate all the cases, the number of calls will be 2n Times. An exponential function whose running time is n.

According to the analysis above, the subproblem is repeated for many times. If there is a good computing order, the subproblem is first computed, and the result is saved. The next time this subproblem occurs, you only need to look up the table. The dynamic planning method requires additional memory space to save computing time.

Top-down method with Memo

The recursive method is still used, but the solution of the subproblem has been saved in the process. When a subproblem is required, check whether the solution has been saved.

Int Memoized_Cut_Rod (int * p, int n) {int I = 0; int * r = (int *) malloc (n + 1) * sizeof (int )); // allocate space to store the computed subproblem for (I = 0; I <= n; I ++) // initialize r [I] =-1; return Memoized_Cut_Rod_Aux (p, n, r);} int Memoized_Cut_Rod_Aux (int * p, int n, int * r) {int q =-1, I; if (r [n]> = 0) // first check whether it has been computed. if yes, return r [n] directly; if (n = 0) // The length of the steel strip is 0, and the return value is 0 q = 0; else {q =-1; for (I = 1; I <= n; I ++) {q = Max (q, p [I] + Memoized_Cut_Rod_Aux (p, n-I, r); // top-down recursion} r [n] = q; // Save the sub-problem value} return q; // return the maximum benefit}
Bottom-up method

Sort sub-problems by scale and solve them in ascending order. When a problem is solved, the sub-problems it depends on have been computed and the results have been saved, in this way, each subproblem only needs to be calculated once.

Int Bottom_Up_Cut_Rod (int * p, int n) {int * r = (int *) malloc (n + 1) * sizeof (int); // allocate space, used to store computed subproblems int I, j, q =-1; r [0] = 0; for (j = 1; j <= n; j ++) {q =-1; for (I = 1; I <= j; I ++) {q = Max (q, p [I] + r [j-I]); // bottom-up, from small to big solving subproblem} r [j] = q; // Save the solution result} return r [n];}
Reconstruction Solution

The above code is the biggest benefit returned, and does not return a specific cut method. The following code saves the size of each dimension, corresponding to the left part (that is, as the overall sold part)

Int Extended_Bottom_Up_Cut_Rod (int * p, int n) {int * r = (int *) malloc (n + 1) * sizeof (int); int I, j, q =-1; s = (int *) malloc (n + 1) * sizeof (int )); // Save the remaining size of the left part before each cutting. s [0] = 0; r [0] = 0; for (j = 1; j <= n; j ++) {q =-1; for (I = 1; I <= j; I ++) {if (p [I] + r [j-I]> q) {s [j] = I; // Save the left part (sold as a whole) q = p [I] + r [j-I] ;}} r [j] = q;} return r [n];}

Specific Cutting Method of input Steel Bars:

Void Print_Cut_Rod_Solution (int * p, int n) {while (n> 0) {printf ("% d", s [n]); // method for cutting steel strip with circular output dimension n = n-s [n];}
Routine
/*************************************** * ********************************** Do not build a high-end ECs in the sand float http://blog.csdn.net/luoshixian099theory (steel strip cutting) june 2, 2015 *************************************** * ********************************/# include
  
   
# Include
   
    
Int * s = NULL; int Memoized_Cut_Rod_Aux (int * p, int n, int * r); int Max (int a, int B) {return a> B? A: B;} int Memoized_Cut_Rod (int * p, int n) {int I = 0; int * r = (int *) malloc (n + 1) * sizeof (int); // allocate space to store computed subproblems for (I = 0; I <= n; I ++) // initialize r [I] =-1; return Memoized_Cut_Rod_Aux (p, n, r);} int Memoized_Cut_Rod_Aux (int * p, int n, int * r) {int q =-1, I; if (r [n]> = 0) // first check whether the computation has been performed. if yes, return r [n] directly; if (n = 0) // The Steel Strip length is 0, and the return value is 0 q = 0; else {q =-1; for (I = 1; I <= n; I ++) {q = Max (q, p [I] + Memoized_Cut_Rod_Aux (p, n-I, r);} r [n] = q; // Save the sub-problem value} return q; // returns the maximum benefit} int Bottom_Up_Cut_Rod (int * p, int n) {int * r = (int *) malloc (n + 1) * sizeof (int); // allocates space to store the computed subquestions int I, j, q =-1; r [0] = 0; for (j = 1; j <= n; j ++) {q =-1; for (I = 1; I <= j; I ++) {q = Max (q, p [I] + r [j-I]); // bottom-up, from small to large solving subproblem} r [j] = q; // Save the solution result} return r [n];} int Extended_Bottom_Up_Cut_Rod (int * p, int n) {int * r = (int *) malloc (n + 1) * sizeof (int); int I, j, q =-1; s = (int *) malloc (n + 1) * sizeof (int); // Save the remaining size of the left part before each cut. s [0] = 0; r [0] = 0; for (j = 1; j <= n; j ++) {q =-1; for (I = 1; I <= j; I ++) {if (p [I] + r [j-I]> q) {s [j] = I; // Save the left part (sold as a whole) q = p [I] + r [j-I] ;}} r [j] = q;} return r [n];} void Print_Cut_Rod_Solution (int * p, int n) {while (n> 0) {printf ("% d", s [n]); // method for cutting steel strip with circular output dimension n = n-s [n] ;}} void main () {int p [] = {, 9, 10, 17,17, 20,24, 30}; int I; for (I = 1; I <= 10; I ++) {// printf ("% d \ n ", memoized_Cut_Rod (p, I); printf ("maximum benefit of size % d: % d", I, Extended_Bottom_Up_Cut_Rod (p, I )); // printf ("% d \ n", s [I]); printf ("Cut Method:"); Print_Cut_Rod_Solution (p, I); free (s ); s = NULL; printf ("\ n ");}}
   
  

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.