当前位置:网站首页>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
边栏推荐
- Check 15 developer tools of Alibaba
- VLAN part of switching technology
- Mmclassification annotation file generation
- Exercise 8-10 output student grades (20 points)
- Legion is a network penetration tool
- Kotlin: collection use
- AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
- Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
- Rhcsa learning practice
- Advanced technology management - how to design and follow up the performance of students at different levels
猜你喜欢
Occasional pit compiled by idea
Four characteristics and isolation levels of database transactions
【Day1】 deep-learning-basics
Delayed message center design
用数据告诉你高考最难的省份是哪里!
Normal vector point cloud rotation
What is an excellent architect in my heart?
BGP ---- border gateway routing protocol ----- basic experiment
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
OSPF summary
随机推荐
Differences among opencv versions
Exercise 8-10 output student grades (20 points)
OSPF comprehensive experiment
Exercise 7-2 finding the maximum value and its subscript (20 points)
RHCE - day one
leetcode1229. Schedule the meeting
Servlet基本原理与常见API方法的应用
Latex insert picture, insert formula
Dynamic memory management
Deep learning 500 questions
Basic principle of servlet and application of common API methods
Check 15 developer tools of Alibaba
Delayed message center design
Custom type: structure, enumeration, union
Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
2021-08-11 function pointer
Vs201 solution to failure to open source file HPP (or link library file)
【Day2】 convolutional-neural-networks
转载:等比数列的求和公式,及其推导过程
Qtreeview+ custom model implementation example