Better terms for sum arrays

This commit is contained in:
Antti H S Laaksonen 2017-02-22 21:25:13 +02:00
parent 8003a2595a
commit 18577f9941
1 changed files with 23 additions and 19 deletions

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.
@ -1316,10 +1316,14 @@ elements in a range $[a,b]$ by $x$.
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 +1352,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 +1382,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