Abstract Finding the most frequent value (mode) in hierarchical data is a critical task in domains ranging from file systems to phylogenetics. While the general Range Mode problem on arrays is computationally expensive ($O(N\sqrt{N})$), this paper introduces a time-optimal $O(N)$ algorithm for the Subtree Mode (SM) problem. By exploiting the laminar property of trees, the proposed Splitting-Counting-Merging (SCM) algorithm scales to datasets with billions of nodes, outperforming existing baselines by an order of magnitude.
Introduction
Finding the "mode"—the most frequent value in a set—is a fundamental task in data science. While simple for a static list, it becomes notoriously difficult when querying the mode for arbitrary ranges within a massive dataset. The classic "Range Mode" problem, which seeks the most frequent element in a specific window of an array, has long been a bottleneck in data structures. Despite decades of research, the best algorithms still take roughly $O(N\sqrt{N})$ time, with theoretical evidence suggesting this may be near the lower bound for general arrays.
However, many real-world datasets are hierarchical rather than flat. Examples include file systems, where one might tag a folder with its most common file type, or phylogenetic trees in biology, where researchers identify dominant genetic traits within specific lineages.
This paper addresses the Subtree Mode (SM) problem: finding the most frequent leaf color in every subtree of an $N$-node tree. The authors demonstrate that the nested, or "laminar," property of subtrees—where any two subtrees are either disjoint or one is contained within the other—allows for a linear-time solution that the general Range Mode problem lacks.
The SCM Algorithm
The authors propose the SCM (Splitting-Counting-Merging) algorithm, which achieves a total runtime of $O(N)$ by breaking the problem into three distinct phases:
- Splitting: The input tree $T$ is decomposed into a forest of single-color trees. For each color present in the leaves, a smaller tree is constructed containing only the nodes necessary to represent the paths to those specific leaves.
- Counting: In each single-color tree, the algorithm performs a bottom-up traversal to count the number of leaf descendants each node has for that specific color.
- Merging: This is the core innovation. The algorithm performs a final bottom-up traversal of the original tree. For each node, it combines frequency information from its children with the specific color counts derived in the previous step.
To maintain efficiency, the algorithm utilizes Lowest Common Ancestor (LCA) queries. By pre-calculating LCAs, the algorithm can jump directly to nodes where a specific color's frequency changes, avoiding redundant work and ensuring the $O(N)$ time complexity.
Experimental Evaluation
The researchers stress-tested the SCM algorithm on four diverse real-world datasets: WebKB (university webpages), Genes (human X chromosome DNA), News (text documents), and Vir (viral genomes). The Vir dataset resulted in a suffix tree with over 7.3 billion nodes, serving as a massive test for scalability.
Key Results
- Scalability: On the 7.3-billion-node dataset, the SCM algorithm finished in less than 20 minutes, while the most efficient baseline required roughly 6.5 hours.
- Speed and Memory: SCM was consistently 10x to 40x faster than the best $O(N \log \Delta)$ baselines and used 26% to 58% less memory.
- Practical Utility: In a case study on SARS-CoV-2, the algorithm distinguished viral sequences from human DNA with 100% accuracy, running up to 30 times faster than state-of-the-art Random Forest classifiers.
Conclusion
The transition from flat arrays to hierarchical trees transforms the mode problem from a sub-linear bottleneck into a linear-time success. The authors also provide a "conditional lower bound" proof showing that if the structure moves from a tree to a Directed Acyclic Graph (DAG), the problem becomes as hard as Boolean Matrix Multiplication, confirming that the tree structure is the essential component for this speedup.
Future work will focus on dynamic Subtree Mode, aiming to handle insertions, deletions, and color changes efficiently without requiring a full re-computation of the data structure.
Summary updated