Note 01 11.7.2 transfer algorithm knapsack problem

https://www.cnblogs.com/DJC-BLOG/p/9416799.html

 

Algorithms to explain too abstract. Is not well understood, the best way is to write out a step by step.

The core knapsack problem is that the m [i] [j] = max (m [i-1] [j], m [i-1] [jw [i]] + v [i]) This formula appreciated that it is still a little trouble especially my kind of mind stupid people. So on my first piece of code, and then analyze that data is a step by step on the line.

First on the code: the code a little look on the line, the key to me the following explanation, go again to understand.

#include <the iostream> 
#include <algorithm> 
the using namespace STD; 
const int N = 15; // assumed that the maximum number of items 
int v [N] = {0,8,10,6,3,7,2 }; / / price 
int w [N] = {0,4,6,2,2,5,1 }; // volume 

int main () 
{ 
    int m [N] [N] = {0}; // m [I ] [j] represents the array of items i, j knapsack capacity can be obtained when the maximum value 
    int n = 6, c = 12 ; // set the number of items 6, the capacity of the backpack 12 is 
    for (int i =. 1 ; i <= n; i ++ ) // number of items traversing 
    { 
        for (int. 1 = J; J <= C; J ++) // where various capacities 
        { 
            IF (J> = W [I]) // If this when the weight of the article greater than the current capacity of the backpack 
                // m where [i-1] [j] is the value of the i-1 th item in capacity j 
                // m [i-1] [ jw [i]] this is the i the maximum value of -1 when the article, knapsack capacity jw [i] when the 
                m [i] [j] = max (m [i-1] [j], m [i-1] [jw [i]] + V [I]); 
            the else // backpack capacity is less than the current weight of the articles
                m [i] [j] = m [i-1] [j]; // to maintain the first i-1 th item when the weight of the backpack 
        } 
    } 
    for (int I =. 1; I <= n-; I ++) 
    { 
        for ( . 1 J = int; J <= C; J ++) 
        { 
            COUT << m [I] [J] << ''; 
        } 
        COUT << endl; 
    } 
    return 0; 
}

 

To explain the meaning of m [i] [[j] is the i in the case of the article, the case where the packet size is j, the maximum value in the packet.

Also: where the value and the volume of a beginning with 00 which is their first added to the list, because the formulas which has a m [i-1] [j], when i = 1 when m [0] [j] is when 0 j equal kinds of goods no matter how much time is certainly 0 friends.

Well, the next in accordance with the code sequence to walk again, to go again to understand. First down data sheet

 

volume value
0 0
4 8
6 10
2 6
2 3
5 7
1 2


 

 

 

 

 

 

We set the code number of articles to 6, the capacity of the backpack 12

provided m [N] [N] N fact is max (number of items, the capacity of the backpack). But the big point and set up all right.

Then first traversal starts with the next I (x, y) instead of m [i] [j] a.

According to the above codes traversal order.

When the kinds of goods 0 0 knapsack capacity or when the total value of 0 is no doubt ----> So (0, j) = 0 and (i, 0) = 0


 

1) In the case where an article, and the article One 0

When the item number is only 1 when the article, when the capacity of a backpack, in which case the current is determined whether a capacity greater than the volume of the backpack 1 article.

Volume can be seen for the 1st item 4, thus maintaining the (i-1, j) is the value of 0 (0,1) time;

Similarly we have the capacity when the backpack is 3, 1 is smaller than the volume of the article, it is equal to their own (i-1, j) when the value in the packet, so are 0.

Thus (1,1) (1,2) (1,3) are 0

In power (1,4) is not the same this time is equal to the volume of the article 1, the capacity of the package. This time we wanted to go into.

How to judge put it hold? We give our backpack to free up space for four to see the value of four volumes make the case package is. No. 1 is then put to the article and before (0,4) comparing the like.

Look formula (i, j) = max ((i-1, j), (i-1, j-1 volume of the article number) value of the number of items + 1);

This simple matter, i-1, which means that items not currently, or in the case of the former i-1 Ge items.

         jw [i] is the current means a current capacity minus items knapsack

         Then (i-1, j-1 volume of the article number) number of value items +1) is placed in 4 volumes vacated after the value of the article 1, the value of this value One article 8

         Obviously, 8> 0, so (1,4) = 8

Next 4-12 Similarly, but only so that an article 8 is then properly properly (1,4) (1,5) ...... (1, 12) are 8


 

2) When there is the case where the article numbers 0,1,2

First, (2,0) = 0 This is nothing wrong

(2,1), when the current capacity of less than 2 backpack article volume, the = 0; (2,1) (2,2) (2,3) are

When the (2,4) the time has changed, (2,4) is still less than the current volume of the article 2, so that we use (1,4) when (1,4) represents the number 0 and 1 of the article when the the maximum value of the bag, we have calculated the above is 8, so (2,4) = 8;

Similarly all the way to (2,5) or 8.

But when (2,6) this time the package has been put down 2, we give 2 items to free up space for the six items into them No. 2, after the space vacated 6, 2 has not put in now and then (1 , 0) = 0, and now the two into it is the value of (2,6) number +2 10 items

Since (1,6) = 8 <10 so (2,6) = 10

Similarly, (2,7) ... (2,9) are the 10

When (2,10) when this change when they send, we give No. 2 make six space discovery (1,4) = 4, was able to put down, so we then attached to the bag 2 items together. It becomes (2,10) = 18;

 

Through these two examples we should be very clear

Now I print look at the result is not the case:

The saw nothing wrong. And I really like to explain, then the following are like this, the last to get the most value. Direct m [6] [12] = 24 on the line.

In short the most critical is that sentence

m[i][j]=max(m[i-1][j],m[i-1][j-w[i]+v[i]])

But understanding is not enough, the best deduction myself again, I think this is the best way to master difficult.

Think of the brain inside it will miss a lot of details, inference again can truly understand.

Guess you like

Origin www.cnblogs.com/islch/p/12567179.html