当前位置:网站首页>0-1 knapsack problem
0-1 knapsack problem
2022-07-07 23:35:00 【A little dog】
0-1 knapsack
0 - 1 knapsack problem
1. Looking for maximum value
The standard 01 knapsack 
What is the maximum value of filling ?
- The solution to violence is dps Deep backtracking algorithm ( Load the backpack And no backpack )
- Non violent use dp The equation is dynamically recursive
1.1 Definition dp Array dp[i][j]
i Indicates the column length , That is, different objects appear
j Indicates the president , But the line at this time , For the possibility of loading different weights , in other words 0 It's also possible not to install , therefore (j = Maximum weight + 1)
And the corresponding dp[i][j] There is value .

1.2 Two dimensional array implementation
import java.util.*;
/** * @author JMWANG */
import java.io.*;
class Good {
// value
int value;
// weight
int weight;
Good(int v, int p) {
this.value = v;
this.weight = p;
}
Good() {
}
public void setValue(int v) {
this.value = v;
}
public void setWeight(int p) {
this.weight = p;
}
}
public class Main {
public static void main(String[] args) throws IOException {
// Maximum backpack weight
int n = 4;
// Build items
List listGood = new ArrayList();
listGood.add(new Good(15, 1));
listGood.add(new Good(20, 3));
listGood.add(new Good(30, 4));
//dp Recursive equation construction
int[][] goods = new int[listGood.size()][n + 1];
// for (int i = 0; i < goods.length; i++) {
// System.out.println(Arrays.toString(goods[i]));
// }
dp(n, goods, listGood);
System.out.println(goods[listGood.size() - 1][n]);
}
public static void dp(int N, int[][] dp, List<Good> listGood) {
// The relationship between the upper and lower levels
//dp[i][j] = max(listGood.get(i),)
for (int i = 0; i < listGood.size(); i++) {
for (int j = 0; j < N + 1; j++) {
// initialization dp equation
if (j == 0) {
dp[i][j] = 0;
continue;
}
if (i == 0) {
dp[i][j] = listGood.get(i).weight <= N ? listGood.get(i).value : 0;
continue;
}
// If at present weight j < The weight of the current item , Just directly transfer the weight of the upper layer down
if (j < listGood.get(i).weight) dp[i][j] = dp[i - 1][j];
// If at present weight j >= The weight of the current item , We need to compare
// The value of the vertical layer is greater still On the left + The value of the current item is greater
else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - listGood.get(i).weight] + listGood.get(i).value);
}
LookDP(dp);
System.out.println("______________________________");
}
}
}
public static void LookDP(int[][] goods) {
for (int i = 0; i < goods.length; i++) {
for (int j = 0; j < goods[i].length; j++) {
System.out.print(goods[i][j] + " ");
}
System.out.println();
}
}
}
1.3 One dimensional array implementation
public static void testWeightBagProblem(int N, List<Good> listGood){
int len = listGood.size();
// Definition dp Array ,dp[j] Indicates that the backpack capacity is j when , The greatest value you can get
int[] dp = new int[N +1];
// traversal order : Go through the items first , Then traverse the backpack capacity
for(int i=0;i<len;i++){
for(int j=N;j>=listGood.get(i).weight;j--){
System.out.println(Arrays.toString(dp));
dp[j] = Math.max(dp[j], dp[j-listGood.get(i).weight]+listGood.get(i).value);
}
}
System.out.println(Arrays.toString(dp));
System.out.println(dp[N]);
}
Please correct me if there is any mistake
边栏推荐
- 谷歌浏览器怎么登录及开启同步功能
- Cloud native data warehouse analyticdb MySQL user manual
- B_QuRT_User_Guide(36)
- 产业共融新势能,城链科技数字峰会厦门站成功举办
- SAP HR 劳动合同信息 0016
- Fibonacci number of dynamic programming
- B_QuRT_User_Guide(38)
- SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
- V-for traversal object
- HDU 4747 mex "recommended collection"
猜你喜欢
![[compilation principle] lexical analysis design and Implementation](/img/8c/a3a50e6b029c49caf0d791f7d4513a.png)
[compilation principle] lexical analysis design and Implementation

C inheritance and interface design polymorphism

2021icpc Shanghai h.life is a game Kruskal reconstruction tree

B_QuRT_User_Guide(37)

Extended tree (I) - graphic analysis and C language implementation

Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform

SAP 内存参数调优过程

Live-Server使用

What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method

One week learning summary of STL Standard Template Library
随机推荐
KeePass realizes automatic input of web pages
Dynamic agent explanation (July 16, 2020)
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
re1攻防世界逆向
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
2022 届的应届生都找到工作了吗?做自媒体可以吗?
Map operation execution process
Fibonacci number of dynamic programming
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
伸展树(一) - 图文解析与C语言实现
在软件工程领域,搞科研的这十年!
B_QuRT_User_Guide(37)
Oracle database backup and recovery
【7.4】25. K 个一组翻转链表
JNI uses asan to check memory leaks
Anxin vb01 offline voice module access intelligent curtain guidance
系统设计概述
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
Archlinux install MySQL
ROS2专题(03):ROS1和ROS2的区别【02】