Revise the knapsack section
This commit is contained in:
parent
a67a72c17c
commit
1a1b242e9f
157
chapter07.tex
157
chapter07.tex
|
@ -650,89 +650,106 @@ The time complexity of the algorithm is $O(n^2)$.
|
||||||
|
|
||||||
\index{knapsack}
|
\index{knapsack}
|
||||||
|
|
||||||
\key{Knapsack} is a classic problem where we
|
The term \key{knapsack} refers to problems where
|
||||||
are given $n$ objects with weights
|
a set of objects is given, and we should find
|
||||||
$w_1,w_2,\ldots,w_n$ and values
|
a subset of objects that has some properties.
|
||||||
$v_1,v_2,\ldots,v_n$.
|
Knapsack problems can often be solved
|
||||||
Our task is to choose a subset of the objects
|
using dynamic programming.
|
||||||
such that the sum of the weights is at most $W$
|
|
||||||
and the sum of the values is as large as possible.
|
In this section, we consider the following
|
||||||
|
problem:
|
||||||
|
Suppose that there are $n$
|
||||||
|
objects whose weights are $\{w_1,w_2,\ldots,w_n\}$,
|
||||||
|
and we should determine all distinct weight sums
|
||||||
|
that can be constructed using the objects.
|
||||||
|
For example, if the weights are
|
||||||
|
$\{1,3,3,5\}$, the following weight
|
||||||
|
sums are possible:
|
||||||
|
|
||||||
\begin{samepage}
|
|
||||||
For example, if the objects are
|
|
||||||
\begin{center}
|
\begin{center}
|
||||||
\begin{tabular}{rrr}
|
\begin{tabular}{rrrrrrrrrrrrr}
|
||||||
object & weight & value \\
|
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\
|
||||||
\hline
|
\hline
|
||||||
A & 5 & 1 \\
|
X & X & & X & X & X & X & X & X & X & & X & X \\
|
||||||
B & 6 & 3 \\
|
|
||||||
C & 8 & 5 \\
|
|
||||||
D & 5 & 3 \\
|
|
||||||
\end{tabular}
|
\end{tabular}
|
||||||
\end{center}
|
\end{center}
|
||||||
\end{samepage}
|
|
||||||
and $W=12$,
|
|
||||||
an optimal solution is to select objects $B$ and $D$.
|
|
||||||
In this case, the sum of weights is
|
|
||||||
$6+5=11 \le 12$, and the sum of values is $3+3=6$.
|
|
||||||
|
|
||||||
This problem can be solved in two different ways
|
In this case, all weight sums between $0 \ldots 12$
|
||||||
using dynamic programming.
|
are possible, expect 2 and 10.
|
||||||
We can either regard the problem as maximizing the
|
For example, the weight sum 7 is possible because we
|
||||||
total value of the objects or
|
can select the weights $\{1,3,3\}$.
|
||||||
as minimizing the total weight of the objects.
|
Note that there may be multiple objects
|
||||||
|
with the same weight.
|
||||||
|
|
||||||
\subsubsection{Solution 1}
|
To solve the problem, we focus on subproblems
|
||||||
|
where we only use the first $k$ objects
|
||||||
|
to construct weight sums.
|
||||||
|
Let $\texttt{possible}(k,x)=\texttt{true}$ if
|
||||||
|
we can construct a weight sum $x$
|
||||||
|
using the first $k$ objects,
|
||||||
|
and otherwise $\texttt{possible}(k,x)=\texttt{false}$.
|
||||||
|
The values of the function can be recursively
|
||||||
|
calculated as follows:
|
||||||
|
\[ \texttt{possible}(k,x) = \texttt{possible}(k-1,x) \lor \texttt{possible}(k-1,x-w_k) \]
|
||||||
|
This means that we can construct a weight sum $x$
|
||||||
|
using the $k$ first objects in two ways
|
||||||
|
depending on whether we
|
||||||
|
use the weight $w_k$ in the sum or not.
|
||||||
|
(The symbol ''$\lor$'' denotes the ''or'' operation.)
|
||||||
|
In addition, $\texttt{possible}(0,0)=\texttt{true}$
|
||||||
|
and $\texttt{possible}(0,x)=\texttt{false}$ when $x \neq 0$.
|
||||||
|
|
||||||
\textit{Maximization:} Let $\texttt{value}(k,u)$
|
The following table shows all values of the function
|
||||||
denote the maximum value
|
for the weights $\{1,3,3,5\}$ (the symbol ''X''
|
||||||
of a subset of objects $1 \ldots k$
|
indicates the true values):
|
||||||
whose weight is $u$.
|
|
||||||
Thus, the solution to the problem is
|
|
||||||
\[\max_{0 \le u \le W} \texttt{value}(n,u).\]
|
|
||||||
|
|
||||||
A recursive formula for calculating
|
\begin{center}
|
||||||
the function is
|
\begin{tabular}{r|rrrrrrrrrrrrr}
|
||||||
\[f(k,u) = \max(f(k-1,u),f(k-1,u-p_k)+a_k),\]
|
& 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\
|
||||||
because we can either include or not include
|
\hline
|
||||||
object $k$ in the solution.
|
0 & X & \\
|
||||||
The base cases are $f(0,0)=0$ and $f(0,u)=-\infty$
|
1 & X & X \\
|
||||||
when $u \neq 0$. The time compexity of
|
2 & X & X & & X & X \\
|
||||||
the solution is $O(nx)$.
|
3 & X & X & & X & X & & X & X \\
|
||||||
|
4 & X & X & & X & X & X & X & X & X & X & & X & X \\
|
||||||
|
\end{tabular}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
In the example case, the optimal solution is
|
After calculating those values, $\texttt{possible}(n,x)$
|
||||||
$f(4,11)=6$ that can be constructed
|
tells us whether we can construct a
|
||||||
using the following sequence:
|
weight sum $x$ using \emph{all} objects.
|
||||||
\[f(4,11)=f(3,6)+3=f(2,6)+3=f(1,0)+3+3=f(0,0)+3+3=6.\]
|
|
||||||
|
|
||||||
\subsubsection{Solution 2}
|
Let $W$ denote the total weight of the objects.
|
||||||
|
The following $O(nW)$ time
|
||||||
|
dynamic programming solution
|
||||||
|
corresponds to the recursive function:
|
||||||
|
\begin{lstlisting}
|
||||||
|
possible[0][0] = true;
|
||||||
|
for (int k = 1; k <= n; k++) {
|
||||||
|
for (int x = 0; x <= W; x++) {
|
||||||
|
possible[k][x] = possible[k-1][x];
|
||||||
|
if (x-w[k] >= 0) possible[k][x] |= possible[k-1][x-w[k]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
\end{lstlisting}
|
||||||
|
|
||||||
\textit{Minimization:} Let $f(k,u)$
|
However, here is a better implementation that only uses
|
||||||
denote the smallest possible total weight
|
a one-dimensional array $\texttt{possible}[x]$
|
||||||
when a subset of objects
|
that indicates whether we can construct a subset with weight sum $x$.
|
||||||
$1 \ldots k$ is selected such
|
The trick is to update the array from right to left for
|
||||||
that the total value is $u$.
|
each new weight:
|
||||||
The solution to the problem is the
|
\begin{lstlisting}
|
||||||
largest value $u$
|
possible[0] = true;
|
||||||
for which $0 \le u \le s$ and $f(n,u) \le x$
|
for (int k = 1; k <= n; k++) {
|
||||||
where $s=\sum_{i=1}^n a_i$.
|
for (int x = W; x >= 0; x--) {
|
||||||
A recursive formula for calculating the function is
|
if (possible[x]) possible[x+w[k]] = true;
|
||||||
\[f(k,u) = \min(f(k-1,u),f(k-1,u-a_k)+p_k)\]
|
}
|
||||||
as in solution 1.
|
}
|
||||||
The base cases are $f(0,0)=0$ and $f(0,u)=\infty$
|
\end{lstlisting}
|
||||||
when $u \neq 0$.
|
|
||||||
The time complexity of the solution is $O(ns)$.
|
|
||||||
|
|
||||||
In the example case, the optimal solution is $f(4,6)=11$
|
In some other knapsack problems, objects have both weights and values,
|
||||||
that can be constructed using the following sequence:
|
and we should find a maximum-value subset whose weight is restricted.
|
||||||
\[f(4,6)=f(3,3)+5=f(2,3)+5=f(1,0)+6+5=f(0,0)+6+5=11.\]
|
We can solve such problems using similar ideas as we used here.
|
||||||
|
|
||||||
~\\
|
|
||||||
It is interesting to note how the parameters of the input
|
|
||||||
affect the efficiency of the solutions.
|
|
||||||
The efficiency of solution 1 depends on the weights
|
|
||||||
of the objects, while the efficiency of solution 2
|
|
||||||
depends on the values of the objects.
|
|
||||||
|
|
||||||
\section{Edit distance}
|
\section{Edit distance}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue