Knapsack problem (greedy algorithm)

Source: Internet
Author: User

Note: This is a knapsack problem, not a 0-1 knapsack problem, knapsack problem can be solved with greedy algorithm, but 0-1 can not be solved by greedy algorithm, need to use dynamic programming algorithm to solve;

First, we summarize the greedy algorithm, and the difference between it and the dynamic programming algorithm:

The two most important properties of the greedy algorithm are:

(1) Greedy choice nature;

(2) optimal substructure properties;

Among them, the greedy choice nature: top-down decision-making, each decision is a local optimal solution, and each decision-making after the problem size has become smaller; optimal substructure properties: That is, the optimal solution structure of the problem contains sub-problems;

The two most important properties of a dynamic programming algorithm:

(1) The nature of overlapping sub-problems;

(2) optimal substructure properties;

The best sub-structure properties and greedy algorithm are similar, the only difference is the nature of overlapping sub-problem, because the dynamic programming algorithm is the bottom-up algorithm, it needs to first decompose the original problem into a number of interconnected sub-problems, when the calculation of some sub-problems may be calculated many times, Therefore, the dynamic programming algorithm will have the solution of these sub-problems in a table, so that the final for these sub-problems only need to solve once (can make the original need to solve problems in the exponential time can be solved in the polynomial problem)

The knapsack Problem Solver code is as follows:

(where the sorting algorithm used is based on the merge sort algorithm)

#ifndef Merge_sort_h #define Merge_sort_h<class type>voidint  n); #endif

//merge_sort.template Implementation Section#include"merge_sort.h"Template<classType>voidMergeSort (Type *a, type *v,intN//A is the weight, V is the value{Type*b =NewType[n]; ints =1;  while(S <N) {Mergepass (A, B, V, S, N); //Merge to Array bs + =s;  Mergepass (b, A, V, S, N); //Merge to array as + =s; }    Deleteb;} Template<classType>voidMergepass (Type *x, type *y, type *v,intSintN) {    inti =0;  while(I <= N-S *2) {Merge (x, Y, V, I, I+ S-1, i +2* S-1);//merging adjacent two-satin arrays of size si + = S *2; }    if(i + S < n)//the remaining elements are less than 2sMerge (x, Y, V, I, i + S-1N1); Else  for(intj = i; J <= N-1; J + +) Y[j]=x[j];} Template<classType>voidMerge (Type *c, type *d, type *v,intLintMintR//merge c[l:m] and C[m+1:r] to D[l:r], where c[l:m] and c[m+1:r] are arrays that have been sorted in ascending order{    inti = l, j = m +1, k =l;  while((i <= m) && (J <=r)) {if((V[i]/c[i]) >= (V[j]/c[j]))//Use descending sort hered[k++] = c[i++]; Elsed[k+ +] = c[j++]; }    if(I >m) for(intQ = j; Q <= r; q++) D[k++] =C[q]; Else  for(intQ = i; Q <= m; q++) D[k++] =c[q];}
//knapsack problem, using greedy algorithm to solve//======================================================#include <iostream>#include"Merge_sort.cpp"using namespacestd;voidInit_data (float*v,float*w,float*x,intN//Initializing Data{cout<<"Please enter the value of each type of object:"<<Endl;  for(inti =0; I < n; i++) {cin>>V[i]; } cout<<"Please enter the weight of each type of object:"<<Endl;  for(inti =0; I < n; i++) {cin>>W[i]; }     for(inti =0; I < n; i++) X[i]=0;}voidKnapsack (intNfloatMfloat*v,float*w,float*x)//N is the number of objects, M is the backpack capacity, V is the value of each type of object, W is the weight of each type of object, X is the share of each type of object, belonging to [0,1]{    inti =0; floatc =M;  MergeSort (W, V, N); //V[i]/w[i] is the unit weight value of each type of object, and then they are sorted in descending order     for(i =0; I < n; i++)    {        if(W[i] >c) Break; X[i]=1; C-=W[i]; }    if(I <N) x[i]= c/w[i];}intMainvoid){    floatM =0.0;//Backpack Capacitycout <<"Please enter backpack capacity:"; CIN>>M; intn =0;//Number of Objectscout <<"\ n Please enter the number of objects:"; CIN>>N; float*v =New float[n]; float*w =New float[n]; float*x =New float[n];  Init_data (V, W, X, N); //Initializing Dataknapsack (n, M, V, W, X); cout<<"Orderly W[i]:"<<Endl;  for(inti =0; I < n; i++) {cout<< W[i] <<"  "; } cout<<"\ n \ nyou Output final decision X[i]:"<<Endl;  for(inti =0; I < n; i++) {cout<< X[i] <<"  "; }    /*MergeSort (W, V, N);    cout << "Output sequence W[i]:" << Endl;    for (int i = 0; i < n; i++) {cout << w[i] << ""; }*/System ("Pause"); DeleteV, W, X; return 0;}

Knapsack problem (greedy algorithm)

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.