Knapsack problem: Dynamic programming and greedy algorithm

Source: Internet
Author: User

1. Dynamic planning

The following text description sources for dynamic planning

Knapsack Problem of Dynamic planning (I.)
Hawstein
Source: http://hawstein.com/posts/dp-knapsack.html

Everything has to be said from a story.

A man went to the forest to play and found a bunch of gems, he counted, there were n. But the only thing he can pack is a backpack with a capacity of C. The man lined up n jewels in a row and numbered: 0,1,2,..., n-1. The volume and value of the first gem corresponds to V[i] and w[i] respectively. After the platoon, the man began to think: the backpack can only be loaded with the volume C of the thing, then I have to install which gems to let me get the best benefits?

OK, if it was you, what would you do? You say categorically: Dynamic planning Ah! Congratulations, that's correct. So let's take a look at two of the most important concepts in dynamic programming: State and state transition equations in this question.

How are we going to define the state? This state can never be imagined or fallen from the sky. For illustrative purposes, let's first instantiate the problem above. In general, when you encounter N, you decisively give n a very small number, such as n=3. Then set the backpack capacity c=10, three jewels of the volume of 5,4,3, corresponding to the value of 20,10,12. For this example, I think people with an IQ greater than 0 know that the positive solution should be to pack 5 and 3 jewels into a backpack, where the corresponding value is 20+12=32. Next, we take the third jewel away, and the backpack capacity minus the volume of the third gem (because it is one of the gems loaded into the backpack), so the parameters of the problem become: n=2,c=7, Volume {5,4}, value {20,10}. OK, now what is the solution to this problem? I think the IQ is equal to 0 of the solution: the volume of 5 of the gem into the backpack (and then the volume of 2, not fit the second gem, can only watch it slip away), at this time the value of 20. In this way, we found that when n=3, put the backpack is No. 0 and 2nd Gem, when n=2, we put in the No. 0 gem. This is not an accident, yes, this is the legendary "Global optimal solution contains local optimal solution" (n=2 is a local sub-problem of the n=3 situation). Around that big circle, you might ask, where is this? What about the state of the deal? What about the state transfer equation? Don't worry, they're already on the horizon.

Let's get back to the example above. When we were n=2, we asked for the top 2 jewels, the maximum value that could be achieved in a 7-pack backpack, and when n=3, we asked for the top 3 jewels, the maximum value that could be achieved in a 10-pack backpack. Have you found that they are actually a sentence pattern! OK, let's formalize them and define the maximum value that D (i,j) can achieve in a backpack with the remaining volume J for the first I gem. Then the above two words are: D (2, 7) and D (3, 10). It's so much more fun to look at, and these two very nice symbols are the state we're looking for. That is, State D (I,J) represents the maximum value that can be achieved in a backpack with the remaining volume J for the first I gem. Above so many words, in a nutshell is: according to sub-problem definition state! If you find a problem, the state will surface. The maximum value we will eventually solve is D (n, C): The Top N Gems (0,1,2...,n-1) are loaded into the backpack with the remaining capacity C as the maximum value. The state finally found, the state transfer equation? As the name implies, the state transfer equation is the equation describing how the state is transferred (good nonsense!). )。 So back to the example, how did D (2, 7) and D (3, 10) shift? Come on, let's talk about number 2nd gems (remember that the gem number starts from 0). From D (2, 7) to D (3, 10) It was separated by this 2nd gem. It can be loaded or not packed in two cases. If loading, in the face of the first 2 gems, the backpack is only the volume 7来 to install them, and the corresponding to add the value of 2nd gem, D (3, Ten) =d (2, 10-3) +12=d (2, 7) +12, if not loaded, the volume is still 10, the value naturally unchanged, D (3, Ten) =d (2, 10). Remember that D (3, 10) represents the maximum value that the first 3 gems can achieve in a backpack with a remaining volume of 10, and since it is the maximum value, there will be D (3, ten) =max{D (2, 2), and D (7,) +12}. Well, this equation describes some of the relationships of State D (I, j), yes, it's the state transfer equation. To formalize it:
d (I,J)=max{d (I?1,J),d (I?1,J?V [I?1])+W [I?1]}
Note that before discussing the pre-I gem pack in the backpack, it is in the examination of the I-1 Jewel pack not loaded into the backpack (because the gem is numbered starting from 0). At this point, the state and state transition equations are already available.

2. Greedy algorithm

As mentioned above is a 0-1 knapsack problem , that is, a gem can only be taken or not taken. Another is the problem of fractional backpacks , which can take away part of a gem, thus giving priority to taking away all the most valuable gems in the unit. Therefore, it is a greedy algorithm problem.

3. C + + code implementation
#include <iostream>#include <vector>#include <algorithm>using namespace STD;classGood { Public:intNumintWeightintValuefloatUnit_value; Good (intNintWintv): num (n), Weight (w), value (v) {if(W = =0) Unit_value =0;ElseUnit_value = (float) v/(float) W; };};classbag{ Public://0-1 knapsack problem: Take or not get maximum value for an item    voidOnezerobag ( vector<Good>GoodsConst intW) {intn = goods.size ();//D[I][J] represents the maximum value that the remaining volume of J can achieve in the first I (0 ~ i-1) Gem        int**d = New2dmat (n +1+ P +1); for(inti =0; I <= N; i++) { for(intj =0; J <= W; J + +) {D[i][j] = (i = =0) ?0: D[i-1][J];//0 jewels worth 0                if(I >0&& J >= goods[i-1].weight) {intQ = d[i-1][j-goods[i-1].weight] + goods[i-1].value;//Take the first ID[I][J] = max (d[i][j], q);//By value choice or not taken}            }        }cout<<"Maximum value:"<< D[n][w] << Endl;//Output selection result        cout<<"Number of products taken:";intj = W; for(inti = n; i >0; i--) {if(D[i][j] > D[i-1][j]) {J-= goods[i-1].weight;cout<< Goods[i-1].num <<" "; }        }cout<< Endl; }//Fractional knapsack problem: Suppose the item can be split    voidFractionalbag ( vector<Good>&goods,Const intW) {Sortgood (goods,0, Goods.size ()-1);//Sort by unit value from high to low        intm =0; for(inti =0; I < goods.size (); i++) {inttake_weight = min (goods[i].weight, w-m);cout<<"Take the goods."<< goods[i].num<<"The weight is"<< take_weight << Endl; M + = Take_weight;if(m = = W) Break; }    }Private:voidSortgood ( vector<Good>&goods,intLintR) {if(L >= R)return;intm = (L + r)/2;        Sortgood (Goods, L, M); Sortgood (goods, M +1, R);    Mergeab (Goods, L, M, R); };voidMergeab ( vector<Good>&goods,intLintMintR) { vector<Good>TempintI, J, K; for(i = l; I <= m; i++) Temp.push_back (Goods[i]); for(j = r; j > m; j--) Temp.push_back (Goods[j]);        i = l, j = r; K = l; while(I <= J) {if(Temp[i].unit_value > Temp[j].unit_value) goods[k++] = temp[i++];Elsegoods[k++] = temp[j--]; }    };int* * New2dmat (intRowintCol) {int**mat =New int*[row]; for(inti =0; i < row; i++) Mat[i] =New int[Col];returnMat; }};intMain () {good G1 (1,5, -); Good G2 (2,4,Ten); Good G3 (3,3, A); Good good_array[] = {g1, G2, G3}; vector<Good>Goods (Good_array, good_array+3);Const intW =Ten; Bag bag;cout<<"score knapsack problem:"<< Endl; Bag. Fractionalbag (goods, W);cout<<"0-1 knapsack problem (take or not take):"<< Endl; Bag. Onezerobag (goods, W);}

The output is:

3315220-1 背包问题(拿或不拿):最大价值:32所拿商品编号:13in0.9s]

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Knapsack problem: Dynamic programming and 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.