0/1 knapsack problem and dynamic planning

Source: Internet
Author: User

Suppose the backpack capacity m=9; (P1,P2,P3,P4,P5,P6) = (15,18,40,56,30,10);(w1,w2,w3,w4,w,5,w6) = (3,2,5,8,5,1).

Algorithmic thinking Variable Interpretation:F (i): The position of the first pair in the array (subscript) L, the position of the first pair of h:si-1 and the last pair of order pairs in the array, i.e. F (i-1) and F (i)-1.  The position of the k:si-1 currently to be added to the Si sequence.  U: The Si-1 is able to produce the last position of the Si1 sequence couple, that is, for U+1<=v<= H's order puppet (PV,WV), there is wv+wi>m.  J: The position of the Si-1 in the SI1 currently being generated. The position of the next:si to be added to the order.

1. Initialization

Initially only S0 information, F (0), P (1), W (1), l,h,f (1), Next

2. Generate Si

(1) Generating sequence pairs (PP,WW) in Si1

In the unhandled Si-1: a. Order pairs with a small weight ratio of WW (P (k), W (k)) are added to the SI (PP,WW) to determine whether they can be placed in SI;

B. if (PP,WW) can be placed in SI, the order of the weight than WW large, such as by (PP,WW) The domination of discarded, not into the SI.

(2) Si1 in the sequence of the generation and determine whether to put in Si, Si-1 if the order even if not joined Si, then all join;

3. Generate the last sequence pairs, backtracking constructs the optimal decision sequence

Sequential couple:

  Dominance rule: if one of the Si-1 and Si1 has (PJ,WJ), the other has (PK,WK), and there is wj>=wk at the same time as PJ<=PK, then the order puppet (PJ,WJ) is discarded

The generation of the sequence pairs of Si

A sequence couple (PP,WW) in each generated Si1 to examine which order pairs are added to the SI

① to add a sequence of w<ww in Si-1 to Si

② by the dominant rule (PP,WW) whether to join SI, to examine the last sequence of W<WW in Si-1 (PG,WG), if pg≥pp, then discard (PP,WW)

Otherwise

③ examines the next sequence puppet in Si-1 (Pg+1, wg+1), wg+1= if pp¬max{pg+1,pp ww

④ (PP,WW)->si

⑤ examines the sequence of W>WW in Si-1 (PP,WW), and discards it if it is ruled.

1  Packagedynamicprogramming;2 3  Public classDknap {4     DoubleM;5     Double[] p,w;6     intN=6;7     int[] x=New int[N+1];8 Dknap () {9M=9;Ten         Double[] temp0={0,15,18,40,56,30,10};//This program p,w the following standard starting from 1 count Onep=temp0; A         Double[] temp1={0,3,2,5,8,5,1}; -w=Temp1; -x=Dknap (p,w,m,x,n); the System.out.println (); -         DoubleSum=0; -System.out.print ("X[n]:"); -          for(inti=1;i<=n;i++){ +sum+=x[i]*P[i]; -System.out.print (x[i]+ ""); +         } ASystem.out.println ("sum=" +sum); at     } -      Public int[] Dknap (Double[] P,Double[] W,DoubleMint[] X,intN) { -         int[] f=New int[N+1]; -         intm=100; -         Double[] p=New Double[m]; -         Double[] w=New Double[m]; in         DoublePp,ww; -         intl,h,i,j,k,next,u,v; toF[0]=1; +p[1]=w[1]=0;//S0 -System.out.println ("S0:"); theSystem.out.print ("(" +p[1]+ "," +w[1]+ ")"); *l=h=1;//start and end positions of the S0 $f[1]=next=2;//the first vacancy of P and WPanax Notoginseng          for(i=1;i<n;i++) {//Generate Si 1~n-1 -k=l; the              for(v=f[i-1];v<f[i];v++){ +                 if(w[v]+w[i]>M) A                      Break; the             } +U=v-1;//The largest V of the +w[i]<=m in L<=r<=h (v); - System.out.println (); $System.out.println ("U:" +u+ "(P[u],w[u]):(" +p[u]+ "," +w[u]+ ")" + "W[i]:" +w[i]); $System.out.println ("S" +i+ ":"); -              for(j=l;j<=u;j++) {//generate si,1 and merge -pp=p[j]+P[i]; theWw=w[j]+w[i];//the next element of the Si,i -                  while(K&LT;=H&AMP;&AMP;W[K]&LT;WW) {//take elements from Si-1 to mergeWuyip[next]=P[k]; thew[next]=W[k]; -System.out.print ("(" +p[k]+ ', ' +w[k]+ ') "); Wunext++; -k++; About                 } $                 if(K&LT;=H&AMP;&AMP;W[K]==WW) {// -pp=Max (pp,p[k]); -k++; -                 } A                 if(Pp>p[next-1]) {//Si-1 Last Order puppet (P[next-1],w[next-1]), if p[next-1]>=pp, Discard (PP,WW) +p[next]=pp; thew[next]=ww; -next++; $System.out.print ("(" +pp+ ', ' +ww+ ') "); the                 } the                  while(K<=h&&p[k]<=p[next-1]) {//Clear thek++; the                 } -             } in                 //incorporating the remaining elements of Si-1 into Si// the                  while(k<=h) { thep[next]=P[k]; Aboutw[next]=W[k]; theSystem.out.print ("(" +p[k]+ ', ' +w[k]+ ') "); thenext++; thek++; +                 } -                 //initial value of Si+1// theL=h+1;BayiH=next-1; thef[i+1]=Next;  the         } -         DoublePx=p[next-1]; -         DoubleWx=w[next-1]; the         DoubleWl=0; the         DoublePl=0; the          for(i=f[n-1];i<f[n]&&w[i]+w[n]<m;i++){ the             if(w[i]>Wl) { -Wl=W[i]; thePl=P[i]; the             } the         }94         Doublepy=pl+P[n]; the         Doublewy=wl+W[n]; the         if(px>Py) { theX[n]=0;98              About         } -         Else{101X[n]=1;102px=Pl;103wx=Wl;104         } the          for(k=n-1;k>0;k--){106         Booleanflag=false;107          for(i=f[k-1];i<f[k];i++){108             if(p[i]==px&&w[i]==Wx) {109flag=true; the                  Break;111             } the         }113         if(!flag) { theX[k]=1; thepx=px-P[k]; thewx=wx-W[k];117         }118         }119         returnX; -     }121      Public DoubleMaxDoubleADoubleb) {122         if(a>=b)123             returnA;124         Else the             returnb;126     }127      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub129         NewDknap (); the     }131  the}
View Code

Test results:

0/1 knapsack problem and dynamic planning

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.