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 ith item with a total weight of W can be derived from the maximum value of the first i1th item with a weight of Wwi
  • Without aftereffect
    • For this issue
      • The maximum value at the current weight of w is still the maximum value at w when the current weight is greater than w.

2|2Why should we use DP

  • Faster than depth first search(DFS)
    • Time complexity achieved using DP is just O(nv)
    • Despite extensive optimization, the time complexity of DFS remains high
  • Higher accuracy than greedy algorithms
    • Obvious, an item cannot be separated.
    • as long as wimodvi0,Greedy algorithms cannot find maximum value.

2|3Our solution for the problem by DP

  • Define the state
    • We define the state Fi,j as the maximum value that a backpack can hold for the first i items with a weight of j.
  • Derive state transition equation
    • Facing the ith item, we only have two choice,then find which one can get maximum value.
      • Don't chose the ith item
        • Obviously, the Fi,j can be transferred from Fi1,j
      • Chose the ith item
        • Consider the weight of the current item
          • We can't chose the item without any weight consumption
          • We need wi weight to hold the item
          • then we should transfer from Fi1,jwi
        • Consider the value of the current item
          • Obviously,when we chose the item, we get vi value。
          • Fi1,jwi+vi
      • Fi,j=max(Fi1,j,Fi1,jwi+vi)
  • Optimize space consumption
    • Obviously,every Fi,j is transferred from Fi1,?
    • 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 i1 state to update the i state.
  • Simple code implementation
for (int i = 1; i <= M; i++) { for (int j = T; j >= v[i]; j--) { dp[j] = max(dp[j], dp[j - v[i]] + w[i]); } }

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 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   加固文明幻景  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示