当前位置:网站首页>Knapsack problem and 0-1 knapsack problem
Knapsack problem and 0-1 knapsack problem
2022-07-04 10:25:00 【sqjddb】
Knapsack problems and 0-1 knapsack problem
knapsack problem
Yes n Two items and a backpack , Design items i The value of vi , Weight is wi , You can choose a part of each item to put into the backpack , Asking how to put items can maximize the total value of the backpack .
Solve with greed
0-1 knapsack problem
Yes n Two items and a backpack , The capacity of the backpack is c, goods i The weight of is wi, The value is vi, There are only two choices for each item: put it in the backpack or not , Ask which items can maximize the total value if the backpack capacity allows .
* Greedy method to solve the knapsack problem

* Dynamic programming solves 0-1 knapsack problem
Ideas :
① Dynamic programming solves 0-1 The basic idea of knapsack problem is , Consider one thing by one , This is from n Start thinking ( From the first one can also , Essentially the same , It's just that the subscript of some data is different from the meaning of the representation , The procedure is also different , One is easy to understand the other , Here from the n Start thinking about it as an example ), Each time, consider whether the current item can be put , Whether the value brought by putting it is worth
② set up m(i,j) The capacity of the backpack is j, The optional items are i~n The optimal value of , Put items into Backpack Capacity j Will change .
for example m(1,c) Express , All items are optional , The current capacity of the backpack is c The optimal value of , That is the solution of the problem . First consider the second n Items to choose from , Find out m[n,c], Consider the next item one by one , Until we find the solution 
③ Then there is the following recursion , This is the core of understanding this problem , Make these recursive formulas clear and solve them by dynamic programming 0-1 The knapsack problem is basically mastered (w It's the weight ,v It's value )
Consider only the n Items :
m(n,j)=0 j< Wn ( There is no place to pack things on the back n)
m(n,j)=Vn j>= Wn ( Backpacks can hold items n)
If the item i I can't put it in , consider i~n Actually, it's about thinking about i+1 To n:
m(i,j)= m(i+1,j) j<Wi ( There is no place to pack things on the back i)
If you can put it in , Then consider having items i And nothing i Which is of great value
m(i,j)=max{m(i+1,j) , m(i+1,j-Wi) +Vi} j>=Wi

The procedure is as follows :
The following procedure determines which items are put into the backpack 
* Backtracking solution 0-1 knapsack problem
Use backtracking to solve 0-1 knapsack problem , To construct a subset tree 

It is also a matter of consideration , One layer represents an object , The left subtree represents the current item , The right subtree represents not to put , Obviously, all items were considered when the tree searched the leaf node . If the backpack has enough capacity , Then enter the left tree , Otherwise don't go in , Enter when the right subtree may contain the optimal solution , When we don't have to enter the right subtree, we prune the right subtree , Under what circumstances is pruning ?
for example :
The procedure is as follows :

* Branch and bound method to solve 0-1 knapsack problem
The basic idea :
There are two ways :
>> Queued


>> Priority queue



边栏推荐
- DML statement of MySQL Foundation
- Exercise 9-1 time conversion (15 points)
- Check 15 developer tools of Alibaba
- Hands on deep learning (37) -- cyclic neural network
- Rhcsa12
- Exercise 8-7 string sorting (20 points)
- 使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
- Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
- Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
- Velodyne configuration command
猜你喜欢
If you don't know these four caching modes, dare you say you understand caching?

IPv6 comprehensive experiment

Number of relationship models

El Table Radio select and hide the select all box

OSPF summary

Work order management system OTRs

Use the data to tell you where is the most difficult province for the college entrance examination!

uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示

Doris / Clickhouse / Hudi, a phased summary in June

The future education examination system cannot answer questions, and there is no response after clicking on the options, and the answers will not be recorded
随机推荐
From programmers to large-scale distributed architects, where are you (2)
Uniapp--- initial use of websocket (long link implementation)
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
[200 opencv routines] 218 Multi line italic text watermark
Lavel document reading notes -how to use @auth and @guest directives in lavel
Exercise 9-5 address book sorting (20 points)
有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
Some summaries of the third anniversary of joining Ping An in China
Differences among opencv versions
Network disk installation
Hands on deep learning (37) -- cyclic neural network
Whether a person is reliable or not, closed loop is very important
A little feeling
VLAN part of switching technology
For programmers, if it hurts the most...
Servlet基本原理与常见API方法的应用
Four characteristics and isolation levels of database transactions
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
MongoDB数据日期显示相差8小时 原因和解决方案
Histogram equalization

