[Algorithm Data Structure Java Implementation] Java implements Dynamic Planning (Knapsack Problem) and data structure java
1. The background follows the footsteps of buptwusuopu, who has recently studied dynamic planning. Dynamic Planning should be called a solution to the problem. Remember to be asked again when I went to a company for an interview. More than the understanding of dynamic planning, this is generally the case. From an empty set, the optimal solution is obtained for every element added until all elements are added. A typical application is a knapsack problem. There is a certain weight package and there are several items. They each have different weights and values, and how to back them to maximize value. Wrong understanding: install more items with the greatest weight/value ratio (I thought so before, but it is not reasonable to do so when the weight of the backpack is certain, and it is easy to think of examples)
2. The question to be implemented is excerpted from another blog of Great God, which is very good. Unfortunately, it is not implemented in java. If you are interested, refer to the reference below.
Description:
There are five items numbered a, B, c, d, and e. Their weights are 2, 2, 6, 5, and 4. Their values are 6, 3, 5, 4, and 6, respectively, now, I want to give you a backpack with a weight of 10. How can we make the items loaded in the backpack the greatest sum of value?
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 packageWheight = 10; // Package Weight Package [] pg = {new Package (6, 2, "a"), new Package (3, 2, "B"), new Package (5, 6, "c"), new Package (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 for} else {int iweight = pg [I-1]. getWheight (); // when the weight of the n-th item is smaller than the weight of the pack, the n-th item or n-th item is not loaded, compare Max 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 ;}}
If you are interested, you 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 is from the blog "Li bogarvin"
* Reprinted please indicate the source: http://blog.csdn.net/buptgshengod
**************************************** **/