Background
Given a rooted, binary tree $T$ with leaves bijectively labeled by $\{1, \ldots, n\}$ (a "phylogenetic tree"). Let $L \subseteq \{1, \ldots, n\}$, and $|L| = k$. The homeomorphic subtree $T_{|L}$ can be defined as the spanning tree of $L$ on $T$ (which will be a subgraph of $T$), and then contract all (non-root) degree-two nodes. This concept of homeomorphic subtrees AFAIK is primarily used in computational evolutionary biology.
E.g., given tree in Newick notation $T = ((1,(2,3)),(4,5));$, The subtree $T_{|1,2,4} = ((1,2),4);$. It would be incorrect to state that the subtree is $((1,2),(4))$, which contains a non-root degree-two node.
My Algorithm
Now, I discovered a $\langle O(n), f(k) \rangle$ algorithm to extract homeomorphic subtrees from an ambient tree $T$ (i.e., preprocesses $T$ in $O(n)$ time and allows $T_{|L}$ queries to be completed in $f(k)$ time), where $f(k)$ is the time to sort $k$ integers in the range $[0, n)$. The algorithm works on a static ($T$ does not change), online (we don't know $L$ before-hand) setting.
The algorithm proceeds as:
Build Stage
- preprocess $T$ by equipping it an LCA structure to allow querying $depth(u)$ and $lca(u,v)$ on arbitrary nodes in $O(1)$ time (using data structures such as from Farach-Colton and Bender)
- DFS $T$ (from root; equivalently pre-order traverse $T$) and obtain $T$'s leaves, i.e., $\{1, \ldots, n\}$, in DFS order. Call this sequence of leaves the DFS order of ambient leaves.
Both steps take $O(n)$ time. Preprocessing thus takes linear time.
Query Stage
Recall that the query input is $L \subseteq \{1, \ldots, n\}$ where $|L| = k$. Our goal is to obtain $T_{|L}$.
- Sort $L$ in the order of DFS on the ambient leaves (result of (2) in the build stage). We treat $L$ as an ordered sequence from here on. [This takes integer sorting time, realistically $O(k \lg k)$]
- Construct the sequence $L' = [L_1, \color{blue}{lca(L_1, L_2)}, L_2, \ldots, \color{blue}{lca(L_{k-1},L_k)}, L_k]$. Observe that $|L'| = 2 k - 1$ and touches all nodes of $T_{|L}$
- Construct the cartesian tree on $L'$ w.r.t. the depth of nodes. The cartesian tree is what we want; it is $T_{|L}$.
Query stage's step (1) dominates the running time by sorting.
Reference Request
I am somewhat inclined to write a paper/preprint about this and actually provide proofs, but I was not able to find related results (it is well known that quartet/quintet trees, e.g., in the case of $k \le 5$ can be handled in $\langle O(n), O(1) \rangle$ time, which are more specific cases of my algorithm here) despite many searches on Google/Google Scholar.
Does anyone know prior published results about this (so maybe to spare my embarrassment of writing something that people have already published about)?