Skip to content

Commit

Permalink
Break solution search into two chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinyu95 committed Jan 21, 2024
1 parent 3c7823d commit d3fac9d
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions search/search-en.tex
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
%\markboth{Solution search}{Elementary Algorithms}

\ifx\wholebook\relax
\chapter{Solution search}
\chapter{Solution search I}
\numberwithin{Exercise}{chapter}
\fi

\def\includetikz{}

Computers enables people to search the solution for many problems: we build robot to search and pick the gadget in assembly lane; we develop car navigator to search the map for the best route; we make smart phone application to search the best shopping plan. This chapter is about the elementary lookup, matching, and solution search algorithms.
Computers enables people to search the solution for many problems: we build robot to search and pick the gadget in assembly lane; we develop car navigator to search the map for the best route; we make smart phone application to search the best shopping plan. This chapter is about the elementary lookup and matching algorithms.

\section{$k$ selection problem}
\index{Selection algorithm}
Expand Down Expand Up @@ -152,7 +152,7 @@ \section{Binary search}
\label{eq:bsearch}
\ee

\subsection{2D search}
\section{2D search}

Extend binary search to 2D or even higher dimension. Consider matrix $M$ of size $m \times n$. The elements in each row, column are ascending nature numbers as shown in \cref{fig:matrix-eg}. How to locate all elements equal to $z$? i.e. find all locations of $(i, j)$, such that $M_{i,j} = z$.

Expand Down Expand Up @@ -1040,17 +1040,18 @@ \section{String matching}

%% Richard Bird presents a formal program deduction to KMP algorithm by using fold fusion law in chapter 17 of \cite{fp-pearls}.

\section{Solution search}
\chapter{Solution search II}
\numberwithin{Exercise}{chapter}

In early years of artificial intelligent, people developed methods to search solutions. Different from sequence and string matching, the solution may not directly exist among a set of candidates. We need construct the solution while try varies of options. Some problems are not solvable. Among the solvable ones, there can be multiple solutions. For example, a maze may have multiple ways out. We need find the optimal solution sometimes.
In early years of artificial intelligent, people developed methods to search solutions. Different from binary search and string matching, the solution may not directly exist among a set of candidates. We need construct the solution while try varies of options. Some problems are not solvable. Among the solvable ones, there can be multiple solutions. For example, a maze may have multiple ways out. We need find the optimal solution on different target.

\subsection{DFS and BFS}
\section{DFS and BFS}
\index{DFS} \index{Deep-first search}
\label{sec:DFS-BFS}

DFS stands for deep-first search, and BFS stands for breadth-first search. They are typical graph search algorithms. We give some examples and skip the formal definition of graph.
DFS stands for deep-first search and BFS stands for breadth-first search. They are typical graph search algorithms. We give some examples and skip the formal definition of graph.

\subsubsection{Maze}
\subsection{Maze}
\index{Maze problem}
Maze is a classic puzzle. There is saying: always turn right. However, it ends up loops as shown in \cref{fig:maze-loop} (b). The decision matters when there are multiple ways. In fairy tales, one takes some bread crumbs in a maze. Select a way, leave a piece of bread. If later enters a died end, then goes back to the last place through the bread crumbs. then goes to another way. Whenever sees bread crumbs left, one knows he visited it before. Then goes back and tries a different way. Repeats the `try and check' step, one will either find the way out, or go back to the starting point (no solution). We use $m \times n$ matrix to define a maze, each element is 0 or 1, means there is a way or not. Below matrix defines the maze in \cref{fig:maze-loop} (b):

Expand Down Expand Up @@ -1202,7 +1203,7 @@ \subsubsection{Maze}
}
\end{Answer}
\subsubsection{The eight queens puzzle}
\subsection{The eight queens puzzle}
\index{8 queens puzzle}
Although cheese has very long history, it was late in 1848, that Max Bezzel gave the 8 queens puzzle\cite{wiki-8-queens}. The queue is a powerful piece. It attacks any other pieces in the same row, column or diagonal at any distance, as shown in \cref{fig:8-queens-puzzle} (a). How to arrange 8 queens in the cheese board, such that none of them attacks each other? \Cref{fig:8-queens-puzzle} (b) gives a solution.
Expand Down Expand Up @@ -1340,7 +1341,7 @@ \subsubsection{The eight queens puzzle}
}
\end{Answer}
\subsubsection{The peg puzzle}
\subsection{The peg puzzle}
\index{Peg puzzle}
As shown in \cref{fig:leapfrog}, there are 6 frogs in 7 stones, grouped on two sides. Each frog can hop to the next stone if not occupied, or leap over to another empty one. The frogs can only move forward or stay, but not go back. \Cref{fig:pegrules} give the rules. How to arrange the frogs to hop, leap, such that the left and right sides swap? Mark the left frogs as -1, the right as 1, the empty stone as 0. We are seeking the solution that transitions from $s =$ \texttt{[-1, -1, -1, 0, 1, 1, 1]} to $e =$ \texttt{[1, 1, 1, 0, -1, -1, -1]}.
Expand Down Expand Up @@ -1522,7 +1523,7 @@ \subsubsection{The peg puzzle}
}
\end{Answer}
\subsubsection{The wolf, goat, and cabbage puzzle}
\subsection{The wolf, goat, and cabbage puzzle}
\index{The wolf, goat, and cabbage puzzle}
This traditional puzzle says that a farmer is going to cross the river with a wolf, a goat, and a bucket of cabbage. There is a small boat. Only the farmer can drive it. The boat can only carry one thing a time. The wolf would kill the goat; the goat would bite the cabbage if they stay alone without the farmer. The puzzle asks to find the best (fast) solution to cross the river.
Expand Down Expand Up @@ -1625,7 +1626,7 @@ \subsubsection{The wolf, goat, and cabbage puzzle}
& & wolf, goat, cabbage, farmer
\etab
\subsubsection{Water jugs puzzle}
\subsection{Water jugs puzzle}
\index{Water jugs puzzle}
Given two water jugs, 9 and 4 litres. How to get 6 litres of water from river? This puzzle has history back to ancient Greece. A story said the French mathematician Poisson solved this puzzle when he was a child. It also appears in Hollywood movie `Die-Hard 3'. P\`{o}lya uses this puzzle as an example of backwards induction\cite{how-to-solve-it}.
Expand Down Expand Up @@ -1868,7 +1869,7 @@ \subsubsection{Water jugs puzzle}
}
\end{Answer}
\subsubsection{Kloski}
\subsection{Kloski}
\index{Kloski puzzle}
Kloski is a block sliding puzzle, as shown in \cref{fig:klotski-cn}. There are 10 blocks of 3 sizes: 4 pieces of $1 \times 1$; 4 pieces of $1 \times 2$, 1 piece of $2 \times 1$, 1 piece of $2 \times 2$. The goal is to slide the big block to the bottom slot. \Cref{fig:klotski-jp} shows variants of this puzzle in Japan.
Expand Down Expand Up @@ -2158,12 +2159,12 @@ \subsubsection{Kloski}
}
\end{Answer}
\subsection{Greedy algorithm}
\section{Greedy algorithm}
\index{Greedy algorithm}
People need find the `best' solution to minimize time, space, cost, energy, and etc. It's not easy to find the optimal solution within limited resource. Many problems don't have solution in polynomial time, however, there exist simple solutions for a small portion of the special problems.
\subsubsection{Huffman coding}
\subsection{Huffman coding}
\index{Huffman coding}
Huffman coding encodes information with the shortest length. The ASCII code needs 7 bits to encode characters, digits, and symbols. It can represent $2^7 = 128$ symbols. We need at least $\log_2 n$ bits of 0/1 to distinguish $n$ symbols. Below table encodes upper case English letters, maps A to Z from 0 to 25, each with 5 bits. Zero is padded as 00000 but not 0. Such scheme is called fixed-length coding. For example, it encodes `INTERNATIONAL' to a binary number of 65 bits:
Expand Down Expand Up @@ -2352,7 +2353,7 @@ \subsubsection{Huffman coding}
\end{Answer}
\subsubsection{Change making problem}
\subsection{Change making problem}
\index{Change making problem}
How to change money with coins as few as possible? Suppose there are 5 coin values: 1, 5, 25, 50, and 100 as a set $C = \{1, 5, 25, 50, 100\}$. To change money of value $x$, we can apply the greedy method, always choose the most valued coin:
Expand Down Expand Up @@ -2448,7 +2449,7 @@ \subsubsection{Change making problem}
}
\end{Answer}
\subsection{Dynamic programming}
\section{Dynamic programming}
\index{Dynamic programming}
Consider how to find the best solution $C_m$ (the list of coins) to change money $x$ for any coin system. We can partition $C_m$ into two groups: $C_1$ and $C_2$, with values $x_1$ and $x_2$ respectively, i.e., $C_m = C_1 \doubleplus C_2$ and $x = x_1 + x_2$. We'll prove that $C_1$ is the optimal solution to change $x_1$, and $C_2$ is the optimal solution to change $x_2$.
Expand Down Expand Up @@ -2576,7 +2577,7 @@ \subsection{Dynamic programming}
\item Overlapping sub-problems. The solution of the sub-problem is reused multiple times to find the overall solution.
\end{enumerate}
\subsubsection{Longest common sub-sequence}
\subsection{Longest common sub-sequence}
\index{LCS} \index{Longest common sub-sequence}
Different with sub-string, the sub-sequence needn't be consecutive. For example: the longest common sub-string of `Mississippi' and `Missunderstanding' is `Miss', while the longest common sub-sequence is `Misssi' as shown in \cref{fig:lcs}. If rotate the figure by 90\degree, it turns to be a `diff' result between them. This is a common function in version control tools. The longest common sub-sequence of $x$ and $y$ are defined as below:
Expand Down Expand Up @@ -2689,7 +2690,7 @@ \subsubsection{Longest common sub-sequence}
}
\end{Answer}
\subsubsection{Subset sum}
\subsection{Subset sum}
\index{Subset sum}
Given a set $X$ of integers, how to find all the subsets $S \subseteq X$, that the sum of elements in $S$ is $s$, i.e., $\sum S = \sum\limits_{i \in S} i = s$? For example, $X$ = \{11, 64, -82, -68, 86, 55, -88, -21, 51\}, there are three subsets with sum $s = 0$: $S = \varnothing$, \{64, -82, 55, -88, 51\}, \{64, -82, -68, 86\}. We need exhaust $2^n$ subset sums, where $n = |X|$, the performance is $O(n 2^n)$.
Expand Down

0 comments on commit d3fac9d

Please sign in to comment.