0-1 knapsack problem

Source: Internet
Author: User

Questions:

Given n kinds of goods and a backpack. The weight of item I is WI, its value is VI, the capacity of the backpack is C.

For an item that is only loaded or not installed, the problem is called a 0-1 knapsack problem.

Ask how to choose the items in your backpack so that the total value of the items loaded into the backpack is the greatest.

Analysis:

n Kinds of Goods: 1 2 3 ..... ... n

  Use M (i, J) to indicate an optional item for I, I+1, ..., n while the backpack capacity is only the value of the item in the backpack when J is the best choice.

At this point, the M (I, J) recursion is defined as follows:

  

That is to say, for the whole problem, if the capacity of the next I, see the next item I pack the value of the backpack, or not pack the value of large (I or not to affect other items).  

  

Algorithm Ideas:

We calculate the nth item first, and then calculate the n-1 until the top one.

Code: 

  

voidKnapack (intV[],intW[],intCintNint**m) {    intJmax = min (W[n]-1, c);  for(intj =0; J <= Jmax; J + +)//if the backpack capacity is less than n size{M[n][j]=0;//it will not fit N    }     for(intj = W[n]; J <= C; J + +)//if the backpack capacity is greater than the size of n{M[n][j]= V[n];//it's loaded with N.    }     for(inti = n-1; I>1; i--) {Jmax= Min (W[i]-1, c);  for(intj =0; J <= Jmax; J + +)//if the backpack capacity is less than the size of I{M[i][j]= M[i +1][J];//I cannot be installed, I have to continue to install the back        }         for(intj = W[i]; J <= C; J + +)//if the backpack capacity is larger than the size of I{M[i][j]= max (M[i +1][J], M[i +1][j-w[i]] + v[i]);//look at the end of the pack I will be better, or not installed better        }    }    //We need special consideration here .m[1][C] = m[2][c]; if(c >= w[1]) {m[1][C] = max (m[1][c], m[2][c-w[1]] + v[1]); }}voidTraceback (intm[][Ten],intW[],intCintNintx[]) {       for(intI=1; i<n; i++)      {          if(M[i][c] = = m[i+1][c]) {X[i]=0; }          Else{X[i]=1; C-=W[i]; }} X[n]= (M[n][c])?1:0; }  

0-1 knapsack 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.