Dynamic Programming Algorithm for 01 knapsack problem

Source: Internet
Author: User

01 Knapsack Problem The solution I learned originally was backtracking, and the first reaction was not solved by a dynamic programming algorithm. The reason is that when learning the dynamic programming algorithm, the problem of matrix multiplication and the longest common substring can be easily discretized into sub-problems of different scales, which is better understood, but for the 01 knapsack problem, it is not easy to think of the knapsack capacity discretization abstract sub-problem, From the emotional preconceived thought that the dynamic programming algorithm is not a good way to solve the 01 knapsack problem, in fact, it is not. In addition, the dynamic programming algorithm does not repeat the problem of the pair, but the bottom up to all the sub-problems are calculated once, until the final problem is calculated the result is the answer we want, a bit like climbing the feeling.

Problem Description: Given n items and a backpack, the weight of item I is WI, its value is VI, the capacity of the backpack is C, the maximum value of the items that can be loaded into the backpack.
With M (i,j) as the maximum value of a backpack capable of carrying a capacity of j from I to n, the maximum value of the item that can be loaded into the backpack is M (1,c).
The recursive type is:


As mentioned above, the problem is that the capacity of the backpack is discretized, so the time complexity is O (NC).

The code is as follows (the variable name used may have a small discrepancy, but it is self-describing):

#include <iostream>using namespace std;struct cargo{int weight; int value;}; const int Totalweight=10;const int totalnumber=5; CARGO GOODS[TOTALNUMBER]={{2,6},{2,3},{6,5},{5,4},{4,6}};//1. The use of the current label starts from 1, 2. Encounters the initialization of a complex structure with the Memset method. int result[totalnumber+1][totalweight+1]={0};int Mymax (int i,int j) {return i>=j?i:j;}    void DP () {int i,j;    The initialization of dynamic programming expressions is i=totalnumber; for (j=1;j<=totalweight;j++) {if (GOODS[I-1].WEIGHT&GT;J)//To ensure logical integrity, this if not deleted {result[i][j]=0        ;        } else {result[i][j]=goods[i-1].value; }} for (i=totalnumber-1;i>0;i--) {for (j=1;j<=totalweight;j++) {if (Goods[i-1].wei            GHT&GT;J) {result[i][j]=result[i+1][j]; } else {Result[i][j]=mymax (result[i+1][j],result[i+1][j-goods[i-1].weight]+goods[i-1            ].value);    }}}}int Main () {DP (); Cout<<result[1][totalweight]<<endl;    Do you understand: Complete the code that uses Result[totalnumber][totalweight] to represent the maximum value of the backpack (the method that solves the sub-problem before). return 0;}



Dynamic Programming Algorithm for 01 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.