You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/dynamic_programming/knapsack.md
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -14,11 +14,11 @@ There are $n$ distinct items and a knapsack of capacity $W$. Each item has 2 att
14
14
You have to select a subset of items to put into the knapsack such that the total weight does not exceed the capacity $W$ and the total value is maximized.
15
15
16
16
In the example above, each object has only two possible states (taken or not taken),
17
-
correspoding to binary 0 and 1. Thus, this type of problem is called "0-1 knapsack problem".
17
+
corresponding to binary 0 and 1. Thus, this type of problem is called "0-1 knapsack problem".
18
18
19
19
## 0-1 Knapsack
20
20
21
-
### Explaination
21
+
### Explanation
22
22
23
23
In the example above, the input to the problem is the following: the weight of $i^{th}$ item $w_{i}$, the value of $i^{th}$ item $v_{i}$, and the total capacity of the knapsack $W$.
24
24
@@ -44,7 +44,7 @@ that should be executed in the **decreasing** order of $j$ (so that $f_{j-w_i}$
44
44
45
45
### Implementation
46
46
47
-
The alorithm described can be implemented in $O(nW)$ as:
47
+
The algorithm described can be implemented in $O(nW)$ as:
48
48
49
49
```.c++
50
50
for (int i = 1; i <= n; i++)
@@ -62,7 +62,7 @@ We can refer to the idea of 0-1 knapsack to define the state: $f_{i, j}$, the ma
62
62
63
63
It should be noted that although the state definition is similar to that of a 0-1 knapsack, its transition rule is different from that of a 0-1 knapsack.
64
64
65
-
### Explaination
65
+
### Explanation
66
66
67
67
The trivial approach is, for the first $i$ items, enumerate how many times each item is to be taken. The time complexity of this is $O(n^2W)$.
The alorithm described can be implemented in $O(nW)$ as:
85
+
The algorithm described can be implemented in $O(nW)$ as:
86
86
87
87
```.c++
88
88
for (int i = 1; i <= n; i++)
@@ -100,7 +100,7 @@ This is equivalent to being able to put item $i$ into the backpack multiple time
100
100
101
101
Multiple knapsack is also a variant of 0-1 knapsack. The main difference is that there are $k_i$ of each item instead of just $1$.
102
102
103
-
### Explaination
103
+
### Explanation
104
104
105
105
A very simple idea is: "choose each item $k_i$ times" is equivalent to "$k_i$ of the same item is selected one by one". Thus converting it to a 0-1 knapsack model, which can be described by the transition function:
106
106
@@ -112,9 +112,9 @@ The time complexity of this process is $O(W\sum\limits_{i=1}^{n}k_i)$
112
112
113
113
We still consider converting the multiple knapsack model into a 0-1 knapsack model for optimization. The time complexity $O(Wn)$ can not be further optimized with the approach above, so we focus on $O(\sum k_i)$ component.
114
114
115
-
Let $A_{i, j}$ denote the $j^{th}$ item split from the $i^{th}$ item. In the trivial approach discussed above, $A_{i, j}$ represents the same item for all $j \leq k_i$. The main reason for our low efficiency is that we are doing a lot of repetetive work. For example, consider selecting $\{A_{i, 1},A_{i, 2}\}$, and selecting $\{A_{i, 2}, A_{i, 3}\}$. These two situations are completely equivalent. Thus optimizing the spiltting method will greatly reduce the time complexity.
115
+
Let $A_{i, j}$ denote the $j^{th}$ item split from the $i^{th}$ item. In the trivial approach discussed above, $A_{i, j}$ represents the same item for all $j \leq k_i$. The main reason for our low efficiency is that we are doing a lot of repetetive work. For example, consider selecting $\{A_{i, 1},A_{i, 2}\}$, and selecting $\{A_{i, 2}, A_{i, 3}\}$. These two situations are completely equivalent. Thus optimizing the splitting method will greatly reduce the time complexity.
116
116
117
-
The grouping is made more effiecent by using binary grouping.
117
+
The grouping is made more efficient by using binary grouping.
118
118
119
119
Specifically, $A_{i, j}$ holds $2^j$ individual items ($j\in[0,\lfloor \log_2(k_i+1)\rfloor-1]$).If $k_i + 1$ is not an integer power of $2$, another bundle of size $k_i-2^{\lfloor \log_2(k_i+1)\rfloor-1}$ is used to make up for it.
0 commit comments