当前位置:网站首页>Dynamic programming - 01 knapsack problem
Dynamic programming - 01 knapsack problem
2022-07-19 02:42:00 【Little sun who wants to be a program yuan】
The problem background :
The thief has a capacity of W The backpack , Yes n Items , The first i Item value vi, weight wi.
The goal is : find xi Such that for all xi = {0,1}, bring sum(xi * wi) <= W And sum(xi * vi) Maximum .
Problem solving :
Solution 1 ( Violence goes back ):
public class Package01 {
public static int search(int W, int[] v, int[] w, int index) {
if(index < 0)
return 0;
int a = W >= w[index]? search(W - w[index], v, w, index - 1) + v[index] : 0;
int b = search(W, v, w, index - 1);
return Math.max(a, b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int ans = search(100, v, w, 4);
System.out.println(ans);
}
}Solution 2 :
Analyze redundancy : In both cases f(n - 1) --- f(n - 1, w) & f(n - 1, w') ---> Cache with a two-dimensional array
public class Package01 {
static int[][] f = new int[5][101];
public static int search(int W, int[] v, int[] w, int index) {
if(index < 0)
return 0;
if(f[index][W] > 0)
return f[index][W];
int a = W >= w[index]? search(W - w[index], v, w, index - 1) + v[index] : 0;
int b = search(W, v, w, index - 1);
f[index][W] = Math.max(a, b);
return f[index][W];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int ans = search(100, v, w, 4);
System.out.println(ans);
}
}limitations :
a. If W It's a double Type of , This caching method cannot be used
b. If W When it's worth a lot , You can't use this method
Solution 3 ( Recurrence ):
public class Package01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int W = 100;
int[][] f = new int[v.length][W + 1];
for(int i = 0; i < v.length; ++i) {
for(int j = 0; j <= W; ++j) {
if(i == 0) {
f[i][j] = j >= w[i]? v[i] : 0;
}
else {
int a = j >= w[i]? f[i - 1][j - w[i]] + v[i] : 0;
int b = f[i - 1][j];
f[i][j] = Math.max(a, b);
}
}
}
System.out.println(f[v.length - 1][W]);
}
}summary :
Dynamic programming is a search algorithm without repetition and omission .
Spatial complexity :n*W
Time complexity :O(n*W)
expand :
If W Particularly large , The space complexity will be very high
Improve the code :
Ideas :f[i] It only needs f[i - 1], Unwanted f[i - 2]; new f[i], delete f[i - 2]
Handle : It can be used %2 The operation of ( Scrolling array )
public class Package01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int W = 100;
int[][] f = new int[2][W + 1];
for(int i = 0; i < v.length; ++i) {
for(int j = 0; j <= W; ++j) {
//f[i] It only needs f[i - 1], Unwanted f[i - 2]
//new f[i], delete f[i - 2]
f[i % 2][j] = 0;
if(i == 0) {
f[i % 2][j] = j >= w[i]? v[i] : 0;
}
else {
int a = j >= w[i]? f[(i - 1) % 2][j - w[i]] + v[i] : 0;
int b = f[(i - 1) % 2][j];
f[i % 2][j] = Math.max(a, b);
}
}
}
System.out.println(f[(v.length - 1) % 2][W]);
}
}
边栏推荐
- [unity Editor Extension] displays the memory size of all files in the resource directory
- 单片机之数码管秒表的动态显示
- [hsjframework] unity time management timemanger timer
- How to add software shortcuts to the right mouse button list
- 最短路/次短路/K短路
- CTFHub----RCE
- [unity Editor Extension] find all objects of a script attached in the scene and resources
- 网络一般知识(详)
- ARM 交叉编译器命名规则
- MySQL初探
猜你喜欢

安装.NET提示“无法建立到信任根颁发机构的证书链”(方法简单有下载地址)

Post man JSON script to JMX script of JMeter

PowerStor500T报错0x01806803

【瑞吉外卖⑩】Linux 粗略学习 & Redis 粗略学习
![[Ruiji takeout ⑩] rough learning of Linux & rough learning of redis](/img/2f/9788ddea24f090d872ccdf82ccd8d8.png)
[Ruiji takeout ⑩] rough learning of Linux & rough learning of redis

Leetcode --- one question per day

Cocoon breaking and rebirth of 3D NFT: caduceus decentralized edge rendering technology

网络层传输协议(详解)

Chapter 1 - multi agent system

Traversal of binary tree
随机推荐
MySQL数据库安装
Performance bottleneck positioning XMIND
This article only commemorates the modulus of negative numbers
Firewalld 防火墙
Static routing (detailed)
Interface (collection/map) - implementation and comparison of interfaces
让我们了解awk~
最长上升子序列----优化
ARM 交叉编译器命名规则
Analysis of the paradise of metauniverse developers the ecological value of the metauniverse protocol caduceus
jmeter连接数据库的方法
Sigaga
Leetcode 70:Climbing Stairs
[unity Editor Extension] unity makes its own exclusive editor panel
MDK Keil/ARM printf 用法说明
Shell脚本整数值比较、逻辑测试、if语句、提取性能监控指标
[unity Editor Extension] displays the memory size of all files in the resource directory
2022 latest software testing tools
种下一颗种子,十年后长成了参天B+树
最短路/次短路/K短路