"Algorithm Design and Analysis" 7, 0/1 knapsack problem, dynamic planning

Source: Internet
Author: User

/*** Book: "Algorithmic Analysis and Design" * Function: given n items and a backpack, the weight of item I is WI, its value is VI, ask how to choose the items loaded into the backpack, so that the total value of the items loaded into the backpack? * File: beibao.cpp* time: November 30, 2014 19:22:47* cutter_point*/#include <iostream> #define Sizebeibao 20using namespace std;//The optimal substructure of this knapsack problem is/* First here there are altogether m kinds of goods, the total capacity of the backpack is W1, starting from the nth start to the front, the initialization of Nth is C[n][j] J is the capacity of the backpack, starting from 0 increments to the maximum capacity of the backpack W when J >= W When the value of the nth Plus to add the vn when 0 <= J < w so low n not add up that is N2, when considering the first I < N, then divided into max{c[i+1][j], C[i+1][j-wi]+vi} When J >= WI is m[i+1][j]*///we use M (i,j) to indicate the maximum value of the backpack that has been judged good 1:i-1 sequence, and the remaining capacity of the backpack is J, the item I is judged//n is the number, W is the backpack maximum capacity, c[n, W] It's a backpack. Under n Items the backpack size is the optimal value of W, V is the value of each, WI is the weight of each int beibao (int n, int w, int c[sizebeibao][sizebeibao], int *v, int *wi) {c[ Sizebeibao][sizebeibao] = {0};//Initialize all values//start with the nth start calculation, initialize nth for (int i = 0; I <= w; ++i)//Capacity weight 0 to W initialization {if (I >= wi[ N]//and the nth to compare that weight, as long as the weight exceeds the value is vn{c[n][i] = v[n];} Else{c[n][i] = 0;//backpack capacity is not reached, then the best value can only be 0}}//here from the nth//goods from the nth back iteration/*2, when considering the first I < N, then divided into max{c[i+1][j], C[i+1][j-wi ]+vi} When J >= Wi C[i+1][j-wi]+vi otherwise is m[i+1][j]*/for (int i = n-1; I >= 0; i) {for (int j = 0; J <= W; ++j)//backpack capacity increased from small to maximum {if (J >= wi[i])//When weight reaches this requirement c[i][j] = C[i + 1][j-wi[i]] + v[i];// The value of the item I get when I add a backpack elsec[i][j] = c[i + 1][j];//When I do not add a backpack}}//the final optimal solution is return c[1][w];/*//when the number of items is from 1 to n for (int i = 1; I &l t;= N; ++i) {C[i][0] = 0;//The value of the backpack corresponding to the 0 capacity is definitely 0//different weights will have different knapsack optimal value for (int wx = 1; wx <= W; ++wx) {//when the corresponding weight is increased// Here to determine whether the weight is larger than the backpack can be loaded, here the number of items I when the IF (Wi[i] > WX) c[i][wx] = C[I-1][WX];//2, when wi > W, the total amount of the first I > Total weight of c[n, w]=c[n -1, W]else//3, when n > 0 and Wi <= w c[n, w]=max{c[n-1, W], c[n-1, W-wi]+vi}{//Compare the two largest int temp = C[i-1][wx-wi [i]] + v[i];if (C[I-1][WX] > Temp) c[i][wx] = c[i-1][wx];elsec[i][wx] = temp;}}} */}//get knapsack problem vector solution void Qiux (int c[sizebeibao][sizebeibao], int *x, int *wi, int w, int n) {for (int i = 1; i < n; ++i)//from a Items to n items {if (c[i][w] = = C[i + 1][w]) x[i] = 0;//If the next item is added but the optimal value does not change, the preceding one is not added to the inside else{x[i] = 1;//If added an item value changed, So that means the new item can be added to the backpack to take up the quality w-= Wi[i];}} Whether the last item is loaded into the last remaining W to see C[N][W]Because the last c[i][w] = = C[i + 1][w] cannot judge x[n] = (c[n][w])? 1:0;} int main () {int wi[4] = {0, 4, 3, 2};int v[4] = {0, 5, 2, 1};int n = 3, W = 6;int C[sizebeibao][sizebeibao] = {0};int x [Sizebeibao] = {0};cout << "The best value is:" << endl;cout << Beibao (N, W, C, V, WI); cout << "vector x is:" &  lt;< Endl;qiux (c, X, WI, W, N); for (int i = 1; I <= n; ++i) cout << x[i] << "; cout << Endl;return 0;}

"Algorithm Design and Analysis" 7, 0/1 knapsack problem, 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.