Merge conflicting changes from upstream in chapter 1

This commit is contained in:
Roope Salmi 2017-02-23 02:33:40 +02:00
commit 1bf55961f6
9 changed files with 66 additions and 84 deletions

View File

@ -398,8 +398,8 @@ because it is possible that the values should be
equal but they are not because of precision errors. equal but they are not because of precision errors.
A better way to compare floating point numbers A better way to compare floating point numbers
is to assume that two numbers are equal is to assume that two numbers are equal
if the difference between them is less than if the difference between them is less than $\varepsilon$,
$\varepsilon$, where $\varepsilon$ is a small number. where $\varepsilon$ is a small number.
In practice, the numbers can be compared In practice, the numbers can be compared
as follows ($\varepsilon=10^{-9}$): as follows ($\varepsilon=10^{-9}$):
@ -852,7 +852,7 @@ secondary school students.
Each country is allowed to send a team of Each country is allowed to send a team of
four students to the contest. four students to the contest.
There are usually about 300 participants There are usually about 300 participants
from 80 countries \cite{iois}. from 80 countries.
The IOI consists of two five-hour long contests. The IOI consists of two five-hour long contests.
In both contests, the participants are asked to In both contests, the participants are asked to
@ -862,7 +862,7 @@ each of which has an assigned score.
Even if the contestants are divided into teams, Even if the contestants are divided into teams,
they compete as individuals. they compete as individuals.
The IOI syllabus \cite{ioiy} regulates the topics The IOI syllabus \cite{iois} regulates the topics
that may appear in IOI tasks. that may appear in IOI tasks.
This book covers almost all the topics in the IOI syllabus. This book covers almost all the topics in the IOI syllabus.
@ -879,11 +879,11 @@ such as the Croatian Open Competition in Informatics (COCI)
and the USA Computing Olympiad (USACO). and the USA Computing Olympiad (USACO).
In addition, In addition,
many problems from Polish contests many problems from Polish contests
are available online \cite{main}. are available online\footnote{Młodzieżowa Akademia Informatyczna (MAIN), \texttt{http://main.edu.pl/}}.
\subsubsection{ICPC} \subsubsection{ICPC}
The International Collegiate Programming Contest (ICPC) \cite{icpc} The International Collegiate Programming Contest (ICPC)
is an annual programming contest for university students. is an annual programming contest for university students.
Each team in the contest consists of three students, Each team in the contest consists of three students,
and unlike in the IOI, the students work together; and unlike in the IOI, the students work together;

View File

@ -252,16 +252,16 @@ there are two possible solutions to the problem:
\begin{tikzpicture}[scale=.65] \begin{tikzpicture}[scale=.65]
\begin{scope} \begin{scope}
\draw (0, 0) grid (4, 4); \draw (0, 0) grid (4, 4);
\node at (1.5,3.5) {$K$}; \node at (1.5,3.5) {\symqueen};
\node at (3.5,2.5) {$K$}; \node at (3.5,2.5) {\symqueen};
\node at (0.5,1.5) {$K$}; \node at (0.5,1.5) {\symqueen};
\node at (2.5,0.5) {$K$}; \node at (2.5,0.5) {\symqueen};
\draw (6, 0) grid (10, 4); \draw (6, 0) grid (10, 4);
\node at (6+2.5,3.5) {$K$}; \node at (6+2.5,3.5) {\symqueen};
\node at (6+0.5,2.5) {$K$}; \node at (6+0.5,2.5) {\symqueen};
\node at (6+3.5,1.5) {$K$}; \node at (6+3.5,1.5) {\symqueen};
\node at (6+1.5,0.5) {$K$}; \node at (6+1.5,0.5) {\symqueen};
\end{scope} \end{scope}
\end{tikzpicture} \end{tikzpicture}
@ -289,10 +289,10 @@ the backtracking algorithm are as follows:
\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) {$Q$}; \node at (-9+0.5,-3+0.5) {\symqueen};
\node at (-3+1+0.5,-3+0.5) {$Q$}; \node at (-3+1+0.5,-3+0.5) {\symqueen};
\node at (3+2+0.5,-3+0.5) {$Q$}; \node at (3+2+0.5,-3+0.5) {\symqueen};
\node at (9+3+0.5,-3+0.5) {$Q$}; \node at (9+3+0.5,-3+0.5) {\symqueen};
\draw (2,0) -- (-7,-2); \draw (2,0) -- (-7,-2);
\draw (2,0) -- (-1,-2); \draw (2,0) -- (-1,-2);
@ -304,14 +304,14 @@ the backtracking algorithm are as follows:
\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) {$Q$}; \node at (-11+1+0.5,-9+0.5) {\symqueen};
\node at (-6+1+0.5,-9+0.5) {$Q$}; \node at (-6+1+0.5,-9+0.5) {\symqueen};
\node at (-1+1+0.5,-9+0.5) {$Q$}; \node at (-1+1+0.5,-9+0.5) {\symqueen};
\node at (4+1+0.5,-9+0.5) {$Q$}; \node at (4+1+0.5,-9+0.5) {\symqueen};
\node at (-11+0+0.5,-10+0.5) {$Q$}; \node at (-11+0+0.5,-10+0.5) {\symqueen};
\node at (-6+1+0.5,-10+0.5) {$Q$}; \node at (-6+1+0.5,-10+0.5) {\symqueen};
\node at (-1+2+0.5,-10+0.5) {$Q$}; \node at (-1+2+0.5,-10+0.5) {\symqueen};
\node at (4+3+0.5,-10+0.5) {$Q$}; \node at (4+3+0.5,-10+0.5) {\symqueen};
\draw (-1,-6) -- (-9,-8); \draw (-1,-6) -- (-9,-8);
\draw (-1,-6) -- (-4,-8); \draw (-1,-6) -- (-4,-8);
@ -368,7 +368,7 @@ 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 diagonal that already contains a queen. column or diagonal that already contains a queen.
For example, the rows and the diagonals of For example, the rows and diagonals of
the $4 \times 4$ board are numbered as follows: the $4 \times 4$ board are numbered as follows:
\begin{center} \begin{center}

View File

@ -80,14 +80,14 @@ the answer for any possible query.
\subsubsection{Sum queries} \subsubsection{Sum queries}
\index{sum array} \index{prefix sum array}
It turns out that we can easily process It turns out that we can easily process
sum queries on a static array, sum queries on a static array,
because we can construct a data structure called because we can use a data structure called
a \key{sum array}. a \key{prefix sum array}.
Each element in a sum array corresponds to Each value in such an array equals
the sum of elements in the original array up to that position. the sum of values in the original array up to that position.
For example, consider the following array: For example, consider the following array:
\begin{center} \begin{center}
@ -115,7 +115,7 @@ For example, consider the following array:
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
The corresponding sum array is as follows: The corresponding prefix sum array is as follows:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.7] \begin{tikzpicture}[scale=0.7]
%\fill[color=lightgray] (3,0) rectangle (7,1); %\fill[color=lightgray] (3,0) rectangle (7,1);
@ -144,7 +144,7 @@ The corresponding sum array is as follows:
\end{center} \end{center}
Let $\textrm{sum}(a,b)$ denote the sum of elements Let $\textrm{sum}(a,b)$ denote the sum of elements
in the range $[a,b]$. in the range $[a,b]$.
Since the sum array contains all values Since the prefix sum array contains all values
of the form $\textrm{sum}(1,k)$, of the form $\textrm{sum}(1,k)$,
we can calculate any value of we can calculate any value of
$\textrm{sum}(a,b)$ in $O(1)$ time, because $\textrm{sum}(a,b)$ in $O(1)$ time, because
@ -180,7 +180,7 @@ For example, consider the range $[4,7]$:
\end{center} \end{center}
The sum in the range is $8+6+1+4=19$. The sum in the range is $8+6+1+4=19$.
This sum can be calculated using This sum can be calculated using
two values in the sum array: two values in the prefix sum array:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.7] \begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (2,0) rectangle (3,1); \fill[color=lightgray] (2,0) rectangle (3,1);
@ -213,7 +213,7 @@ Thus, the sum in the range $[4,7]$ is $27-8=19$.
It is also possible to generalize this idea It is also possible to generalize this idea
to higher dimensions. to higher dimensions.
For example, we can construct a two-dimensional For example, we can construct a two-dimensional
sum array that can be used for calculating prefix sum array that can be used for calculating
the sum of any rectangular subarray in $O(1)$ time. the sum of any rectangular subarray in $O(1)$ time.
Each value in such an array is the sum of a subarray Each value in such an array is the sum of a subarray
that begins at the upper-left corner of the array. that begins at the upper-left corner of the array.
@ -441,7 +441,7 @@ we can conclude that $\textrm{rmq}(2,7)=1$.
\index{Fenwick tree} \index{Fenwick tree}
A \key{binary indexed tree} or \key{Fenwick tree} \cite{fen94} A \key{binary indexed tree} or \key{Fenwick tree} \cite{fen94}
can be seen as a dynamic variant of a sum array. can be seen as a dynamic version of a prefix sum array.
This data structure supports two $O(\log n)$ time operations: This data structure supports two $O(\log n)$ time operations:
calculating the sum of elements in a range calculating the sum of elements in a range
and modifying the value of an element. and modifying the value of an element.
@ -449,9 +449,9 @@ and modifying the value of an element.
The advantage of a binary indexed tree is The advantage of a binary indexed tree is
that it allows us to efficiently update that it allows us to efficiently update
the array elements between the sum queries. the array elements between the sum queries.
This would not be possible using a sum array, This would not be possible using a prefix sum array,
because after each update, we should build the because after each update, we should build the
whole sum array again in $O(n)$ time. whole array again in $O(n)$ time.
\subsubsection{Structure} \subsubsection{Structure}
@ -628,7 +628,7 @@ the following values:
Hence, the sum of elements in the range $[1,7]$ is $16+7+4=27$. Hence, the sum of elements in the range $[1,7]$ is $16+7+4=27$.
To calculate the sum of elements in any range $[a,b]$, To calculate the sum of elements in any range $[a,b]$,
we can use the same trick that we used with sum arrays: we can use the same trick that we used with prefix sum arrays:
\[ \textrm{sum}(a,b) = \textrm{sum}(1,b) - \textrm{sum}(1,a-1).\] \[ \textrm{sum}(a,b) = \textrm{sum}(1,b) - \textrm{sum}(1,a-1).\]
Also in this case, only $O(\log n)$ values are needed. Also in this case, only $O(\log n)$ values are needed.
@ -1314,12 +1314,18 @@ retrieve single values.
We focus on an operation that increases all We focus on an operation that increases all
elements in a range $[a,b]$ by $x$. elements in a range $[a,b]$ by $x$.
\index{difference array}
Surprisingly, we can use the data structures Surprisingly, we can use the data structures
presented in this chapter also in this situation. presented in this chapter also in this situation.
To do this, we build an \key{inverse sum array} To do this, we build a \key{difference array}
for the array. for the array.
The idea is that the original array is the sum array of the In such an array, each value indicates
inverse sum array. the difference between two consecutive values
in the original array.
Thus, the original array is the
prefix sum array of the
difference array.
For example, consider the following array: For example, consider the following array:
\begin{center} \begin{center}
@ -1348,7 +1354,7 @@ For example, consider the following array:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
The inverse sum array for the above array is as follows: The difference array for the above array is as follows:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.7] \begin{tikzpicture}[scale=0.7]
\draw (0,0) grid (8,1); \draw (0,0) grid (8,1);
@ -1378,10 +1384,10 @@ The inverse sum array for the above array is as follows:
For example, the value 5 at position 6 in the original array For example, the value 5 at position 6 in the original array
corresponds to the sum $3-2+4=5$. corresponds to the sum $3-2+4=5$.
The advantage of the inverse sum array is The advantage of the difference array is
that we can update a range that we can update a range
in the original array by changing just in the original array by changing just
two elements in the inverse sum array. two elements in the difference array.
For example, if we want to For example, if we want to
increase the elements in the range $2 \ldots 5$ by 5, increase the elements in the range $2 \ldots 5$ by 5,
it suffices to increase the value at position 2 by 5 it suffices to increase the value at position 2 by 5

View File

@ -546,4 +546,4 @@ it is difficult to find out if the nodes
in a graph can be colored using $k$ colors in a graph can be colored using $k$ colors
so that no adjacent nodes have the same color. so that no adjacent nodes have the same color.
Even when $k=3$, no efficient algorithm is known Even when $k=3$, no efficient algorithm is known
but the problem is NP-hard \cite{gar79}. but the problem is NP-hard.

View File

@ -5,7 +5,8 @@ In this chapter, we focus on two classes of directed graphs:
\begin{itemize} \begin{itemize}
\item \key{Acyclic graphs}: \item \key{Acyclic graphs}:
There are no cycles in the graph, There are no cycles in the graph,
so there is no path from any node to itself. so there is no path from any node to itself\footnote{Directed acyclic
graphs are sometimes called DAGs.}.
\item \key{Successor graphs}: \item \key{Successor graphs}:
The outdegree of each node is 1, The outdegree of each node is 1,
so each node has a unique successor. so each node has a unique successor.

View File

@ -559,9 +559,5 @@ and both $x_i$ and $x_j$ become false.
A more difficult problem is the \key{3SAT problem} A more difficult problem is the \key{3SAT problem}
where each part of the formula is of the form where each part of the formula is of the form
$(a_i \lor b_i \lor c_i)$. $(a_i \lor b_i \lor c_i)$.
This problem is NP-hard \cite{gar79}, so no efficient algorithm This problem is NP-hard, so no efficient algorithm
for solving the problem is known. for solving the problem is known.

View File

@ -689,9 +689,9 @@ using a depth-first search:
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
However, we use a bit different variant of
However, we use a bit different technique where the tree traversal array where
we add each node to the tree traversal array \emph{always} we add each node to the array \emph{always}
when the depth-first search visits the node, when the depth-first search visits the node,
and not only at the first visit. and not only at the first visit.
Hence, a node that has $k$ children appears $k+1$ times Hence, a node that has $k$ children appears $k+1$ times

View File

@ -314,9 +314,9 @@ because both cases take a total of $O(n \sqrt n)$ time.
\index{Mo's algorithm} \index{Mo's algorithm}
\key{Mo's algorithm} \footnote{According to \cite{cod15}, this algorithm \key{Mo's algorithm}\footnote{According to \cite{cod15}, this algorithm
is named after Mo Tao, a Chinese competitive programmer. However, is named after Mo Tao, a Chinese competitive programmer, but
the technique has appeared earlier in the literature \cite{ken06}.} can be used in many problems the technique has appeared earlier in the literature.} can be used in many problems
that require processing range queries in that require processing range queries in
a \emph{static} array. a \emph{static} array.
Before processing the queries, the algorithm Before processing the queries, the algorithm

View File

@ -88,7 +88,7 @@
F. Le Gall. F. Le Gall.
Powers of tensors and fast matrix multiplication. Powers of tensors and fast matrix multiplication.
In \emph{Proceedings of the 39th International Symposium on Symbolic and Algebraic Computation}, In \emph{Proceedings of the 39th International Symposium on Symbolic and Algebraic Computation},
296--303. 296--303, 2014.
\bibitem{gar79} \bibitem{gar79}
M. R. Garey and D. S. Johnson. M. R. Garey and D. S. Johnson.
@ -116,20 +116,8 @@
A method for the construction of minimum-redundancy codes. A method for the construction of minimum-redundancy codes.
\emph{Proceedings of the IRE}, 40(9):1098--1101, 1952. \emph{Proceedings of the IRE}, 40(9):1098--1101, 1952.
\bibitem{icpc}
The ACM-ICPC International Collegiate Programming Contest,
\url{https://icpc.baylor.edu/}
\bibitem{ioi}
International Olympiad in Informatics -- Official site,
\url{http://www.ioinformatics.org/}
\bibitem{iois} \bibitem{iois}
International Olympiad in Informatics -- Statistics, The International Olympiad in Informatics Syllabus, available at
\url{http://stats.ioinformatics.org/}
\bibitem{ioiy}
MisoF's IOI Syllabus page,
\url{https://people.ksp.sk/~misof/ioi-syllabus/} \url{https://people.ksp.sk/~misof/ioi-syllabus/}
\bibitem{kar87} \bibitem{kar87}
@ -142,11 +130,6 @@
The statistics of dimers on a lattice: I. The number of dimer arrangements on a quadratic lattice. The statistics of dimers on a lattice: I. The number of dimer arrangements on a quadratic lattice.
\emph{Physica}, 27(12):1209--1225, 1961. \emph{Physica}, 27(12):1209--1225, 1961.
\bibitem{ken06}
C. Kent, G. m. Landau and M. Ziv-Ukelson.
On the complexity of sparse exon assembly.
\emph{Journal of Computational Biology}, 13(5):1013--1027, 2006.
\bibitem{kru56} \bibitem{kru56}
J. B. Kruskal. J. B. Kruskal.
On the shortest spanning subtree of a graph and the traveling salesman problem. On the shortest spanning subtree of a graph and the traveling salesman problem.
@ -157,10 +140,6 @@
An $O(n \log n)$ algorithm for finding all repetitions in a string. An $O(n \log n)$ algorithm for finding all repetitions in a string.
\emph{Journal of Algorithms}, 5(3):422--432, 1984. \emph{Journal of Algorithms}, 5(3):422--432, 1984.
\bibitem{main}
Młodzieżowa Akademia Informatyczna (MAIN),
\url{http://main.edu.pl/en}
\bibitem{pac13} \bibitem{pac13}
J. Pachocki and J. Radoszweski. J. Pachocki and J. Radoszweski.
Where to use and how not to use polynomial string hashing. Where to use and how not to use polynomial string hashing.
@ -186,4 +165,4 @@
Dimer problem in statistical mechanics -- an exact result. Dimer problem in statistical mechanics -- an exact result.
\emph{Philosophical Magazine}, 6(68):1061--1063, 1961. \emph{Philosophical Magazine}, 6(68):1061--1063, 1961.
\end{thebibliography} \end{thebibliography}