Corrections
This commit is contained in:
parent
3dd874a4fa
commit
3f31020076
3 changed files with 87 additions and 86 deletions
46
luku05.tex
46
luku05.tex
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
\key{Complete search}
|
||||
is a general method that can be used
|
||||
for solving almost any algorithm problem.
|
||||
to solve almost any algorithm problem.
|
||||
The idea is to generate all possible
|
||||
solutions to the problem using brute force,
|
||||
and then select the best solution or count the
|
||||
|
|
@ -30,7 +30,7 @@ or use bit operations of integers.
|
|||
|
||||
An elegant way to go through all subsets
|
||||
of a set is to use recursion.
|
||||
The following function \texttt{gen}
|
||||
The following function
|
||||
generates the subsets of the set
|
||||
$\{1,2,\ldots,n\}$.
|
||||
The function maintains a vector
|
||||
|
|
@ -128,8 +128,8 @@ which corresponds to an integer between $0 \ldots 2^n-1$.
|
|||
The ones in the bit sequence indicate
|
||||
which elements are included in the subset.
|
||||
|
||||
The usual convention is that element $k$
|
||||
is included in the subset if the $k$th last bit
|
||||
The usual convention is that the $k$th element
|
||||
is included in the subset exactly when the $k$th last bit
|
||||
in the sequence is one.
|
||||
For example, the bit representation of 25
|
||||
is 11001, that corresponds to the subset $\{1,4,5\}$.
|
||||
|
|
@ -143,8 +143,8 @@ for (int b = 0; b < (1<<n); b++) {
|
|||
}
|
||||
\end{lstlisting}
|
||||
|
||||
The following code shows how we can derive
|
||||
the elements in a subset from the bit sequence.
|
||||
The following code shows how we can find
|
||||
the elements of a subset that corresponds to a bit sequence.
|
||||
When processing each subset,
|
||||
the code builds a vector that contains the
|
||||
elements in the subset.
|
||||
|
|
@ -165,14 +165,14 @@ for (int b = 0; b < (1<<n); b++) {
|
|||
Next we will consider the problem of generating
|
||||
all permutations of a set of $n$ elements.
|
||||
Again, there are two approaches:
|
||||
we can either use recursion or go trough the
|
||||
we can either use recursion or go through the
|
||||
permutations iteratively.
|
||||
|
||||
\subsubsection{Method 1}
|
||||
|
||||
Like subsets, permutations can be generated
|
||||
using recursion.
|
||||
The following function \texttt{gen} goes
|
||||
The following function goes
|
||||
through the permutations of the set $\{1,2,\ldots,n\}$.
|
||||
The function builds a vector that contains
|
||||
the elements in the permutation,
|
||||
|
|
@ -180,7 +180,7 @@ and the search begins when the function is
|
|||
called without parameters.
|
||||
|
||||
\begin{lstlisting}
|
||||
void haku() {
|
||||
void gen() {
|
||||
if (v.size() == n) {
|
||||
// process permutation v
|
||||
} else {
|
||||
|
|
@ -188,7 +188,7 @@ void haku() {
|
|||
if (p[i]) continue;
|
||||
p[i] = 1;
|
||||
v.push_back(i);
|
||||
haku();
|
||||
gen();
|
||||
p[i] = 0;
|
||||
v.pop_back();
|
||||
}
|
||||
|
|
@ -275,8 +275,9 @@ any of the queens placed before.
|
|||
A solution has been found when all
|
||||
$n$ queens have been placed to the board.
|
||||
|
||||
For example, when $n=4$, the backtracking
|
||||
algorithm generates the following tree:
|
||||
For example, when $n=4$,
|
||||
some partial solutions generated by
|
||||
the backtracking algorithm are as follows:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=.55]
|
||||
|
|
@ -436,7 +437,7 @@ the $4 \times 4$ board are numbered as follows:
|
|||
\end{center}
|
||||
|
||||
The above backtracking
|
||||
algorithm shows that
|
||||
algorithm tells us that
|
||||
there are 92 ways to place 8
|
||||
queens to the $8 \times 8$ chessboard.
|
||||
When $n$ increases, the search quickly becomes slow,
|
||||
|
|
@ -453,7 +454,7 @@ on a modern computer
|
|||
We can often optimize backtracking
|
||||
by pruning the search tree.
|
||||
The idea is to add ''intelligence'' to the algorithm
|
||||
so that it will realize as soon as possible
|
||||
so that it will notice as soon as possible
|
||||
if a partial solution cannot be extended
|
||||
to a complete solution.
|
||||
Such optimizations can have a tremendous
|
||||
|
|
@ -484,7 +485,8 @@ One of the paths is as follows:
|
|||
\end{tikzpicture}
|
||||
\end{center}
|
||||
|
||||
Next we will concentrate on the $7 \times 7$ case.
|
||||
We will concentrate on the $7 \times 7$ case,
|
||||
because its level of difficulty is appropriate to our needs.
|
||||
We begin with a straightforward backtracking algorithm,
|
||||
and then optimize it step by step using observations
|
||||
how the search can be pruned.
|
||||
|
|
@ -509,7 +511,7 @@ recursive calls: 76 billions
|
|||
|
||||
\subsubsection{Optimization 1}
|
||||
|
||||
In any solution, we first move a step
|
||||
In any solution, we first move one step
|
||||
down or right.
|
||||
There are always two paths that
|
||||
are symmetric
|
||||
|
|
@ -551,7 +553,7 @@ For example, the following paths are symmetric:
|
|||
\end{center}
|
||||
|
||||
Hence, we can decide that we always first
|
||||
move down,
|
||||
move one step down,
|
||||
and finally multiply the number of the solutions by two.
|
||||
|
||||
\begin{itemize}
|
||||
|
|
@ -683,9 +685,9 @@ i.e., at the top of the search tree.
|
|||
|
||||
\key{Meet in the middle} is a technique
|
||||
where the search space is divided into
|
||||
two equally large parts.
|
||||
two parts of about equal size.
|
||||
A separate search is performed
|
||||
for each of the parts,
|
||||
for both of the parts,
|
||||
and finally the results of the searches are combined.
|
||||
|
||||
The technique can be used
|
||||
|
|
@ -727,8 +729,8 @@ to a list $S_A$.
|
|||
Correspondingly, the second search creates
|
||||
a list $S_B$ from $B$.
|
||||
After this, it suffices to check if it is possible
|
||||
to choose one number from $S_A$ and another
|
||||
number from $S_B$ so that their sum is $x$.
|
||||
to choose one element from $S_A$ and another
|
||||
element from $S_B$ so that their sum is $x$.
|
||||
This is possible exactly when there is a way to
|
||||
form the sum $x$ using the numbers in the original list.
|
||||
|
||||
|
|
@ -736,7 +738,7 @@ 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]$.
|
||||
After this, we create lists
|
||||
$S_A=[0,2,4,6]$ and $S_B=[0,5,9,14]$.
|
||||
In this case, 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$
|
||||
and the number $9$ from $S_B$,
|
||||
which corresponds to the solution $[2,4,9]$.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue