"Algorithm data structure Java Implementation" Java implementation dynamic Planning (knapsack problem)

Source: Internet
Author: User

1. Background    Following the footsteps of Buptwusuopu, he has recently been studying dynamic planning.       Dynamic planning should be called a problem-solving idea, remember that once again to a company interview was asked this.           More than the understanding of dynamic planning, in general, starting from an empty set, each additional element will be the optimal solution, until all the elements are added, the total optimal solution is obtained. The typical application is the knapsack problem, there is a certain weight of the package, there are several items, they each have different weight and value, how to back to achieve maximum value.wrong understanding: to go to the value/weight ratio of the largest items (I think so, but in the case of the backpack weight is not reasonable, the example is easy to think of)
2. TopicsTo achieve the topic is excerpt from another great God's blog, very good, but not Java implementation, interested can see the following references.

Title Description:

There are five items numbered a,b,c,d,e, their weight is 2,2,6,5,4, their value is 6,3,5,4,6, now give you a load-bearing of 10 of the backpack, how to make the backpack loaded items have the greatest value of the sum?

name weight value 1 2 3 4 5 6 7 8 9 10
A 2 6 0 6 6 9 9 12 12 15 15 15
B 2 3 0 3 3 6 6 9 9 9 10 11
C 6 5 0 0 0 6 6 6 6 6 10 11
D 5 4 0 0 0 6 6 6 6 6 10 10
E 4 6 0 0 0 6 6 6 6 6 6 6


3. Code implementation
public class Main {public static void main (string[] args) {//TODO auto-generated method stub final int packagewhe ight=10;//bag weight package[] pg={new Package (6,2, "a"), new Packages (3,2, "B"), new Pack (5,6, "C"), new kit (4,5, "D" ), new package (6,4, "E")};int[][] bestvalues = new int[pg.length+1][packagewheight+1];for (int i=0;i<=pg.length;i++) {for (int j=0;j<=packagewheight;j++) {if (i==0| | j==0) {bestvalues[i][j]=0;//critical condition}else{if (J<pg[i-1].getwheight ()) {Bestvalues[i][j] = bestvalues[i-1][j];//                        When the weight of the nth item is greater than the weight of the package, the best value is}else{int iweight = Pg[i-1].getwheight () of the former n-1 piece, and/////When the nth item weighs less than the weight of the package, it is divided into two cases, that is, the nth or not installed, the maximum                            int ivalue = Pg[i-1].getvalue ();          BESTVALUES[I][J] = Math.max (Bestvalues[i-1][j], Ivalue + bestvalues[i-1][j-iweight]); }}}}system.out.print ("+bestvalues[pg.length][packagewheight");}} public class Package {int Value;int wheight; String name; Package (int value,int wheight,string Name) {This.value=value;this.wheight=wheight;this.name=name;} public int getwheight () {return wheight;} public int GetValue () {return value;} Public String GetName () {return name;}}


interested students can go to my github to clone this project: Https://github.com/jimenbian/DynamicPrograme
reference: "1" http://blog.csdn.net/mu399/article/details/7722810
"2" http://www.cnblogs.com/SDJL/archive/2008/08/22/1274312.html                 "3" http://blog.163.com/guixl_001/blog/static/41764104200863015855721/


/********************************

* This article from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

******************************************/


"Algorithm data structure Java Implementation" Java implementation dynamic Planning (knapsack problem)

Related Article

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.