0-1 knapsack Problem 1

Source: Internet
Author: User

Tinker for a long time finally understood some

0-1 knapsack Problem Description

There was a thief stealing a shop, found that there are n items, the first item value is VI, the weight of WI, assuming that VI and WI are integers. He hopes to take away the goods the more valuable the better, but his backpack can only be loaded with W-pounds of Things, W is a whole number. What kind of things should he take?

"Note" 0-1 knapsack problem: Each item is either taken away or left, (0-1 choices are required). A thief cannot take only one part of an item or take more than two times the same item.

Using dynamic programming algorithm to solve 0-1 knapsack problem,

Define the state with sub-problem: f[i][J] means that the first I item is put into a backpack with a capacity of J to get maximum value. The core State transfer equation is:

f[i][j]=max{f[i-1][j],f[i-1][J-a[i]]+c[i]}

That is, "put the first I item into a backpack with a capacity of J" This sub-problem, if only consider the article I item strategy (put or not), can be converted into a only 1 items of the first I-item problem. If I do not put the article I, then the problem is converted to "the first I-1 items into the capacity of the backpack", the value of f[i-1][j]; If you put the item I, then the problem translates into "The first I-1 items into the remaining capacity of j-a[i] backpack", the maximum value can be obtained is f[ i-1][J-a[i]] plus the value obtained by placing the article I item c[i]

Specific implementation procedures are described in https://github.com/Wuyanan520/boruishangge/, the source code for the. Ipython file, to facilitate your review, dump a copy of the HTML file.

0-1 knapsack Problem 1

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.