Dynamic planning--chain cutting problem

Source: Internet
Author: User

Steel bar Cutting problem: Given a length of n-inch steel bars and a price list pi (i=1,2,..., N) to cut the steel bar scheme, making the largest sales yield rn.

Note that if the price pn is large enough for the length of the n-inch steel bar, the optimal solution may be that there is no need for cutting at all.

Idea: First cut the steel bar into two pieces, there is a n-1 scheme, each solution is equal to the optimal solution of two sub-rods. We're going to pick out the best solution from this n-1 pseudo-optimal solution.

Here is the pseudo-code:

1 Cut-rod (P,n)

2 if n = = 0

3 Return 0

4 q= Negative Infinity

5 for i = 1 to n

6 Q=max (Q,p[i]+cut-rod (p,n-i))

7 return Q

The above only uses a divide and conquer strategy, the performance of this algorithm is very poor T (N) =2n, in the solution of sub-problems are many duplicates.

Dynamic planning is about avoiding these repetitions. Generally there are two ideas 1. Record Intermediate Solutions

1 Mem-cut-rod

2 Let R[0..N] is a new array

3 for i = 0 to n

4 r[i]= Negative Infinity

5 return Mem-cut-rod-aux (P,N,R)

6

7 Mem-cut-rod-aux (P,N,R)

8 If R[n]>=0

9 return R[n]

If n==0

Q=0

else q= negative infinity

I=1 to N

Q=max (Q,p[i]+mem-cut-rod-aux (p,n-i,r))

R[n]=q

return Q

2. To avoid duplication through reasonable arrangement of problem solving order

1 Bottom-up-cut-rod (P,n)

2 Let R[0..N] is a new array

3 r[0]=0

4 for J=1 to n

5 q= Negative Infinity

6 for I=1 to J

7 Q=max (Q,p[i]+r[j-i])

8 r[j]=q

9 return R[n]

The following pseudo-code also retains the cut length

1 Extend-bottom-up-cut-rod (P,n)

2 let R[0..N] and S[0..N] being new arrays

3 r[0]=0

4 for j = 1 to N

5 q= Negative Infinity

6 for I =1 to J

7 if q < P[i]+r[j-i]

8 Q=p[i]+r[j-i]

9 s[j]=i

Ten r[j]=q

Return R and S

12

Print-cut-rod-solution (P,n)

(r,s) =extend-bottom-up-cut-rod (p,n)

>0 N

Print S[n]

N=n-s[n]

Optimization problems solved using dynamic programming methods should have two elements: optimal substructure and sub-problem overlap

C Language Implementation code;

#defineMinNum-200#defineMaxnum 20#include<stdio.h>#include<stdlib.h>intMax (intAintb) {    returna > B?a:b;}intCut_rod (intP[],intN) {    intQ; if(n = =0)        return 0; Q=Minnum;  for(inti =1; I <= N; i++) {Q= Max (Q,p[i] + cut_rod (p,n-i)); }    returnq;}intBottom_up_rod (intP[],intNintr[]) {    intQ;  for(inti =1; I <= N; i++) R[i]=Minnum; r[0] =0;  for(intj =1; J <= N; J + +) {Q=Minnum;  for(inti =1; I <= J; i++) {Q= Max (Q,p[i] + r[j-i]); } R[j]=Q; }    returnr[n];}intMemoized_cut_rod_aux (intP[],intNintr[]) {    intQ; if(R[n] >=0)        returnR[n]; if(n = =0) Q=0; Else{Q=Minnum;  for(inti =1; I <= N; i++) Q= Max (Q,p[i] + memoized_cut_rod_aux (p,n-i,r)); } R[n]=Q; returnq;}voidMemoized_cut_rod (intP[],intN) {    intR[maxnum];  for(inti =1; I <= N; i++) R[i]=Minnum;}voidExtend_down_up_rod (intR[],intS[],intP[],intN) {r[0] =0;  for(intj =1; J <= N; j + +)    {        intQ =Minnum;  for(inti =1; I <= J; i++)        {            if(Q < p[i] + r[j-i]) {Q= P[i] + r[j-i]; S[J]=i; }} R[j]=Q; }}voidPrint_cut_rod_solution (intP[],intNints[]) {printf ("----------------------------------\ n"); printf ("A cut scheme of length%d is \ n", N);  while(N >0) {printf ("%3d", S[n]); N= N-S[n]; } printf ("\ n");}intMain () {intP[] = {0,1,5,8,9,Ten, -, -, -, -}; intR[maxnum]; intS[maxnum]; Extend_down_up_rod (R,s,p,9);  for(inti =1; I <=9; i++) {printf ("the maximum gain for a length of%d is:%d\n", I,r[i]); } print_cut_rod_solution (P,9, s); System ("Pause"); return 0;}

Dynamic planning--chain cutting problem

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.