Nine-chapter algorithm surface test questions 58 knapsack problem

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/58/


Topics

There is a backpack of size m (integers), and n items with a positive integer size (A[i]). Put this n items in a backpack, how much volume can I fill?


Online test

http://www.lintcode.com/problem/backpack/


Answer

Knapsack problem is a typical problem of dynamic programming. Dynamic planning issues we generally consider the following four points.

1. Status state

2. Equation function

3. Initialize Intialization

4. Answer Answer


The question is the most basic knapsack problem, the characteristic is: each kind of item only has one piece, may choose to put or not to put, then we look at the Backpack this topic dynamic Plan Four point is how?

1. State:dp[i][s] Represents the first I item, take out some can compose and for S volume of backpack

2. Function:f[i][s] = F[i-1][s-a[i]] or f[i-1][s] (a[i) indicates the size of the article I)

Transfer equation to get F[i][s] before I take out some items to make a backpack of S volume. Then it can be converted from two states.

(1) f[i-1][s-a[i]] put the first item, and the first i-1 can take out some of the components and the size of the backpack for S-a[i].

(2) F[i-1][s] Do not put the article I items, and the first i-1 can remove some of the composition and the size of the backpack for S.

3. intialize:f[1...n][0] = true; F[0][1 ... m] = False

Initialize F[1...n][0] represents the first 1...N items, remove some can be composed and for 0 size backpack always true.

Other initialized to False

4. Answer: Look for the largest S that makes F[n][s] a value of true. (the value range of S is 1 to m)

Since there are some requirements for this problem space, we still need to optimize the space complexity after knowing the answer to the idea. First consider how to achieve the basic idea above, there must be a main loop I=1..N, every time the two-dimensional array f[i][0..s] all values. So, if you only use an array of f[0..s], can you guarantee that the f[s] in the end of the first cycle is the state F[i][s] that we define? F[i][s] [f[i-1][s-a[i]] and F[i-1][s] Two sub-problems are recursive, can you guarantee that when the push F[i][s] (that is, the main loop in the first time to push F[s]) can be obtained f[i-1][s-a[i]] and f[i-1][s] value? In fact, this requires us to s=m in every main loop ... 0 in the order of the push f[s], so as to ensure that push f[s] f[s-a[i]] saved is the value of state F[i-1][s-a[i]]. The pseudo code is as follows:

For I=1..N

For s=m ... 0

F[s]=f[s] or f[s-a[i]];


Nine-chapter algorithm surface test questions 58 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.