首页 > 其他分享 >Solving 0/1 knapsack problem with dynamic programming (英语课汇报)

Solving 0/1 knapsack problem with dynamic programming (英语课汇报)

时间:2023-11-20 17:24:16浏览次数:30  
标签:weight dynamic Solving programming maximum value item knapsack problem

Solving 0/1 knapsack problem with dynamic programming

Introduction

0/1 knapsack problems

A long time ago, an explorer went to an island where there were treasures, but his knapsack could only hold a maximum weight of \(W\). Each treasure had its corresponding weight \(w_i\) and value \(v_i\). He was very worried about how to maximize the total value of the items in his knapsack

Content

Why can we use dynamic programing(DP) to solve the problem.

  • Principle of optimality
  • Definition
    • The optimal solution of the problem includes the optimal solution of the subproblem
    • In other words, we can derive the optimal solution of the problem from the optimal solution of the subproblem.
    • For this issue
      • The maximum value of the \(i_{th}\) item with a total weight of \(W\) can be derived from the maximum value of the first \({i-1}_{th}\) item with a weight of \(W - w_i\)
  • Without aftereffect
  • Definition
    • Once the state of a certain stage is determined, it is not affected by decisions made in subsequent stages
    • 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\).

Why should we use DP

  • Faster than depth first search(DFS)
    • Time complexity achieved using DP is just \(\operatorname 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 \(w_i\mod v_i \neq 0\),Greedy algorithms cannot find maximum value.

How to solve the problem by DP

  • Define the state
    • We define the state \(F_{i,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 \(i_{th}\) item, we only have two choice
      • Don't chose the \(i_{th}\) item
        • Obviously, the \(F_{i,j}\) can be transferred from \(F_{i - 1, j}\)
      • Chose the \(i_{th}\) item
        • Consider the weight of the current item
          • We can't chose the item without any weight consumption
          • We need \(w_i\) weight to hold the item
          • then we should transfer from \(F_{i - 1,j - w_i}\)
        • Consider the value of the current item
          • Obviously,when we chose the item, we get \(v_i\) value。
          • then \(F_{i - 1,j - w_i} + v_i\)
        • \(F_{i,j} = \max(F_{i,j}, F_{i - 1, j- w_i} + v_i)\)
  • Optimize space consumption
    • Obviously,every \(F_{i,j}\) is transferred from \(F_{i - 1, ?}\)
    • 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 \(i - 1\) state to update the \(i\) state.
  • 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]);
	}
}    

Conclusion

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

标签:weight,dynamic,Solving,programming,maximum,value,item,knapsack,problem
From: https://www.cnblogs.com/kdlyh/p/17844404.html

相关文章

  • Dynamic CRM 组织服务对Word模版生成PDF文件
    目的:解决用户手动下载word模版再上传问题解决方案:组织服务直接对指定的word模版文件生成PDF文件流1.word模版必须上传到系统文档模版后:设置->模版->文档模版 2.组织调用“ExportpdfDocument”,返回PDF文件字节信息。另外实体信息需要把“注释”勾选上,否则执行代码会报错,如下:......
  • 命令式编程(Imperative Programming)和声明式编程(Declarative Programming)的区别
    命令式编程(ImperativeProgramming)和声明式编程(DeclarativeProgramming)都是计算机编程的范式,它们有着不同的特点和适用场景。首先,我们讨论命令式编程。在命令式编程中,程序员需要明确地告诉计算机需要执行哪些步骤来达到预期的结果。我们可以把这种范式比作烹饪食谱:食谱会明确地......
  • es定制 dynamic mapping template(type)
    定制dynamicmappingtemplate(type)PUT/my_index{"mappings":{"my_type":{"dynamic_templates":[{"en":{"match":"*_en","match_mapping_type":"string","mapping&quo......
  • C# 22H2之后的windows版本使用SetDynamicTimeZoneInformation设置时区失败处理
    使用SetDynamicTimeZoneInformation设置时区返回false,设置失败。使用PowerShell设置Set-TimeZone成功。///<summary>///设置本地时区///参数取值"ChinaStandardTime",即可设置为中国时区///</summary>///<paramname="timeZoneId"></param>///<retur......
  • asp.net core api 3.1 dynamic 入参转json对象
    比如接口publicobjectGetList(dynamicobj){//varjElement=(JsonElement)obj;//使用system.text.json处理varstr=obj.GetRawText(); if(val!=JsonValueKind.Undefined&&val!=JsonValueKind.Null)           {if(obj.valueKind==JsonValueKind.Array)......
  • The 2020 ICPC Asia Yinchuan Regional Programming Contest
    Preface好久没有和队友一起打比赛了,然后今天纯战犯,G一个初值设错WA了三发还卡了1h,最后冲D也因为细节原因没调出来但这场现场的榜只能用惨淡来形容,6题就稳Au了,而且感觉如果最后能出7个题的话甚至能有出线机会?看来还是前面题目区分度太小了A.BestPlayer签到题,按题意模拟即可......
  • implement a parallel batch processing in X++ of Dynamics 365 F&O
    OneofthepowerfulfeaturesofDynamics365FinanceandOperationsisaBatchframework.Inthispost,Iexplainhowyoucanconvertyourexistingbatchjobtomulti-threadedtoincreaseitsperformance.InitialexampleLet'sconsiderthefollowing......
  • The 2020 ICPC Asia Shenyang Regional Programming Contest M. United in Stormwind
    Preface先补一下这周一队友VP的ICPC2020沈阳,这场由于我在补作业+晚上有大物实验,因此只参与了中间一个多小时,纯口胡了几个简单题因为我没怎么参与所以过的其它题就不写补题+写博客了,毕竟队友会等于我会那么就主要把我比赛时看了但没啥思路的M补了,AI祁神好像在补那我就不管了,后面......
  • Chen Shuo's Practical Network Programming - TTCP Lecture代码注释
    下面是C语言版本的TTCP,主要注释的是voidreceive(constOptions&opt);函数,负责在服务器接收客户端发送的数据://muduo/examples/ace/ttcp/ttcp_blocking.cc#include...//接受新的TCP连接staticintacceptOrDie(uint16_tport){...}//完整的写N个字节staticint......
  • 2023 China Collegiate Programming Contest Shenzhen Site
    目录写在前面AFGLIEMK写在最后写在前面补题地址:vjudge。以下按照场上过题顺序排序。首银。比游记更早出来,没想到吧。游记链接:留坑。A场上先开的这道。直觉是考虑先全部区间加直到最小值,然后将非最小值全单点加,再重复上述过程。然而会被递增序列卡掉。瓶颈在于单点加太多......