Indexing Range Maximum-Sum Segment Queries with Offsets
Abstract
Given an array of real numbers, the maximum segment sum (MSS) problem is to find a contiguous subarray that has the largest sum. While the MSS problem can be solved optimally with Kadane’s algorithm in time, the study of its indexing version spawned new extensions such as (a) retrieving the MSS after subtracting a query offset parameter for all array entries or (b) retrieving the MSS for arbitrary query ranges. We here study the combination of both problems (a) and (b), which requires retrieving the MSS for arbitrary query ranges after subtracting a query offset parameter for all array entries. For that, we present an index whose query time is only slower than the best known for (a) by a factor of . In detail, our index uses space, supports queries in time, and can be constructed in time. As side results, we study our combined problem in the context of run-length compressed input, and also deduce a solution for (a) that works in run-length compressed space and time. Finally we give supportive lower bounds for our query problem, showing that there is only a polylogarithmic gap of improvement left.
Keywords and phrases:
maximum segment sum, data structure, range queryFunding:
Seungbum Jo: Seungbum Jo was supported by the National Research Foundation of Korea (NRF) grant funded by the Korea government (MSIT) (RS-202523963814).Copyright and License:
2012 ACM Subject Classification:
Theory of computation Data structures design and analysisEditor:
Pierre FraigniaudSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
Given an array of real numbers, the maximum-sum segment (MSS) problem is to find an interval within that maximizes the sum of its elements, which we call an MSS of the sequence. This problem found applications, among others, in image processing [11], pattern recognition [19], and biological sequence analysis [26].
The textbook algorithm for solving the MSS problem is Kadane’s algorithm, cf. [10, 4.4 Maximum Subarray Sum]. Since the various applications often require adaptations to specific problem variations, it is not surprising that the MSS problem has been studied in various settings, cf. [6] for an encyclopedic survey of MSS and its variations. Chen and Chao [7] considered an indexing problem, where it is allowed to preprocess and then answer queries on intervals of . For that task, they devised a linear-time constructible data structure that supports constant-time queries of an MSS for any query interval. They also showed that this problem is linearly equivalent to the range-minimum query (RMQ) problem, meaning that a data structure with construction time and query time solving the MSS problem can be transformed into a data structure solving RMQs with the time complexities and for some constant , respectively. Gawrychowski and Nicholson [12] gave a -bits data structure that also achieves constant query times in the setting that the answer is only the interval and not the maximum sum itself. They also show that bits are necessary for supporting this query type.
A maximal local MSS is a local MSS that is not a segment of any local MSS other than it, where a local MSS is a segment that has itself as its only MSS. Ruzzo and Tompa [21] gave an algorithm that finds all distinct maximal local MSSs in time, and Sakai [22] designed a linear-time constructible data structure supporting constant-time queries of the maximal local MSS of any given segment that contains any given position.
Bengtsson and Chen [3] showed that an arbitrarily given number of non-overlapping segments that maximize the sum of all their elements can be found in linear time. Yu et al. [28] considered the MSS problem where each element of the input sequence is uncertain within a specific interval and proposed a linear-time algorithm for this problem. The density of a segment is defined as the mean of all elements in the segment. Cheng et al. [8] considered the MSS problem with the condition that the density of the segment to be found is between given lower and upper bounds. Bae [1] and Liu and Chao [17] considering returning non-overlapping segments that maximize the sum of all their elements.
Another variant is the maximum-density segment query [16, 13, 15] that asks to find a segment of length between a given lower and upper bound that maximizes the density. Chung and Lu [9] showed that this query is solvable in linear time.
Another natural extension of the one-dimensional maximum segment sum problem is the maximum subarray problem, where the input is a two-dimensional array with length in each dimension, and the goal is to find a contiguous rectangular subarray that has the largest sum. This problem has been addressed by Bentley [4], who gave an -time algorithm for the problem. Subsequently, the time complexity has been improved to by Takaoka [24] and further to by Tamaki and Tokuyama [25]. They showed that the problem can be reducible to -matrix multiplication. Using this result, the current fastest algorithm runs in time by Chan and Williams [5], which also applies to higher-dimensional arrays. In addition, Fukuda et al. [11] introduced the maximum convex sum problem, which is a variant of the maximum subarray problem where the retrieved subarray must be convex (i.e., the intersection of the subarray with any row or column is connected). Here, Bae and Takaoka [2] gave an -time parallel algorithm solving the maximum convex sum problem for processors.
Our Contribution.
Among the aforementioned variations, we focus on two indexing versions of the MSS problem, where we are allowed to preprocess the input array and then answer queries (a) on intervals of the input array, or (b) after subtracting a query offset parameter from all array entries. In particular, we are interested in the combinations of both problems (a) and (b), where we want to answer queries on intervals of the input array after subtracting a query offset parameter from all array entries. We present an indexing data structure solving this combined problem in Theorem 8. The main tool is to answer this problem for the special case that the output interval has to be a prefix or a suffix of the input array, and to reuse known results for the problem (b) to solve the general case on segments. The results are summarized in Table 1. Subsequently, we focus on the special case of run-length compressed input, where we present a data structure in Corollary 15 for the combined problem that uses space linear in the size of the compressed input and answers queries in time polylogarithmic in the size of the compressed input. We can improve the query time for the plain array or in the run-length compressed setting if we allow only integer offsets (Corollaries 11 and 16). Finally, we conclude with lower bounds for our combined problem, which show that our data structures are optimal up to polylogarithmic factors (Corollary 19).
2 Preliminaries and Query Definitions
Our computation model is the RAM model. Let denote the set of all real numbers. We write for two integers , and use the same notation for intervals of real numbers in case that . We assume that all real numbers can be stored in a single word and that basic arithmetic operations on these numbers can be performed in constant time. (We also assume that there are no errors in computational precision due to rounding or overflow.)
Let be the input array of real numbers. is subject to the following queries, for which we present examples in Figure 1. While the queries are initially stated to return the value of a specific subarray of , it is beneficial to also return the interval of the subarray that gives the answer, which we call the answer range. Since we here mostly focus on data structures using space, it suffices to focus on finding answer ranges – we will shortly later see that the actual value can be obtained from an answer range with an -space array of prefix sums. We state our base problem as follows.
Maximum-Segment-Sum (MSS)
Input: array of real numbers .
Output: value and answer range achieving .
We also study the special case that the answer range of the MSS problem is constrained to be a suffix of .
Suffix Maximum-Segment-Sum (SMSS)
Input: array of real numbers
Output: value and integer achieving .
Sakai [23] introduced the following indexing problem that generalizes the MSS problem by allowing a query offset parameter to be subtracted from all array entries before answering the query.
Offset Maximum-Segment-Sum (OMSS)
Construction Input: array of real numbers
Query Input: real-value offset .
Query Output: value and answer range achieving .
Defining the array with , the OMSS query on with offset is the MSS query on . Hence, OMSS is a generalization of MSS, since the answer to an MSS query on is the same as the answer to an OMSS query on with offset . Sakai [23] presented an approach answering an OMSS query as follows.
Lemma 1 ([23]).
Given an array of real numbers, we can compute a list of at most (offset, interval)-pairs in time using space such that the solution to an OMSS query for a given offset is the interval of the predecessor offset of in . By sorting descendingly by the offsets, a binary search for the predecessor of in can return an answer range of an OMSS query in time.
The predecessor of in is the largest offset in that is smaller than or equal to . Three observations are in order regarding Lemma 1.
First, while the list of Lemma 1 does not explicitly return the MSS for a query offset , we can compute the sum for an entry in in constant time if we have the array of prefix lengths of , where for each . To see that, define the prefix sum of for each as , and the sum of the subarray as for any and with . Then .
Second, the upper bound is tight since can reach the size for the input , where the OMSS query for gives the range for every and .
Finally, there can be multiple answer ranges for a query, in particular if the interval covers trailing zeros (after applying the offset). While implicit, the intervals of Lemma 1 do not contain any zero-sum suffix nor prefix.
Now we present our main problem of interest, which combines the OMSS and MSS queries on subarrays of .
Range Offset Maximum-Segment-Sum (R-OMSS)
Construction Input: array of real numbers
Query Input: real-value offset and a range .
Query Output: value and answer range achieving .
Using the notation of , the answer to an R-OMSS query on with query range and offset can be computed as . The R-OMSS is a generalization of the OMSS problem (with the query range ) and the range MSS problem introduced by Chen et al. [7], which asks for the MSS of a query range without the offset parameter (i.e., ). Like for MSS and its restriction SMSS, we can also define the suffix and prefix versions of R-OMSS queries, where the answer range is constrained to be a suffix or a prefix of the query range, respectively.
Suffix Range Offset Maximum-Segment-Sum (Suffix R-OMSS)
Construction Input: array of real numbers
Query Input: real-value offset and a range .
Query Output: value and integer achieving .
A symmetric definition can be given for prefix R-OMSS queries, where the answer range is constrained to be a prefix of the query range.
3 Data structure for R-OMSS Queries
Our main goal of this paper is to devise data structures for answering R-OMSS queries on , where a non-negative offset and a query range are given at query time. There are two naive approaches prioritizing either (1) space or (2) time.
-
(1)
We can apply Kadane’s algorithm on . Since any value in can be computed in time from , Kadane’s algorithm answers R-OMSS in time using space additional to .
-
(2)
Alternatively, we can maintain the data structure of Lemma 1 for all possible query ranges. This results in an -space data structure with query time.
In what follows, we present two non-trivial data structures for R-OMSS queries that finds its complexities between the solutions (1) and (2). We start with an -space data structure answering suffix R-OMSS queries on in time. By symmetry, the same data structure results can be obtained for prefix R-OMSS queries. Next, in Section 3.2, we consider a data structure for (general) R-OMSS queries. By combining the data structure of Lemma 1 with the suffix R-OMSS data structures, we obtain an -space data structure that can answer an R-OMSS query in time.
3.1 Data Structure for Suffix R-OMSS Queries
We start with devising a data structure for range suffix OMSS queries. The main idea is to maintain the canonical ranges (defined below) of using a suffix binary search tree (SBST). Each node in the tree stores a threshold offset. By comparing with the offsets stored along a root-to-leaf path in the tree, as in a standard binary search tree, we can track the endpoint of the range corresponding to the query answer when the query range is canonical. Finally, we partition the query range into canonical ranges and answer the query using a dynamic programming approach.
The canonical ranges of are the elements of a recursive binary partition of into ranges, which can be represented by a binary tree. The canonical ranges of are defined as follows:
-
(1)
is a canonical range, and
-
(2)
for every canonical range with , both and are also canonical ranges.
Under this definition, there are canonical ranges in total. We then define the SBST on as follows:
-
The root of the SBST on is the canonical range .
-
If , then the SBSTs on and are the left and right child subtrees of the root, respectively. Otherwise, g=h; a leaf is added with canonical range [g, g].
See Figure 2 for an example. (The reader familiar with segment trees may notice that the SBST is a specialization of a segment tree indexing all integers in .)
Let denote the SBST on . From the definition, each canonical range of is a node in , and vice versa. The leaves of are all canonical ranges of size , i.e., for each . By construction, is a binary tree with nodes and height . Finally, we augment each node of with a threshold offset . Before defining the threshold offsets, we introduce the following key property for answering SMSS queries.
Proposition 2.
For any positive and with , the answer range of the SMSS query on is a subrange of the answer range of the SMSS query on .
Proof.
Let and be the answer ranges of the SMSS query on and , respectively. Suppose that . Since , it follows that the sum , which implies . This leads to a contradiction since .
Let be the range corresponding to the node , and let be the answer range of the SMSS problem on for some variable . We then define the threshold offset at node as the maximum value of such that , that is, lies in the range corresponding to the left child of . If no such exists, or if is a leaf node, we define . By Proposition 2, the value is well defined as a single constant.
The data structure for suffix R-OMSS queries is composed of (1) the array , and (2) the SBST on . Each node of the SBST stores its threshold offset and its canonical range. (1) and (2) together use space in total.
We now describe how to answer a query using this data structure. We first consider the case where the query range is a canonical range corresponding to a node in . In this case, it suffices to find the position such that is the answer range. To find , we start from the node and compare with . As in a standard binary search tree, we recursively perform the same comparison: we proceed to the left child if , and to the right child otherwise, until we reach a leaf node corresponding to the canonical range . Then from the following proposition.
Proposition 3.
Let be an internal node in with the corresponding range , and let . If (resp. ), then the leftmost endpoints of the answer ranges of the SMSS queries on and (resp. on and ) are the same.
Proof.
Here we consider only the case that ; the case can be proved analogously. Let and be the leftmost endpoints of the answer ranges of the SMSS queries on and . We now claim that . To prove the claim, suppose that (the case can be handled analogously). Since , it follows that . Consequently, , which contradicts the definition of .
Since the height of is , we can answer a suffix R-OMSS query on in time when the query range is canonical.
Finally, we consider how to answer a suffix R-OMSS query on for a general query range using the data structure. We first partition into distinct canonical ranges , ordered from left to right (i.e., , , and for all ). This partition can be computed by starting from the root of and traversing the tree downwards in two directions for finding the leaves of the canonical ranges and , respectively. We then collect canonical ranges corresponding to children on the two root-to-leaf paths. At each level of , there are at most two nodes whose canonical ranges satisfy but ; only these nodes require recursion to the next level, while any other intersecting node is fully contained in and can be reported as a canonical range immediately. Therefore, we can compute these canonical ranges in time.
If (i.e., is a canonical range), we can answer the R-OMSS query in time using . Otherwise, we apply a dynamic programming approach as follows. Define an array of size , where for each , stores the answer range of the SMSS query on . Then stores the answer of the suffix R-OMSS query.
To compute , we first handle the base case by computing in time using . Next, for , we compute from as follows. To compute , it suffices to determine the answer range of the SMSS query on . Now we introduce the following lemma, which is used to compute .
Lemma 4.
Let and be the answer ranges of the SMSS queries on and , respectively. Then is either (1) or (2) , where is the answer range of the SMSS query on .
Proof.
If or , then , which contradicts the fact that is the answer range of .
Similarly, if or , then , which contradicts the fact that is the answer range of .
The answer ranges of the SMSS queries on and can be obtained from and from , respectively, in time. Thus, by Lemma 4, given , the value can be computed in time, which implies that can be computed in time.
Construction time.
As can be constructed in time, it suffices to analyze the construction time of . To construct , for each node whose corresponding range is with , we compute its threshold offset as follows. Consider the point set , that is, each point corresponds to a suffix of , where the -coordinate is the suffix length and the -coordinate is its sum. Among these points, we keep only those that can be optimal for some . Equivalently, we keep the vertices of the upper convex hull of (since we maximize ). The convex hull can be constructed in time, since the points in are already sorted by their first coordinates [14].
For any suffix represented by a point , we have . Thus, given an offset , answering the SMSS query on reduces to finding the point that maximizes . By point–line duality, each point corresponds to the line , and equals the value of the upper envelope at , which can be computed in time from the convex hull of . The upper envelope is piecewise linear and changes only at intersection points of consecutive envelope lines. Therefore, is the breakpoint on the envelope where the optimal suffix switches from a suffix whose starting position lies in the left half of to one whose starting position lies in the right half. Since the envelope breakpoints are sorted by slope, can be found by binary search over the hull in time (using Proposition 2). See Figure 3 for an example.
Thus, the nodes at each level of can be constructed in time, implying that the entire tree can be constructed in time overall.
We summarize the results of this section in the following theorem.
Theorem 5.
Given an array of size , there exists an -space data structure that can answer a prefix/suffix R-OMSS query on in time for any positive offset . When the query range is a canonical range of , the data structure can answer a prefix/suffix R-OMSS query in time. The data structure can be constructed in time.
3.2 Data Structure for General R-OMSS Queries
Building on Section 3.1, we now generalize our findings and devise a data structure for answering (general) R-OMSS queries on for any positive offset with the query range . The data structure is composed of the following three parts:
-
(1)
the array ,
-
(2)
the data structures of Theorem 5 on , which can answer a prefix and suffix R-OMSS query on in time when the query range is canonical, and
-
(3)
an -space data structure of Lemma 1 constructed for each canonical range , which enables answering the OMSS query on in time.
Since the total length of all canonical ranges of is , (3) uses space in total. As both (1) and (2) use space, the overall data structure requires space.
To answer an R-OMSS query on with query range , we first partition into distinct canonical ranges , ordered from left to right, in time. If , we answer the query directly in time using the corresponding OMSS data structure of Lemma 1. Otherwise, we apply a dynamic programming approach similar to that in Section 3.1, as follows. We define an array , where for each , stores the answer range of the R-OMSS query on with query range , under the condition that the rightmost endpoint of the answer range is in .
Then the answer to the R-OMSS query on with range is the maximum value in , since the rightmost endpoint of the answer range may lie in any of the canonical ranges.
The base case can be computed in time using the corresponding OMSS data structure of Lemma 1. For , we compute from , we partition the range into five subranges as follows (see Figure 4 for a schematic sketch):
-
: The range
-
: The answer range of the R-OMSS query on with query range under the condition that the rightmost endpoint of the answer range is in , i.e., the answer range corresponding to .
-
: The range .
-
: The maximal prefix of that does not intersect with .
-
: The answer range of the R-OMSS query on with query range .
-
: The range .
(and hence and ) can be obtained from and (and hence and ) can be computed in time using the data structure of Lemma 1 on the canonical range . Thus, all the subranges above can be identified in time. Now we introduce the following lemma, which we use to compute .
Lemma 6.
Let be the answer range of the R-OMSS query on with query range , under the condition that the rightmost endpoint of the answer range falls within . Then must be one of the following five ranges:
-
1.
,
-
2.
,
-
3.
,
-
4.
, or
-
5.
.
Here, denotes the answer range of the suffix R-OMSS query on with offset and query range , and denotes the answer range of the prefix R-OMSS query on with offset and query range .
Proof.
Observe that the sum of elements in over any suffix of is negative; otherwise, would include such a suffix of . Similarly, the sum of elements in over any prefix of is negative. Hence, does not contain any position from either or .
Also, if , then is the answer range of the SMSS problem on . Otherwise, suppose that is the answer range of the SMSS problem on . By the definition of , we have . Then the sum of over the range , which is neither nor empty (since ), is greater than the sum of over . This contradicts the definition of as the answer range on . Therefore, when contains positions in , must be either or satisfy . By a similar argument for , we obtain that or .
From the above observations, we consider the following three cases:
-
(A)
,
-
(B)
, and
-
(C)
but .
For Case (A), we have by the definition of . For Case (B), must be completely contained in by the observation above. Thus, in this case, is either or (the latter corresponds to the case where ). For Case (C), since by the observation above, is either or (again, the latter corresponds to the case where ).
Example 7.
Figure 5 illustrates how Lemma 6 operates. In this example, the range is partitioned into three canonical ranges , , and (here the notion of canonical ranges is defined differently from that in the main text for convenience).
To answer the query with offset on the query range , consider the step of computing from . In this case, both and are non-empty. Therefore, is the maximum between the sum of over and the sum over , which are and , respectively. Hence, .
Query time.
can be computed in time using the suffix R-OMSS data structure on on the canonical query range with . Similarly, can be computed in time using the prefix R-OMSS data structure on on the canonical query range . Therefore, by Lemma 6, we can compute from in time, which implies that all values in can be computed in time. Finally, we can find the maximum value in in time by a linear scan.
Construction time.
The construction time of the data structure in Lemma 1 is , where denotes the input size. Since the total length of all canonical ranges of is , this yields a total construction time of [23]. In addition, constructing the data structure of Theorem 5 requires time. Hence, the overall construction time of the data structure is .
We summarize the results of this section in the following theorem.
Theorem 8.
Given an array of size , there exists an -space data structure that can answer an R-OMSS query on in time for any positive offset . The construction time of the data structure is .
4 Special Types of Inputs
Based on the results of the previous section, we analyze and devise specializations of our data structures for R-OMSS queries on under special input types. Specifically, we consider the following three scenarios: (1) the offset is restricted to positive integers, (2) is an array over an alphabet of size , and (3) is given in run-length encoded (RLE) form.
Integer Offsets.
When the offset is restricted to positive integers, the data structure of Theorem 8 can be improved by the following modifications. First, we introduce a lemma that states the data structure of Lemma 1 can be improved under this restriction.
Lemma 9.
When the offset is restricted to positive integers, there exists an -space data structure that can answer an OMSS query on in time.
Proof.
We modify the data structure of Lemma 1 as follows. For each (offset, interval) pair in (the list defined in the statement of Lemma 1), we replace it with the integer pair . If there exist two distinct pairs and in such that and , we discard the pair and maintain only in . We then maintain using a y-fast trie [27], which supports predecessor searches on in time using space.
Next, for each canonical range of , let be the rooted subtree of the SBST on (defined in Section 3.1) whose root corresponds to . We define a list of (offset, interval) pairs as follows: if and only if is a threshold offset stored in the node of , and is the answer range of the suffix R-OMSS query on with query range and offset . The list has size , since the number of distinct offsets equals the number of nodes in . Moreover, by the algorithm for answering suffix R-OMSS queries on using , for any offset , the answer range of the suffix R-OMSS query on with the query range is exactly the interval such that , where is the predecessor of in .
Therefore, plays the same role as the data structure of Lemma 1 for the suffix R-OMSS query on when the query range is restricted to . By maintaining such lists for all canonical ranges of , together with the modifications used in the proof of Lemma 9, we obtain the following lemma, since the total length of all canonical ranges of is .
Lemma 10.
When the offset is restricted to positive integers and the query range is restricted to a canonical range of , there exists an -space data structure that can answer an suffix R-OMSS query on in time.
Finally, by combining the data structures of Lemma 9 and Lemma 10, together with , we obtain the following corollary.
Corollary 11.
When the offset is restricted to positive integers, there exists an -space data structure that can answer an R-OMSS query on in time.
Bounded Alphabet Size.
When is an array over an alphabet of size , we show that the data structure in Lemma 1 requires space, even when is a binary array. Recall that this data structure partitions the domain of offsets into the ranges , , and assigns each such range an answer range such that is the answer range for each offset (defining the border cases and ). Therefore, the question arises how large must be. We say that two answer ranges and for the OMSS queries on with two different offsets and , respectively, are non-compatible if or . Distinct answer ranges are compatible if and only if their respective subarrays in are permutations of each other. For the analysis, we say that two offsets and are non-compatible if the offsets and have no answer range in common, i.e., there is no answer range of that is compatible with any answer range of (and vice versa).
Theorem 12.
There exists a binary array of size with non-compatible offsets.
Proof.
Consider the input for . Then, for each , when the offset lies in the interval , the answer range of the OMSS query on is the minimal prefix range that covers ones. Since for each such there is exactly one answer range, and all these answer ranges are non-compatible, the claim follows.
For the bounded-alphabet setting, improving the space lower bound for the data structure of Lemma 1, or designing an indexing data structure for OMSS queries that uses space, remains open. In Section 5, we will discuss the space usage of the data structure of Theorem 5 and Theorem 8 in the bounded-alphabet case.
Run-Length Encoded (RLE) Input.
If the input array for and of size has distinct runs, we can reduce the space usage of the data structures in Lemma 1, Theorem 5, and Theorem 8 to space by indexing the RLE form of . The RLE form of is a compact representation of that encodes each run of by a pair of value and the length of the run . Hence, can be represented in space with this RLE form.
We make use of the RLE form of as follows. As common components of our data structure, we maintain the following arrays:
-
1.
(i.e., in RLE form),
-
2.
of size , where , and
-
3.
of size , where for all .
These arrays use space in total. We stipulate that .
Given a position of with for some , i.e., is in the -th run , we can compute from in time using a binary search on . Then, we have and . Conversely, given a position of , corresponds to the -th run that starts at position and ends at position in . We now present a lemma showing that the data structure of Lemma 1 can be improved under this representation.
Lemma 13.
There exists a data structure of size that supports OMSS queries on in time.
Proof.
We construct the data structure of Lemma 1 on using space, which supports OMSS queries on in time. To answer an OMSS query on with offset , we first compute the OMSS on with the same offset in time. Let be the answer range of the OMSS on . We claim that is the answer range on , where (resp. ) is the leftmost (resp. rightmost) position in corresponding to (resp. ).
To prove the claim, it suffices to show that the endpoints of the answer range on must lie on run boundaries. Assume the contrary, i.e., one side of the answer range partially covers a run. By definition, this run covering position corresponds to a positive value in , otherwise the range could be shortened to exclude all values of this run and thus increase the sum. However, since is positive, extending the range to include the entire run further increases the sum, and thus contradicts the assumption.
Since the claim holds and the corresponding positions and can be computed in time, we can answer an OMSS query on in time overall.
From the claim used in the proof of Lemma 13, we can also obtain data structures in Theorem 5 and Theorem 8 parameterized by , as summarized in the following corollaries.
Corollary 14.
There exists a data structure of size that supports suffix R-OMSS queries on in time.
Proof.
We construct the data structure of Theorem 5 on using space, which supports OMSS queries on in time. To answer a suffix R-OMSS query on with query range and offset , we first compute the OMSS on with the same offset and query range in time, where and are the positions in corresponding to the runs of containing the positions and , respectively.
Let be the answer range of the OMSS on , and and be the leftmost and the rightmost positions in corresponding to and , respectively. Then, by the claim in the proof of Lemma 13, the answer range of the query on is .
Corollary 15.
There exists a data structure of size that supports R-OMSS queries on in time.
Proof.
The proof is analogous to that of Corollary 14, exchanging the data structure of Theorem 5 with that of Theorem 8. The only difference is that we return as the answer range, where is the rightmost position in corresponding to (all notations are the same as in the proof of Corollary 14).
Remark 16.
As all values in are integers, we can locate any position in and its corresponding position in (and vice versa) in time by maintaining a y-fast trie on . Consequently, by applying the results of Lemma 9, Lemma 10, and Corollary 11, the query times of the data structures in Lemma 13, Corollary 14, and Corollary 15 can be improved to , , and time, respectively, when the offset is restricted to positive integers.
5 Lower Bounds
Finally, we present lower bound results for data structures that answer R-OMSS queries on . We first show a reduction from the predecessor query to the OMSS query, and subsequently show that any encoding data structure that answers suffix R-OMSS queries requires at least bits.
Time-Space Trade-Offs Lower Bound.
In the following theorem, we show there exists a reduction from the predecessor query to OMSS query.
Theorem 17.
For any set of values from a universe , given a value , the predecessor of in can be determined by a OMSS query and an access on an array of size .
Proof.
Suppose we want to answer a predecessor query for a value in . For that, we have constructed, in a precomputation step, an OMSS data structure on , where stores the values of in ascendingly sorted order, and compute the OMSS query with offset . If the answer range for is , then the predecessor of is either (if ) or (if ). If the answer range is , then there is no predecessor of in . Otherwise, the answer range is with . Then the predecessor of is since (a) for all , (b) , and (c) for all . We here assume that the answer range of the OMSS query is the shortest one (omitting trailing zero entries), which can be ensured by a tie-breaking rule in the definition of the OMSS query.
From the predecessor lower bound of Pătraşcu and Thorup [18], the data structure of Lemma 1 achieves an optimal tradeoff between space and query time. Moreover, the data structures of Theorem 5 and Theorem 8 also provide optimal space–query time tradeoffs up to a factor in the query time, since they can answer OMSS queries.
Space Lower Bounds in the Encoding Model.
One may also study the R-OMSS query in the variant where only the answer range in is returned. In this setting, the problem is considered under the encoding model: after preprocessing , we maintain only the information necessary to answer queries, without storing explicitly. For many range queries, encoding data structures use less space than explicitly storing (see [20] for details). For example, there exists an -bit data structure for answering range MSS queries under a word-RAM model with a word size of bits [12], which uses asymptotically less space than the bits required to store under the same model. We now prove the following theorem, which shows that for the suffix R-OMSS queries (and hence for R-OMSS queries), any encoding data structure must use asymptotically as much space as storing the input. This implies that the data structure of Theorem 5 uses asymptotically optimal space.
Theorem 18.
Any permutation of can be reconstructed from an encoding that supports suffix R-OMSS queries. Consequently, any encoding data structure that answers suffix R-OMSS queries requires at least bits.
Proof.
We first identify the position of as follows. Let and , where is a fixed constant. For each , we perform R-OMSS queries on the range with offsets and . From the definition of the offsets, if and only if the answer ranges for the query range are under and under . Since is a permutation, there exists exactly one such position unless . To check whether , we perform two queries with query range : one query with offset and expected answer range , and another query with offset and expected answer range .
After locating the position of , we split the permutation into the two subarrays and . We then find the position of in the same manner, which must lie in exactly one of these two subarrays. Repeating this procedure recursively determines the position of every value up to , and hence reconstructs .
As a corollary of Theorem 18 we can also obtain the space lower bound for suffix R-OMSS queries (and hence for R-OMSS queries) in the encoding model with bounded alphabet size.
Corollary 19.
Any encoding data structure that supports suffix R-OMSS queries on an array of size over an alphabet of size requires at least bits.
Proof.
Let be the set of all arrays of size obtained by concatenating permutations of (we assume that is a multiple of , which does not affect the asymptotic lower bound). Then , and hence . Moreover, any array in can be reconstructed from suffix R-OMSS queries, since each permutation block can be reconstructed by Theorem 18. This proves the corollary.
Corollary 19 implies that even in the bounded-alphabet setting, any encoding data structure for suffix R-OMSS queries (and hence for R-OMSS queries) must use asymptotically as much space as required to store the input.
6 Open Problems
There are many gaps left open regarding time and space complexities. It is puzzling to us whether we can construct the data structure of Lemma 1 in optimal time. This would improve the construction time of our R-OMSS data structures in Theorem 8. Here, we additionally want to improve the space to , which would be optimal by Theorem 18. Improving the query time to is also an interesting open problem, which would be optimal by Theorem 17 for general offsets. In case that the number of distinct values in is bounded, we wonder whether we can improve lower and upper bounds on the number of non-compatible offsets.
For future directions, we want to study the variations of the MSS problem described in the introduction as query problems with offset and/or query range parameters.
References
- [1] Sung Eun Bae. Sequential and Parallel Algorithms for the Generalized Maximum Subarray Problem. PhD thesis, University of Canterbury, 2007.
- [2] Sung Eun Bae, Tong-Wook Shinn, and Tadao Takaoka. Efficient algorithms for the maximum sum problems. Algorithms, 10(1), 2017. doi:10.3390/a10010005.
- [3] Fredrik Bengtsson and Jingsen Chen. Computing Maximum-Scoring segments optimally. Research Report 2007:03, Luleå University of Technology, Department of Computer Science and Electrical Engineering, 2007.
- [4] Jon Louis Bentley. Perspective on performance. Commun. ACM, 27(11):1087–1092, 1984. doi:10.1145/1968.381154.
- [5] Timothy M. Chan and R. Ryan Williams. Deterministic apsp, orthogonal vectors, and more: Quickly derandomizing razborov-smolensky. ACM Trans. Algorithms, 17(1):2:1–2:14, 2021. doi:10.1145/3402926.
- [6] Kun-Mao Chao. Maximum-sum segments. In Ming-Yang Kao, editor, Encyclopedia of Algorithms, pages 1242–1244. Springer New York, New York, NY, 2016. doi:10.1007/978-1-4939-2864-4_226.
- [7] Kuan-Yu Chen and Kun-Mao Chao. On the range maximum-sum segment query problem. Discrete Applied Mathematics, 155(16):2043–2052, 2007. doi:10.1016/J.DAM.2007.05.018.
- [8] Chih-Huai Cheng, Hsiao-Fei Liu, and Kun-Mao Chao. Optimal algorithms for the average-constrained maximum-sum segment problem. Inf. Process. Lett., 109(3):171–174, 2009. doi:10.1016/J.IPL.2008.09.024.
- [9] Kai-Min Chung and Hsueh-I Lu. An optimal algorithm for the maximum-density segment problem. SIAM J. Comput., 34(2):373–387, 2004. doi:10.1137/S0097539704440430.
- [10] Christoph Dürr and Jill-Jênn Vie. Competitive Programming in Python: 128 Algorithms to Develop your Coding Skills. Cambridge University Press, 2020. URL: https://books.google.de/books?id=JA0FEAAAQBAJ.
- [11] Takeshi Fukuda, Yasuhiko Morimoto, Shinichi Morishita, and Takeshi Tokuyama. Data mining with optimized two-dimensional association rules. ACM Trans. Database Syst., 26(2):179–213, 2001. doi:10.1145/383891.383893.
- [12] Paweł Gawrychowski and Patrick K Nicholson. Encodings of range maximum-sum segment queries and applications. In Annual Symposium on Combinatorial Pattern Matching, pages 196–206. Springer, 2015.
- [13] Michael H. Goldwasser, Ming-Yang Kao, and Hsueh-I Lu. Fast algorithms for finding maximum-density segments of a sequence with applications to bioinformatics. In Proc. WABI, volume 2452 of LNCS, pages 157–171, 2002. doi:10.1007/3-540-45784-4_12.
- [14] Ronald L. Graham. An efficient algorith for determining the convex hull of a finite planar set. Information processing letters, 1(4):132–133, 1972. doi:10.1016/0020-0190(72)90045-2.
- [15] Sung Kwon Kim. Linear-time algorithm for finding a maximum-density segment of a sequence. Information Processing Letters, 86(6):339–342, 2003. doi:10.1016/S0020-0190(03)00225-4.
- [16] Yaw-Ling Lin, Tao Jiang, and Kun-Mao Chao. Efficient algorithms for locating the length-constrained heaviest segments with applications to biomolecular sequence analysis. J. Comput. Syst. Sci., 65(3):570–586, 2002. doi:10.1016/S0022-0000(02)00010-7.
- [17] Hsiao-Fei Liu and Kun-Mao Chao. Algorithms for finding the weight-constrained k longest paths in a tree and the length-constrained k maximum-sum segments of a sequence. Theor. Comput. Sci., 407(1-3):349–358, 2008. doi:10.1016/J.TCS.2008.06.052.
- [18] Mihai Pătraşcu and Mikkel Thorup. Time-space trade-offs for predecessor search. In Proceedings of the thirty-eighth annual ACM symposium on Theory of computing, pages 232–240, 2006.
- [19] Kalyan S. Perumalla and Narsingh Deo. Parallel algorithms for maximum subsequence and maximum subarray. Parallel Process. Lett., 5:367–373, 1995. doi:10.1142/S0129626495000345.
- [20] Rajeev Raman. Encoding data structures. In International Workshop on Algorithms and Computation, pages 1–7. Springer, 2015. doi:10.1007/978-3-319-15612-5_1.
- [21] Walter L. Ruzzo and Martin Tompa. A linear time algorithm for finding all maximal scoring subsequences. In Proc. ISMB, pages 234–241, 1999. URL: http://www.aaai.org/Library/ISMB/1999/ismb99-027.php.
- [22] Yoshifumi Sakai. A maximal local maximum-sum segment data structure. IEICE Trans. Fundam. Electron. Commun. Comput. Sci., 101-A(9):1541–1542, 2018. doi:10.1587/TRANSFUN.E101.A.1541.
- [23] Yoshifumi Sakai. A Data Structure for the Maximum-Sum Segment Problem with Offsets. In 35th Annual Symposium on Combinatorial Pattern Matching (CPM 2024), volume 296 of Leibniz International Proceedings in Informatics (LIPIcs), pages 26:1–26:15. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2024. doi:10.4230/LIPIcs.CPM.2024.26.
- [24] Tadao Takaoka. Efficient algorithms for the maximum subarray problem by distance matrix multiplication. Electronic Notes in Theoretical Computer Science, 61:191–200, 2002. CATS’02, Computing: the Australasian Theory Symposium. doi:10.1016/S1571-0661(04)00313-5.
- [25] Hisao Tamaki and Takeshi Tokuyama. Algorithms for the maximum subarray problem based on matrix multiplication. Interdisciplinary Information Sciences, 6(2):99–104, 2000. doi:10.4036/iis.2000.99.
- [26] Lusheng Wang and Ying Xu. SEGID: identifying interesting segments in (multiple) sequence alignments. Bioinform., 19(2):297–298, 2003. doi:10.1093/BIOINFORMATICS/19.2.297.
- [27] Dan E Willard. Log-logarithmic worst-case range queries are possible in space . Information Processing Letters, 17(2):81–84, 1983.
- [28] Hung-I Yu, Tien-Ching Lin, and D. T. Lee. Finding maximum sum segments in sequences with uncertainty. Theor. Comput. Sci., 850:221–235, 2021. doi:10.1016/J.TCS.2020.11.005.
