当前位置:网站首页>In depth analysis of multiple knapsack problem (Part 2)
In depth analysis of multiple knapsack problem (Part 2)
2022-07-21 22:34:00 【Worthless research monk】
In depth analysis of multiple knapsack problem ( The next part )
Preface
In the previous three articles , We have discussed it carefully 01 knapsack problem and Complete knapsack problem as well as Multiple backpacks Part 1 , In this article, we mainly introduce Multiple knapsack problem An optimization method of —— Binary optimization Multiple backpack , If you haven't seen it yet Multiple backpacks Part 1 , You need to read Multiple backpacks Part 1 .
Introduction to multiple knapsack problem
Yes N N N Items and a capacity are V V V The backpack . The first i i i Items At most s i s_i si Pieces of , The volume of each piece is v i v_i vi, The value is w i w_i wi. Find out which items to pack in your backpack , It can make the total volume of the goods not exceed the capacity of the backpack , And the sum of values is the largest .
Be careful : The meaning of the characters used above is the same in this article .
Multiple knapsack problems are related to 01 knapsack and Completely backpack The difference is in the number of times items are available ,01 knapsack Can only be used once , Multiple backpack It can be used countless times , and Multiple backpack It can be used many times .
Binary optimization of multiple knapsacks
Implementation of binary optimization
In the formal analysis Binary optimization of multiple knapsacks Before , Let's analyze it The time complexity of multiple backpacks , Suppose we have N N N Item , The average number of each item is S S S, The time complexity of multiple backpacks is O ( N S V ) O(NSV) O(NSV). The binary optimization of multiple knapsacks can reduce the time complexity to O ( N V l o g ( S ) ) O(NVlog(S)) O(NVlog(S)).
stay Multiple backpacks Part 1 In the previous article, we mentioned the dynamic transfer equation of multiple knapsacks ( T = m i n ( S , V v i ) T = min(S, \frac{V}{v_i}) T=min(S,viV), among S S S Indicates the number of times an item can be selected , v i v_i vi Represents the volume of the object , V V V Indicates the capacity of the current backpack ):
d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j ] , d p [ i − 1 ] [ j − v [ i ] ] + w [ i ] , d p [ i − 1 ] [ j − v [ i ] ∗ 2 ] + w [ i ] ∗ 2 , . . . , d p [ i − 1 ] [ j − v [ i ] ∗ T ] + w [ i ] ∗ T } dp[i][j] = max\\ \{ \\ dp[i - 1][j], \\ dp[i - 1][j - v[i]] + w[i],\\ dp[i - 1][j - v[i] * 2] + w[i] * 2, \\ ..., \\ dp[i - 1][j - v[i] * T] + w[i] * T\\ \} dp[i][j]=max{ dp[i−1][j],dp[i−1][j−v[i]]+w[i],dp[i−1][j−v[i]∗2]+w[i]∗2,...,dp[i−1][j−v[i]∗T]+w[i]∗T}
From the above formula, we can know , For someone who has S S S Of the items , We should choose a suitable number to maximize our income , This number may be 1 , 2 , 3 , 4 , . . . , S 1, 2, 3, 4, ..., S 1,2,3,4,...,S. We are in the article Multiple backpacks Part 1 It is mentioned that we can put Multiple backpack Turn it into 01 knapsack , We will S S S Items are logically divided into those with the same volume and value S S S Two different things , Is divided into S S S When making dynamic selection, different items are compared with S S S The same items are the same . For example, for S S S Same items A A A, We are choosing 3 The profit can reach the maximum at the time of , So for the transformed 01 knapsack problem Just choose 3 One and A A A Items with the same volume and value can .
According to the above analysis, we can know Multiple backpack Can be transformed into 01 knapsack The reason is that multiple backpacks are transforming into 01 knapsack after ,01 knapsack Can have Multiple backpack choose 1 individual , choose 2 individual , choose 3 individual ,…, choose S S S The effect of .
And our binary optimization is mainly focused on this place . Multiple backpack Binary optimization of also transforms the multiple knapsack problem into 01 knapsack problem , But not to S S S The same items are converted into S S S Different items with the same volume and value . According to the above analysis, we know , We are going to Multiple backpack Turn it into 01 knapsack Then there is the need to ensure 01 knapsack Can achieve Multiple backpack choose 1 individual , choose 2 individual , choose 3 individual ,…, choose S S S The effect of . So how can we achieve this ? The following code mainly shows that binary optimization will Multiple backpack Turn it into 01 knapsack What should be added to the collection of the value and volume of the goods .
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int V = scanner.nextInt();
ArrayList<Integer> v = new ArrayList<>();
ArrayList<Integer> w = new ArrayList<>();
for (int i = 0; i < N; i++) {
// This means the second i The volume of items
int vi = scanner.nextInt();
// This means the second i The value of an item
int wi = scanner.nextInt();
// This means the second i How many items are there
int si = scanner.nextInt();
// This code is mainly to realize that multiple backpacks can choose 1 individual
// choice 2 individual ,..., choice S The effect of
for (int j = 1; j <= si; j *= 2) {
si -= j ;
v.add(vi * j);
w.add(wi * j);
}
if (si > 0) {
v.add(vi * si);
w.add(wi * si);
}
}
Let's take an example to analyze the above code , Suppose we add an item A A A, Its number is 9, The value and volume are 5 and 3. Then gather v v v And collection w w w The data are :
v = [ 3 , 6 , 12 , 6 ] v = [3, 6, 12, 6] v=[3,6,12,6]
w = [ 5 , 10 , 20 , 10 ] w = [5, 10, 20, 10] w=[5,10,20,10]
The above example will 9 individual A A A Divided into A 1 A_1 A1, A 2 A_2 A2, A 3 A_3 A3, as well as A 4 A_4 A4, A 1 A_1 A1 To A 4 A_4 A4 Its volume and value are equivalent to 1 individual ,2 individual ,4 individual ,2 One of the A A A The volume and value of . We mentioned above , We are going to Multiple backpack Turn it into 01 knapsack Then there is the need to ensure 01 knapsack Can achieve Multiple backpack choose 1 individual , choose 2 individual , choose 3 individual ,…, choose S S S The effect of , So how does the above transformation achieve this effect ?
- One A A A: amount to A 1 A_1 A1.
- Two A A A: amount to A 2 A_2 A2.
- Three A A A: amount to A 1 + A 2 A_1 + A_2 A1+A2, That is, when you choose dynamically A 1 A_1 A1 and A 2 A_2 A2 Two items .
- four A A A: amount to A 3 A_3 A3.
- Five A A A: amount to A 1 + A 3 A_1 + A_3 A1+A3, That is, when you choose dynamically A 1 A_1 A1 and A 3 A_3 A3 Two items .
- six A A A: amount to A 2 + A 3 A_2 + A_3 A2+A3, That is, when you choose dynamically A 2 A_2 A2 and A 3 A_3 A3 Two items .
- Seven A A A: amount to A 1 + A 2 + A 3 A_1 + A_2 + A_3 A1+A2+A3, That is, when you choose dynamically A 1 A_1 A1、 A 2 A_2 A2 and A 3 A_3 A3 Three items .
- Eight A A A: amount to A 2 + A 3 + A 4 A_2 + A_3 + A_4 A2+A3+A4, That is, when you choose dynamically A 2 A_2 A2、 A 3 A_3 A3 and A 4 A_4 A4 Three items .
- Nine A A A: amount to A 1 + A 2 + A 3 + A 4 A_1 + A_2 + A_3 + A_4 A1+A2+A3+A4, That is, when you choose dynamically A 1 A_1 A1、 A 2 A_2 A2、 A 3 A_3 A3 and A 4 A_4 A4 Four items .
I believe that after the above example, you have roughly understood the general implementation process of binary optimization , Binary optimization will also Multiple backpack Turn it into 01 knapsack But different from the previous transformation , We are not going to S S S Items A A A Divide into S S S Items of the same volume and value , Instead, it is divided into volumes and values that are the original items 1 times 、2 times 、3 times ,…, 2 n 2^n 2n Multiple items , namely 1 + 2 + 4 + . . . + 2 n + a = S 1 + 2 + 4 + ... + 2^n + a = S 1+2+4+...+2n+a=S, among a a a Is the last remaining remainder ( a < 2 n + 1 a \lt 2^{n + 1} a<2n+1), Like the last one above 2 Namely a = 9 - 1 - 2 - 4
. We can know this division , The number of items after we divide will be very small . If the maximum number of items is int
Maximum of type , If we divide one by one, we can divide more than 20 100 million items , And the above division , We can't divide more than 32 individual , Therefore, the time complexity is greatly reduced .
Before dividing one by one, our time complexity is O ( N S V ) O(NSV) O(NSV), The largest time complexity of dividing us as above is O ( N V l o g ( S ) ) O(NVlog(S)) O(NVlog(S)), among N N N Represents the number of items , S S S Indicates the average number of times an item can be selected , V V V Indicates the capacity of the backpack .
The above is how we use binary optimization to Multiple backpack Turn it into 01 knapsack The way , The complete code is as follows ( The following code uses single row array optimization ):
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int V = scanner.nextInt();
ArrayList<Integer> v = new ArrayList<>();
ArrayList<Integer> w = new ArrayList<>();
for (int i = 0; i < N; i++) {
int vi = scanner.nextInt();
int wi = scanner.nextInt();
int si = scanner.nextInt();
for (int j = 1; j <= si; j *= 2) {
si -= j ;
v.add(vi * j);
w.add(wi * j);
}
if (si > 0) {
v.add(vi * si);
w.add(wi * si);
}
System.out.println(v);
System.out.println(w);
}
int[] f = new int[V + 1];
for (int i = 0; i < v.size(); i++) {
for (int j = V; j >= v.get(i); j--) {
f[j] = Math.max(f[j], f[j - v.get(i)] + w.get(i));
}
}
System.out.println(f[V]);
}
}
The essence of binary optimization
We know that any number has its binary form , Any number can be represented by 2 Add the integer powers of to get :
If we have 15 Items A A A, Then we will get A 1 A_1 A1, A 2 A_2 A2, A 3 A_3 A3, A 4 A_4 A4, Their value and volume are A A A Of 1 times ,2 times ,4 Times and 8 times , These four items can form items equivalent to any integral multiple A A A The value and weight of , Among this problem is 1, 2, 4, 8 It can make up 1~15 Any number between .
Because we may finally S S S All items are selected , So when we turn multiple backpacks into 01 Behind the backpack , The value and volume of all transformed items need to be equal to S S S Items are the same , and S S S It doesn't necessarily happen to be n n n Add the values of integer powers , Therefore, it is also mentioned above a a a, a a a It ensures that we can finally get 1~ S S S Any number between .
summary
This article mainly introduces Multiple knapsack problem Binary optimization of , The logic inside is still a little complicated , You may need to experience it carefully , When you read the text, you can refer to the code and analyze it carefully , It can be understood better .
That's all the content of this article , I hope you can get something , I am a LeHung, See you next time !!!( Remember give the thumbs-up Collection !)
More wonderful content collections can be accessed to the project :https://github.com/Chang-LeHung/CSCore
Official account : Worthless research monk , Learn more about computers (Java、Python、 Fundamentals of computer system 、 Algorithm and data structure ) knowledge .
边栏推荐
- Interviewer: have you made a judgment on the number of rows affected by your update?
- Heap - principle to application - heap sort, priority queue
- 概率论-最大似然估计
- Easily self-made Pascal VOC dataset
- 02-selenium的进一步学习(控制浏览器窗口+)
- 10 classic C language interview basic algorithms
- Why not use scheduled tasks to close orders?
- Does NMN product really have its effect? What effect does it have after taking NMN for several months
- Common shortcut keys of robotframework
- Learning notes for beginners to OpenGL (II) Download glew, configure the environment of glew and initialization of glew
猜你喜欢
04-unittest单元测试框架
Is there any bad effect of NMN? Deeply analyze the efficacy and effect of NMN
Task and Await: Consuming Awaitable Methods
Teach you how to use yolov4 training and testing data set on the server (nanny level)
Heap - principle to application - heap sorting, priority queue
What is a direct drinking machine? What is its working principle and advantages?
前 3 名突然变了?揭秘 7 月编程语言最新排行榜
Further learning of 02 selenium (control browser window +)
概率论-方差和协方差、相关系数
企业如何做好数据管理?产品选型怎么做?
随机推荐
Do you think sub database and sub table are really suitable for your system? Talk about how to select sub databases, sub tables and newsql
437. 路径总和 III
Visual Studio 特性进阶
西瓜书第三章-线性模型
What is a direct drinking machine? What is its working principle and advantages?
Advanced visual studio features
Source insight 4.0 personalization and shortcut keys
Comparison between ts and ES6
线性回归大结局(岭(Ridge)、 Lasso回归原理、公式推导),你想要的这里都有
Verification code is the natural enemy of automation? See how the great God solved it
Interview question: what is the difference between clustered index and non clustered index?
Robotframework -- implementation of browser drive and operation (1)
What are the components of the Internet of things?
集合间的映射和集合的势
Linear regression finale (ridge, Lasso regression principle, formula derivation), you want everything here
Learn SCP NFS TFTP together
Liteos connector script (I)
03-selenium的自动化测试模型
流批一体?实时数据处理场景化应用实例~
手把手教你在服务器上用YOLOv4训练和测试数据集(保姆级)