From 18577f9941b77f1686f65ea4f99897100bcb793b Mon Sep 17 00:00:00 2001 From: Antti H S Laaksonen Date: Wed, 22 Feb 2017 21:25:13 +0200 Subject: [PATCH] Better terms for sum arrays --- chapter09.tex | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/chapter09.tex b/chapter09.tex index 6b3eacf..fd2948f 100644 --- a/chapter09.tex +++ b/chapter09.tex @@ -80,14 +80,14 @@ the answer for any possible query. \subsubsection{Sum queries} -\index{sum array} +\index{prefix sum array} It turns out that we can easily process sum queries on a static array, -because we can construct a data structure called -a \key{sum array}. -Each element in a sum array corresponds to -the sum of elements in the original array up to that position. +because we can use a data structure called +a \key{prefix sum array}. +Each value in such an array equals +the sum of values in the original array up to that position. For example, consider the following array: \begin{center} @@ -115,7 +115,7 @@ For example, consider the following array: \node at (7.5,1.4) {$8$}; \end{tikzpicture} \end{center} -The corresponding sum array is as follows: +The corresponding prefix sum array is as follows: \begin{center} \begin{tikzpicture}[scale=0.7] %\fill[color=lightgray] (3,0) rectangle (7,1); @@ -144,7 +144,7 @@ The corresponding sum array is as follows: \end{center} Let $\textrm{sum}(a,b)$ denote the sum of elements 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)$, we can calculate any value of $\textrm{sum}(a,b)$ in $O(1)$ time, because @@ -180,7 +180,7 @@ For example, consider the range $[4,7]$: \end{center} The sum in the range is $8+6+1+4=19$. This sum can be calculated using -two values in the sum array: +two values in the prefix sum array: \begin{center} \begin{tikzpicture}[scale=0.7] \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 to higher dimensions. 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. Each value in such an array is the sum of a subarray 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} 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: calculating the sum of elements in a range 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 that it allows us to efficiently update 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 -whole sum array again in $O(n)$ time. +whole array again in $O(n)$ time. \subsubsection{Structure} @@ -628,7 +628,7 @@ the following values: 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]$, -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).\] 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 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. -The idea is that the original array is the sum array of the -inverse sum array. +In such an array, each value indicates +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: \begin{center} @@ -1348,7 +1352,7 @@ For example, consider the following array: \end{tikzpicture} \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{tikzpicture}[scale=0.7] \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 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 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 increase the elements in the range $2 \ldots 5$ by 5, it suffices to increase the value at position 2 by 5