Introduction to algorithms Reading Notes-steel strip cutting
Introduction to algorithms Reading Notes-steel strip cutting
Given a steel strip with a length of n inches and a price list pi (I = ,..., 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.
If the length of the steel strip is I, the price of the steel strip is Pi. How to cut the steel strip with a given length can achieve the maximum benefit?
Length I 1 2 3 4 5 6 7 8 9 10
Price Pi 1 5 8 9 10 17 20 14 30
When I = 1, steel bars cannot be cut, r [1] = 1;
When I = 2, steel bars can be divided into 1 + 1 at a price of 2. If not separated (0 + 2), the price is 5. That is, r [2] = 5;
When I = 3, the steel strip can be divided into 0 + 3, 1 + 2. R [3] = 8;
Likewise:
R [4] = 10 (2 + 2 );
R [5] = 13 (2 + 3 );
R [6] = 17 (0 + 6 );
R [7] = 18 (1 + 6 or 4 + 3 => 2 + 2 + 3 );
.......
We can find that when the length is 7, we cut it into a steel bar with the length of 4 and 3, and find the optimal solution for the two steel bars respectively: the optimal solution of length 4 is r [4] = 10 (2 + 2), and the optimal solution of length 3 is r [3] = 8, r [7] = r [4] + r [3] => the optimal solution of the original problem is equal to the maximum value of the optimal solution of the subproblem.
We cut the section with the length of I on the left of the steel strip, and continue to cut the section with the length of n-I on the right (recursive solution). We will not cut the section on the left. That is to say, the problem is decomposed into the first section on the left of the steel strip with a length of n and the result of the remaining part. In this way, a scheme without any cutting operation can be described as follows: the length of the first segment is n, the benefit is pn, the length of the remaining part is 0, and the corresponding benefit is r0 = 0. So the simplified version of the formula:
Therefore, when calculating r [I], the obtained value is r [0] + r [I], r [1] + r [I-1], r [2] + r [I-2],..., the maximum value between r [I-1] + r [1]. In dynamic planning, r [0] -- the value of r [I-1] has been saved before r [I] is calculated, and a few operations can be performed to obtain the optimal result.
Two equivalent implementation methods are available to solve the optimal strip cutting problem using the dynamic planning algorithm.
The first method is the top-down method with Memo. This method is written in recursion mode, but the process will save the solution of each sub-problem-use the array r [I] to record the maximum benefit value of the steel pipe whose total length is I. When you need a subproblem solution, the process first checks whether the solution has been saved. If yes, the saved value is directly returned. Otherwise, the subproblem is computed recursively.
The second method is called the bottom-up method, which is a common method for dynamic planning. This method requires us to sort sub-problems by scale, solve the problem in the order from small to big -- in this question, we solve the problem in the order of I from 1 to n. Each subproblem only needs to be solved once, and the results are recorded in time for later calls.
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 Code is as follows:
/* Name: steel strip cutting problem Copyright: Author: Qiao RuO Zhuo Date: 12-12-14 19: 49 Description: */# include
# Include
# Define MAX 20001 // maximum length # define INFINITY-999999 // negative INFINITY void PrintCutRodSolution (int p [], int n ); // bottom-up void BottomUpCutRod (int p [], int r [], int s [], int n ); // record the maximum return value r [I] of the steel pipe whose total length is I and the cut length s [I] void MemoizedCutRod (int p [], int n) of the steel pipe whose total length is I ); // top-down int MemoizedCutRodAux (int p [], int r [], int s [], int n) with Memo ); // recursively calculate the value of r [I] int main () {int p [MAX] = {0}; int I, n = 50; for (I = 1; I <= n; I ++) // obtain the length price at random, but ensure that the length value is higher than the short value {Do {p [I] = (I> 4 )? (P [I-4] + rand () % 20 + 1): rand () % 20 + 1;} while (p [I] <= p [I-1]); printf ("% 7d", p [I]);} printf ("\ n"); PrintCutRodSolution (p, n ); // bottom-up method printf ("\ n"); MemoizedCutRod (p, n); // top-down method with Memo return 0 ;} void PrintCutRodSolution (int p [], int n) // bottom-up method {int r [MAX] = {0 }; // r [I] the maximum return value of the steel pipe whose total length is I int s [MAX] = {0 }; // s [I] records the cutting length of the first steel bar corresponding to r [I] int I; BottomUpCutRod (p, r, s, n ); // printf ("maximum value: \ n"); // for (I = 1; I <= n; I ++) // {// printf ("% 2d: % 3d", r [I], s [I]); //} // printf ("\ n "); printf ("maximum value: % d \ n", r [n]); printf ("Cut scheme: \ n"); while (n> 0) {printf ("% d \ t", s [n]); n-= s [n];} printf ("\ n ");} void BottomUpCutRod (int p [], int r [], int s [], int n) // record the maximum return value r [I] of the steel pipe whose total length is I and the cut length s [I] {int I, j, max; r [0] = 0; for (I = 1; I <= n; I ++) // The total length of the steel pipe is I {max = INFINITY; for (j = 1; j <= I; j ++) {if (max <p [j] + r [I-j]) // If you break down the result into two parts: length j and length I-j, the maximum benefit value max is updated, and record the cutting length of the first steel bar s [I] {max = p [j] + r [I-j]; s [I] = j ;}} r [I] = max; // The maximum return value of the steel pipe whose total length is I} void MemoizedCutRod (int p [], int n) // top-down method with Memo {int r [MAX] = {0 }; // r [I] the maximum return value of the steel pipe whose total length is I int s [MAX] = {0 }; // s [I] records the cutting length of the first steel bar corresponding to r [I] int I; for (I = 0; I <= n; I ++) // initialize r [I] = INFINITY; printf ("maximum value: % d \ n", MemoizedCutRodAux (p, r, s, n); printf ("cutting scheme: \ n "); while (n> 0) {printf (" % d \ t ", s [n]); n-= s [n];} printf ("\ n");} int MemoizedCutRodAux (int p [], int r [], int s [], int n) // recursively calculate the value of r [I] and record the value of s [I] {int I, max, temp; if (r [n]> = 0) // if the record has been recorded, return r [n] will not be repeated; if (n = 0) max = 0; else {max = INFINITY; for (I = 1; I <= n; I ++) {temp = p [I] + MemoizedCutRodAux (p, r, s, n-I ); // calculate the maximum benefit if (temp> max) obtained by dividing the steel strip into I and n-I) // update the maximum benefit value and record the {max = temp; s [n] = I ;}} return r [n] = max;} value corresponding to s [I ;}