Use 0-indexing with C++ arrays

This commit is contained in:
Antti H S Laaksonen 2017-04-17 17:59:27 +03:00
parent 11564fd4d3
commit 35d53d39d4
7 changed files with 616 additions and 635 deletions

View File

@ -354,12 +354,12 @@ However, by designing a better algorithm, it
is possible to solve the problem in $O(n^2)$ is possible to solve the problem in $O(n^2)$
time and even in $O(n)$ time. time and even in $O(n)$ time.
Given an array of $n$ integers $x_1,x_2,\ldots,x_n$, Given an array of $n$ numbers,
our task is to find the our task is to calculate the
\key{maximum subarray sum}\footnote{J. Bentley's \key{maximum subarray sum}, i.e.,
book \emph{Programming Pearls} \cite{ben86} made the problem popular.}, i.e.,
the largest possible sum of numbers the largest possible sum of numbers
in a contiguous region in the array. in a contiguous region in the array\footnote{J. Bentley's
book \emph{Programming Pearls} \cite{ben86} made the problem popular.}.
The problem is interesting when there may be The problem is interesting when there may be
negative numbers in the array. negative numbers in the array.
For example, in the array For example, in the array
@ -375,16 +375,6 @@ For example, in the array
\node at (5.5,0.5) {$2$}; \node at (5.5,0.5) {$2$};
\node at (6.5,0.5) {$-5$}; \node at (6.5,0.5) {$-5$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
\begin{samepage} \begin{samepage}
@ -402,23 +392,15 @@ the following subarray produces the maximum sum $10$:
\node at (5.5,0.5) {$2$}; \node at (5.5,0.5) {$2$};
\node at (6.5,0.5) {$-5$}; \node at (6.5,0.5) {$-5$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\footnotesize
\node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
\end{samepage} \end{samepage}
\subsubsection{Algorithm 1} \subsubsection{Algorithm 1}
A straightforward algorithm to solve the problem Let us assume that the numbers are stored in
an array \texttt{x}.
A straightforward way to solve the problem
is to go through all possible ways of is to go through all possible ways of
selecting a subarray, calculate the sum of selecting a subarray, calculate the sum of
the numbers in each subarray and maintain the numbers in each subarray and maintain
@ -427,8 +409,8 @@ The following code implements this algorithm:
\begin{lstlisting} \begin{lstlisting}
int p = 0; int p = 0;
for (int a = 1; a <= n; a++) { for (int a = 0; a < n; a++) {
for (int b = a; b <= n; b++) { for (int b = a; b < n; b++) {
int s = 0; int s = 0;
for (int c = a; c <= b; c++) { for (int c = a; c <= b; c++) {
s += x[c]; s += x[c];
@ -439,8 +421,6 @@ for (int a = 1; a <= n; a++) {
cout << p << "\n"; cout << p << "\n";
\end{lstlisting} \end{lstlisting}
The code assumes that the numbers are stored in an array \texttt{x}
with indices $1 \ldots n$.
The variables $a$ and $b$ determine the first and last The variables $a$ and $b$ determine the first and last
number in the subarray, number in the subarray,
and the sum of the numbers is calculated to the variable $s$. and the sum of the numbers is calculated to the variable $s$.
@ -460,9 +440,9 @@ The result is the following code:
\begin{lstlisting} \begin{lstlisting}
int p = 0; int p = 0;
for (int a = 1; a <= n; a++) { for (int a = 0; a < n; a++) {
int s = 0; int s = 0;
for (int b = a; b <= n; b++) { for (int b = a; b < n; b++) {
s += x[b]; s += x[b];
p = max(p,s); p = max(p,s);
} }
@ -502,7 +482,7 @@ for each ending position from left to right.
The following code implements the algorithm: The following code implements the algorithm:
\begin{lstlisting} \begin{lstlisting}
int p = 0, s = 0; int p = 0, s = 0;
for (int k = 1; k <= n; k++) { for (int k = 0; k < n; k++) {
s = max(x[k],s+x[k]); s = max(x[k],s+x[k]);
p = max(p,s); p = max(p,s);
} }

View File

@ -49,15 +49,15 @@ For example, the array
\node at (6.5,0.5) {$5$}; \node at (6.5,0.5) {$5$};
\node at (7.5,0.5) {$6$}; \node at (7.5,0.5) {$6$};
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
will be as follows after sorting: will be as follows after sorting:
@ -73,15 +73,15 @@ will be as follows after sorting:
\node at (6.5,0.5) {$8$}; \node at (6.5,0.5) {$8$};
\node at (7.5,0.5) {$9$}; \node at (7.5,0.5) {$9$};
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -97,18 +97,17 @@ A famous $O(n^2)$ time sorting algorithm
is \key{bubble sort} where the elements is \key{bubble sort} where the elements
''bubble'' in the array according to their values. ''bubble'' in the array according to their values.
Bubble sort consists of $n-1$ rounds. Bubble sort consists of $n$ rounds.
On each round, the algorithm iterates through On each round, the algorithm iterates through
the elements of the array. the elements of the array.
Whenever two consecutive elements are found Whenever two consecutive elements are found
that are not in correct order, that are not in correct order,
the algorithm swaps them. the algorithm swaps them.
The algorithm can be implemented as follows The algorithm can be implemented as follows
for an array for an array \texttt{x}:
$\texttt{t}[1],\texttt{t}[2],\ldots,\texttt{t}[n]$:
\begin{lstlisting} \begin{lstlisting}
for (int i = 1; i <= n-1; i++) { for (int i = 0; i < n; i++) {
for (int j = 1; j <= n-i; j++) { for (int j = 0; j < n-1; j++) {
if (t[j] > t[j+1]) swap(t[j],t[j+1]); if (t[j] > t[j+1]) swap(t[j],t[j+1]);
} }
} }
@ -118,7 +117,7 @@ After the first round of the algorithm,
the largest element will be in the correct position, the largest element will be in the correct position,
and in general, after $k$ rounds, the $k$ largest and in general, after $k$ rounds, the $k$ largest
elements will be in the correct positions. elements will be in the correct positions.
Thus, after $n-1$ rounds, the whole array Thus, after $n$ rounds, the whole array
will be sorted. will be sorted.
For example, in the array For example, in the array
@ -135,16 +134,16 @@ For example, in the array
\node at (5.5,0.5) {$2$}; \node at (5.5,0.5) {$2$};
\node at (6.5,0.5) {$5$}; \node at (6.5,0.5) {$5$};
\node at (7.5,0.5) {$6$}; \node at (7.5,0.5) {$6$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -165,16 +164,16 @@ as follows:
\node at (7.5,0.5) {$6$}; \node at (7.5,0.5) {$6$};
\draw[thick,<->] (3.5,-0.25) .. controls (3.25,-1.00) and (2.75,-1.00) .. (2.5,-0.25); \draw[thick,<->] (3.5,-0.25) .. controls (3.25,-1.00) and (2.75,-1.00) .. (2.5,-0.25);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -191,17 +190,16 @@ as follows:
\node at (7.5,0.5) {$6$}; \node at (7.5,0.5) {$6$};
\draw[thick,<->] (5.5,-0.25) .. controls (5.25,-1.00) and (4.75,-1.00) .. (4.5,-0.25); \draw[thick,<->] (5.5,-0.25) .. controls (5.25,-1.00) and (4.75,-1.00) .. (4.5,-0.25);
%
% \footnotesize
\footnotesize % \node at (0.5,1.4) {$1$};
\node at (0.5,1.4) {$1$}; % \node at (1.5,1.4) {$2$};
\node at (1.5,1.4) {$2$}; % \node at (2.5,1.4) {$3$};
\node at (2.5,1.4) {$3$}; % \node at (3.5,1.4) {$4$};
\node at (3.5,1.4) {$4$}; % \node at (4.5,1.4) {$5$};
\node at (4.5,1.4) {$5$}; % \node at (5.5,1.4) {$6$};
\node at (5.5,1.4) {$6$}; % \node at (6.5,1.4) {$7$};
\node at (6.5,1.4) {$7$}; % \node at (7.5,1.4) {$8$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -218,17 +216,16 @@ as follows:
\node at (7.5,0.5) {$6$}; \node at (7.5,0.5) {$6$};
\draw[thick,<->] (6.5,-0.25) .. controls (6.25,-1.00) and (5.75,-1.00) .. (5.5,-0.25); \draw[thick,<->] (6.5,-0.25) .. controls (6.25,-1.00) and (5.75,-1.00) .. (5.5,-0.25);
%
% \footnotesize
\footnotesize % \node at (0.5,1.4) {$1$};
\node at (0.5,1.4) {$1$}; % \node at (1.5,1.4) {$2$};
\node at (1.5,1.4) {$2$}; % \node at (2.5,1.4) {$3$};
\node at (2.5,1.4) {$3$}; % \node at (3.5,1.4) {$4$};
\node at (3.5,1.4) {$4$}; % \node at (4.5,1.4) {$5$};
\node at (4.5,1.4) {$5$}; % \node at (5.5,1.4) {$6$};
\node at (5.5,1.4) {$6$}; % \node at (6.5,1.4) {$7$};
\node at (6.5,1.4) {$7$}; % \node at (7.5,1.4) {$8$};
\node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -245,16 +242,16 @@ as follows:
\node at (7.5,0.5) {$9$}; \node at (7.5,0.5) {$9$};
\draw[thick,<->] (7.5,-0.25) .. controls (7.25,-1.00) and (6.75,-1.00) .. (6.5,-0.25); \draw[thick,<->] (7.5,-0.25) .. controls (7.25,-1.00) and (6.75,-1.00) .. (6.5,-0.25);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -289,16 +286,16 @@ For example, in the array
\node at (5.5,0.5) {$5$}; \node at (5.5,0.5) {$5$};
\node at (6.5,0.5) {$9$}; \node at (6.5,0.5) {$9$};
\node at (7.5,0.5) {$8$}; \node at (7.5,0.5) {$8$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
the inversions are $(6,3)$, $(6,5)$ and $(9,8)$. the inversions are $(6,3)$, $(6,5)$ and $(9,8)$.
@ -360,16 +357,16 @@ For example, consider sorting the following array:
\node at (5.5,0.5) {$2$}; \node at (5.5,0.5) {$2$};
\node at (6.5,0.5) {$5$}; \node at (6.5,0.5) {$5$};
\node at (7.5,0.5) {$9$}; \node at (7.5,0.5) {$9$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -389,16 +386,16 @@ as follows:
\node at (6.5,0.5) {$2$}; \node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$5$}; \node at (7.5,0.5) {$5$};
\node at (8.5,0.5) {$9$}; \node at (8.5,0.5) {$9$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (5.5,1.4) {$5$}; % \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$6$}; % \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$7$}; % \node at (7.5,1.4) {$7$};
\node at (8.5,1.4) {$8$}; % \node at (8.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -419,16 +416,16 @@ as follows:
\node at (6.5,0.5) {$5$}; \node at (6.5,0.5) {$5$};
\node at (7.5,0.5) {$8$}; \node at (7.5,0.5) {$8$};
\node at (8.5,0.5) {$9$}; \node at (8.5,0.5) {$9$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (5.5,1.4) {$5$}; % \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$6$}; % \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$7$}; % \node at (7.5,1.4) {$7$};
\node at (8.5,1.4) {$8$}; % \node at (8.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -445,16 +442,16 @@ subarrays and creates the final sorted array:
\node at (5.5,0.5) {$6$}; \node at (5.5,0.5) {$6$};
\node at (6.5,0.5) {$8$}; \node at (6.5,0.5) {$8$};
\node at (7.5,0.5) {$9$}; \node at (7.5,0.5) {$9$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -546,6 +543,7 @@ whose indices are elements in the original array.
The algorithm iterates through the original array The algorithm iterates through the original array
and calculates how many times each element and calculates how many times each element
appears in the array. appears in the array.
\newpage
For example, the array For example, the array
\begin{center} \begin{center}
@ -559,16 +557,16 @@ For example, the array
\node at (5.5,0.5) {$3$}; \node at (5.5,0.5) {$3$};
\node at (6.5,0.5) {$5$}; \node at (6.5,0.5) {$5$};
\node at (7.5,0.5) {$9$}; \node at (7.5,0.5) {$9$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
corresponds to the following bookkeeping array: corresponds to the following bookkeeping array:
@ -658,6 +656,7 @@ int n = 7; // array size
int t[] = {4,2,5,3,5,8,3}; int t[] = {4,2,5,3,5,8,3};
sort(t,t+n); sort(t,t+n);
\end{lstlisting} \end{lstlisting}
\newpage
The following code sorts the string \texttt{s}: The following code sorts the string \texttt{s}:
\begin{lstlisting} \begin{lstlisting}
string s = "monkey"; string s = "monkey";
@ -776,7 +775,7 @@ For example, the following code searches for
an element $x$ in the array \texttt{t}: an element $x$ in the array \texttt{t}:
\begin{lstlisting} \begin{lstlisting}
for (int i = 1; i <= n; i++) { for (int i = 0; i < n; i++) {
if (t[i] == x) {} // x found at index i if (t[i] == x) {} // x found at index i
} }
\end{lstlisting} \end{lstlisting}
@ -815,7 +814,7 @@ depending on the value of the middle element.
The above idea can be implemented as follows: The above idea can be implemented as follows:
\begin{lstlisting} \begin{lstlisting}
int a = 1, b = n; int a = 0, b = n-1;
while (a <= b) { while (a <= b) {
int k = (a+b)/2; int k = (a+b)/2;
if (t[k] == x) {} // x found at index k if (t[k] == x) {} // x found at index k
@ -848,9 +847,9 @@ been found or we know that it does not appear in the array.
The following code implements the above idea: The following code implements the above idea:
\begin{lstlisting} \begin{lstlisting}
int k = 1; int k = 0;
for (int b = n/2; b >= 1; b /= 2) { for (int b = n/2; b >= 1; b /= 2) {
while (k+b <= n && t[k+b] <= x) k += b; while (k+b < n && t[k+b] <= x) k += b;
} }
if (t[k] == x) {} // x was found at index k if (t[k] == x) {} // x was found at index k
\end{lstlisting} \end{lstlisting}

View File

@ -22,9 +22,9 @@ dynamic programming, may be needed.
We first consider the problem of generating We first consider the problem of generating
all subsets of a set of $n$ elements. all subsets of a set of $n$ elements.
For example, the subsets of $\{1,2,3\}$ are For example, the subsets of $\{0,1,2\}$ are
$\emptyset$, $\{1\}$, $\{2\}$, $\{3\}$, $\{1,2\}$, $\emptyset$, $\{0\}$, $\{1\}$, $\{2\}$, $\{0,1\}$,
$\{1,3\}$, $\{2,3\}$ and $\{1,2,3\}$. $\{0,2\}$, $\{1,2\}$ and $\{0,1,2\}$.
There are two common methods for this: There are two common methods for this:
we can either implement a recursive search we can either implement a recursive search
or use bit operations of integers. or use bit operations of integers.
@ -35,21 +35,21 @@ An elegant way to go through all subsets
of a set is to use recursion. of a set is to use recursion.
The following function The following function
generates the subsets of the set generates the subsets of the set
$\{1,2,\ldots,n\}$. $\{0,1,\ldots,n-1\}$.
The function maintains a vector \texttt{v} The function maintains a vector \texttt{subset}
that will contain the elements of each subset. that will contain the elements of each subset.
The search begins when the function is called The search begins when the function is called
with parameter 1. with parameter 0.
\begin{lstlisting} \begin{lstlisting}
void gen(int k) { void gen(int k) {
if (k == n+1) { if (k == n) {
// process subset v // process subset
} else { } else {
gen(k+1); gen(k+1);
v.push_back(k); subset.push_back(k);
gen(k+1); gen(k+1);
v.pop_back(); subset.pop_back();
} }
} }
\end{lstlisting} \end{lstlisting}
@ -59,7 +59,7 @@ candidate to be included in the subset.
The function considers two cases that both The function considers two cases that both
generate a recursive call: generate a recursive call:
either $k$ is included or not included in the subset. either $k$ is included or not included in the subset.
Finally, when $k=n+1$, all elements have been processed Finally, when $k=n$, all elements have been processed
and one subset has been generated. and one subset has been generated.
The following tree illustrates how the function is The following tree illustrates how the function is
@ -72,41 +72,41 @@ We can always choose either the left branch
\begin{tikzpicture}[scale=.45] \begin{tikzpicture}[scale=.45]
\begin{scope} \begin{scope}
\small \small
\node at (0,0) {$\texttt{gen}(1)$}; \node at (0,0) {$\texttt{gen}(0)$};
\node at (-8,-4) {$\texttt{gen}(2)$}; \node at (-8,-4) {$\texttt{gen}(1)$};
\node at (8,-4) {$\texttt{gen}(2)$}; \node at (8,-4) {$\texttt{gen}(1)$};
\path[draw,thick,->] (0,0-0.5) -- (-8,-4+0.5); \path[draw,thick,->] (0,0-0.5) -- (-8,-4+0.5);
\path[draw,thick,->] (0,0-0.5) -- (8,-4+0.5); \path[draw,thick,->] (0,0-0.5) -- (8,-4+0.5);
\node at (-12,-8) {$\texttt{gen}(3)$}; \node at (-12,-8) {$\texttt{gen}(2)$};
\node at (-4,-8) {$\texttt{gen}(3)$}; \node at (-4,-8) {$\texttt{gen}(2)$};
\node at (4,-8) {$\texttt{gen}(3)$}; \node at (4,-8) {$\texttt{gen}(2)$};
\node at (12,-8) {$\texttt{gen}(3)$}; \node at (12,-8) {$\texttt{gen}(2)$};
\path[draw,thick,->] (-8,-4-0.5) -- (-12,-8+0.5); \path[draw,thick,->] (-8,-4-0.5) -- (-12,-8+0.5);
\path[draw,thick,->] (-8,-4-0.5) -- (-4,-8+0.5); \path[draw,thick,->] (-8,-4-0.5) -- (-4,-8+0.5);
\path[draw,thick,->] (8,-4-0.5) -- (4,-8+0.5); \path[draw,thick,->] (8,-4-0.5) -- (4,-8+0.5);
\path[draw,thick,->] (8,-4-0.5) -- (12,-8+0.5); \path[draw,thick,->] (8,-4-0.5) -- (12,-8+0.5);
\node at (-14,-12) {$\texttt{gen}(4)$}; \node at (-14,-12) {$\texttt{gen}(3)$};
\node at (-10,-12) {$\texttt{gen}(4)$}; \node at (-10,-12) {$\texttt{gen}(3)$};
\node at (-6,-12) {$\texttt{gen}(4)$}; \node at (-6,-12) {$\texttt{gen}(3)$};
\node at (-2,-12) {$\texttt{gen}(4)$}; \node at (-2,-12) {$\texttt{gen}(3)$};
\node at (2,-12) {$\texttt{gen}(4)$}; \node at (2,-12) {$\texttt{gen}(3)$};
\node at (6,-12) {$\texttt{gen}(4)$}; \node at (6,-12) {$\texttt{gen}(3)$};
\node at (10,-12) {$\texttt{gen}(4)$}; \node at (10,-12) {$\texttt{gen}(3)$};
\node at (14,-12) {$\texttt{gen}(4)$}; \node at (14,-12) {$\texttt{gen}(3)$};
\node at (-14,-13.5) {$\emptyset$}; \node at (-14,-13.5) {$\emptyset$};
\node at (-10,-13.5) {$\{3\}$}; \node at (-10,-13.5) {$\{2\}$};
\node at (-6,-13.5) {$\{2\}$}; \node at (-6,-13.5) {$\{1\}$};
\node at (-2,-13.5) {$\{2,3\}$}; \node at (-2,-13.5) {$\{1,2\}$};
\node at (2,-13.5) {$\{1\}$}; \node at (2,-13.5) {$\{0\}$};
\node at (6,-13.5) {$\{1,3\}$}; \node at (6,-13.5) {$\{0,2\}$};
\node at (10,-13.5) {$\{1,2\}$}; \node at (10,-13.5) {$\{0,1\}$};
\node at (14,-13.5) {$\{1,2,3\}$}; \node at (14,-13.5) {$\{0,1,2\}$};
\path[draw,thick,->] (-12,-8-0.5) -- (-14,-12+0.5); \path[draw,thick,->] (-12,-8-0.5) -- (-14,-12+0.5);
@ -135,14 +135,14 @@ The usual convention is that the $k$th element
is included in the subset exactly when the $k$th last bit is included in the subset exactly when the $k$th last bit
in the 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 $\{0,3,4\}$.
The following code goes 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}
for (int b = 0; b < (1<<n); b++) { for (int b = 0; b < (1<<n); b++) {
// process subset b // process subset
} }
\end{lstlisting} \end{lstlisting}
@ -154,9 +154,9 @@ elements in the subset.
\begin{lstlisting} \begin{lstlisting}
for (int b = 0; b < (1<<n); b++) { for (int b = 0; b < (1<<n); b++) {
vector<int> v; vector<int> subset;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
if (b&(1<<i)) v.push_back(i+1); if (b&(1<<i)) subset.push_back(i);
} }
} }
\end{lstlisting} \end{lstlisting}
@ -167,9 +167,9 @@ for (int b = 0; b < (1<<n); b++) {
Next we will consider the problem of generating Next we will consider the problem of generating
all permutations of a set of $n$ elements. all permutations of a set of $n$ elements.
For example, the permutations of $\{1,2,3\}$ are For example, the permutations of $\{0,1,2\}$ are
$(1,2,3)$, $(1,3,2)$, $(2,1,3)$, $(2,3,1)$, $(0,1,2)$, $(0,2,1)$, $(1,0,2)$, $(1,2,0)$,
$(3,1,2)$ and $(3,2,1)$. $(2,0,1)$ and $(2,1,0)$.
Again, there are two approaches: Again, there are two approaches:
we can either use recursion or go through the we can either use recursion or go through the
permutations iteratively. permutations iteratively.
@ -179,36 +179,34 @@ permutations iteratively.
Like subsets, permutations can be generated Like subsets, permutations can be generated
using recursion. using recursion.
The following function goes The following function goes
through the permutations of the set $\{1,2,\ldots,n\}$. through the permutations of the set $\{0,1,\ldots,n-1\}$.
The function builds a vector \texttt{v} that contains The function builds a vector \texttt{perm} that contains
the elements in the permutation, the elements in the permutation,
and the search begins when the function is and the search begins when the function is
called without parameters. called without parameters.
\begin{lstlisting} \begin{lstlisting}
void gen() { void gen() {
if (v.size() == n) { if (perm.size() == n) {
// process permutation v // process permutation
} else { } else {
for (int i = 1; i <= n; i++) { for (int i = 0; i < n; i++) {
if (p[i]) continue; if (chosen[i]) continue;
p[i] = 1; chosen[i] = true;
v.push_back(i); perm.push_back(i);
gen(); gen();
p[i] = 0; chosen[i] = false;
v.pop_back(); perm.pop_back();
} }
} }
} }
\end{lstlisting} \end{lstlisting}
Each function call adds a new element to Each function call adds a new element to
the vector \texttt{v}. the vector \texttt{perm}.
The array \texttt{p} indicates which The array \texttt{chosen} 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 the size of \texttt{perm} equals the size of the set,
and if $\texttt{p}[k]=1$, element $k$ is included.
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}
@ -224,13 +222,13 @@ 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:
\begin{lstlisting} \begin{lstlisting}
vector<int> v; vector<int> perm;
for (int i = 1; i <= n; i++) { for (int i = 0; i < n; i++) {
v.push_back(i); perm.push_back(i);
} }
do { do {
// process permutation v // process permutation
} while (next_permutation(v.begin(),v.end())); } while (next_permutation(perm.begin(),perm.end()));
\end{lstlisting} \end{lstlisting}
\section{Backtracking} \section{Backtracking}
@ -344,7 +342,7 @@ The following code implements the search:
\begin{lstlisting} \begin{lstlisting}
void search(int y) { void search(int y) {
if (y == n) { if (y == n) {
c++; count++;
return; return;
} }
for (int x = 0; x < n; x++) { for (int x = 0; x < n; x++) {
@ -359,7 +357,7 @@ void search(int y) {
The search begins by calling \texttt{search(0)}. The search begins by calling \texttt{search(0)}.
The size of the board is $n$, The size of the board is $n$,
and the code calculates the number of solutions and the code calculates the number of solutions
to $c$. to \texttt{count}.
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.

View File

@ -122,7 +122,7 @@ Thus, the recursive formula is
where the function $\min$ gives the smallest where the function $\min$ gives the smallest
of its parameters. of its parameters.
In the general case, for the coin set In the general case, for the coin set
$\{c_1,c_2,\ldots,c_k\}$, $\{c_0,c_1,\ldots,c_{k-1}\}$,
the recursive formula is the recursive formula is
\[f(x) = \min(f(x-c_1),f(x-c_2),\ldots,f(x-c_k))+1.\] \[f(x) = \min(f(x-c_1),f(x-c_2),\ldots,f(x-c_k))+1.\]
The base case for the function is The base case for the function is
@ -153,7 +153,7 @@ int f(int x) {
\end{lstlisting} \end{lstlisting}
The code assumes that the available coins are The code assumes that the available coins are
$\texttt{c}[1], \texttt{c}[2], \ldots, \texttt{c}[k]$, stored in an array $\texttt{c}$,
and the constant \texttt{INF} denotes infinity. and the constant \texttt{INF} denotes infinity.
This function works but it is not efficient yet, This function works but it is not efficient yet,
because it goes through a large number because it goes through a large number
@ -324,7 +324,7 @@ a sum $x$ using the coins.
For example, $f(5)=6$ when the coins are $\{1,3,4\}$. For example, $f(5)=6$ when the coins are $\{1,3,4\}$.
The value of $f(x)$ can be calculated recursively The value of $f(x)$ can be calculated recursively
using the formula using the formula
\[ f(x) = f(x-c_1)+f(x-c_2)+\cdots+f(x-c_k),\] \[ f(x) = f(x-c_1)+f(x-c_2)+\cdots+f(x-c_{k}),\]
because to form the sum $x$, we have to first because to form the sum $x$, we have to first
choose some coin $c_i$ and then form the sum $x-c_i$. choose some coin $c_i$ and then form the sum $x-c_i$.
The base cases are $f(0)=1$, because there is exactly The base cases are $f(0)=1$, because there is exactly
@ -393,7 +393,7 @@ possibilities of dynamic programming.
\index{longest increasing subsequence} \index{longest increasing subsequence}
Given an array that contains $n$ Given an array that contains $n$
numbers $x_1,x_2,\ldots,x_n$, numbers,
our task is to find the our task is to find the
\key{longest increasing subsequence} \key{longest increasing subsequence}
of the array. of the array.
@ -416,14 +416,14 @@ For example, in the array
\node at (7.5,0.5) {$3$}; \node at (7.5,0.5) {$3$};
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
the longest increasing subsequence the longest increasing subsequence
@ -449,14 +449,14 @@ contains 4 elements:
\draw[thick,->] (4.6,-0.25) .. controls (5.0,-1.00) and (6.0,-1.00) .. (6.5,-0.25); \draw[thick,->] (4.6,-0.25) .. controls (5.0,-1.00) and (6.0,-1.00) .. (6.5,-0.25);
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -465,19 +465,19 @@ longest increasing subsequence
that ends at position $k$. that ends at position $k$.
Using this function, the answer to the problem Using this function, the answer to the problem
is the largest of the values is the largest of the values
$f(1),f(2),\ldots,f(n)$. $f(0),f(1),\ldots,f(n-1)$.
For example, in the above array For example, in the above array
the values of the function are as follows: the values of the function are as follows:
\[ \[
\begin{array}{lcl} \begin{array}{lcl}
f(0) & = & 1 \\
f(1) & = & 1 \\ f(1) & = & 1 \\
f(2) & = & 1 \\ f(2) & = & 2 \\
f(3) & = & 2 \\ f(3) & = & 1 \\
f(4) & = & 1 \\ f(4) & = & 3 \\
f(5) & = & 3 \\ f(5) & = & 2 \\
f(6) & = & 2 \\ f(6) & = & 4 \\
f(7) & = & 4 \\ f(7) & = & 2 \\
f(8) & = & 2 \\
\end{array} \end{array}
\] \]
@ -486,11 +486,12 @@ there are two possibilities how the subsequence
that ends at position $k$ is constructed: that ends at position $k$ is constructed:
\begin{enumerate} \begin{enumerate}
\item The subsequence \item The subsequence
only contains the element $x_k$. In this case $f(k)=1$. only contains the element at position $k$. In this case $f(k)=1$.
\item The subsequence is constructed \item The subsequence is constructed
by adding the element $x_k$ to by adding the element at position $k$ to
a subsequence that ends at position $i$ a subsequence that ends at position $i$
where $i<k$ and $x_i<x_k$. In this case $f(k)=f(i)+1$. where $i<k$ and the element at position $i$
is smaller than the element at position $k$. In this case $f(k)=f(i)+1$.
\end{enumerate} \end{enumerate}
For example, in the above example $f(7)=4$, For example, in the above example $f(7)=4$,

View File

@ -54,16 +54,16 @@ For example, the array
\node at (5.5,0.5) {$1$}; \node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$}; \node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$}; \node at (7.5,0.5) {$3$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
contains a subarray whose sum is 8: contains a subarray whose sum is 8:
@ -80,16 +80,16 @@ contains a subarray whose sum is 8:
\node at (5.5,0.5) {$1$}; \node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$}; \node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$}; \node at (7.5,0.5) {$3$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -118,16 +118,16 @@ with target sum $x=8$:
\node at (5.5,0.5) {$1$}; \node at (5.5,0.5) {$1$};
\node at (6.5,0.5) {$2$}; \node at (6.5,0.5) {$2$};
\node at (7.5,0.5) {$3$}; \node at (7.5,0.5) {$3$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -152,16 +152,16 @@ the next element 5 would make the sum larger than $x$.
\draw[thick,->] (0.5,-0.7) -- (0.5,-0.1); \draw[thick,->] (0.5,-0.7) -- (0.5,-0.1);
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1); \draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -185,16 +185,16 @@ the sum would become too large.
\draw[thick,->] (1.5,-0.7) -- (1.5,-0.1); \draw[thick,->] (1.5,-0.7) -- (1.5,-0.1);
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1); \draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -220,16 +220,16 @@ where the sum of the elements is $x$.
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1); \draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
\draw[thick,->] (4.5,-0.7) -- (4.5,-0.1); \draw[thick,->] (4.5,-0.7) -- (4.5,-0.1);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -283,16 +283,16 @@ with target sum $x=12$:
\node at (5.5,0.5) {$9$}; \node at (5.5,0.5) {$9$};
\node at (6.5,0.5) {$9$}; \node at (6.5,0.5) {$9$};
\node at (7.5,0.5) {$10$}; \node at (7.5,0.5) {$10$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -318,16 +318,16 @@ that is smaller than $x$.
\draw[thick,->] (0.5,-0.7) -- (0.5,-0.1); \draw[thick,->] (0.5,-0.7) -- (0.5,-0.1);
\draw[thick,->] (7.5,-0.7) -- (7.5,-0.1); \draw[thick,->] (7.5,-0.7) -- (7.5,-0.1);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -352,16 +352,16 @@ and the sum becomes $4+7=11$.
\draw[thick,->] (1.5,-0.7) -- (1.5,-0.1); \draw[thick,->] (1.5,-0.7) -- (1.5,-0.1);
\draw[thick,->] (4.5,-0.7) -- (4.5,-0.1); \draw[thick,->] (4.5,-0.7) -- (4.5,-0.1);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -386,16 +386,16 @@ $5+7=12$ has been found.
\draw[thick,->] (2.5,-0.7) -- (2.5,-0.1); \draw[thick,->] (2.5,-0.7) -- (2.5,-0.1);
\draw[thick,->] (4.5,-0.7) -- (4.5,-0.1); \draw[thick,->] (4.5,-0.7) -- (4.5,-0.1);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -477,16 +477,16 @@ As an example, consider the following array:
\node at (5.5,0.5) {$3$}; \node at (5.5,0.5) {$3$};
\node at (6.5,0.5) {$4$}; \node at (6.5,0.5) {$4$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -510,16 +510,16 @@ and the nearest smaller element of 3 is 1.
\draw[thick,->] (2.5,-0.25) .. controls (2.25,-1.00) and (1.75,-1.00) .. (1.6,-0.25); \draw[thick,->] (2.5,-0.25) .. controls (2.25,-1.00) and (1.75,-1.00) .. (1.6,-0.25);
\draw[thick,->] (1.4,-0.25) .. controls (1.25,-1.00) and (0.75,-1.00) .. (0.5,-0.25); \draw[thick,->] (1.4,-0.25) .. controls (1.25,-1.00) and (0.75,-1.00) .. (0.5,-0.25);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -543,16 +543,16 @@ Its nearest smaller element is 1:
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\draw[thick,->] (3.5,-0.25) .. controls (3.00,-1.00) and (1.00,-1.00) .. (0.5,-0.25); \draw[thick,->] (3.5,-0.25) .. controls (3.00,-1.00) and (1.00,-1.00) .. (0.5,-0.25);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -575,16 +575,16 @@ its nearest smaller element is 2:
\draw[thick,->] (3.4,-0.25) .. controls (3.00,-1.00) and (1.00,-1.00) .. (0.5,-0.25); \draw[thick,->] (3.4,-0.25) .. controls (3.00,-1.00) and (1.00,-1.00) .. (0.5,-0.25);
\draw[thick,->] (4.5,-0.25) .. controls (4.25,-1.00) and (3.75,-1.00) .. (3.6,-0.25); \draw[thick,->] (4.5,-0.25) .. controls (4.25,-1.00) and (3.75,-1.00) .. (3.6,-0.25);
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -651,16 +651,16 @@ As an example, consider the following array:
\node at (5.5,0.5) {$4$}; \node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$}; \node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -679,16 +679,16 @@ At the first window position, the smallest element is 1:
\node at (5.5,0.5) {$4$}; \node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$}; \node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\draw[thick,->] (3.5,-0.25) .. controls (3.25,-1.00) and (2.75,-1.00) .. (2.6,-0.25); \draw[thick,->] (3.5,-0.25) .. controls (3.25,-1.00) and (2.75,-1.00) .. (2.6,-0.25);
\draw[thick,->] (2.4,-0.25) .. controls (2.25,-1.00) and (1.75,-1.00) .. (1.5,-0.25); \draw[thick,->] (2.4,-0.25) .. controls (2.25,-1.00) and (1.75,-1.00) .. (1.5,-0.25);
@ -715,16 +715,16 @@ The smallest element is still 1.
\node at (5.5,0.5) {$4$}; \node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$}; \node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\draw[thick,->] (4.5,-0.25) .. controls (4.25,-1.00) and (1.75,-1.00) .. (1.5,-0.25); \draw[thick,->] (4.5,-0.25) .. controls (4.25,-1.00) and (1.75,-1.00) .. (1.5,-0.25);
@ -750,16 +750,16 @@ is added to the chain.
\node at (5.5,0.5) {$4$}; \node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$}; \node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\draw[thick,->] (5.5,-0.25) .. controls (5.25,-1.00) and (4.75,-1.00) .. (4.5,-0.25); \draw[thick,->] (5.5,-0.25) .. controls (5.25,-1.00) and (4.75,-1.00) .. (4.5,-0.25);
\end{tikzpicture} \end{tikzpicture}
@ -782,16 +782,16 @@ and it will only contain the element 1:
\node at (5.5,0.5) {$4$}; \node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$}; \node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\fill[color=black] (6.5,-0.25) circle (0.1); \fill[color=black] (6.5,-0.25) circle (0.1);
@ -816,16 +816,16 @@ is still 1.
\node at (5.5,0.5) {$4$}; \node at (5.5,0.5) {$4$};
\node at (6.5,0.5) {$1$}; \node at (6.5,0.5) {$1$};
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\draw[thick,->] (7.5,-0.25) .. controls (7.25,-1.00) and (6.75,-1.00) .. (6.5,-0.25); \draw[thick,->] (7.5,-0.25) .. controls (7.25,-1.00) and (6.75,-1.00) .. (6.5,-0.25);
\end{tikzpicture} \end{tikzpicture}

View File

@ -16,7 +16,7 @@ Typical range queries are:
\item \key{minimum query}: find the smallest element \item \key{minimum query}: find the smallest element
\item \key{maximum query}: find the largest element \item \key{maximum query}: find the largest element
\end{itemize} \end{itemize}
For example, consider the range $[4,7]$ in the following array: For example, consider the range $[3,6]$ in the following array:
\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);
@ -32,14 +32,14 @@ For example, consider the range $[4,7]$ in the following array:
\node at (7.5,0.5) {$4$}; \node at (7.5,0.5) {$4$};
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
In this range, the sum of elements is $4+6+1+3=16$, In this range, the sum of elements is $4+6+1+3=16$,
@ -105,14 +105,14 @@ For example, consider the following array:
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
The corresponding prefix sum array is as follows: The corresponding prefix sum array is as follows:
@ -132,27 +132,27 @@ The corresponding prefix sum array is as follows:
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\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 prefix 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}(0,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
\[ \textrm{sum}(a,b) = \textrm{sum}(1,b) - \textrm{sum}(1,a-1).\] \[ \textrm{sum}(a,b) = \textrm{sum}(0,b) - \textrm{sum}(0,a-1).\]
By defining $\textrm{sum}(1,0)=0$, By defining $\textrm{sum}(0,-1)=0$,
the above formula also holds when $a=1$. the above formula also holds when $a=1$.
For example, consider the range $[4,7]$: For example, consider the range $[3,6]$:
\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);
@ -168,14 +168,14 @@ For example, consider the range $[4,7]$:
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\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$.
@ -198,17 +198,17 @@ two values in the prefix sum array:
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
Thus, the sum in the range $[4,7]$ is $27-8=19$. Thus, the sum in the range $[3,6]$ 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.
@ -274,14 +274,14 @@ For example, for the array
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
the following values will be calculated: the following values will be calculated:
@ -292,14 +292,14 @@ the following values will be calculated:
\begin{tabular}{ccc} \begin{tabular}{ccc}
$a$ & $b$ & $\textrm{rmq}(a,b)$ \\ $a$ & $b$ & $\textrm{rmq}(a,b)$ \\
\hline \hline
1 & 1 & 1 \\ 0 & 0 & 1 \\
2 & 2 & 3 \\ 1 & 1 & 3 \\
3 & 3 & 4 \\ 2 & 2 & 4 \\
4 & 4 & 8 \\ 3 & 3 & 8 \\
5 & 5 & 6 \\ 4 & 4 & 6 \\
6 & 6 & 1 \\ 5 & 5 & 1 \\
7 & 7 & 4 \\ 6 & 6 & 4 \\
8 & 8 & 2 \\ 7 & 7 & 2 \\
\end{tabular} \end{tabular}
& &
@ -307,13 +307,13 @@ $a$ & $b$ & $\textrm{rmq}(a,b)$ \\
\begin{tabular}{ccc} \begin{tabular}{ccc}
$a$ & $b$ & $\textrm{rmq}(a,b)$ \\ $a$ & $b$ & $\textrm{rmq}(a,b)$ \\
\hline \hline
1 & 2 & 1 \\ 0 & 1 & 1 \\
2 & 3 & 3 \\ 1 & 2 & 3 \\
3 & 4 & 4 \\ 2 & 3 & 4 \\
4 & 5 & 6 \\ 3 & 4 & 6 \\
4 & 5 & 1 \\
5 & 6 & 1 \\ 5 & 6 & 1 \\
6 & 7 & 1 \\ 6 & 7 & 2 \\
7 & 8 & 2 \\
\\ \\
\end{tabular} \end{tabular}
@ -322,12 +322,12 @@ $a$ & $b$ & $\textrm{rmq}(a,b)$ \\
\begin{tabular}{ccc} \begin{tabular}{ccc}
$a$ & $b$ & $\textrm{rmq}(a,b)$ \\ $a$ & $b$ & $\textrm{rmq}(a,b)$ \\
\hline \hline
1 & 4 & 1 \\ 0 & 3 & 1 \\
2 & 5 & 3 \\ 1 & 4 & 3 \\
2 & 5 & 1 \\
3 & 6 & 1 \\ 3 & 6 & 1 \\
4 & 7 & 1 \\ 4 & 7 & 1 \\
5 & 8 & 1 \\ 0 & 7 & 1 \\
1 & 8 & 1 \\
\\ \\
\\ \\
\end{tabular} \end{tabular}
@ -352,7 +352,7 @@ We can calculate the value of $\textrm{rmq}(a,b)$ using the formula
In the above formula, the range $[a,b]$ is represented In the above formula, the range $[a,b]$ is represented
as the union of the ranges $[a,a+k-1]$ and $[b-k+1,b]$, both of length $k$. as the union of the ranges $[a,a+k-1]$ and $[b-k+1,b]$, both of length $k$.
As an example, consider the range $[2,7]$: As an example, consider the range $[1,6]$:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.7] \begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (1,0) rectangle (7,1); \fill[color=lightgray] (1,0) rectangle (7,1);
@ -368,21 +368,21 @@ As an example, consider the range $[2,7]$:
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
The length of the range is 6, The length of the range is 6,
and the largest power of two that does and the largest power of two that does
not exceed 6 is 4. not exceed 6 is 4.
Thus the range $[2,7]$ is Thus the range $[1,6]$ is
the union of the ranges $[2,5]$ and $[4,7]$: the union of the ranges $[1,4]$ and $[3,6]$:
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.7] \begin{tikzpicture}[scale=0.7]
\fill[color=lightgray] (1,0) rectangle (5,1); \fill[color=lightgray] (1,0) rectangle (5,1);
@ -398,14 +398,14 @@ the union of the ranges $[2,5]$ and $[4,7]$:
\node at (7.5,0.5) {$2$}; \node at (7.5,0.5) {$2$};
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
\begin{center} \begin{center}
@ -424,18 +424,18 @@ the union of the ranges $[2,5]$ and $[4,7]$:
\footnotesize \footnotesize
\node at (0.5,1.4) {$1$}; \node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$2$}; \node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$3$}; \node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$4$}; \node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$5$}; \node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$6$}; \node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$7$}; \node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$8$}; \node at (7.5,1.4) {$7$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
Since $\textrm{rmq}(2,5)=3$ and $\textrm{rmq}(4,7)=1$, Since $\textrm{rmq}(1,4)=2$ and $\textrm{rmq}(3,6)=0$,
we can conclude that $\textrm{rmq}(2,7)=1$. we can conclude that $\textrm{rmq}(1,6)=1$.
\section{Binary indexed trees} \section{Binary indexed trees}
@ -458,9 +458,12 @@ whole array again in $O(n)$ time.
\subsubsection{Structure} \subsubsection{Structure}
In this section we assume that one-based indexing
is used in all arrays.
A binary indexed tree can be represented as an array A binary indexed tree can be represented as an array
where the value at position $x$ where the value at position $x$
equals the sum of elements in the range $[x-k+1,x]$, equals the sum of elements in the range $[x-k+1,x]$
of the original array,
where $k$ is the largest power of two that divides $x$. where $k$ is the largest power of two that divides $x$.
For example, if $x=6$, then $k=2$, because 2 divides 6 For example, if $x=6$, then $k=2$, because 2 divides 6
but 4 does not divide 6. but 4 does not divide 6.

View File

@ -168,17 +168,17 @@ Hence, the corresponding tree traversal array is as follows:
\node at (6.5,0.5) {$8$}; \node at (6.5,0.5) {$8$};
\node at (7.5,0.5) {$9$}; \node at (7.5,0.5) {$9$};
\node at (8.5,0.5) {$5$}; \node at (8.5,0.5) {$5$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\node at (8.5,1.4) {$9$}; % \node at (8.5,1.4) {$9$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -203,17 +203,17 @@ nodes in the subtree of node $4$:
\node at (6.5,0.5) {$8$}; \node at (6.5,0.5) {$8$};
\node at (7.5,0.5) {$9$}; \node at (7.5,0.5) {$9$};
\node at (8.5,0.5) {$5$}; \node at (8.5,0.5) {$5$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\node at (8.5,1.4) {$9$}; % \node at (8.5,1.4) {$9$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
Using this fact, we can efficiently process queries Using this fact, we can efficiently process queries
@ -306,17 +306,17 @@ For example, the array for the above tree is as follows:
\node at (6.5,-1.5) {$3$}; \node at (6.5,-1.5) {$3$};
\node at (7.5,-1.5) {$1$}; \node at (7.5,-1.5) {$1$};
\node at (8.5,-1.5) {$1$}; \node at (8.5,-1.5) {$1$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\node at (8.5,1.4) {$9$}; % \node at (8.5,1.4) {$9$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -366,17 +366,17 @@ can be found as follows:
\node at (6.5,-1.5) {$3$}; \node at (6.5,-1.5) {$3$};
\node at (7.5,-1.5) {$1$}; \node at (7.5,-1.5) {$1$};
\node at (8.5,-1.5) {$1$}; \node at (8.5,-1.5) {$1$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\node at (8.5,1.4) {$9$}; % \node at (8.5,1.4) {$9$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -478,17 +478,17 @@ For example, the following array corresponds to the above tree:
\node at (6.5,-1.5) {$12$}; \node at (6.5,-1.5) {$12$};
\node at (7.5,-1.5) {$10$}; \node at (7.5,-1.5) {$10$};
\node at (8.5,-1.5) {$6$}; \node at (8.5,-1.5) {$6$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\node at (8.5,1.4) {$9$}; % \node at (8.5,1.4) {$9$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}
@ -535,17 +535,17 @@ the array changes as follows:
\node at (6.5,-1.5) {$13$}; \node at (6.5,-1.5) {$13$};
\node at (7.5,-1.5) {$11$}; \node at (7.5,-1.5) {$11$};
\node at (8.5,-1.5) {$6$}; \node at (8.5,-1.5) {$6$};
%
\footnotesize % \footnotesize
\node at (0.5,1.4) {$1$}; % \node at (0.5,1.4) {$1$};
\node at (1.5,1.4) {$2$}; % \node at (1.5,1.4) {$2$};
\node at (2.5,1.4) {$3$}; % \node at (2.5,1.4) {$3$};
\node at (3.5,1.4) {$4$}; % \node at (3.5,1.4) {$4$};
\node at (4.5,1.4) {$5$}; % \node at (4.5,1.4) {$5$};
\node at (5.5,1.4) {$6$}; % \node at (5.5,1.4) {$6$};
\node at (6.5,1.4) {$7$}; % \node at (6.5,1.4) {$7$};
\node at (7.5,1.4) {$8$}; % \node at (7.5,1.4) {$8$};
\node at (8.5,1.4) {$9$}; % \node at (8.5,1.4) {$9$};
\end{tikzpicture} \end{tikzpicture}
\end{center} \end{center}