Solving 0/1 knapsack problem with dynamic programming
1|0Introduction
1|1Story of 0/1 knapsack problems
1|2Application of 0/1 knapsack problem
-
On 0-1 Knapsack Problem Model for Security Investment Decision(economics)
-
A new security investment decision model is established
- this model represents decision variables with each security investment project
- represents weight with the number of investments in each security investment project,
- represents value with the weight value of each security investment project.
-
LUO Jing-feng. On 0-1 Knapsack Problem Model for Security Investment Decision[J]. West Forum on Economy and Management, 2016, 27(1): 75-78.
-
-
And all the problems that can be abstracted as this model.
2|0Content
2|1Why can we use dynamic programing(DP) to solve the problem.
- Principle of optimality
- For this issue
- The maximum value of the
item with a total weight of can be derived from the maximum value of the first item with a weight of
- The maximum value of the
- For this issue
- Without aftereffect
- For this issue
- The maximum value at the current weight of
is still the maximum value at w when the current weight is greater than .
- The maximum value at the current weight of
- For this issue
2|2Why should we use DP
- Faster than depth first search(DFS)
- Time complexity achieved using DP is just
- Despite extensive optimization, the time complexity of DFS remains high
- Time complexity achieved using DP is just
- Higher accuracy than greedy algorithms
- Obvious, an item cannot be separated.
- as long as
,Greedy algorithms cannot find maximum value.
2|3Our solution for the problem by DP
- Define the state
- We define the state
as the maximum value that a backpack can hold for the first items with a weight of .
- We define the state
- Derive state transition equation
- Facing the
item, we only have two choice,then find which one can get maximum value.- Don't chose the
item- Obviously, the
can be transferred from
- Obviously, the
- Chose the
item- Consider the weight of the current item
- We can't chose the item without any weight consumption
- We need
weight to hold the item - then we should transfer from
- Consider the value of the current item
- Obviously,when we chose the item, we get
value。
- Obviously,when we chose the item, we get
- Consider the weight of the current item
- Don't chose the
- Facing the
- Optimize space consumption
- Obviously,every
is transferred from - So we don't need to declare a two-dimensional array.
- Instead of that, we use a one-dimensional array and do reverse enumeration so that we can use the
state to update the state.
- Obviously,every
- Simple code implementation
3|0Conclusion
In a word, use our DP solution to the 0/1 knapsack problem.
You can get:
- Better time complexity
- save your time
- Better space complexity
- reduce your space consumption
- Simple code implementation
- Easy to maintain
__EOF__

本文作者:Kdlyh
本文链接:https://www.cnblogs.com/kdlyh/p/17844404.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/kdlyh/p/17844404.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!