Faster Linear-Space Data Structures for Path Frequency Queries
Abstract
We present linear-space data structures for several frequency queries on trees, namely: path mode, path least frequent element, and path -minority queries. We present the first linear-space data structures, requiring preprocessing time, that can answer path mode and path least frequent element queries in time. This improves upon the best previously known bound of achieved by Durocher et al. [10] in 2016.
For the path -minority problem, where is specified at query time, we reduce the query time of the linear-space data structure of Durocher et al. [10] from down to by employing a simple randomized algorithm with a success probability .
We also present the first linear-space data structure supporting “Path Maximum -value Color” queries in time, requiring preprocessing time. This general framework encapsulates both path mode and path least frequent element queries. For our data structures, we consider the word-RAM model with , where is the word size in bits.
Keywords and phrases:
Data structure, Range query, Mode, Minority, Least frequent element, Trees, Linear-space, Path query2012 ACM Subject Classification:
Theory of computation Sorting and searchingAcknowledgements:
I wish to thank Professor Mikkel Thorup for his valuable support as my Master’s Thesis supervisor. I also wish to thank the anonymous reviewers of SWAT 2026 for their meaningful feedback, which helped improve the paper.Funding:
Supported by VILLUM Foundation grant 54451, Basic Algorithms Research Copenhagen (BARC).Editor:
Pierre FraigniaudSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
According to Chan et al. [6], given a multiset , the frequency of an element in , denoted by , is the number of occurrences of the element in . A mode of is an element such that for all , . Given an array of integers, a range mode of is a mode of the multiset determined by the range . Alternatively, one least frequent element in a multiset is an element of minimum multiplicity in .
Generalizing the range mode query problem to trees, we study the problem of building a data structure that, given two nodes , can efficiently find the mode of the multiset of nodes of the path . An alternative to the path mode query is the path least frequent element query, which asks to report any single element on the path of minimal multiplicity.
Historically, there has always been a gap between the query times for the array and tree versions of the mode and least frequent element problems. Chan et al. [6] developed the fastest known linear-space data structure with query time for the array mode query problem, while the time for tree path queries remained . Later, in 2016, Durocher et al. [10] reduced the time on trees down to , for both mode and least frequent element queries. In this paper, we bridge this gap by reducing the time for both path mode queries and least frequent element queries on trees down to , in the context of linear-space data structures.
We also consider a new, more general problem, which we call the Range Maximum -value Color query. By abstracting specific frequency conditions into a generic function , we provide a unified algorithmic framework that simultaneously solves both the mode and least frequent element problems, while also capturing more complex frequency-based metrics. An array of positive integers (the “array of colors”) and a function are given. We require to satisfy several properties, which are also satisfied by the range mode function. A detailed formal definition of these properties is provided in Section 2.
The Range Maximum -value Color problem is to preprocess and the function to efficiently answer queries of the following form:
Given two indices of , determine:
In other words, the query asks to report any single color corresponding to the maximum value of .
We study the generalization of this problem to trees, which we call Path Maximum -value Color, and prove that there exists a linear-space data structure supporting these queries in time, requiring preprocessing time. Because this problem acts as a generalization, it directly reduces the worst-case query time required by the previous linear-space data structures for path mode and path least frequent element queries [10], down to .
We conclude our work by presenting an improvement over the path -minority problem, studied by Durocher et al. [10]. The path -minority query for a tree path asks to report any single color that occurs at most an fraction of the time on the path between nodes and . We prove that by applying a simple randomized algorithm with success probability , their linear-space data structure can support -minority queries in time (where is specified at query time), improving on the previous query time.
We assume the Word RAM model of computation using words of bits, where .
1.1 Related Work and Contribution
Krizanc et al. [13] presented -space data structures that support range mode queries in time on arrays and time on trees. Chan et al. [6] achieved query time with an -space data structure that supports queries in time on arrays. Durocher et al. [10] present an space data structure supporting path mode queries on trees in time. Durocher et al. [10] ask whether the gap between the path mode query time on trees and the range mode query time on arrays can be closed. In this paper, we present the first space data structure that supports path mode queries in query time, thus eliminating the gap between path mode and range mode query times.
In the context of the least frequent element problem, Chan et al. [7] presented an -space data structure that supports range least frequent element queries on arrays in time. Durocher et al. [10] improve the range least frequent element query time down to , and designed the first path least frequent element query data structure for trees, with an query time. In our paper, we present the first linear-space data structure that supports path least frequent element queries on trees in time, thus also closing the gap between the array and tree query times for this problem. A history of previous best query times, achieved by linear-space data structures for mode queries and least frequent element queries, is presented in Tables 1, 2.
| Authors | Array Query Time | Tree Query Time | Source |
|---|---|---|---|
| Krizanc et al. | [13] | ||
| Chan et al. | – | [6] | |
| Durocher et al. | – | [10] | |
| This Paper | – | – |
| Authors | Array Query Time | Tree Query Time | Source |
|---|---|---|---|
| Chan et al. | – | [7] | |
| Durocher et al. | [10] | ||
| This Paper | – | – |
In their work, Durocher et al. [10] present a linear-space data structure that can answer path -minority queries on trees in time, with specified at query time. The path -minority query for a tree path asks to report any color that occurs at most an fraction of the tree path between nodes and . We prove that by applying a simple randomized algorithm with success probability , their linear-space data structure can support -minority queries in time, improving on the previous query time. A history of query times achieved by linear-space data structures for -minority is given in Table 3.
2 Problem Presentation
We begin with a detailed formalization of the main problem studied in this paper, namely, the Path Maximum -value Color problem. We formulated this problem to generalize the path mode query problem as broadly as possible. We also note that we present the first efficient linear-space data structure for the range version of the maximum -value color problem, as this general problem has not been studied previously.
Our study of this problem and the methods used to solve it were inspired by the domain of retrieval/counting problems and array range query problems. A description of retrieval/counting problems is given by Chazelle [8], and some interesting instances of array range query problems can be found in Skala et al. [15]. For clarity, we first present the problem in the context of arrays, and subsequently generalize it to trees.
2.1 The Range Maximum -value Color Problem
An instance of our problem consists of an array of integers (referred to as the “array of colors”) and an integer function . Let . We assume that . If does not fulfill this requirement, can be transformed in time to an array with values compressed to the interval .
The starting point of our study was the range mode function. We developed our data structure using only a subset of the properties of the range and path mode functions. We require to satisfy the following constraints, all of which are shared by the range mode function:
-
1.
Domain: The function takes three integer arguments, where are indices of , and is a color present in the subarray .
-
2.
Output: produces an integer output that fits into a single machine word.
-
3.
Range Contraction: For any valid input ,
where and are the minimum and maximum indices, respectively, such that and . In other words, the value of depends only on the range spanned by the occurrences of within the query interval.
-
4.
Oracle Complexity: We assume can be computed efficiently given a contracted range. Specifically, computing such that takes time. This computation may use an additional -space static data structure.
The Range Maximum -value Color problem is to preprocess and the function to efficiently answer queries of the following form:
Given two indices of , determine:
In other words, determine any single color corresponding to the maximum value of .
2.2 Generalization to trees
This problem generalizes naturally to trees. Instead of an array, we are given a rooted, node-colored tree and a function defined on tree paths. An input to consists of a triple , where are nodes in and is a color occurring on the simple path . From now on, we will consider to be rooted at node .
The properties of remain analogous. Crucially, the Range Contraction property (Property 3) translates as follows:
where is the node on closest to such that , and is the node on closest to such that , where denotes the color of node in the tree .
We refer to this generalized problem as the Path Maximum -value Color problem. This is the main problem we focus on in our work. In the following sections, we present a linear-space data structure supporting path maximum -value color queries in time, requiring preprocessing time.
2.3 Applications of the path maximum g-value color problem
To motivate why this abstract definition is interesting in its own right, we introduce the Range Maximum Sum Color problem. Given a color array and a cost array , we wish to find the color in a range that maximizes the sum of its associated costs. Formally:
This is a specific instance of the maximum -value color problem, as the summation function satisfies all properties of (specifically, the sum of costs for color in range is identical to the sum in the contracted range ).
This problem, again, naturally generalizes to trees; thus, we obtain the Path Maximum Sum Color problem. By using the virtual tree data structure, and Lemma 2 presented in Section 3, we can easily verify that:
| (1) |
where is the lowest common colored ancestor of and (which requires to be rooted), are both -colored nodes, and is the root of the virtual tree of color . We can verify that can be computed in time using relation 1 if we store for each the values in a table of size .
Both on arrays and on trees, the Maximum Sum Color problem acts as a generalization for both the Mode problem (where for all ) and the Least Frequent Element problem (where for all , allowing us to maximize the negated sum).
2.3.1 Practical Example: Financial Time Series
As an additional motivation towards the independent study of the Range Maximum -value Color problem, consider a log of chronologically ordered stock records: “Stock changed by value ”. Storing stock IDs in and changes in , the Maximum Sum Color problem answers: “Which stock had the highest net increase/decrease between time and ?”
Furthermore, our general -value framework allows for more complex queries, such as identifying the stock with the maximum mean fluctuation per record within a time period, provided the mean is calculated over the stock’s records. This demonstrates that the -value framework extends beyond standard frequency queries to elegantly handle cumulative weights and penalties.
2.4 Hierarchy and Contribution
We conclude this section by outlining the difficulty hierarchy. We focus on solving the most general variant: the Path Maximum -value Color problem on trees. We present a linear-space data structure for this problem with a query time of . Remarkably, this matches the query time of the fastest known data structure for the range mode query problem (Chan et al. [6]), which corresponds to the simplest case in our hierarchy.
3 Prerequisites
We mention that we consider the colored tree given as input to be rooted at node . Prior to presenting our solution, we introduce several prerequisite results and definitions. We rely on the following standard results for tree data structures:
Lemma 1 (Bender and Farach-Colton [3]; Berkman et al. [5]; Dietz [9]).
There exists a linear-space data structure supporting level ancestor queries on trees in time, requiring preprocessing time.
Lemma 2 (Bender and Farach-Colton [2]; Berkman and Vishkin [4]).
There exists a linear-space data structure supporting lowest common ancestor (LCA) queries on trees in time, requiring preprocessing time.
3.1 Virtual Trees
We define virtual trees, which are essential for our data structure.
Definition 3.
Given a colored tree and a color , the virtual tree of color , denoted , is a rooted tree defined over the set of nodes .
The structure of is defined by the following parent relationship: for any node , the parent of in is the nearest proper ancestor of in that also has color .
To ensure is a single connected tree (rather than a forest), we introduce a virtual root node for each color . If a node of color has no ancestors of color in , we set its parent in to be .
It is possible to construct all virtual trees for all colors in total time using a single depth-first traversal of . Since the node sets for each color form a partition of the colored nodes in , the total space required is linear, . We defer the algorithm for computing all the virtual trees to the full version of this paper, available as a preprint on arXiv [14].
By applying standard LCA and Level Ancestor structures (Lemmas 1 and 2) to these virtual trees, we obtain the following corollary:
Corollary 4.
There exists a linear-space data structure, constructed in time, that answers the following queries in time:
-
1.
Given two nodes of the same color , determine the lowest common ancestor of and with respect to color (i.e., the LCA of and in ).
-
2.
Given a node of color and an integer , determine the -th ancestor of of color (i.e., the -th ancestor of in ).
Proof.
For each distinct color present in , we explicitly construct the virtual tree . Over each , we build instances of the data structures described in Lemmas 1 and 2.
Let . The construction and preprocessing for a specific color takes time and space. Summing over all colors, the total preprocessing time is , and the total space is similarly . The queries are answered by querying the specific structure for in time.
4 Blocking Technique
Krizanc et al. [13], Chan et al. [6, 7], and Durocher et al. [10] successfully employed blocking techniques to solve non-trivial frequency queries on arrays and trees. We adapt their framework to develop a multi-level blocking strategy, allowing us to achieve the desired query time for path maximum -value color queries on trees.
Lemma 5 (Durocher et al. [10]).
Let be a tree with nodes, and let be an integer parameter (the blocking factor). There exists a subset of marked nodes and an associated rooted tree with vertex set , satisfying the following properties:
-
1.
Size: ;
-
2.
LCA Closure: is closed under the lowest common ancestor operation (i.e., for any , the lowest common ancestor of and in is also in );
-
3.
Gap Size: Any simple path in consisting entirely of unmarked nodes has length at most .
Two nodes are adjacent in if and only if the simple path between them in contains no other nodes from . The set and the tree can be constructed in time.
Lemma 6 (Durocher et al. [10]).
Given a tree with nodes, an integer , and a set of marked nodes (obtained according to Lemma 5), there exists a partition of the vertex set , denoted by , satisfying the following properties:
-
1.
Block Count: The number of blocks is ;
-
2.
Block Size: For each block , the number of nodes is ;
-
3.
Connectivity: For each block , the subgraph of induced by the nodes in is connected.
The partition can be computed in time.
We defer the full details of the proofs of Lemmas 5 and 6 to the full version of this paper [14]. Further, for our approach, we require a multilevel partition of the nodes of . From Lemma 6, we derive the following corollary, which formalizes the construction of a hierarchical partition.
Corollary 7.
Given a colored tree , two blocking factors , and the set of marked nodes with its associated tree (obtained according to Lemma 5), there exists a subset and a partition of , and a tree .
These can be computed in time and satisfy the following properties:
-
1.
Consistency: The set is a valid set of marked nodes for with blocking factor (satisfying all properties of Lemma 5).
-
2.
Relative Gap: Any simple path in the compressed tree consisting entirely of nodes from has length .
-
3.
Partition Structure: The partition divides the nodes of into blocks, each of size , such that the subgraph of induced by each block is connected.
Proof.
We apply the construction procedures from Lemma 5 and Lemma 6 directly to the compressed tree , treating it as the input tree with a blocking factor of . Since , the construction requires time. The resulting marked nodes and partition satisfy the size and connectivity properties with respect to by definition. Furthermore, since preserves the LCA closure property of , and is constructed to preserve LCA closure on , maintains the necessary topological properties for .
Crucially, the partition of implicitly defines a coarser partition of the original vertex set . A high-level block consists of a set of connected low-level blocks from . To enumerate the nodes of a high-level block , we identify the corresponding block of marked nodes (where ) and iterate through the low-level block associated with each .
Proof.
By Property 3 of Corollary 7, the subgraph of induced by any block is connected. Since is a tree, any connected subgraph is also a tree. To construct these explicit tree representations, we iterate through the edges of . For each edge , if both and belong to the same block , we add the edge to . This traversal visits every edge exactly once, resulting in a total runtime of . We refer to as the block tree of . We depict an example of a -level block partition in Figure 2.
We define the notions of home blocks for nodes and node representatives for blocks.
Definition 9 (Home Blocks and Representatives).
For any node and level , let denote the unique block in the partition that contains . We assume each block is associated with a unique representative node in .
This definition allows us to examine a nested structure around a node . The node resides in a local block , which is nested within , which is further nested within and so on.
4.1 Parameter Selection
For our specific data structure, we employ a -level partition of . We assume the first blocking factor is provided as a parameter. We then define the higher-level factors as follows:
During the preprocessing phase, we compute the associated structures , , and for . Specifically, we construct the full block partitions using the hierarchical method described in Corollary 7. Based on the established results, this entire preprocessing step requires time.
5 Overview and Strategy
Having established the prerequisites, we now provide a high-level overview of our query answering strategy. Our approach refines the blocking technique of Durocher et al. [10]. In their solution, the query algorithm identifies a set of candidate nodes. For every distinct color appearing in this set of nodes, they perform a frequency verification step taking time.
To improve upon this, we introduce a hierarchical candidate selection process. Instead of a single set, we identify two distinct sets of candidate nodes, denoted and . These sets act as proxies for the colors we need to verify:
-
Primary Candidate Nodes (): A small set of nodes (). For every color appearing in , we perform a standard verification taking time.
-
Secondary Candidate Nodes (): A larger set of nodes (). For every color appearing in , we store a triple , where and are the first occurrences of , when traversing the path in the direction and , respectively. We need only time to compute for one such color.
This split allows us to process a larger total number of candidate nodes (and thus potential colors) without degrading the overall query complexity.
5.1 Decomposition into Disjoint Subtasks
To efficiently identify these candidate nodes, we decompose the query based on the hierarchy of blocks defined in Section 4. We decompose the problem into disjoint subtasks by partitioning the set of distinct colors present on the query path. Each subtask identifies a unique subset of colors based on their presence or absence in the hierarchical blocks surrounding and .
Let denote the set of all distinct colors present on the query path. For any block , let denote the set of distinct colors present in that block.
We define the complement of a block’s color set with respect to the entire tree. Let be the universe of all distinct colors in the tree . We define:
This represents the set of all colors in the tree that are not present in block .
The set of path colors is partitioned into the following disjoint subsets using intersection operations on these color sets:
-
1. The Global Colors: .
Colors on the path that are not present in the Level 3 blocks of either endpoint.
-
2. The Level 3 Ascent: .
Colors on the path that are in ’s Level 3 block, but not in ’s Level 2 block or ’s Level 3 block.
-
3. The Level 2 Ascent: .
Colors on the path that are in ’s Level 2 block, but not in ’s Level 1 block or ’s Level 3 block.
-
4. The Level 3 Descent: .
Symmetric to .
-
5. Level 3 Intersection: .
Colors on the path present in both Level 3 blocks, but in neither Level 2 block.
-
6. Mixed Intersection: .
Colors on the path present in ’s Level 2 block and ’s Level 3 block, excluding inner blocks.
-
7. The Level 2 Descent: .
Symmetric to .
-
8. Mixed Intersection: .
Symmetric to .
-
9. Level 2 Intersection: .
Colors on the path present in both Level 2 blocks, excluding both Level 1 blocks.
-
10. Local Colors: .
Colors on the path present in the smallest (Level 1) blocks of either endpoint.
In the subsequent analysis, we will omit the subtasks marked as symmetric to others (e.g., , , ), as their solutions are algorithmically identical to their counterparts, differing only in orientation.
For each of the subtasks 4-9, we will identify sets of candidate nodes of size , together with the corresponding endpoints , and add them to the set . For subtasks and , we will identify sets of candidates of size or , and add them to . At the end, we process the sets and , in time and , respectively, and return the color with the largest -value. Since , processing and takes in total.
5.1.1 Proof of Completeness
We prove that these subtasks partition . Consider any color . Based on the block hierarchy, we can determine the membership of in the color sets of the blocks surrounding and . Let be the proposition and be the proposition for . Since , we have . The definitions of through correspond precisely to the disjoint logical conjunctions of these propositions (e.g., corresponds to ). The set captures the remaining cases where the color is present in or . Thus, every color satisfies exactly one of these conditions, ensuring that and all are disjoint.
6 Solving the Subtasks
In this section, we present the algorithmic solutions for the subtasks defined previously. While the query procedures are concise, the precomputation strategies vary in complexity across subtasks; we sketch the data structures here, deferring full construction details to the full version of this paper [14]. We first state a key result from Durocher et al. [10] that enables efficient verification for our primary candidate set .
Lemma 10 (Durocher et al. [10]).
There exists an -space data structure that supports lowest colored ancestor queries on trees in time.
To verify any , we must evaluate , which requires identifying the first and last occurrences of on . Using the lowest colored ancestor structure from Lemma 10, we locate these endpoints in time, and compute in .
We analyze the space complexity, query time, and preprocessing time for each subtask. Note that any candidate color identified in these subtasks is added to the candidate set (verification taking time) or (verification taking time), as per the strategy in Section 5.
6.1 Solving Subtask 1: The Global Region
We precompute a table of size , indexed by the identifiers of the blocks in . For every pair of blocks with representatives , the entry stores the color that maximizes , restricted to colors appearing on the path but not in . During a query, we identify the blocks and , retrieve the candidate in time, and add it to the primary candidate set . The table construction requires preprocessing time and space, measured in words.
Complexity.
Preprocessing ; Space words; Query Time .
6.2 Solving Subtask 2: The Level 3 Ascent
We employ a lookup table of size , indexed by pairs representing a Level 2 block and a Level 3 block. The entry stores the relative index (using bits) of a specific Level 1 block (or -node) inside that contains at least one occurrence of the optimal color. During a query, we iterate through every distinct color in this retrieved -node’s subtree and add each to .
Complexity.
Preprocessing ; Space bits; Query Time .
6.3 Solving Subtask 3: The Level 2 Ascent
This solution is analogous to Subtask 2. We use a table of size . For a pair of blocks , (Level 1 and Level 3), stores the index of a -node inside containing the optimal color. All colors in that -node are added to .
Complexity.
Preprocessing ; Space bits; Query Time .
6.4 Prerequisites for Subtasks 5, 6 and 9
To efficiently solve the intersection subtasks, we require advanced data structures that allow us to locate specific color occurrences within the block hierarchy in constant time. We refer to trees with size bounded by as small trees.
Lemma 11.
There exists an -space data structure, constructible in time, that answers the following query in time: Given a small tree , a binary string of length , and a node , return the lowest ancestor of marked with in . If no such ancestor exists, return a marked node such that no ancestor of (other than potentially ) is marked.
Proof.
We employ the tabulation method. We precompute answers for all possible canonical instances of small trees. The number of distinct rooted trees with size up to is bounded by . For each tree topology , there are possible bitstrings . Thus, the total number of distinct query inputs is bounded by:
We construct a lookup table indexed by the canonical ID of , the bitstring , and the node index . We populate using a standard DFS traversal for each instance. During the query phase, we map the input small tree to its canonical ID and retrieve the answer from in time. The total space and preprocessing time are sublinear.
Lemma 12.
Given a colored tree and blocking factors with , there exists a linear-space perfect hashing structure (preprocessed in expected time) that answers the following in time: Given color and -block , return a binary string of length indicating which -blocks inside contain .
Proof.
We construct a dictionary of valid pairs. Let be the set of triples , where is a color present in the -block , and is the bitmask of length representing the presence of in the constituent -blocks. The size of is bounded by because each node in belongs to exactly one -block, contributing exactly one bit to one mask in the entire structure. We store using a static perfect hashing scheme (e.g., Fredman, Komlós and Szemerédi [11], Hagerup and Torben [12], or Belazzougui, Botelho and Dietzfelbinger [1]), allowing worst-case lookups to retrieve for a pair , or returning null if .
Combining these lemmas with the block tree definitions, we obtain the following result for locating path endpoints.
Corollary 13.
There exists a linear-space data structure that, given endpoints , blocks (), and a color present in the blocks but not in their predecessors , determines the first occurrence of on the path in time.
Proof.
We assume the data structures from Lemmas 11 and 12 are constructed. Without loss of generality, we focus on finding the first occurrence of in closest to .
First, we query the hashing structure from Lemma 12 with to retrieve the bitmask . Treating the hierarchy of -blocks inside as a small tree , we use the structure from Lemma 11 with start node to find the lowest marked ancestor block . This block is the -block closest to that contains .
We distinguish two cases based on the topological relationship between and , as illustrated in Figure 3.
| (a) Case 1 | (b) Case 2 |
Case 1: is not an ancestor of .
Here, the path to must pass through the LCA of the blocks. We maintain a precomputed table storing the topmost occurrence of every color in every -block. Let this node be . We compute the immediate colored ancestor of , denoted . If is a proper ancestor of , we locate the first occurrence by finding the occurrence on the path from (say ) and performing a level ancestor query for the node at distance . To solve this case, we require an additional linear-space perfect hashing data structure to determine the topmost occurrence of a color inside a block.
Case 2: is an ancestor of .
Here, the path traverses intermediate blocks that do not contain . By the LCA Closure property, the relevant occurrence lies on an edge connecting blocks. We maintain a secondary perfect hashing structure storing pairs , where is a block that does not contain , but the edge connecting to its parent block contains an occurrence of . If is the found block, we compute the depth difference between and , jump to the -th ancestor block (the child of on the path), and query the secondary hash table for to find the occurrence on the entering edge.
The perfect hashing data structure for determining the occurrence of color on the edge from a block to its parent block, requires additional words of space, because each occurrence of a color , appears in at most one path connecting to its parent block, according to the LCA Closure property.
We must note that the same approach, with slight modifications, can be used to determine the desired nodes at which a color occurs, even in the degenerate case, where the block is an ancestor of or vice versa.
6.5 Solving Subtask 5: Level 3 Intersection
We apply a stratified blocking approach. We define a sequence of blocking factors such that , , and . Note that .
For each blocking factor , let be the set of blocks which partitions . For an block , let denote the list of all nodes from , contained inside block , ordered in increasing order of the node index. We construct tables for each level . For every configuration of blocks , we store the approximate position (using the most significant bits) of the optimal color within the linearized list of nodes of the blocks . The total space required is:
where . The query iterates through candidate colors suggested by these tables. For each candidate , we verify it is in using Lemma 12 in time, find its endpoints using Corollary 13, and add to .
6.6 Solving Subtasks 6, 9, and 10
Subtask 6 (Mixed Level 2/3).
Subtask 9 (Level 2 Intersection).
Analogous to Subtask 6; runtime is .
Subtask 10 (Local).
We iterate through every color in and add them directly to (verified in ). The cost is .
Finally, to determine the actual answer to the query, we need to iterate through the colors inside , find their endpoints on the path by using the time query from Lemma 10, and compute the associated value of . Then, we iterate through the triplets , and compute . The answer to the query for the path is the color corresponding to the maximum value of computed in this step.
Processing will take time, and processing will take time. Since , the total time of processing the sets of candidates and is .
We will present the preprocessing algorithms and analyze their respective runtime for each data structure in the full version of this paper [14]. Now we only mention that a preprocessing runtime of can be achieved. The total space required by our data structure is bits. By substituting by , we formalize our result as follows.
Lemma 14.
Given a colored tree , of nodes, a function respecting the properties from Section 2, and a blocking factor , there exists a data structure, supporting path maximum -value color queries in time, requiring space in bits, and an preprocessing time.
By setting , while keeping the values of and the same as a function of , we conclude this section with our main result directly following Lemma 14.
Theorem 15.
Given a colored tree , of nodes, a function respecting the properties from Section 2, there exists a linear-space data structure, supporting path maximum -value color queries in time, requiring an preprocessing time.
7 Faster Path -Minority Queries on Trees
An -minority in a multiset , for some parameter , is defined as an element that occurs at least once in but constitutes no more than an fraction of the total size of . The path -minority query for a tree path asks to report any single color that occurs at most an fraction of the tree path between nodes and . Durocher et al. [10] introduced a linear-space data structure capable of answering path -minority queries on trees in time, where the parameter is specified at query time, and can differ for every query. In this section, we show that by incorporating randomness, the query time can be improved to for a Monte Carlo algorithm (success probability ) and to expected for a Las Vegas algorithm (success probability ).
7.1 Time Algorithm with Success Probability
We use the following result from Durocher et al. [10] to efficiently retrieve candidate colors near the endpoints of the path.
Lemma 16 (Durocher et al. [10]).
There exists an -space data structure that supports -nearest distinct ancestor queries on trees in time. The query returns the distinct colors closest to a node on the path to the root, in order of increasing distance.
Let be the query path, and let . We define the length of the path as . We propose the following -phase query algorithm:
-
Phase 1 (Candidate Collection): We retrieve the sets of distinct colors closest to the endpoints. Let .
Note that if the path segment contains fewer than distinct colors, will simply contain all distinct colors on that segment (and similarly for ).
-
Phase 2 (Pruning Majorities): We remove obvious majorities. For each , if the frequency of on the sub-path exceeds , we remove from . We perform the symmetric check for on . This is valid because if a color is a majority on a sub-path with frequency exceeding the threshold for the entire path, it is certainly an -majority for the whole path.
-
Phase 3 (Exact Verification of Overlap): Identify colors . For these colors, we have identified both their first occurrence from and their first occurrence from . Since the sets and are built searching inwards from the endpoints, if appears in both, we have bounded its occurrences. We can compute the exact frequency of on in time (using the precomputed frequency data structures). If the frequency is , we return immediately as the answer. We remove all colors from and respectively. Thus, at the end of this phase, .
-
Phase 4 (Random Sampling): If no answer was returned in Phase 3, we construct the set of remaining candidates . We select a color uniformly at random from and return it.
Lemma 17.
The query algorithm returns an -minority in time with probability .
Proof.
The time complexity is dominated by the retrieval of colors and the set operations, which take time. The frequency checks in Phase 2 and 3 take using the standard Level Ancestor and precomputed depth/frequency arrays (as employed in the virtual tree structures).
We analyze the success probability in two cases:
-
1.
Case 1: The path has few distinct colors. Suppose the number of distinct colors on is at most . In this scenario, and effectively cover the entire path. Any color present on the path is in . Specifically, any -minority is in this set. Thus, the algorithm succeeds with probability .
-
2.
Case 2: The path has many distinct colors. Suppose there are more than distinct colors. The number of -majorities (colors with frequency ) is strictly less than . Our candidate set has size up to . Even in the worst case where every -majority is present in , there are at most “bad” candidates. The number of “good” candidates (-minorities) in is at least . Therefore, the probability of selecting an -minority at random is at least:
7.2 Expected Time Algorithm
To achieve a success probability of , we extend Phase 4 into a Las Vegas algorithm. Instead of returning the random candidate immediately, we verify it. We can determine the exact endpoints of on using an -time predecessor/successor query (using the structure from Lemma 10). Once the endpoints are known, calculating the frequency takes time. We repeatedly pick a color at random from , and verify its frequency, until an -minority is found.
Since the probability of success in one sampling step is at least , the number of trials follows a geometric distribution with success parameter . The expected number of trials is at most . The total expected time complexity is:
References
- [1] Djamal Belazzougui, Fabiano C Botelho, and Martin Dietzfelbinger. Hash, displace, and compress. In European Symposium on Algorithms, pages 682–693. Springer, 2009. doi:10.1007/978-3-642-04128-0_61.
- [2] Michael A Bender and Martin Farach-Colton. The lca problem revisited. In Latin American Symposium on Theoretical Informatics, pages 88–94. Springer, 2000. doi:10.1007/10719839_9.
- [3] Michael A Bender and Martın Farach-Colton. The level ancestor problem simplified. Theoretical Computer Science, 321(1):5–12, 2004. doi:10.1016/J.TCS.2003.05.002.
- [4] Omer Berkman and Uzi Vishkin. Recursive star-tree parallel data structure. SIAM Journal on Computing, 22(2):221–242, 1993. doi:10.1137/0222017.
- [5] Omer Berkman and Uzi Vishkin. Finding level-ancestors in trees. Journal of computer and System Sciences, 48(2):214–230, 1994. doi:10.1016/S0022-0000(05)80002-9.
- [6] Timothy M Chan, Stephane Durocher, Kasper Green Larsen, Jason Morrison, and Bryan T Wilkinson. Linear-space data structures for range mode query in arrays. Theory of Computing Systems, 55(4):719–741, 2014. doi:10.1007/S00224-013-9455-2.
- [7] Timothy M Chan, Stephane Durocher, Matthew Skala, and Bryan T Wilkinson. Linear-space data structures for range minority query in arrays. In Scandinavian Workshop on Algorithm Theory, pages 295–306. Springer, 2012. doi:10.1007/978-3-642-31155-0_26.
- [8] Bernard Chazelle. Filtering search: A new approach to query-answering. SIAM Journal on Computing, 15(3):703–724, 1986. doi:10.1137/0215051.
- [9] Paul F Dietz. Finding level-ancestors in dynamic trees. In Workshop on Algorithms and Data Structures, pages 32–40. Springer, 1991. doi:10.1007/BFB0028247.
- [10] Stephane Durocher, Rahul Shah, Matthew Skala, and Sharma V Thankachan. Linear-space data structures for range frequency queries on arrays and trees. Algorithmica, 74:344–366, 2016. doi:10.1007/S00453-014-9947-8.
- [11] Michael L. Fredman, János Komlós, and Endre Szemerédi. Storing a sparse table with 0(1) worst case access time. J. ACM, 31(3):538–544, June 1984. doi:10.1145/828.1884.
- [12] Torben Hagerup and Torsten Tholey. Efficient minimal perfect hashing in nearly minimal space. In STACS 2001: 18th Annual Symposium on Theoretical Aspects of Computer Science Dresden, Germany, February 15–17, 2001 Proceedings 18, pages 317–326. Springer, 2001. doi:10.1007/3-540-44693-1_28.
- [13] Danny Krizanc, Pat Morin, and Michiel Smid. Range mode and range median queries on lists and trees. Nordic Journal of Computing, 12(1):1–17, 2005.
- [14] Ovidiu Rata. Faster linear-space data structures for path frequency queries, 2026. arXiv:2604.18667.
- [15] Matthew Skala. Array Range Queries, pages 333–350. Springer Berlin Heidelberg, Berlin, Heidelberg, 2013. doi:10.1007/978-3-642-40273-9_21.
