当前位置:网站首页>Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
2022-08-05 00:45:00 【yeah yeah yeah yeah yeah】
前言
The knapsack problem is also a dynamic programming problem.
Dynamic programming is all about transforming a big, complex problem into a small problem,Then turn small problems into smaller, easier-to-solve problems;By taking the smallest easy-to-solve problem to solve,Further derivation step by step、Solve for the final result.
背包问题分类:
Five-step method of dynamic programming
- 确定dp数组的含义
- 确定递推公式
- 确定初始值
- 确定遍历顺序
- 手动推导dp数组的值,Whether it is consistent with the program results
01背包模型

背包的容量为15kg,物品0~4The weight and value respectively are as shown.
问背包能背的物品最大价值是多少?
| 项目 | Value | Weight |
|---|---|---|
| 物品0 | $4 | 12kg |
| 物品1 | $2 | 1kg |
| 物品2 | $10 | 4kg |
| 物品3 | $1 | 1kg |
| 物品4 | $2 | 2kg |
每一件物品其实只有两个状态,取或者不取,Then the time complexity of the brute force method is O(2^n).
1. 确定dp数组的含义
dp[i][j],当背包容量为j时,前 i+1 Participate in putting items into the backpack,The maximum value of the items that the backpack can carry at this time
2. 确定递推公式
Backpack capacity is a constant.当物品 i 不放入背包时 dp[i][j] = dp[i-1][j];当物品 i 放入背包时dp[i][i] = dp[i-1][j-wi] + vi;
所以 dp[i][j] = max( dp[i-1][j], dp[i-1][j-wi]+vi )
3. 确定初始值
根据递推公式,需要确定i=0和j=0时的初值
4. 确定遍历顺序
根据5,This question has no special requirements on the traversal order
5. dp数组
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 物品0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 4 | 4 | 4 |
| 物品1 | 0 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 4 | 6 | 6 | 6 |
| 物品2 | 0 | 2 | 2 | 2 | 10 | 12 | 12 | 12 | 12 | 12 | 12 | 12 | 12 | 12 | 12 | 12 |
| 物品3 | 0 | 2 | 3 | 3 | 10 | 12 | 13 | 13 | 13 | 13 | 13 | 13 | 13 | 13 | 13 | 13 |
| 物品4 | 0 | 2 | 3 | 4 | 10 | 12 | 13 | 14 | 15 | 15 | 15 | 15 | 15 | 15 | 15 | 15 |
程序示例
vector<int> w = {
12,1,4,1,2};
vector<int> v = {
4,2,10,1,2};
int begWight = 15;
vector<vector<int>> dp(w.size(), vector<int>(begWight+1, 0));
for (int j=0;j<=begWight; ++j){
//dp数组初始化
if (w[0] <= j){
dp[0][j] = v[0];
}
}
//Calculated according to the recursive formuladp
for (int i=1; i<w.size(); ++i){
for (int j=1; j<=begWight; ++j){
if (j<w[i])
dp[i][j] = dp[i-1][j];
else
dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i]);
}
}
//打印dp
for (int i=0; i<dp.size(); ++i){
for (int j=0; j<dp[0].size(); ++j){
cout << dp[i][j] << " ";
}
cout << endl;
}
优化dp的维度
- 根据递推公式dp[i][j] = max( dp[i-1][j], dp[i-1][j-wi]+vi ),i取决于i-1,So it can be reduced to two dimensionsdpdown to one dimension.
此时dp[j]means the capacity is jThe backpack can carry the maximum value of the item. - The recursion formula becomes:dp[j] = max(dp[j], dp[j-wi]+vi)
- dp数组初始化,全都为0就行
- Traversal order is required,物品i在外部循环,背包的容量jloop inside.j从大到小
for (int i=0; i<w.size(); ++i){
//先遍历物品
for (int j=begWight; j>=w[i]; --j){
//再遍历背包
dp[j] = max(dp[j], dp[j-w[i]]+v[i]);
}
}
- dp数组
当i=0; dp 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 4
当i=1; dp 0 2 2 2 2 2 2 2 2 2 2 2 4 6 6 6
当i=2; dp 0 2 2 2 10 12 12 12 12 12 12 12 12 12 12 12
当i=3; dp 0 2 3 3 10 12 13 13 13 13 13 13 13 13 13 13
当i=4; dp 0 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15
代码示例
vector<int > dp(begWight+1, 0);
// 以下forThe traversal order of the loop andj的顺序不能变,Think about what would happen if it changed
for (int i=0; i<w.size(); ++i){
//先遍历物品
for (int j=begWight; j>=w[i]; --j){
//再遍历背包,从大到小
dp[j] = max(dp[j], dp[j-w[i]]+v[i]);
}
}
for (auto c:dp){
cout << c<< " ";
}
备注
关于4.一维dpThe order in which the array is traversed is very particular,can be carefully studied.
When traversing the backpack first and then traversing the items(j从大到小,i从小到大),每个dp[j]就只会放入一个物品.
When traversing items first and then traversing the backpack(i从小到大,j从小到大),dp[j]The same item will be placed multiple times,不符合01Backpack principle.
完全背包
有N件物品和一个最多能背重量为W的背包.第i件物品的重量是weight[i],得到的价值是value[i] .每件物品都有无限个(也就是可以放入背包多次),求解将哪些物品装入背包里物品价值总和最大.
完全背包和01背包问题唯一不同的地方就是,每种物品有无限件.
例如:
| W | V | |
|---|---|---|
| 物品0 | 1 | 15 |
| 物品1 | 3 | 20 |
| 物品2 | 4 | 30 |
- dp[j]代表容量为jThe backpack can carry the maximum value of the item
- dp[j] = max(dp[j], dp[j-wi]+vi)
- Initialize for this question0就好
- The traversal order can be discussed briefly.A full backpack is one where items can be put into the backpack repeatedly,So the traversal order is to traverse the items first(i有小到大),再遍历背包(j由小到大).
其实,It is also possible to traverse the backpack first and then traverse the items. - 略
程序示例
vector<int> weight = {
1, 3, 4};
vector<int> value = {
15, 20, 30};
int bagWeight = 4;
vector<int> dp(bagWeight + 1, 0);
for(int i = 0; i < weight.size(); i++) {
// 遍历物品
for(int j = weight[i]; j <= bagWeight; j++) {
// 遍历背包容量
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
总结
01The knapsack model is the basis for all knapsack problems.Complete backpack at the back,多重背包,are fully understood01Further analysis after the backpack,理解了01背包,完全背包、Multiple backpacks are very easy to understand.
递推公式:
背包总价值:dp[j] = max(dp[j], dp[j - w[i]] + v[i])
装满背包:dp[j] += dp[j - nums[i]]
遍历顺序:
| 01背包 | 完全背包 | |
|---|---|---|
| 顺序问题 | There is only one order,先遍历物品(from to large)再遍历背包(There are big to small) | 先遍历物品(由小到大)再遍历背包(有小到大),In fact, the reverse is also possible |
边栏推荐
- More than 2022 cattle school training topic Link with the second L Level Editor I
- leetcode: 269. The Martian Dictionary
- 2022杭电多校第三场 L题 Two Permutations
- oracle create user
- FSAWS 的全球基础设施和网络
- leetcode: 267. Palindromic permutations II
- D - I Hate Non-integer Number (count of selected number dp
- oracle create tablespace
- E - Many Operations (bitwise consideration + dp thought to record the result after the operation
- E - Distance Sequence (prefix and optimized dp
猜你喜欢

leetcode: 266. All Palindromic Permutations

MongoDB搭建及基础操作

B站7月榜单丨飞瓜数据B站UP主排行榜发布!

Inter-process communication and inter-thread communication

DHCP的工作过程

### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep

4. PCIe 接口时序

活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!

面试汇总:为何大厂面试官总问 Framework 的底层原理?

"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
随机推荐
leetcode: 266. All Palindromic Permutations
leetcode: 267. Palindromic permutations II
leetcode:267. 回文排列 II
软件测试面试题:系统测试的策略有?
Pytorch使用和技巧
软件测试面试题:软件验收测试的合格通过准则?
gorm联表查询-实战
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
"No title"
软件测试面试题:做好测试计划的关键是什么?
僵尸进程和孤儿进程
Pytorch usage and tricks
OPENWIFI实践1:下载并编译SDRPi的HDL源码
QSunSync Qiniu cloud file synchronization tool, batch upload
2022牛客多校训练第二场 H题 Take the Elevator
JUC线程池(一): FutureTask使用
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
node使用redis
5.PCIe官方示例
Matlab uses plotting method for data simulation and simulation