The basic solution of knapsack problem--"Backpack Ninth lecture" notes

Source: Internet
Author: User

Compared with the reprinted article, I prefer to write a note, the beginning of the original link. In this way, to have some of their own things, summed up, the understanding of knowledge can deepen a layer, others see, also more valuable.

Today do Usaco topic, a problem will not, online to find the solution is 01 backpack, so re-read the "Backpack Nine talk." Compared to the first time to see, understand the more deep, I am still in progress, as long as I did not stop the pace. If you want to see the original text, then only need to Baidu "backpack Nine" on the good, Baidu Library in the "Backpack Nine Speak 2.0" is a genuine, the author is Cui Tim Wing predecessor, online seems to call him DD Daniel. This article can be said to be the authority of "knapsack problem", if I know the correct, knapsack problem of the entire solution is this article summed up. In this, thank Tri Tian Wing predecessors selfless dedication!

This note, temporarily only 01 backpack, complete knapsack problem basic solution to make a record, in the future if more, perhaps will come back to update.

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Knapsack problem Model: backpack with a capacity of V, there are n items, items numbered I, occupy the capacity of CI, the value of the creation of WI.

01 knapsack problem: Only one piece of each item, you can choose to put or not put (that is, take 0 pieces or 1 pieces, hence the name 01).

Complete knapsack problem: Each item has infinite pieces, as long as the amount of the load can be put down (the number of items taken, all values may be taken, so the name is complete).

Backpack question set up Q:

1. What is the maximum value that can be created?

2, when the backpack full, the maximum (minimum) can create how much value?

3. When the backpack slows down, how many kinds of programs are there in total?

......

This is the most basic knapsack problem, the following one by one explains.

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Let's take the example of 01 knapsack + Problem 1 first.

For the knapsack problem, our basic idea is this: we put the item on one piece, starting from the 1th to the nth piece. D[I][V] is the maximum value of the amount of V when the first item is placed.

Since our question 1 does not require a full backpack, then obviously there is a boundary condition d[0][0~n]=0, which means that when we do not put a thing, it can be considered to occupy an arbitrary capacity, the maximum value created is 0.

Then, we start a piece of items, C + + code as follows:

 for (int i=1; i<=n;++i)      for (int v=ci;v<=v;++v) D[v][i]=max (d[v][i-1],d[v-ci][i-1]+wi);

It should be easy to understand that d[v][i] can only be updated in two cases, one is d[v][i-1], which means that item I is not put into a backpack (or put in 0 pieces); one is a d[v-ci][i-1]+wi, which means that item I is put in a backpack. We use the maximum value of these two cases to update d[v][i], then we consider all the circumstances, thus getting the d[v][i] to get the value that satisfies the definition.

This is the idea of dynamic planning, it is easy to understand, but also very good to write. So let's analyze its complexity: Time complexity O (nv), Space Complexity O (nv).

Let's consider optimization. This algorithm is considered in all cases, thus guaranteeing the correctness, so the time complexity of the basic is not optimized, and space, you can see that we are layered to push up, to find the first item of the d[0~v][i], we only need to use d[0~v][i-1] only, So we can at least reduce the space to two levels, i.e. O (2V). In fact, the subject is allowed to be a layer of space, that is, O (V).

At this point, our initialization code is:

 for (int v=0; v<=v;++v) d[v]=0;

and recursion the code is as follows:

 for (int i=1; i<=n;++i)      for (int v=v;v>=ci;--v) D[v]=max (D[V],D[V-CI]+WI);

It is not difficult to understand that we have reversed the order of inner loops. In this way, it is easy to see that the inner layer loops to V, D[v+1~v] is actually d[v+1~v][i], and D[0~v] is d[0~v][i-1]. Therefore, when we execute D[v]=max (D[V],D[V-CI]+WI), the party is actually equivalent to D[v][i]=max (D[V][I-1],D[V-CI][I-1]+WI), because after the execution of this sentence, D[v] is the equivalent of d[v][i].

If you understand the above, then you understand the 01 knapsack problem solution, Time O (NV) space O (V) solution.

So we're talking about the next 01 backpacks + Question 2:

Question 2 It's easy, we're going to fill up the backpack, so we just need to modify the initialization code:

d[0]=0;  for (int v=1; v<=v;++v) d[v]=-∞;

Well understood, when an item is not put, only d[0] has meaning, the remaining capacity is not attainable. Therefore, we set the-∞ to ensure that the latter is not selected by Max. In the back, you just need to use the same recursive code, and the meaning is exactly the same. The correctness of this algorithm is too obvious for me to explain.

Next, let's discuss the complete knapsack problem :

In a complete backpack, each item can be placed in infinite pieces. If we consider an O (NV) algorithm, then it will be relatively complex and not discussed here, and in fact, we only need the space of O (V), the code is as follows:

 for (int i=1; i<=n;++i)      for (int v=ci;v<=v;++v) D[v]=max (D[V],D[V-CI]+WI);

It's easy to just restore the inner loop to the positive sequence. I do not have any explanation here, I believe we can understand it simply by thinking about it.

For question 1 and question 2, as with 01 backpacks, the initialization section should be modified to the exact same code.

So, what about question 3 ?

The problem is easy, and I'm not going to give the code. Interested readers can think for themselves, I do that problem on the Usaco is this type, can not think out of the classmate may refer to, this is a blog link.

--------------------------------------------------------------------------------------------------------------- ---------------------------------

At this point, this article on the knapsack problem of the basic content of the complete explanation. If you are still interested to know, may wish to look at the "Backpack Nine Talk" original.

The basic solution of knapsack problem--"Backpack Ninth lecture" notes

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.