Corrections

This commit is contained in:
Antti H S Laaksonen 2017-01-31 21:06:46 +02:00
parent 47259d0d5c
commit 1e8a4e5504
1 changed files with 155 additions and 158 deletions

View File

@ -1,32 +1,30 @@
\chapter{Complete search} \chapter{Complete search}
\key{Compelete search} \key{Complete search}
is a general method that can be used is a general method that can be used
for solving almost any algorithm problem. for solving almost any algorithm problem.
The idea is to generate all possible The idea is to generate all possible
solutions for the problem using brute force, solutions to the problem using brute force,
and select the best solution or count the and then select the best solution or count the
number of solutions, depending on the problem. number of solutions, depending on the problem.
Complete search is a good technique Complete search is a good technique
if it is feasible to go through all the solutions, if there is enough time to go through all the solutions,
because the search is usually easy to implement because the search is usually easy to implement
and it always gives the correct answer. and it always gives the correct answer.
If complete search is too slow, If complete search is too slow,
greedy algorithms or dynamic programming, other techniques, such as greedy algorithms or
presented in the next chapters, dynamic programming, may be needed.
may be used.
\section{Generating subsets} \section{Generating subsets}
\index{subset} \index{subset}
We first consider the case where We first consider the problem of generating
the possible solutions for the problem all subsets of a set of $n$ elements.
are the subsets of a set of $n$ elements. There are two common methods for this:
In this case, a complete search algorithm we can either implement a recursive search
has to generate or use bit operations of integers.
all $2^n$ subsets of the set.
\subsubsection{Method 1} \subsubsection{Method 1}
@ -35,11 +33,10 @@ of a set is to use recursion.
The following function \texttt{gen} The following function \texttt{gen}
generates the subsets of the set generates the subsets of the set
$\{1,2,\ldots,n\}$. $\{1,2,\ldots,n\}$.
The function maintains a vector \texttt{v} The function maintains a vector
that will contain the elements in the subset. that will contain the elements of each subset.
The generation of the subsets The search begins when the function is called
begins when the function with parameter 1.
is called with parameter $1$.
\begin{lstlisting} \begin{lstlisting}
void gen(int k) { void gen(int k) {
@ -54,18 +51,19 @@ void gen(int k) {
} }
\end{lstlisting} \end{lstlisting}
The parameter $k$ is the number that is the next The parameter $k$ is the next
candidate to be included in the subset. candidate to be included in the subset.
The function branches to two cases: The function considers two cases that both
either $k$ is included or it is not included in the subset. generate a recursive call:
Finally, when $k=n+1$, a decision has been made for either $k$ is included or not included in the subset.
all the numbers and one subset has been generated. Finally, when $k=n+1$, all elements have been processed
and one subset has been generated.
For example, when $n=3$, the function calls The following tree illustrates how the function is
create a tree illustrated below. called when $n=3$.
At each call, the left branch doesn't include We can always choose either the left branch
the number and the right branch includes the number ($k$ is not included in the subset) or the right branch
in the subset. ($k$ is included in the subset).
\begin{center} \begin{center}
\begin{tikzpicture}[scale=.45] \begin{tikzpicture}[scale=.45]
@ -122,21 +120,21 @@ in the subset.
\subsubsection{Method 2} \subsubsection{Method 2}
Another way to generate the subsets is to exploit Another way to generate subsets is to exploit
the bit representation of integers. the bit representation of integers.
Each subset of a set of $n$ elements Each subset of a set of $n$ elements
can be represented as a sequence of $n$ bits, can be represented as a sequence of $n$ bits,
which corresponds to an integer between $0 \ldots 2^n-1$. which corresponds to an integer between $0 \ldots 2^n-1$.
The ones in the bit representation indicate The ones in the bit sequence indicate
which elements of the set are included in the subset. which elements are included in the subset.
The usual interpretation is that element $k$ The usual convention is that element $k$
is included in the subset if $k$th bit from the is included in the subset if the $k$th last bit
end of the bit sequence is one. in the sequence is one.
For example, the bit representation of 25 For example, the bit representation of 25
is 11001 that corresponds to the subset $\{1,4,5\}$. is 11001, that corresponds to the subset $\{1,4,5\}$.
The following iterates through all subsets The following code goes through all subsets
of a set of $n$ elements of a set of $n$ elements
\begin{lstlisting} \begin{lstlisting}
@ -145,11 +143,11 @@ for (int b = 0; b < (1<<n); b++) {
} }
\end{lstlisting} \end{lstlisting}
The following code converts each bit The following code shows how we can derive
representation to a vector \texttt{v} the elements in a subset from the bit sequence.
that contains the elements in the subset. When processing each subset,
This can be done by checking which bits the code builds a vector that contains the
are one in the bit representation. elements in the subset.
\begin{lstlisting} \begin{lstlisting}
for (int b = 0; b < (1<<n); b++) { for (int b = 0; b < (1<<n); b++) {
@ -164,21 +162,22 @@ for (int b = 0; b < (1<<n); b++) {
\index{permutation} \index{permutation}
Another common situation is that the solutions Next we will consider the problem of generating
for the problem are permutations of a all permutations of a set of $n$ elements.
set of $n$ elements. Again, there are two approaches:
In this case, a complete search algorithm has to we can either use recursion or go trough the
generate $n!$ possible permutations. permutations iteratively.
\subsubsection{Method 1} \subsubsection{Method 1}
Like subsets, permutations can be generated Like subsets, permutations can be generated
using recursion. using recursion.
The following function \texttt{gen} iterates The following function \texttt{gen} goes
through the permutations of the set $\{1,2,\ldots,n\}$. through the permutations of the set $\{1,2,\ldots,n\}$.
The function uses the vector \texttt{v} The function builds a vector that contains
for storing the permutations, and the generation the elements in the permutation,
begins by calling the function without parameters. and the search begins when the function is
called without parameters.
\begin{lstlisting} \begin{lstlisting}
void haku() { void haku() {
@ -198,25 +197,25 @@ void haku() {
\end{lstlisting} \end{lstlisting}
Each function call adds a new element to Each function call adds a new element to
the permutation in the vector \texttt{v}. the vector \texttt{v}.
The array \texttt{p} indicates which The array \texttt{p} indicates which
elements are already included in the permutation. elements are already included in the permutation:
If $\texttt{p}[k]=0$, element $k$ is not included, if $\texttt{p}[k]=0$, element $k$ is not included,
and if $\texttt{p}[k]=1$, element $k$ is included. and if $\texttt{p}[k]=1$, element $k$ is included.
If the size of the vector equals the size of the set, If the size of \texttt{v} equals the size of the set,
a permutation has been generated. a permutation has been generated.
\subsubsection{Method 2} \subsubsection{Method 2}
\index{next\_permutation@\texttt{next\_permutation}} \index{next\_permutation@\texttt{next\_permutation}}
Another method is to begin from permutation Another method for generating permutations
$\{1,2,\ldots,n\}$ and at each step generate the is to begin with the permutation
next permutation in increasing order. $\{1,2,\ldots,n\}$ and repeatedly
use a function that constructs the next permutation
in increasing order.
The C++ standard library contains the function The C++ standard library contains the function
\texttt{next\_permutation} that can be used for this. \texttt{next\_permutation} that can be used for this:
The following code generates the permutations
of the set $\{1,2,\ldots,n\}$ using the function:
\begin{lstlisting} \begin{lstlisting}
vector<int> v; vector<int> v;
@ -233,23 +232,21 @@ do {
\index{backtracking} \index{backtracking}
A \key{backtracking} algorithm A \key{backtracking} algorithm
begins from an empty solution begins with an empty solution
and extends the solution step by step. and extends the solution step by step.
At each step, the search branches The search recursively
to all possible directions how the solution goes through all different ways how
can be extended. a solution can be constructed.
After processing one branch, the search
continues to other possible directions.
\index{queen problem} \index{queen problem}
As an example, consider the \key{queen problem} As an example, consider the \key{queen problem}
where our task is to calculate the number where the task is to calculate the number
of ways we can place $n$ queens to of ways we can place $n$ queens to
an $n \times n$ chessboard so that an $n \times n$ chessboard so that
no two queens attack each other. no two queens attack each other.
For example, when $n=4$, For example, when $n=4$,
there are two possible solutions for the problem: there are two possible solutions to the problem:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=.65] \begin{tikzpicture}[scale=.65]
@ -272,14 +269,14 @@ there are two possible solutions for the problem:
The problem can be solved using backtracking The problem can be solved using backtracking
by placing queens to the board row by row. by placing queens to the board row by row.
More precisely, we should place exactly one queen More precisely, exactly one queen will
to each row so that no queen attacks be placed to each row so that no queen attacks
any of the queens placed before. any of the queens placed before.
A solution is ready when we have placed all A solution has been found when all
$n$ queens to the board. $n$ queens have been placed to the board.
For example, when $n=4$, the tree produced by For example, when $n=4$, the backtracking
the backtracking algorithm begins like this: algorithm generates the following tree:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=.55] \begin{tikzpicture}[scale=.55]
@ -291,10 +288,10 @@ the backtracking algorithm begins like this:
\draw (3, -6) grid (7, -2); \draw (3, -6) grid (7, -2);
\draw (9, -6) grid (13, -2); \draw (9, -6) grid (13, -2);
\node at (-9+0.5,-3+0.5) {$K$}; \node at (-9+0.5,-3+0.5) {$Q$};
\node at (-3+1+0.5,-3+0.5) {$K$}; \node at (-3+1+0.5,-3+0.5) {$Q$};
\node at (3+2+0.5,-3+0.5) {$K$}; \node at (3+2+0.5,-3+0.5) {$Q$};
\node at (9+3+0.5,-3+0.5) {$K$}; \node at (9+3+0.5,-3+0.5) {$Q$};
\draw (2,0) -- (-7,-2); \draw (2,0) -- (-7,-2);
\draw (2,0) -- (-1,-2); \draw (2,0) -- (-1,-2);
@ -306,14 +303,14 @@ the backtracking algorithm begins like this:
\draw (-1, -12) grid (3, -8); \draw (-1, -12) grid (3, -8);
\draw (4, -12) grid (8, -8); \draw (4, -12) grid (8, -8);
\draw[white] (11, -12) grid (15, -8); \draw[white] (11, -12) grid (15, -8);
\node at (-11+1+0.5,-9+0.5) {$K$}; \node at (-11+1+0.5,-9+0.5) {$Q$};
\node at (-6+1+0.5,-9+0.5) {$K$}; \node at (-6+1+0.5,-9+0.5) {$Q$};
\node at (-1+1+0.5,-9+0.5) {$K$}; \node at (-1+1+0.5,-9+0.5) {$Q$};
\node at (4+1+0.5,-9+0.5) {$K$}; \node at (4+1+0.5,-9+0.5) {$Q$};
\node at (-11+0+0.5,-10+0.5) {$K$}; \node at (-11+0+0.5,-10+0.5) {$Q$};
\node at (-6+1+0.5,-10+0.5) {$K$}; \node at (-6+1+0.5,-10+0.5) {$Q$};
\node at (-1+2+0.5,-10+0.5) {$K$}; \node at (-1+2+0.5,-10+0.5) {$Q$};
\node at (4+3+0.5,-10+0.5) {$K$}; \node at (4+3+0.5,-10+0.5) {$Q$};
\draw (-1,-6) -- (-9,-8); \draw (-1,-6) -- (-9,-8);
\draw (-1,-6) -- (-4,-8); \draw (-1,-6) -- (-4,-8);
@ -329,10 +326,10 @@ the backtracking algorithm begins like this:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
At the bottom level, the three first subsolutions At the bottom level, the three first boards
are not valid because the queens attack each other. are not valid, because the queens attack each other.
However, the fourth subsolution is valid However, the fourth board is valid
and it can be extended to a full solution by and it can be extended to a complete solution by
placing two more queens to the board. placing two more queens to the board.
\begin{samepage} \begin{samepage}
@ -360,16 +357,16 @@ to the variable $c$.
The code assumes that the rows and columns The code assumes that the rows and columns
of the board are numbered from 0. of the board are numbered from 0.
The function places a queen to row $y$ The function places a queen to row $y$
when $0 \le y < n$. where $0 \le y < n$.
Finally, if $y=n$, one solution has been found Finally, if $y=n$, a solution has been found
and the variable $c$ is increased by one. and the variable $c$ is increased by one.
The array \texttt{r1} keeps track of the columns The array \texttt{r1} keeps track of the columns
that already contain a queen. that already contain a queen,
Similarly, the arrays \texttt{r2} and \texttt{r3} and the arrays \texttt{r2} and \texttt{r3}
keep track of the diagonals. keep track of the diagonals.
It is not allowed to add another queen to a It is not allowed to add another queen to a
column or to a diagonal. column or diagonal that already contains a queen.
For example, the rows and the diagonals of For example, the rows and the diagonals of
the $4 \times 4$ board are numbered as follows: the $4 \times 4$ board are numbered as follows:
@ -438,37 +435,37 @@ the $4 \times 4$ board are numbered as follows:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
Using the presented backtracking The above backtracking
algorithm, we can calculate that, algorithm shows that
for example, there are 92 ways to place 8 there are 92 ways to place 8
queens to an $8 \times 8$ chessboard. queens to the $8 \times 8$ chessboard.
When $n$ increases, the search quickly becomes slow When $n$ increases, the search quickly becomes slow,
because the number of the solutions increases because the number of the solutions increases
exponentially. exponentially.
For example, calculating the ways to For example, calculating the ways to
place 16 queens to the $16 \times 16$ place 16 queens to the $16 \times 16$
chessboard already takes about a minute chessboard already takes about a minute
on a modern computer
(there are 14772512 solutions). (there are 14772512 solutions).
\section{Pruning the search} \section{Pruning the search}
A backtracking algorithm can often be optimized We can often optimize backtracking
by pruning the search tree. by pruning the search tree.
The idea is to add ''intelligence'' to the algorithm The idea is to add ''intelligence'' to the algorithm
so that it will notice as soon as possible so that it will realize as soon as possible
if is not possible to extend a subsolution into if a partial solution cannot be extended
a full solution. to a complete solution.
This kind of optimization can have a tremendous Such optimizations can have a tremendous
effect on the efficiency of the search. effect on the efficiency of the search.
Let us consider a problem where Let us consider a problem
our task is to calculate the number of paths of calculating the number of paths
in an $n \times n$ grid from the upper-left corner in an $n \times n$ grid from the upper-left corner
to the lower-right corner so that each square to the lower-right corner so that each square
will be visited exactly once. will be visited exactly once.
For example, in the $7 \times 7$ grid, For example, in a $7 \times 7$ grid,
there are 111712 possible paths from the there are 111712 such paths.
lower-right corner to the upper-right corner.
One of the paths is as follows: One of the paths is as follows:
\begin{center} \begin{center}
@ -487,11 +484,10 @@ One of the paths is as follows:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
We will concentrate on the $7 \times 7$ case Next we will concentrate on the $7 \times 7$ case.
because it is computationally suitable difficult.
We begin with a straightforward backtracking algorithm, We begin with a straightforward backtracking algorithm,
and then optimize it step by step using observations and then optimize it step by step using observations
how the search tree can be pruned. how the search can be pruned.
After each optimization, we measure the running time After each optimization, we measure the running time
of the algorithm and the number of recursive calls, of the algorithm and the number of recursive calls,
so that we will clearly see the effect of each so that we will clearly see the effect of each
@ -499,10 +495,10 @@ optimization on the efficiency of the search.
\subsubsection{Basic algorithm} \subsubsection{Basic algorithm}
The first version of the algorithm doesn't contain The first version of the algorithm does not contain
any optimizations. We simply use backtracking to generate any optimizations. We simply use backtracking to generate
all possible paths from the upper-left corner to all possible paths from the upper-left corner to
the lower-right corner. the lower-right corner and count the number of such paths.
\begin{itemize} \begin{itemize}
\item \item
@ -513,8 +509,8 @@ recursive calls: 76 billions
\subsubsection{Optimization 1} \subsubsection{Optimization 1}
The first step in a solution is either In any solution, we first move a step
downward or to the right. down or right.
There are always two paths that There are always two paths that
are symmetric are symmetric
about the diagonal of the grid about the diagonal of the grid
@ -554,8 +550,8 @@ For example, the following paths are symmetric:
\end{tabular} \end{tabular}
\end{center} \end{center}
Thus, we can decide that the first step Hence, we can decide that we always first
in the solution is always downward, move down,
and finally multiply the number of the solutions by two. and finally multiply the number of the solutions by two.
\begin{itemize} \begin{itemize}
@ -571,7 +567,7 @@ If the path reaches the lower-right square
before it has visited all other squares of the grid, before it has visited all other squares of the grid,
it is clear that it is clear that
it will not be possible to complete the solution. it will not be possible to complete the solution.
An example of this is the following case: An example of this is the following path:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=.55] \begin{tikzpicture}[scale=.55]
@ -585,7 +581,7 @@ An example of this is the following case:
\end{scope} \end{scope}
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
Using this observation, we can terminate the search branch Using this observation, we can terminate the search
immediately if we reach the lower-right square too early. immediately if we reach the lower-right square too early.
\begin{itemize} \begin{itemize}
\item \item
@ -597,10 +593,10 @@ recursive calls: 20 billions
\subsubsection{Optimization 3} \subsubsection{Optimization 3}
If the path touches the wall so that there is If the path touches the wall so that there is
an unvisited square at both sides, an unvisited square on both sides,
the grid splits into two parts. the grid splits into two parts.
For example, in the following case For example, in the following path
both the left and the right squares both the left and right squares
are unvisited: are unvisited:
\begin{center} \begin{center}
@ -616,8 +612,8 @@ are unvisited:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
Now it will not be possible to visit every square, Now it will not be possible to visit every square,
so we can terminate the search branch. so we can terminate the search.
This optimization is very useful: It turns out that this optimization is very useful:
\begin{itemize} \begin{itemize}
\item \item
@ -636,7 +632,7 @@ of the current square are unvisited and
the left and right neighbors are the left and right neighbors are
wall or visited (or vice versa). wall or visited (or vice versa).
For example, in the following case For example, in the following path
the top and bottom neighbors are unvisited, the top and bottom neighbors are unvisited,
so the path cannot visit all squares so the path cannot visit all squares
in the grid anymore: in the grid anymore:
@ -652,8 +648,9 @@ in the grid anymore:
\end{scope} \end{scope}
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
The search becomes even faster when we terminate Thus, we can terminate the search in all such cases.
the search branch in all such cases: After this optimization, the search will be
very efficient:
\begin{itemize} \begin{itemize}
\item \item
@ -663,22 +660,22 @@ recursive calls: 69 millions
\end{itemize} \end{itemize}
~\\ ~\\
Now it's a good moment to stop optimization Now it is a good moment to stop optimizing
and remember our starting point. the algorithm and see what we have achieved.
The running time of the original algorithm The running time of the original algorithm
was 483 seconds, was 483 seconds,
and now after the optimizations, and now after the optimizations,
the running time is only 0.6 seconds. the running time is only 0.6 seconds.
Thus, the algorithm became nearly 1000 times Thus, the algorithm became nearly 1000 times
faster after the optimizations. faster thanks to the optimizations.
This is a usual phenomenon in backtracking This is a usual phenomenon in backtracking,
because the search tree is usually large because the search tree is usually large
and even simple optimizations can prune and even simple observations can effectively
a lot of branches in the tree. prune the search.
Especially useful are optimizations that Especially useful are optimizations that
occur at the top of the search tree because occur during the first steps of the algorithm,
they can prune the search very efficiently. i.e., at the top of the search tree.
\section{Meet in the middle} \section{Meet in the middle}
@ -691,10 +688,10 @@ A separate search is performed
for each of the parts, for each of the parts,
and finally the results of the searches are combined. and finally the results of the searches are combined.
The meet in the middle technique can be used The technique can be used
if there is an efficient way to combine the if there is an efficient way to combine the
results of the searches. results of the searches.
In this case, the two searches may require less In such a situation, the two searches may require less
time than one large search. time than one large search.
Typically, we can turn a factor of $2^n$ Typically, we can turn a factor of $2^n$
into a factor of $2^{n/2}$ using the meet in the into a factor of $2^{n/2}$ using the meet in the
@ -702,52 +699,52 @@ middle technique.
As an example, consider a problem where As an example, consider a problem where
we are given a list of $n$ numbers and we are given a list of $n$ numbers and
an integer $x$. a number $x$.
Our task is to find out if it is possible Our task is to find out if it is possible
to choose some numbers from the list so that to choose some numbers from the list so that
the sum of the numbers is $x$. their sum is $x$.
For example, given the list $[2,4,5,9]$ and $x=15$, For example, given the list $[2,4,5,9]$ and $x=15$,
we can choose the numbers $[2,4,9]$ to get $2+4+9=15$. we can choose the numbers $[2,4,9]$ to get $2+4+9=15$.
However, if the list remains the same but $x=10$, However, if $x=10$,
it is not possible to form the sum. it is not possible to form the sum.
A standard solution for the problem is to A standard solution for the problem is to
go through all subsets of the elements and go through all subsets of the elements and
check if the sum of any of the subsets is $x$. check if the sum of any of the subsets is $x$.
The time complexity of this solution is $O(2^n)$ The running time of such a solution is $O(2^n)$,
because there are $2^n$ possible subsets. because there are $2^n$ subsets.
However, using the meet in the middle technique, However, using the meet in the middle technique,
we can create a more efficient $O(2^{n/2})$ time solution. we can achieve a more efficient $O(2^{n/2})$ time solution.
Note that $O(2^n)$ and $O(2^{n/2})$ are different Note that $O(2^n)$ and $O(2^{n/2})$ are different
complexities because $2^{n/2}$ equals $\sqrt{2^n}$. complexities because $2^{n/2}$ equals $\sqrt{2^n}$.
The idea is to divide the list given as input The idea is to divide the list into
to two lists $A$ and $B$ that each contain two lists $A$ and $B$ such that both
about half of the numbers. lists contain about half of the numbers.
The first search generates all subsets The first search generates all subsets
of the numbers in the list $A$ and stores their sums of the numbers in $A$ and stores their sums
to list $S_A$. to a list $S_A$.
Correspondingly, the second search creates Correspondingly, the second search creates
the list $S_B$ from the list $B$. a list $S_B$ from $B$.
After this, it suffices to check if it is possible After this, it suffices to check if it is possible
to choose one number from $S_A$ and another to choose one number from $S_A$ and another
number from $S_B$ so that their sum is $x$. number from $S_B$ so that their sum is $x$.
This is possible exactly when there is a way to This is possible exactly when there is a way to
form the sum $x$ using the numbers in the original list. form the sum $x$ using the numbers in the original list.
For example, assume that the list is $[2,4,5,9]$ and $x=15$. For example, suppose that the list is $[2,4,5,9]$ and $x=15$.
First, we divide the list into $A=[2,4]$ and $B=[5,9]$. First, we divide the list into $A=[2,4]$ and $B=[5,9]$.
After this, we create the lists After this, we create lists
$S_A=[0,2,4,6]$ and $S_B=[0,5,9,14]$. $S_A=[0,2,4,6]$ and $S_B=[0,5,9,14]$.
The sum $x=15$ is possible to form In this case, the sum $x=15$ is possible to form
because we can choose the number $6$ from $S_A$ because we can choose the number $6$ from $S_A$
and the number $9$ from $S_B$. and the number $9$ from $S_B$,
This choice corresponds to the solution $[2,4,9]$. which corresponds to the solution $[2,4,9]$.
The time complexity of the algorithm is $O(2^{n/2})$ The time complexity of the algorithm is $O(2^{n/2})$,
because both lists $A$ and $B$ contain $n/2$ numbers because both lists $A$ and $B$ contain $n/2$ numbers
and it takes $O(2^{n/2})$ time to calculate the sums of and it takes $O(2^{n/2})$ time to calculate the sums of
their subsets to lists $S_A$ and $S_B$. their subsets to lists $S_A$ and $S_B$.
After this, it is possible to check in After this, it is possible to check in
$O(2^{n/2})$ time if the sum $x$ can be created $O(2^{n/2})$ time if the sum $x$ can be formed
using the numbers in $S_A$ and $S_B$. using the numbers in $S_A$ and $S_B$.