From 945e52e1f735e90c469b7e2ce21a6acba5c2dcc6 Mon Sep 17 00:00:00 2001 From: Antti H S Laaksonen Date: Thu, 25 May 2017 14:18:48 +0300 Subject: [PATCH] Revisio almost complete --- chapter10.tex | 173 ++++++++++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 75 deletions(-) diff --git a/chapter10.tex b/chapter10.tex index e513d9d..3ee0ebd 100644 --- a/chapter10.tex +++ b/chapter10.tex @@ -646,9 +646,8 @@ to change an iteration over permutations into an iteration over subsets\footnote{This technique was introduced in 1962 by M. Held and R. M. Karp \cite{hel62}.}. The benefit of this is that -$n!$, the number of permutations of an $n$ element set, -is much larger than $2^n$, the number of subsets -of the same set. +$n!$, the number of permutations, +is much larger than $2^n$, the number of subsets. For example, if $n=20$, then $n! \approx 2.4 \cdot 10^{18}$ and $2^n \approx 10^6$. Thus, for certain values of $n$, @@ -689,16 +688,20 @@ The idea is to calculate for each subset of people two values: the minimum number of rides needed and the minimum weight of people who ride in the last group. -Let $\texttt{rides}(S)$ and $\texttt{weight}(S)$ denote -the minimum number of rides and the minimum -weight of the last group, where $S$ is a subset -of people. For example, +Let $\texttt{weight}[p]$ denote the weight of +person $p$ where $0 \le p < n$. +Then we define two functions: +$\texttt{rides}(S)$ is the minimum number of +rides for a subset $S$, +and $\texttt{last}(S)$ is the minimum weight +of the last ride. +For example, in the above scenario \[ \texttt{rides}(\{B,D,E\})=2 \hspace{10px} \textrm{and} -\hspace{10px} \texttt{weight}(\{B,D,E\})=5,\] +\hspace{10px} \texttt{last}(\{B,D,E\})=5,\] because the optimal rides are $\{B,E\}$ and $\{D\}$, and the second ride has weight 5. Of course, our final goal is to calculate the value -of $\texttt{rides}(X)$ where $X$ is a set that +of $\texttt{rides}(P)$ where $P$ is a set that contains all the people. We can calculate the values @@ -706,50 +709,74 @@ of the functions recursively and then apply dynamic programming. The idea is to go through all people who belong to $S$ and optimally -choose the last person who enters the elevator. -For example, if $S=\{B,D,E\}$, -one of $B$, $D$ and $E$ is the last person -who enters the elevator. +choose the last person $p$ who enters the elevator. Each such choice yields a subproblem for a smaller subset of people. +If $\texttt{last}(S \setminus p)+\texttt{weight}[p] \le x$, +we can add $p$ to the last ride. +Otherwise, we have to reserve a new ride +that initially only contains $p$. -Note that we can use a loop like +A convenient way to implement a dynamic programming +solution is to declare an array \begin{lstlisting} -for (int b = 0; b < (1< best[1<