Abstract 1 Introduction 2 Preliminaries and Query Definitions 3 Data structure for R-OMSS Queries 4 Special Types of Inputs 5 Lower Bounds 6 Open Problems References

Indexing Range Maximum-Sum Segment Queries with Offsets

Seungbum Jo ORCID Chungnam National University, Daejeon, Republic of Korea    Dominik Köppl ORCID University of Yamanashi, Kofu, Japan
Abstract

Given an array of n 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 O(n) 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 O(logn). In detail, our index uses O(nlogn) space, supports queries in O(log2n) time, and can be constructed in O(nlog3n) 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 query
Funding:
Seungbum Jo: Seungbum Jo was supported by the National Research Foundation of Korea (NRF) grant funded by the Korea government (MSIT) (RS-202523963814).
Dominik Köppl: This work was supported by JSPS KAKENHI Grant Number 25K21150.
Copyright and License:
[Uncaptioned image] © Seungbum Jo and Dominik Köppl; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Data structures design and analysis
Editor:
Pierre Fraigniaud

1 Introduction

Given an array X of real numbers, the maximum-sum segment (MSS) problem is to find an interval [i,j] within X 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 X and then answer queries on intervals of X. 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 tc(n) and query time tq(n) solving the MSS problem can be transformed into a data structure solving RMQs with the time complexities tc(cn) and tq(cn) for some constant c, respectively. Gawrychowski and Nicholson [12] gave a Θ(n)-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 1.89113nΘ(logn) 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 O(n) 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 k 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 n 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 O(n3)-time algorithm for the problem. Subsequently, the time complexity has been improved to O(n3loglogn/logn) by Takaoka [24] and further to O(n3loglogn/logn) by Tamaki and Tokuyama [25]. They showed that the problem can be reducible to (min,+)-matrix multiplication. Using this result, the current fastest algorithm runs in n3/2Ω(logn)1/2 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 O(n)-time parallel algorithm solving the maximum convex sum problem for n2 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 O(logn) 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).

Table 1: Summary of best known complexities for the MSS query and its variants.
Query type Space Query time Reference
MSS O(1) O(n) Kadane’s algorithm
OMSS O(n) O(logn) Sakai [23]
R-OMSS without offset O(n) O(1) Chen et al. [7]
Suffix R-OMSS O(n) O(logn) Theorem 5
R-OMSS O(nlogn) O(log2n) Theorem 8

2 Preliminaries and Query Definitions

Figure 1: Examples for the MSS query and its variants.

Our computation model is the RAM model. Let denote the set of all real numbers. We write [i,j]={i,i+1,i+2,,j} for two integers ij, and use the same notation [i,j] for intervals of real numbers in case that i,j. 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 X0[1..n] be the input array of real numbers. X0 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 X0, 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 Ω(n) 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 O(n)-space array of prefix sums. We state our base problem as follows.

Maximum-Segment-Sum (MSS)
Input: array of real numbers X0[1..n].

Output: value and answer range [,r] achieving max[,r][1,n]i=rX0[i].

We also study the special case that the answer range of the MSS problem is constrained to be a suffix of X0.

Suffix Maximum-Segment-Sum (SMSS)
Input: array of real numbers X0[1..n]

Output: value and integer achieving max[1,n]i=nX0[i].

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 X0[1..n]

Query Input: real-value offset α0.

Query Output: value and answer range [,r] achieving max[,r][1,n]i=r(X0[i]α).

Defining the array Xα[1..n] with Xα[i]:=X0[i]α, the OMSS query on X0 with offset α is the MSS query on Xα. Hence, OMSS is a generalization of MSS, since the answer to an MSS query on X0 is the same as the answer to an OMSS query on Xα with offset α=0. Sakai [23] presented an approach answering an OMSS query as follows.

Lemma 1 ([23]).

Given an array X0[1..n] of n real numbers, we can compute a list L of at most n (offset, interval)-pairs in O(nlog2n) time using O(n) space such that the solution to an OMSS query for a given offset α is the interval of the predecessor offset of α in L. By sorting L descendingly by the offsets, a binary search for the predecessor of α in L can return an answer range of an OMSS query in O(logn) time.

The predecessor of α in L is the largest offset in L 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 i=ab(X0[i]α) for an entry (α,[a..b]) in L in constant time if we have the array of prefix lengths C0[1..n] of X0, where C0[i]:=k=1iX0[k] for each i[1,n]. To see that, define the prefix sum of Xα for each i[1,n] as Cα[i]:=k=1iXα[k]=C0[i]αk, and the sum of the subarray Xα[i..j] as Sα(i,j)=k=ijXα[k]=C0[j]C0[i1]α(ji+1) for any i and j with 1ijn. Then i=ab(X0[i]α)=Sα(a,b).

Second, the upper bound |L|n is tight since L can reach the size n for the input X0=[1,2,3,,n], where the OMSS query for α=k+ϵ gives the range [k+1,n] for every ϵ(0,1) and k[0,n].

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 X0.

Range Offset Maximum-Segment-Sum (R-OMSS)
Construction Input: array of real numbers X0[1..n]

Query Input: real-value offset α0 and a range [a,b][1,n].

Query Output: value and answer range [,r] achieving max[,r][a,b]i=r(X0[i]α).

Using the notation of Sα(i,j), the answer to an R-OMSS query on X0 with query range [a,b] and offset α can be computed as max[,r][a,b]Sα(,r). The R-OMSS is a generalization of the OMSS problem (with the query range [1,n]) and the range MSS problem introduced by Chen et al. [7], which asks for the MSS of a query range [a,b] without the offset parameter (i.e., α=0). 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 X0[1..n]

Query Input: real-value offset α0 and a range [a,b][1,n].

Query Output: value and integer achieving max[a,b]i=b(X0[i]α).

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 X0, where a non-negative offset α0 and a query range [a,b] are given at query time. There are two naive approaches prioritizing either (1) space or (2) time.

  1. (1)

    We can apply Kadane’s algorithm on Xα[a..b]. Since any value in Xα can be computed in O(1) time from X0, Kadane’s algorithm answers R-OMSS in O(n) time using O(1) space additional to X0.

  2. (2)

    Alternatively, we can maintain the data structure of Lemma 1 for all Θ(n2) possible query ranges. This results in an O(n3)-space data structure with O(logn) 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 O(n)-space data structure answering suffix R-OMSS queries on X0 in O(log2n) 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 O(nlogn)-space data structure that can answer an R-OMSS query in O(log2n) time.

3.1 Data Structure for Suffix R-OMSS Queries

Figure 2: SBST on X0=[4,1,6,1,8,0]. Each node is labeled with its corresponding canonical range and threshold offset.

We start with devising a data structure for range suffix OMSS queries. The main idea is to maintain the canonical ranges (defined below) of X0 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 [a,b] into O(logn) canonical ranges and answer the query using a dynamic programming approach.

The canonical ranges of X0 are the elements of a recursive binary partition of [1,n] into O(n) ranges, which can be represented by a binary tree. The canonical ranges of X0 are defined as follows:

  1. (1)

    [1,n] is a canonical range, and

  2. (2)

    for every canonical range [g,h] with gh, both [g,g+(hg)/2] and [g+(hg)/2+1,h] are also canonical ranges.

Under this definition, there are O(n) canonical ranges in total. We then define the SBST on X0[g,h] as follows:

  • The root of the SBST on X0[g,h] is the canonical range [g,h].

  • If g<h, then the SBSTs on X0[g,g+(hg)/2] and X0[g+(hg)/2+1,h] 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 [1,n].)

Let T denote the SBST on X0[1,n]. From the definition, each canonical range of X0 is a node in T, and vice versa. The leaves of T are all canonical ranges of size 1, i.e., [i,i]={i} for each i[1,n]. By construction, T is a binary tree with O(n) nodes and height O(logn). Finally, we augment each node p of T with a threshold offset αp. 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 Xβ is a subrange of the answer range of the SMSS query on Xα.

Proof.

Let [x,n] and [x,n] be the answer ranges of the SMSS query on Xα and Xβ, respectively. Suppose that x<x. Since Sβ(x,n)>Sβ(x,n), it follows that the sum Sβ(x,x1)>0, which implies Sα(x,x1)=Sβ(x,x1)+(βα)(xx)>0. This leads to a contradiction since Sα(x,n)=Sα(x,n)+Sα(x,x1)>Sα(x,n).

Let [xp,yp] be the range corresponding to the node p, and let [xα,yp][xp,yp] be the answer range of the SMSS problem on Xα[xp,yp] for some variable α. We then define the threshold offset αp at node p as the maximum value of α such that xα[xp,xp+(ypxp)/2], that is, xα lies in the range corresponding to the left child of p. If no such α exists, or if p is a leaf node, we define αp=1. By Proposition 2, the value αp is well defined as a single constant.

The data structure for suffix R-OMSS queries is composed of (1) the array C0, and (2) the SBST T on X0. Each node of the SBST stores its threshold offset and its canonical range. (1) and (2) together use O(n) 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 [xp,yp] corresponding to a node p in T. In this case, it suffices to find the position x such that [x,yp] is the answer range. To find x, we start from the node p and compare α with αp. As in a standard binary search tree, we recursively perform the same comparison: we proceed to the left child if ααp, and to the right child otherwise, until we reach a leaf node corresponding to the canonical range [x,x]. Then x=x from the following proposition.

Proposition 3.

Let p be an internal node in T with the corresponding range [xp,yp], and let zp=xp+(ypxp)/2. If ααp (resp. α>αp), then the leftmost endpoints of the answer ranges of the SMSS queries on Xα[xp,yp] and Xα[xp,zp] (resp. on Xα[xp,yp] and Xα[zp+1,yp]) are the same.

Proof.

Here we consider only the case that ααp; the case α>αp can be proved analogously. Let x and x be the leftmost endpoints of the answer ranges of the SMSS queries on Xα[xp,yp] and Xα[xp,zp]. We now claim that x=x. To prove the claim, suppose that x<x (the case x>x can be handled analogously). Since Sα(x,yp)>Sα(x,yp), it follows that Sα(x,x1)>0. Consequently, Sα(x,zp)=Sα(x,zp)+Sα(x,x1)>Sα(x,zp), which contradicts the definition of x.

Since the height of T is O(logn), we can answer a suffix R-OMSS query on X0 in O(logn) time when the query range is canonical.

Finally, we consider how to answer a suffix R-OMSS query on X0 for a general query range [a,b] using the data structure. We first partition [a,b] into c=O(logn) distinct canonical ranges [a1,b1],,[ac,bc], ordered from left to right (i.e., a1=a, bc=b, and bk+1=ak+1 for all k[c1]). This partition can be computed by starting from the root of T and traversing the tree downwards in two directions for finding the leaves of the canonical ranges [a,a] and [b,b], respectively. We then collect O(logn) canonical ranges corresponding to children on the two root-to-leaf paths. At each level of T, there are at most two nodes whose canonical ranges [x,y] satisfy [x,y][a,b] but [x,y][a,b]; only these nodes require recursion to the next level, while any other intersecting node is fully contained in [a,b] and can be reported as a canonical range immediately. Therefore, we can compute these canonical ranges in O(logn) time.

If c=1 (i.e., [a,b] is a canonical range), we can answer the R-OMSS query in O(logn) time using T. Otherwise, we apply a dynamic programming approach as follows. Define an array A[1..c] of size c, where for each k[1,c], A[k] stores the answer range of the SMSS query on Xα[a1,bk]. Then A[c] stores the answer of the suffix R-OMSS query.

To compute A[c], we first handle the base case by computing A[1] in O(logn) time using T. Next, for k>1, we compute A[k] from A[k1] as follows. To compute A[k], it suffices to determine the answer range [x,bk] of the SMSS query on Xα[a1,bk]. Now we introduce the following lemma, which is used to compute A[k].

Lemma 4.

Let [x,bk] and [x,bk1] be the answer ranges of the SMSS queries on Xα[a1,bk] and Xα[a1,bk1], respectively. Then x is either (1) x or (2) z, where [z,bk] is the answer range of the SMSS query on Xα[ak,bk].

Proof.

If x<x or x<xbk1, then Sα(x,bk)=Sα(x,bk1)+Sα(ak,bk)>Sα(x,bk1)+Sα(ak,bk), which contradicts the fact that [x,bk1] is the answer range of Xα[a1,bk1].

Similarly, if akx<z or x>z, then Sα(x,bk)>Sα(z,bk), which contradicts the fact that [z,bk] is the answer range of Xα[ak,bk].

The answer ranges of the SMSS queries on Xα[a1,bk1] and Xα[ak,bk] can be obtained from A[k1] and from T, respectively, in O(logn) time. Thus, by Lemma 4, given A[k1], the value A[k] can be computed in O(logn) time, which implies that A[c] can be computed in O(log2n) time.

Figure 3: Example illustrating the computation of the threshold offset at the root of T in Figure 2. (a) The point set P with its convex hull. (b) The dual lines corresponding to the convex hull of P. The red segments represent the upper envelope of these lines.

Construction time.

As C0 can be constructed in O(n) time, it suffices to analyze the construction time of T. To construct T, for each node p whose corresponding range is [xp,yp] with p=ypxp+1, we compute its threshold offset αp as follows. Consider the point set P={(t,S0(ypt+1,yp))1tp}, that is, each point corresponds to a suffix of [xp,yp], where the x-coordinate t is the suffix length and the y-coordinate is its sum. Among these points, we keep only those that can be optimal for some α>0. Equivalently, we keep the vertices of the upper convex hull of P (since we maximize yαx). The convex hull can be constructed in O(p) time, since the points in P are already sorted by their first coordinates [14].

For any suffix represented by a point (x,y)P, we have Sα(ypx+1,yp)=yαx. Thus, given an offset α, answering the SMSS query on X0[xp,yp] reduces to finding the point (x,y)P that maximizes yαx. By point–line duality, each point (x,y) corresponds to the line L(x,y)(w)=ywx, and max(x,y)P(yαx) equals the value of the upper envelope at w=α, which can be computed in O(p) time from the convex hull of P. The upper envelope is piecewise linear and changes only at intersection points of consecutive envelope lines. Therefore, αp is the breakpoint on the envelope where the optimal suffix switches from a suffix whose starting position lies in the left half of [xp,yp] to one whose starting position lies in the right half. Since the envelope breakpoints are sorted by slope, αp can be found by binary search over the hull in O(logp) time (using Proposition 2). See Figure 3 for an example.

Thus, the nodes at each level of T can be constructed in O(n) time, implying that the entire tree T can be constructed in O(nlogn) time overall.

We summarize the results of this section in the following theorem.

Theorem 5.

Given an array X0 of size n, there exists an O(n)-space data structure that can answer a prefix/suffix R-OMSS query on X0 in O(log2n) time for any positive offset α. When the query range is a canonical range of X0, the data structure can answer a prefix/suffix R-OMSS query in O(logn) time. The data structure can be constructed in O(nlogn) 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 X0 for any positive offset α>0 with the query range [a,b]. The data structure is composed of the following three parts:

  1. (1)

    the array C0,

  2. (2)

    the data structures of Theorem 5 on X0, which can answer a prefix and suffix R-OMSS query on X0 in O(logn) time when the query range is canonical, and

  3. (3)

    an O(|hg|)-space data structure of Lemma 1 constructed for each canonical range [g,h], which enables answering the OMSS query on X0[g,h] in O(logn) time.

Since the total length of all canonical ranges of X0 is O(nlogn), (3) uses O(nlogn) space in total. As both (1) and (2) use O(n) space, the overall data structure requires O(nlogn) space.

To answer an R-OMSS query on X0 with query range [a,b], we first partition [a,b] into c=O(logn) distinct canonical ranges [a1,b1],,[ac,bc], ordered from left to right, in O(logn) time. If c=1, we answer the query directly in O(logn) 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 A[1..c], where for each k[1,c], A[k] stores the answer range of the R-OMSS query on X0 with query range [a1,bk], under the condition that the rightmost endpoint of the answer range is in [ak,bk].

Then the answer to the R-OMSS query on X0 with range [a,b] is the maximum value in A, since the rightmost endpoint of the answer range may lie in any of the canonical ranges.

The base case k=1 can be computed in O(logn) time using the corresponding OMSS data structure of Lemma 1. For k>1, we compute A[k] from A[k1], we partition the range [a1,bk] into five subranges as follows (see Figure 4 for a schematic sketch):

  • I0: The range [a1,bk1]A[k1]

  • I1: The answer range of the R-OMSS query on X0 with query range [a1,bk1] under the condition that the rightmost endpoint of the answer range is in [ak1,bk1], i.e., the answer range corresponding to A[k1].

  • I2: The range [ak1,bk1](I0I1).

  • I3: The maximal prefix of X0[ak,bk] that does not intersect with I4.

  • I4: The answer range of the R-OMSS query on X0 with query range [ak,bk].

  • I5: The range [ak,bk](I3I4).

Figure 4: Subranges that partition the range [a1,bk].

I1 (and hence I0 and I2) can be obtained from A[k1] and I4 (and hence I3 and I5) can be computed in O(logn) time using the data structure of Lemma 1 on the canonical range [ak,bk]. Thus, all the subranges above can be identified in O(logn) time. Now we introduce the following lemma, which we use to compute A[k].

Lemma 6.

Let I be the answer range of the R-OMSS query on X0 with query range [a1,bk], under the condition that the rightmost endpoint of the answer range falls within [ak,bk]. Then I must be one of the following five ranges:

  1. 1.

    I4,

  2. 2.

    I1I2I3,

  3. 3.

    I1I2I3I4,

  4. 4.

    I2I3, or

  5. 5.

    I2I3I4.

Here, I2 denotes the answer range of the suffix R-OMSS query on X0 with offset α and query range [ak1,bk1], and I3 denotes the answer range of the prefix R-OMSS query on X0 with offset α and query range [ak,bk].

Proof.

Observe that the sum of elements in Xα over any suffix of I0 is negative; otherwise, I1 would include such a suffix of I0. Similarly, the sum of elements in Xα over any prefix of I5 is negative. Hence, I does not contain any position from either I0 or I5.

Also, if I2I1, then I1I2 is the answer range of the SMSS problem on Xα[a1,bk1]. Otherwise, suppose that II1I2 is the answer range of the SMSS problem on Xα[a1,bk1]. By the definition of I2, we have I2I2I. Then the sum of Xα over the range II2, which is neither I1 nor empty (since I2I1), is greater than the sum of Xα over I1. This contradicts the definition of I1 as the answer range on [a1,bk1]. Therefore, when I2 contains positions in [a1,bk1], I2 must be either I1I2 or satisfy I2I2. By a similar argument for I3, we obtain that I3=I3I4 or I3I3.

From the above observations, we consider the following three cases:

  1. (A)

    I[ak,bk],

  2. (B)

    II1, and

  3. (C)

    II1= but II2.

For Case (A), we have I=I4 by the definition of I4. For Case (B), I1 must be completely contained in I by the observation above. Thus, in this case, I is either I1I2I3 or I1I2I3I4 (the latter corresponds to the case where I3=I3I4). For Case (C), since I2I2 by the observation above, I is either I2I3 or I2I3I4 (again, the latter corresponds to the case where I3=I3I4).

Figure 5: Supplement figure for Example 7. Canonical ranges are represented by thick vertical bars, and the intervals Ij by red horizontal lines in their respective rows.
Example 7.

Figure 5 illustrates how Lemma 6 operates. In this example, the range [1,20] is partitioned into three canonical ranges [1,4], [5,12], and [13,20] (here the notion of canonical ranges is defined differently from that in the main text for convenience).

To answer the query with offset α=8 on the query range [1,20], consider the step of computing A[3] from A[2]. In this case, both I2I1 and I3I4 are non-empty. Therefore, A[3] is the maximum between the sum of X8 over I4 and the sum over I1I2I3I4, which are 26 and 32, respectively. Hence, A[3]=32.

Query time.

I2 can be computed in O(logn) time using the suffix R-OMSS data structure on X0 on the canonical query range [ak1,bk1] with I1. Similarly, I3 can be computed in O(logn) time using the prefix R-OMSS data structure on X0 on the canonical query range [ak,bk]. Therefore, by Lemma 6, we can compute A[k] from A[k1] in O(logn) time, which implies that all values in A can be computed in O(log2n) time. Finally, we can find the maximum value in A in c=O(logn) time by a linear scan.

Construction time.

The construction time of the data structure in Lemma 1 is O(log2), where denotes the input size. Since the total length of all canonical ranges of X0 is O(nlogn), this yields a total construction time of O(nlog3n) [23]. In addition, constructing the data structure of Theorem 5 requires O(nlogn) time. Hence, the overall construction time of the data structure is O(nlog3n).

We summarize the results of this section in the following theorem.

Theorem 8.

Given an array X0 of size n, there exists an O(nlogn)-space data structure that can answer an R-OMSS query on X0 in O(log2n) time for any positive offset α. The construction time of the data structure is O(nlog3n).

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 X0 under special input types. Specifically, we consider the following three scenarios: (1) the offset α is restricted to positive integers, (2) X0 is an array over an alphabet of size σ, and (3) X0 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 O(n)-space data structure that can answer an OMSS query on X0 in O(loglogn) time.

Proof.

We modify the data structure of Lemma 1 as follows. For each (offset, interval) pair (α,I) in L (the list defined in the statement of Lemma 1), we replace it with the integer pair (α,I). If there exist two distinct pairs (α1,I1) and (α2,I2) in L such that α1<α2 and α1=α2, we discard the pair (α1,I1) and maintain only (α2,I2) in L. We then maintain L using a y-fast trie [27], which supports predecessor searches on L in O(loglogn) time using O(n) space.

Next, for each canonical range [g,h] of X0, let T[g,h] be the rooted subtree of the SBST T on X0 (defined in Section 3.1) whose root corresponds to [g,h]. We define a list L[g,h] of (offset, interval) pairs as follows: (β,I)L[g,h] if and only if β is a threshold offset stored in the node of T[g,h], and I is the answer range of the suffix R-OMSS query on X0 with query range [g,h] and offset β. The list L[g,h] has size O(|hg|), since the number of distinct offsets equals the number of nodes in T[g,h]. Moreover, by the algorithm for answering suffix R-OMSS queries on X0 using T, for any offset α, the answer range of the suffix R-OMSS query on X0 with the query range [g,h] is exactly the interval I such that (β,I)L[g,h], where β is the predecessor of α in L[g,h].

Therefore, L[g,h] plays the same role as the data structure of Lemma 1 for the suffix R-OMSS query on X0 when the query range is restricted to [g,h]. By maintaining such lists for all canonical ranges of X0, 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 X0 is O(nlogn).

Lemma 10.

When the offset α is restricted to positive integers and the query range is restricted to a canonical range of X0, there exists an O(nlogn)-space data structure that can answer an suffix R-OMSS query on X0 in O(loglogn) time.

Finally, by combining the data structures of Lemma 9 and Lemma 10, together with C, we obtain the following corollary.

Corollary 11.

When the offset α is restricted to positive integers, there exists an O(nlogn)-space data structure that can answer an R-OMSS query on X0 in O(lognloglogn) time.

Bounded Alphabet Size.

When X0 is an array over an alphabet Σ of size σ, we show that the data structure in Lemma 1 requires Ω(n) space, even when X0 is a binary array. Recall that this data structure partitions the domain of offsets into the ranges [0,α1), [α1,α2), [α2,α3),,[αk1,αk),[αk,) and assigns each such range [αi,αi+1) an answer range [ai,bi] such that [ai,bi] is the answer range for each offset α[αi,αi+1) (defining the border cases α0=0 and αk+1=). Therefore, the question arises how large k must be. We say that two answer ranges [a,b] and [a,b] for the OMSS queries on X0 with two different offsets α and α, respectively, are non-compatible if Sα(a,b)>Sα(a,b) or Sα(a,b)<Sα(a,b). Distinct answer ranges are compatible if and only if their respective subarrays in X0 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 X0 of size n with Θ(n) non-compatible offsets.

Proof.

Consider the input X0=11011021031040k for k=Θ(n). Then, for each k[k1], when the offset α lies in the interval (1/(k+1),1/k), the answer range of the OMSS query on X0 is the minimal prefix range that covers 1+1/k ones. Since for each such k there is exactly one answer range, and all these answer ranges are non-compatible, the claim follows.

For the bounded-alphabet setting, improving the Ω(n) space lower bound for the data structure of Lemma 1, or designing an indexing data structure for OMSS queries that uses o(n) 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 X0=X0[j1]k1X0[j2]k2X0[jz]kz for j1=1<j2<<jz=n and k1,,kz1 of size n has z distinct runs, we can reduce the space usage of the data structures in Lemma 1, Theorem 5, and Theorem 8 to O(z) space by indexing the RLE form of X0. The RLE form of X0 is a compact representation of X0 that encodes each run X0[ji]ki of X0 by a pair of value X0[ji] and the length of the run ki. Hence, X0 can be represented in O(z) space with this RLE form.

We make use of the RLE form of X0 as follows. As common components of our data structure, we maintain the following arrays:

  1. 1.

    X:=[X0[j1],X0[j2],,X0[jz]] (i.e., X0 in RLE form),

  2. 2.

    D of size z, where D[i]=t=1ikt, and

  3. 3.

    C of size z, where C[i]=t=1iktX[t] for all i[1,z].

These arrays use O(z) space in total. We stipulate that D[0]=0.

Given a position i[1,n] of X0 with i[D[i1]+1,D[i]] for some i[1,z], i.e., i is in the i-th run X0, we can compute i from i in O(logz) time using a binary search on D. Then, we have X0[i]=X[i] and C0[i]=C[i1]+(iD[i1])X[i]. Conversely, given a position i[1,z] of X, i corresponds to the i-th run that starts at position D[i1]+1 and ends at position D[i] in X0. 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 O(z) that supports OMSS queries on X0 in O(logz) time.

Proof.

We construct the data structure of Lemma 1 on X using O(z) space, which supports OMSS queries on X in O(logz) time. To answer an OMSS query on X with offset α, we first compute the OMSS on X with the same offset in O(logz) time. Let [i,j] be the answer range of the OMSS on X. We claim that [i,j] is the answer range on X, where i (resp. j) is the leftmost (resp. rightmost) position in X corresponding to i (resp. j).

To prove the claim, it suffices to show that the endpoints of the answer range on X0 must lie on run boundaries. Assume the contrary, i.e., one side j of the answer range partially covers a run. By definition, this run covering position X0[j] corresponds to a positive value in Xα[j], otherwise the range could be shortened to exclude all values of this run and thus increase the sum. However, since Xα[j] 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 i and j can be computed in O(logz) time, we can answer an OMSS query on X0 in O(logz) 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 z, as summarized in the following corollaries.

Corollary 14.

There exists a data structure of size O(z) that supports suffix R-OMSS queries on X0 in O(log2z) time.

Proof.

We construct the data structure of Theorem 5 on X using O(z) space, which supports OMSS queries on X in O(log2z) time. To answer a suffix R-OMSS query on X0 with query range [a,b] and offset α, we first compute the OMSS on X with the same offset and query range [a,b] in O(log2z) time, where a and b are the positions in X corresponding to the runs of X0 containing the positions a and b, respectively.

Let [i,j] be the answer range of the OMSS on X, and i and j be the leftmost and the rightmost positions in X0 corresponding to i and j, respectively. Then, by the claim in the proof of Lemma 13, the answer range of the query on X0 is [max(a,i),b].

Corollary 15.

There exists a data structure of size O(zlgz) that supports R-OMSS queries on X0 in O(log2z) 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 [max(a,i),min(b,j)] as the answer range, where j is the rightmost position in X0 corresponding to j (all notations are the same as in the proof of Corollary 14).

 Remark 16.

As all values in D are integers, we can locate any position in X and its corresponding position in X (and vice versa) in O(loglogz) time by maintaining a y-fast trie on D. 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 O(loglogz), O(logzloglogz), and O(logzloglogz) 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 X0. 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 Ω(nlogn) 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 S of n values from a universe U, given a value αU, the predecessor of α in S can be determined by a OMSS query and an access on an array of size n.

Proof.

Suppose we want to answer a predecessor query for a value αU in S. For that, we have constructed, in a precomputation step, an OMSS data structure on X0, where X0 stores the values of S in ascendingly sorted order, and compute the OMSS query with offset α. If the answer range for α is [n,n], then the predecessor of α is either X0[n] (if X0[n]α) or X0[n1] (if X0[n]>α). If the answer range is [1,n], then there is no predecessor of α in S. Otherwise, the answer range is [i,n] with i[2,n1]. Then the predecessor of α is X0[i1] since (a) Xα[k]<0 for all ki2, (b) Xα[i1]0, and (c) Xα[k]>0 for all ki. 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 logn 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 X0 is returned. In this setting, the problem is considered under the encoding model: after preprocessing X0, we maintain only the information necessary to answer queries, without storing X0 explicitly. For many range queries, encoding data structures use less space than explicitly storing X0 (see [20] for details). For example, there exists an O(n)-bit data structure for answering range MSS queries under a word-RAM model with a word size of Θ(logn) bits [12], which uses asymptotically less space than the Ω(nlogn) bits required to store X0 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 X0 of [1,n] 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 Ω(nlogn) bits.

Proof.

We first identify the position of 1 as follows. Let α1=0 and α2=1+ϵ, where 0<ϵ<1 is a fixed constant. For each a[1,n1], we perform R-OMSS queries on the range [a,a+1] with offsets α1 and α2. From the definition of the offsets, X0[i]=1 if and only if the answer ranges for the query range [i,i+1] are [i,i+1] under α1 and [i+1,i+1] under α2. Since X0 is a permutation, there exists exactly one such position i unless X0[n]=1. To check whether X0[n]=1, we perform two queries with query range [n1,n]: one query with offset α1 and expected answer range [n1,n], and another query with offset α2 and expected answer range [n1,n1].

After locating the position i of 1, we split the permutation into the two subarrays X0[1,i1] and X0[i+1,n]. We then find the position of 2 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 n, and hence reconstructs X0.

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 n over an alphabet of size σ requires at least Ω(nlogσ) bits.

Proof.

Let 𝒳 be the set of all arrays of size n obtained by concatenating n/σ permutations of [1,σ] (we assume that n is a multiple of σ, which does not affect the asymptotic lower bound). Then |𝒳|=(σ!)n/σ, and hence log|𝒳|=Ω(nlogσ). 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 O(n) 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 O(n), which would be optimal by Theorem 18. Improving the query time to O(logn) 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 X0 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 θ(n). 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.