text:
# Cavity Matchings, Label Compressions, and Unrooted Evolutionary TreesA preliminary version appeared as part of General techniques for comparing unrooted evolutionary trees, in Proceedings of the 29th Annual ACM Symposium on Theory of Computing, 1997, pp. 54–65, and part of All-cavity Maximum Matchings, in Proceedings of the 8th Annual International Symposium on Algorithms and Computation, 1997, pp. 364-373.

## 1 Introduction

An evolutionary tree is one whose leaves are labeled with distinct symbols representing species. Evolutionary trees are useful for modeling the evolutionary relationship of species . An agreement subtree of two evolutionary trees is an evolutionary tree that is also a topological subtree of the two given trees. A maximum agreement subtree is one with the largest possible number of leaves. Different models about the evolutionary relationship of the same species may result in different evolutionary trees. A fundamental problem in computational biology is to determine how much two models of evolution have in common. To a certain extent, this problem can be solved by computing a maximum agreement subtree of two given evolutionary trees .

Algorithms for computing a maximum agreement subtree of two unrooted evolutionary trees as well as two rooted trees have been studied intensively in the past few years. The unrooted case is more difficult than the rooted case. There is indeed a linear-time reduction from the rooted case to the unrooted one, but the reverse is not known. Steel and Warnow  gave the first polynomial-time algorithm for unrooted trees, which runs in $`O(n^{4.5}\mathrm{log}n)`$ time. Farach and Thorup reduced the time to $`O(n^{2+o(1)})`$ for unrooted trees  and $`O(n^{1.5}\mathrm{log}n)`$ for rooted trees . For the unrooted case, the time was improved by Lam, Sung and Ting  to $`O(n^{1.75+o(1)})`$. Algorithms that work well for rooted trees with degrees bounded by a constant have also been revealed recently. The algorithm of Farach, Przytycka and Thorup  takes $`O(n\mathrm{log}^3n)`$ time, and that of Kao  takes $`O(n\mathrm{log}^2n)`$ time. Cole and Hariharan  gave an $`O(n\mathrm{log}n)`$-time algorithm for the case where the input is further restricted to binary rooted trees.

This paper presents an algorithm for computing a maximum agreement subtree of two unrooted trees. It takes $`O(n^{1.5}\mathrm{log}n)`$ time for trees with unbounded degrees, matching the best known time complexity for the rooted case . If the degrees are bounded by a constant, the running time is only $`O(n\mathrm{log}^4n)`$. We omit the details of this reduction since Przytycka  recently devised an $`O(n\mathrm{log}n)`$-time algorithm for the same case.

Our algorithm allows the input trees to be mixed trees, i.e., trees that may contain directed and undirected edges at the same time . Such trees can handle a broader range of information than rooted and unrooted trees. To simplify the discussion, this paper focuses on unrooted trees. Our subtree algorithm adopts a conceptually simple recursive strategy exploiting a novel technique called label compression. This technique enables our algorithm to process overlapping subtrees iteratively while keeping the total tree size very close to the original input size. Label compression builds on an unexpectedly fast algorithm for the all-cavity maximum weight matching problem , which asks for the weight of a maximum weight matching in $`G\{u\}`$ for each node $`u`$ of a bipartite graph $`G`$ with integer edge weights. If $`G`$ has $`n`$ nodes, $`m`$ edges and maximum edge weight $`N`$, the algorithm takes $`O(\sqrt{n}m\mathrm{log}(nN))`$ time, which matches the best known time bound for computing a single maximum weight matching of $`G`$ due to Gabow and Tarjan .

In §2, we solve the all-cavity matching problem. In §3, we formally define maximum agreement subtrees and outline our recursive strategy for computing them. We describe label compression in §4, detail our subtree algorithm in §5, and discuss how to compute auxiliary information for label compression in §6 and §7. We conclude by extending the subtree algorithm to mixed trees in §8.

## 2 All-cavity maximum weight matching

Let $`G=(X,Y,E)`$ be a bipartite graph with $`n`$ nodes and $`m`$ edges where each edge $`(u,v)`$ has a positive integer weight $`w(u,v)N`$. Let $`\text{mwm}(G)`$ denote the weight of a maximum weight matching in $`G`$. The all-cavity matching problem asks for $`\text{mwm}(G\{u\})`$ for all $`uXY`$. A naive approach to solve this problem is to compute $`\text{mwm}(G\{u\})`$ separately for each $`u`$ using the fastest algorithm for computing a single maximum weight matching , thus taking $`O(n^{1.5}m\mathrm{log}(nN))`$ total time. A main finding of this paper is that the matchings in different subgraphs $`G\{u\}`$ are closely related and can be represented succinctly. From this representation, we can solve the problem in $`O(\sqrt{n}m\mathrm{log}(nN))`$ time. By symmetry, we only detail how to compute $`\text{mwm}(G\{u\})`$ for all $`uX`$. Below we assume $`mn/2`$; otherwise, we remove the degree-zero nodes and work on the smaller resultant graph.

A node $`v`$ of $`G`$ is matched by a matching of $`G`$ if $`v`$ is an endpoint of an edge in the matching. In the remainder of this section, let $`M`$ be a fixed maximum weight matching of $`G`$; also let $`w(H)`$ be the total weight of a set $`H`$ of edges. An alternating path is a simple path $`P`$ in $`G`$ such that (1) $`P`$ starts with an edge in $`M`$, (2) the edges of $`P`$ alternate between $`M`$ and $`EM`$, and (3) if $`P`$ ends at an edge $`(u,v)M`$, then $`v`$ is not matched by $`M`$. An alternating cycle is a simple cycle $`C`$ in $`G`$ whose edges alternate between $`M`$ and $`EM`$. $`P`$ (respectively, $`C`$) can transform $`M`$ to another matching $`M^{}=PMPM`$ (respectively, $`CMCM`$). The net change induced by $`P`$, denoted by $`\mathrm{\Delta }(P)`$, is $`w(M^{})w(M)`$, i.e., the total weight of the edges of $`P`$ in $`EM`$ minus that of the edges of $`P`$ in $`M`$. The net change induced by $`C`$ is defined similarly.

The next lemma divides the computation of $`\text{mwm}(G\{u\})`$ into two cases.

###### Lemma 1.

Let $`uX`$.

1. If $`u`$ is not matched by $`M`$, then $`M`$ is also a maximum weight matching in $`G\{u\}`$ and $`\text{mwm}(G\{u\})=\text{mwm}(G)`$.
2. If $`u`$ is matched by $`M`$, then $`G`$ contains an alternating path $`P`$ starting from $`u`$, which can transform $`M`$ to a maximum weight matching in $`G\{u\}`$.

###### Proof.

Statement 1 is straightforward. To prove Statement 2, let $`M^{}`$ be a maximum weight matching in $`G\{u\}`$. Consider the edges in $`MM^{}MM^{}`$. They form a set $`S`$ of alternating paths and cycles. Since $`u`$ is matched by $`M`$ but not by $`M^{}`$, $`u`$ is of degree one in $`MM^{}MM^{}`$. Let $`P`$ be the alternating path in $`S`$ with $`u`$ as an endpoint. Let $`M^{\prime \prime }`$ be the matching obtained by transforming $`M`$ only with $`P`$. Since $`u`$ is not matched by $`M^{\prime \prime }`$, $`M^{\prime \prime }`$ is a matching in $`G\{u\}`$. $`M^{}`$ can be obtained by further transforming $`M^{\prime \prime }`$ with the remaining alternating paths and cycles in $`S`$. The net change induced by each of these alternating paths and cycles is non-positive; otherwise, such a path or cycle can improve $`M`$ and we obtain a contradiction. Therefore, $`w(M^{\prime \prime })w(M^{})`$, i.e., both $`M^{}`$ and $`M^{\prime \prime }`$ are maximum weight matchings in $`G\{u\}`$. ∎

By Lemma 1(2), we can compute $`\text{mwm}(G\{u\})`$ for any $`uX`$ matched by $`M`$ by finding the alternating path starting from $`u`$ with the largest net change. Below we construct a directed graph $`D`$, which enables us to identify such an alternating path for every node easily. The node set of $`D`$ is $`XY\{t\}`$, where $`t`$ is a new node. The edge set of $`D`$ is defined as follows; see Figure 1 for an example.

* If $`xX`$ is not matched by $`M`$, $`D`$ has an edge from $`x`$ to $`t`$ with weight zero.
* If $`yY`$ is matched by $`M`$, $`D`$ has an edge from $`y`$ to $`t`$ with weight zero.
* If $`M`$ has an edge $`(x,y)`$ where $`xX`$ and $`yY`$, $`D`$ has an edge from $`x`$ to $`y`$ with weight $`w(x,y)`$.
* If $`EM`$ has an edge $`(x,y)`$ where $`xX`$ and $`yY`$, $`D`$ has an edge from $`y`$ to $`x`$ with weight $`w(x,y)`$.

Note that $`D`$ has $`n+1`$ nodes and at most $`n+m`$ edges. The weight of each edge in $`D`$ is an integer in $`[N,N]`$.

###### Lemma 2.

1. $`D`$ contains no positive-weight cycle.
2. Each alternating path $`P`$ in $`G`$ that starts from $`uX`$ corresponds to a simple path $`Q`$ in $`D`$ from $`u`$ to $`t`$, and vice versa. Also, $`\mathrm{\Delta }(P)=w(Q)`$.
3. For each $`uX`$ matched by $`M`$, $`\text{mwm}(G\{u\})`$ is the sum of $`\text{mwm}(G)`$ and the weight of the longest path in $`D`$ from $`u`$ to $`t`$.

###### Proof.

Statement 1. Consider a simple cycle $`C=u_1,u_2,\mathrm{},u_k,u_1`$ in $`D`$. Since $`t`$ has no outgoing edges, no $`u_i`$ equals $`t`$. By the definition of $`D`$, $`C`$ is also an alternating cycle in $`G`$. Therefore, $`w(C)`$ is the net change induced by transforming $`M`$ with $`C`$. Since $`M`$ is a maximum weight matching in $`G`$, this net change is non-positive.

Statement 2. Consider an alternating path $`P=u,u_1,u_2,\mathrm{},u_k`$ in $`G`$ starting from $`u`$. In $`D`$, $`P`$ is also a simple path. If $`u_kX`$, then $`u_k`$ is not matched by $`M`$, and $`D`$ contains the edge $`(u_k,t)`$. If $`u_kY`$, then $`u_k`$ is matched by $`M`$, and $`D`$ again contains the edge $`(u_k,t)`$. Therefore, $`D`$ contains the simple path $`Q=u,u_1,u_2,\mathrm{},u_k,t`$. The weight of $`Q`$ is $`\mathrm{\Delta }(P)`$. The reverse direction of the statement is straightforward.

Statement 3. This statement follows from Lemma 1(2) and Statement 2. ∎

###### Theorem 3.

Given $`G`$, we can compute $`\text{mwm}(G\{u\})`$ for all nodes $`uG`$ in $`O(\sqrt{n}m\mathrm{log}(nN))`$ time.

###### Proof.

By symmetry and Lemmas 1(1) and 2(3), we compute $`\text{mwm}(G\{u\})`$ for all $`uX`$ as follows.

1. Compute a maximum weight matching $`M`$ of $`G`$.
2. Construct $`D`$ as above and find the weights of its longest paths to $`t`$.
3. For each $`uX`$, if $`u`$ is matched by $`M`$, then $`\text{mwm}(G\{u\})`$ is the sum of $`\text{mwm}(G)`$ and the weight of the longest path from $`u`$ to $`t`$ in $`D`$; otherwise, $`\text{mwm}(G\{u\})=\text{mwm}(G)`$.

Step 1 takes $`O(\sqrt{n}m\mathrm{log}(nN))`$ time. At Step 2, constructing $`D`$ takes $`O(n+m)`$ time, and the single-destination longest paths problem takes $`O(\sqrt{n}m\mathrm{log}N)`$ time . Step 3 takes $`O(n)`$ time. Thus, the total time is $`O(\sqrt{n}m\mathrm{log}(nN))`$. ∎

## 3 The main result

This section gives a formal definition of maximum agreement subtrees and an overview of our new subtree algorithm.

### 3.1 Basics

Throughout the remainder of this paper, unrooted trees are denoted by $`U`$ or $`X`$, and rooted trees by $`T`$, $`W`$ or $`R`$. A node of degree 0 or 1 is a *leaf*; otherwise, it is internal. Adopted to avoid technical trivialities, this definition is somewhat nonstandard in that if the root of a rooted tree is of degree 1, it is also a leaf.

For an unrooted tree $`U`$ and a node $`uU`$, let $`U^u`$ denote the rooted tree constructed by rooting $`U`$ at $`u`$. For a rooted tree $`T`$ and a node $`vT`$, let $`T^v`$ denote the rooted subtree of $`T`$ that comprises $`v`$ and its descendants. Similarly, for a node $`vU^u`$, $`U^{uv}`$ is the rooted subtree of $`U^u`$ rooted at $`v`$, which is also called a rooted subtree of $`U`$.

An evolutionary tree is a tree whose leaves are labeled with distinct symbols. Let $`T`$ be a rooted evolutionary tree with leaves labeled over a set $`L`$. A label subset $`L^{}L`$ induces a subtree of $`T`$, denoted by $`T|L^{}`$, whose nodes are the leaves of $`T`$ labeled over $`L^{}`$ as well as the least common ancestors of such leaves in $`T`$, and whose edges preserve the ancestor-descendant relationship of $`T`$. Consider two rooted evolutionary trees $`T_1`$ and $`T_2`$ labeled over $`L`$. Let $`T_1^{}`$ be a subtree of $`T_1`$ induced by some subset of $`L`$. We similarly define $`T_2^{}`$ for $`T_2`$. If there exists an isomorphism between $`T_1^{}`$ and $`T_2^{}`$ mapping each leaf in $`T_1^{}`$ to one in $`T_2^{}`$ with the same label, then $`T_1^{}`$ and $`T_2^{}`$ are each called agreement subtrees of $`T_1`$ and $`T_2`$. Note that this isomorphism is unique. Consider any nodes $`uT_1`$ and $`vT_2`$. We say that $`u`$ is mapped to $`v`$ in $`T_1^{}`$ and $`T_2^{}`$ if this isomorphism maps $`u`$ to $`v`$. A maximum agreement subtree of $`T_1`$ and $`T_2`$ is one containing the largest possible number of labels. Let $`\text{mast}(T_1,T_2)`$ denote the number of labels in such a tree. A maximum agreement subtree of two unrooted evolutionary trees $`U_1`$ and $`U_2`$ is one with the largest number of labels among the maximum agreement subtrees of $`U_1^u`$ and $`U_2^v`$ over all nodes $`uU_1`$ and $`vU_2`$. Let

$`\text{mast}(U_1,U_2)`$ $`=`$ $`\mathrm{max}\{\text{mast}(U_1^u,U_2^v)uU_1,vU_2\}.`$ (1)
Remark. The nodes $`u`$ (or $`v`$) can be restricted to internal nodes when the trees have at least three nodes. We can also generalize the above definition to handle a pair of rooted tree and unrooted tree $`(T,U)`$. That is, $`\text{mast}(T,U)`$ is defined to be $`\mathrm{max}\{\text{mast}(T,U^v)vU\}`$.

### 3.2 Our subtree algorithm

The next theorem is our main result. The size $`|U|`$ (or $`|T|`$) of an unrooted tree $`U`$ (or a rooted tree $`T`$) is its node count.

###### Theorem 4.

Let $`U_1`$ and $`U_2`$ be two unrooted evolutionary trees. We can compute $`\text{mast}(U_1,U_2)`$ in $`O(N^{1.5}\mathrm{log}N)`$ time, where $`N=\mathrm{max}\{|U_1|,|U_2|\}`$.

We prove Theorem 4 by presenting our algorithm in a top-down manner with an outline here. As in previous work, our algorithm only computes $`\text{mast}(U_1,U_2)`$ and can be augmented to report a corresponding subtree. It uses graph separators. A separator of a tree is an internal node whose removal divides the tree into connected components each containing at most half of the tree’s nodes. Every tree that contains at least three nodes has a separator, which can be found in linear time.

If $`U_1`$ or $`U_2`$ has at most two nodes, $`\text{mast}(U_1,U_2)`$ as defined in Equation (1) can easily be computed in $`O(N)`$ time. Otherwise, both trees have at least three nodes each, and we can find a separator $`x`$ of $`U_1`$. We then consider three cases.

Case 1: In some maximum agreement subtree of $`U_1`$ and $`U_2`$, the node $`x`$ is mapped to a node $`yU_2`$. In this case, $`\text{mast}(U_1,U_2)=\text{mast}(U_1^x,U_2)`$. To compute $`\text{mast}(U_1^x,U_2)`$, we might simply evaluate $`\text{mast}(U_1^x,U_2^y)`$ for different $`y`$ in $`U_2`$. This approach involves solving the mast problem for $`\mathrm{\Theta }(N)`$ different pairs of rooted trees and introduces much redundant computation. For example, consider a rooted subtree $`R`$ of $`U_2`$. For all $`yU_2R`$, $`R`$ is a common subtree of $`U_2^y`$. Hence, $`R`$ is examined repeatedly in the computation of $`\text{mast}(U_1^x,U_2^y)`$ for these $`y`$. To speed up the computation, we devise the technique of label compression in §4 to elicit sufficient information between $`U_1^x`$ and $`R`$ so that we can compute $`\text{mast}(U_1^x,U_2^y)`$ for all $`yU_2R`$ without examining $`R`$. This leads to an efficient algorithm for handling Case 1, the time complexity is stated in the following lemma.

###### Lemma 5.

Assume that $`U_1`$ and $`U_2`$ have at least three nodes each. Given an internal node $`xU_1`$, we can compute $`\text{mast}(U_1^x,U_2)`$ in $`O(N^{1.5}\mathrm{log}N)`$ time.

###### Proof.

See §4 to §7. ∎

Case 2: In some maximum agreement subtree of $`U_1`$ and $`U_2`$, two certain nodes $`v_1`$ and $`v_2`$ of $`U_1`$ are mapped to nodes in $`U_2`$, and $`x`$ is on the path in $`U_1`$ between $`v_1`$ and $`v_2`$. This case is similar to Case 1. Let $`\stackrel{~}{U}_2`$ be the tree constructed by adding a dummy node in the middle of every edge in $`U_2`$. Then, $`\text{mast}(U_1,U_2)=\text{mast}(U_1^x,\stackrel{~}{U}_2^y)`$ for some dummy node $`y`$ in $`\stackrel{~}{U}_2`$. Thus, $`\text{mast}(U_1,U_2)=\text{mast}(U_1^x,\stackrel{~}{U}_2)`$. As in Case 1, $`\text{mast}(U_1^x,\stackrel{~}{U}_2)`$ can be computed in $`O(N^{1.5}\mathrm{log}N)`$ time.

Case 3: None of the above two cases. Let $`U_{1,1},U_{1,2},\mathrm{},U_{1,b}`$ be the evolutionary trees formed by the connected components of $`U_1\{x\}`$. Let $`J_1,\mathrm{},J_b`$ be the sets of labels in these components, respectively. Then, a maximum agreement subtree of $`U_1`$ and $`U_2`$ is labeled over some $`J_i`$. Therefore, $`\text{mast}(U_1,U_2)=\mathrm{max}\{\text{mast}(U_{1,i},U_2|J_i)i[1,b]\}`$, and we compute each $`\text{mast}(U_{1,i},U_2|J_i)`$ recursively.

Figure 2 summarizes the steps for computing $`\text{mast}(U_1,U_2)`$. Here we analyze the time complexity $`T(N)`$ based on Lemma 5. Cases 1 and 2 each take $`O(N^{1.5}\mathrm{log}N)`$ time. Let $`N_i=|U_{1,i}|`$. Then Case 3 takes $`_{i[1,b]}T(N_i)`$ time. By recursion,

$$T(N)=O(N^{1.5}\mathrm{log}N)+\underset{i[1,b]}{}T(N_i).$$

Since $`x`$ is a separator of $`U_1`$, $`N_i\frac{N}{2}`$. Then, since $`_{i[1,b]}N_iN`$, $`T(N)=O(N^{1.5}\mathrm{log}N)`$  and the time bound in Theorem 4 follows. To complete the proof of Theorem 4, we devote §4 through §7 to proving Lemma 5.

## 4 Label compressions

To compute a maximum agreement subtree, our algorithm recursively processes overlapping subtrees of the input trees. The technique of label compression compresses overlapping parts of such subtrees to reduce their total size. We define label compressions with respect to a rooted subtree in §4.1 and with respect to two label-disjoint rooted subtrees in §4.2. We do not use label compression with respect to three or more trees.

As a warm-up, let us define a concept called subtree shrinking, which is a primitive form of label compression. Let $`T`$ be a rooted tree. Let $`R`$ be a rooted subtree of $`T`$. Let $`TR`$ denote the rooted tree obtained by replacing $`R`$ with a leaf $`\gamma `$. We say that $`\gamma `$ is a shrunk leaf. The other leaves are atomic leaves. Similarly, for two label-disjoint rooted subtrees $`R_1`$ and $`R_2`$ of $`T`$, let $`T(R_1,R_2)`$ denote the rooted tree obtained by replacing $`R_1`$ and $`R_2`$ with shrunk leaves $`\gamma _1`$ and $`\gamma _2`$, respectively. We extend these notions to an unrooted tree $`U`$ and define $`UR`$ and $`U(R_1,R_2)`$ similarly.

### 4.1 Label compression with respect to one rooted subtree

Let $`T`$ be a rooted tree. Let $`v`$ be a node in $`T`$ and $`u`$ an ancestor of $`v`$. Let $`P`$ be the path of $`T`$ from $`u`$ to $`v`$. A node lies between $`u`$ and $`v`$ if it is in $`P`$ but differs from $`u`$ and $`v`$. A subtree of $`T`$ is attached to $`u`$ if it is some $`T^w`$ where $`w`$ is a child of $`u`$. A subtree of $`T`$ hangs between $`u`$ and $`v`$ if it is attached to some node lying between $`u`$ and $`v`$, but its root is not in $`P`$ and is not $`v`$.

We are now ready to define the concept of label compression. Let $`T`$ and $`R`$ be rooted evolutionary trees labeled over $`L`$ and $`K`$, respectively. The compression of $`T`$ with respect to $`R`$, denoted by $`TR`$, is a tree constructed by affixing extra nodes to $`T|(LK)`$ with the following steps; see Figure 3 for an example. Consider each node $`y`$ in $`T|(LK)`$, let $`x`$ be its parent in $`T|(LK)`$.

* Let $`𝒜(T,K,y)`$ denote the set of subtrees of $`T`$ that are attached to $`y`$ and whose leaves are all labeled over $`K`$. If $`𝒜(T,K,y)`$ is non-empty, compress all the trees in $`𝒜(T,K,y)`$ into a single node $`z_1`$ and attach it to $`y`$.
* Let $`(T,K,y)`$ denote the set of subtrees of $`T`$ that hang between $`x`$ and $`y`$ (by definition of $`T|(LK)`$, these subtrees are all labeled over $`K`$). If $`(T,K,y)`$ is non-empty, compress the parents $`p_1,\mathrm{},p_m`$ of the roots of the trees in $`(T,K,y)`$ into a single node $`p_1`$, and insert it between $`x`$ and $`y`$; also compress all the trees in $`(T,K,y)`$ into a single node $`z_2`$ and attach it to $`p_1`$.

The nodes $`z_1`$, $`z_2`$ and $`p_1`$ are called compressed nodes, and the leaves in $`TR`$ that are not compressed are atomic leaves.

We further store in $`TR`$ some auxiliary information about the relationship between $`T`$ and $`R`$. For an internal node $`v`$ in $`TR`$, let $`\alpha (v)`$ $`=`$ $`\text{mast}(T^v,R)`$. For a compressed leaf $`v`$ in $`TR`$, if it is compressed from a set of subtrees $`T^{v_1},\mathrm{},T^{v_s}`$, let $`\alpha (v)=\mathrm{max}\{\text{mast}(T^{v_1},R)`$, $`\mathrm{},\text{mast}(T^{v_s},R)\}`$.

Let $`T_1`$ and $`T_2`$ be two rooted evolutionary trees. Assume $`T_2`$ contains a rooted subtree $`R`$. Given $`T_1R`$, we can compute $`\text{mast}(T_1,T_2)`$ without examining $`R`$. We first construct $`T_1R`$ by replacing $`R`$ of $`T_2`$ with a shrunk leaf and then compute $`\text{mast}(T_1,T_2)`$ from $`T_1R`$ and $`T_2R`$. To further our discussion, we next generalize the definition of maximum agreement subtree for a pair of trees that contain compressed leaves and a shrunk leaf, respectively.

Let $`W_1=T_1R`$ and $`W_2=T_2R`$. Let $`\gamma `$ be the shrunk leaf in $`W_2`$. We define an agreement subtree of $`W_1`$ and $`W_2`$ similar to that of ordinary evolutionary trees. An atomic leaf must still be mapped to an atomic leaf with the same label. However, the shrunk leaf $`\gamma `$ of $`W_2`$ can be mapped to any internal node or compressed leaf $`v`$ of $`W_1`$ as long as $`\alpha (v)>0`$. The size of an agreement subtree is the number of its atomic leaves, plus $`\alpha (v)`$ if $`\gamma `$ is mapped to a node $`vW_1`$. A maximum agreement subtree of $`W_1`$ and $`W_2`$ is one with the largest size. Let $`\text{mast}(W_1,W_2)`$ denote the size of such a subtree. The following lemma is the cornerstone of label compression.

###### Lemma 6.

$`\text{mast}(T_1,T_2)=\text{mast}(W_1,W_2)`$.

###### Proof.

It follows directly from the definition. ∎

We can compute $`\text{mast}(W_1,W_2)`$ as if $`W_1`$ and $`W_2`$ were ordinary rooted evolutionary trees  with a special procedure on handling the shrunk leaf. The time complexity is stated in the following lemma. Let $`n=\mathrm{max}\{|W_1|,|W_2|\}`$ and $`N=\mathrm{max}\{|T_1|,|T_2|\}`$.

###### Lemma 7.

Suppose that all the auxiliary information of $`W_1`$ has been given. Then $`\text{mast}(W_1,W_2)`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time and afterwards we can retrieve $`\text{mast}(W_1^v,W_2)`$ for any node $`vW_1`$ in $`O(1)`$ time.

###### Proof.

We adapt Farach and Thorup’s rooted subtree algorithm  to compute $`\text{mast}(W_1,W_2)`$. Details are given in §A. ∎

We demonstrate a scenario where label compression speeds up the computation of $`\text{mast}(U_1^x,U_2)`$ for Lemma 5. Suppose that we can identify a rooted subtree $`R`$ of $`U_2`$ such that $`x`$ is mapped to a node outside $`R`$, i.e., we can reduce Equation (1) to

$`\text{mast}(U_1^x,U_2)`$ $`=`$ $`\mathrm{max}\{\text{mast}(U_1^x,U_2^y)y\text{ is an internal node not in }R\}.`$ (2)
Note that every $`U_2^y`$ contains $`R`$ as a common subtree. To avoid overlapping computation on $`R`$, we construct $`W=U_1^xR`$ and $`X=U_2R`$. Then $`X^y=U_2^yR`$ and from Lemma 6, $`\text{mast}(U_1^x,U_2^y)=`$ $`\text{mast}(W,X^y)`$. We rewrite Equation (2) as

$`\text{mast}(U_1^x,U_2)`$ $`=`$ $`\mathrm{max}\{\text{mast}(W,X^y)y\text{ is an internal node of }X\}.`$ (3)
If $`R`$ is large, then $`W`$ and $`X`$ are much smaller than $`U_1^x`$ and $`U_2`$. Consequently, it is beneficial to compress $`U_1^x`$ and compute $`\text{mast}(U_1^x,U_2)`$ according to Equation (3).

### 4.2 Label compression with respect to two rooted subtrees

Let $`T`$, $`R_1`$, $`R_2`$ be rooted evolutionary trees labeled over $`L`$, $`K_1`$, $`K_2`$, respectively, where $`K_1K_2=\varphi `$. Let $`K=K_1K_2`$. The compression of $`T`$ with respect to $`R_1`$ and $`R_2`$, denoted by $`T(R_1,R_2)`$, is a tree constructed from $`T|(LK)`$ by the following two steps. For each node $`y`$ and its parent $`x`$ in $`T|(LK)`$,

1. if $`𝒜(T,K,y)`$ is non-empty, compress all the trees in $`𝒜(T,K,y)`$ into a single leaf $`z`$ and attach it to $`y`$; create and attach an auxiliary node $`\overline{z}`$ to $`y`$;
2. if $`(T,K,y)`$ is non-empty, compress the parents $`p_1`$, $`\mathrm{}`$, $`p_m`$ of the roots of the subtrees in $`(T,K,y)`$ into a single node $`p_1`$ and insert it between $`x`$ and $`y`$; compress the subtrees in $`(T,K,y)`$ into a single node $`z`$ and attach it to $`p_1`$; create and insert an auxiliary node $`\overline{p}_1`$ between $`p_1`$ and $`y`$; create auxiliary nodes $`\overline{z}`$ and $`\overline{\overline{z}}`$ and attach them to $`p_1`$ and $`\overline{p}_1`$, respectively.

The nodes $`p_1`$ and $`z`$ are compressed nodes of $`T(R_1,R_2)`$. The nodes $`\overline{p}_1,\overline{z}`$, and $`\overline{\overline{z}}`$ are auxiliary nodes. These nodes are added to capture the topology of $`T`$ that is isomorphic with the subtrees $`R_1`$ and $`R_2`$ of $`T^{}`$.

We also store auxiliary information in $`T(R_1,R_2)`$. Let $`R^+`$ be the tree obtained by connecting $`R_1`$ and $`R_2`$ together with a node, which becomes the root of $`R^+`$.

Consider the internal nodes of $`T(R_1,R_2)`$. If $`v`$ is an internal node inherited from $`T|(LK)`$, then let $`\alpha _1(v)=\text{mast}(T^v,R_1)`$ and $`\alpha _2(v)=\text{mast}(T^v,R_2)`$. If $`p_1`$ and $`\overline{p}_1`$ are internal nodes compressed from some path $`p_1,\mathrm{},p_m`$ of $`T`$, then only $`p_1`$ stores the values $`\alpha _1(p_1)=\text{mast}(T^{p_1},R_1)`$, $`\alpha _2(p_1)=\text{mast}(T^{p_1},R_2)`$, and $`\alpha _+(p_1)=\text{mast}(T^{p_1},R^+)`$.

We do not store any auxiliary information at the atomic leaves in $`T(R_1,R_2)`$. Consider the other leaves in $`T(R_1,R_2)`$ based on how they are created.

Case 1: Nodes $`z,\overline{z}`$ are leaves created with respect to $`𝒜(T,K,y)`$ for some node $`y`$ in $`T|(LK)`$. Let $`𝒜(T,K,y)=\{T^{v_1},\mathrm{},T^{v_k}\}`$. We store the following values at $`z`$.

* $`\alpha _1(z)=\mathrm{max}\{\text{mast}(T^{v_i},R_1)i[1,k]\}`$, $`\alpha _2(z)=\mathrm{max}\{\text{mast}(T^{v_i},R_2)i[1,k]\}`$, $`\alpha _+(z)=\mathrm{max}\{\text{mast}(T^{v_i},R^+)i[1,k]\}`$;
* $`\beta (z)=\mathrm{max}\{\text{mast}(T^{v_i},R_1)+\text{mast}(T^{v_i^{}},R_2)`$ $`T^{v_i}`$ and $`T^{v_i^{}}`$ are distinct subtrees in $`𝒜(T,K,y)\}`$.

Case 2: Nodes $`z,\overline{z}`$, and $`\overline{\overline{z}}`$ are leaves created with respect to the subtrees in $`(T,K,y)=\{T^{v_1},\mathrm{},T^{v_k}\}`$ for some node $`y`$ in $`T|(LK)`$. We store the following values at $`z`$:

* $`\alpha _1(z)`$, $`\alpha _2(z)`$, and $`\alpha _+(z)`$ as in Case 1;
* $`\beta (z)=\mathrm{max}\{\text{mast}(T^{v_i},R_1)+\text{mast}(T^{v_j},R_2)`$ $`T^{v_i}`$ and $`T^{v_j}`$ are distinct subtrees in $`(T,K,y)`$ that are attached to the same node in $`T\}`$;
* $`\beta _{12}(z)=\mathrm{max}\{\text{mast}(T^{v_j},R_1)+\text{mast}(T^{v_j^{}},R_2)(j,j^{})Z\}`$ and

$`\beta _{21}(z)=\mathrm{max}\{\text{mast}(T^{v_j},R_2)+\text{mast}(T^{v_j^{}},R_1)(j,j^{})Z\}`$,

where $`Z`$ $`=\{(j,j^{})`$ $`T^{v_j}`$, $`T^{v_j^{}}(T,K,y)`$ and the parent of $`v_j`$ in $`T`$ is a proper ancestor of the parent of $`v_j^{}`$ }.

Let $`T_1`$ and $`T_2`$ be rooted evolutionary trees. Let $`R_1`$ and $`R_2`$ be label-disjoint rooted subtrees of $`T_2`$. Let $`W_1=T(R_1,R_2)`$ and $`W_2=T^{}(R_1,R_2)`$. Below, we give the definition of a maximum agreement subtree of $`W_1`$ and $`W_2`$.

Let $`\gamma _1`$ and $`\gamma _2`$ be the two shrunk leaves in $`W_2`$ representing $`R_1`$ and $`R_2`$, respectively. Let $`y_c`$ be the least common ancestor of $`\gamma _1`$ and $`\gamma _2`$ in $`W_2`$. Intuitively, in a pair of agreement subtrees $`(W_1^{},W_2^{})`$ of $`W_1`$ and $`W_2`$, atomic leaves are mapped to atomic leaves, and shrunk leaves are mapped to internal nodes or leaves. Moreover, we allow $`W_2^{}`$ to contain $`y_c`$ as a leaf, which can be mapped to an internal node or leaf of $`W_1^{}`$. More formally, we require that there is an isomorphism between $`W_1^{}`$ and $`W_2^{}`$ satisfying the following conditions:

1. Every atomic leaf is mapped to an atomic leaf with the same label.
2. If $`W_2^{}`$ contains $`y_c`$ as a leaf and thus neither $`\gamma _1`$ nor $`\gamma _2`$ is found in $`W_2^{}`$, then $`y_c`$ is mapped to a node $`v`$ with $`\alpha _+(v)>0`$.
3. If only one of $`\gamma _1`$ and $`\gamma _2`$ exists in $`W_2^{}`$, say $`\gamma _1`$, then it is mapped to a node $`v`$ with $`\alpha _1(v)>0`$.
4. If both $`\gamma _1`$ and $`\gamma _2`$ exist in $`W_2^{}`$, then any of the following cases is permitted:

* $`\gamma _1`$ and $`\gamma _2`$ are respectively mapped to a compressed leaf $`z`$ and its sibling $`\overline{z}`$ in $`W_1^{}`$ with $`\beta (z)>0`$.
    * $`\gamma _1`$ and $`\gamma _2`$ are respectively mapped to a compressed leaf $`z`$ and the accompanying auxiliary leaf $`\overline{\overline{z}}`$ in $`W_1^{}`$ with $`\beta _{12}(z)>0`$, or the leaves $`\overline{\overline{z}}`$ and $`z`$ in $`W_1^{}`$ with $`\beta _{21}(z)>0`$.
    * $`\gamma _1`$ and $`\gamma _2`$ are respectively mapped to two leaves or internal nodes $`v`$ and $`w`$ with $`\alpha _1(v)`$, $`\alpha _2(w)>0`$.

The way we measure the size of $`W_1^{}`$ and $`W_2^{}`$ depends on their isomorphism. For example, if $`y_c`$ is mapped to some node $`v`$ in $`W_1^{}`$, then the size is the total number of atomic leaves in $`W_1^{}`$ plus $`\alpha _+(v)`$. More precisely, the size of $`W_1^{}`$ and $`W_2^{}`$ is defined to be the total number of atomic leaves in $`W_1^{}`$ plus the corresponding $`\alpha `$ or $`\beta `$ values depending on the isomerphism between $`W_1^{}`$ and $`W_2^{}`$. A maximum agreement subtree of $`W_1`$ and $`W_2`$ is one with the largest possible size. Let $`\text{mast}(W_1,W_2)`$ denote the size of such a subtree. The following lemma, like Lemma 6, is also the cornerstone of label compression.

###### Lemma 8.

$`\text{mast}(T_1,T_2)=\text{mast}(W_1,W_2)`$.

###### Proof.

It follows directly from the definition of $`\text{mast}(W_1,W_2)`$. ∎

Again, $`\text{mast}(W_1,W_2)`$ can be computed by adapting Farach and Thorup’s rooted subtree algorithm . The time complexity is stated in the following lemma. Let $`n`$ $`=`$ $`\mathrm{max}\{|W_1|`$,$`|W_2|\}`$ and $`N=\mathrm{max}\{|T_1|,|T_2|\}`$.

###### Lemma 9.

Suppose that all the auxiliary information of $`W_1`$ has been given. Then we can compute $`\text{mast}(W_1,W_2)`$ in $`O(n^{1.5}\mathrm{log}N)`$ time. Afterwards we can retrieve $`\text{mast}(W_1^v,W_2)`$ for any $`vW`$ in $`O(1)`$ time.

###### Proof.

See §A. ∎

## 5 Computing mast$`\mathbf{(}𝑼_\mathrm{𝟏}^𝒙\mathbf{,}𝑼_\mathrm{𝟐}\mathbf{)}`$ — Proof of Lemma 5

At a high level, we first apply label compression to the input instance $`(U_1^x,U_2)`$. We then reduce the problem to a number of smaller subproblems $`(W,X)`$, each of which is similar to $`(U_1^x,U_2)`$ and is solved recursively. For each $`(W,X)`$ generated, $`X`$ is a subtree of $`U_2`$ with at most two shrunk leaves, and $`W`$ is a label compression of $`U_1^x`$ with respect to some rooted subtrees of $`U_2`$ that are represented by the shrunk leaves of $`X`$. Also, $`W`$ and $`X`$ contain the same number of atomic leaves.

### 5.1 Recursive computation of mast$`\mathbf{(}𝑾\mathbf{,}𝑿\mathbf{)}`$

Our subtree algorithm initially sets $`W=U_1^x`$ and $`X=U_2`$. In general, $`W=U_1^xR`$ and $`X=U_2R`$, or $`W=U_1^x(R,R^{})`$ and $`X=U_2(R,R^{})`$ for some rooted subtrees $`R`$ and $`R^{}`$ of $`U_2`$. If $`W`$ or $`X`$ has at most two nodes, then $`\text{mast}(W,X)`$ can easily be computed in linear time. Otherwise, both $`W`$ and $`X`$ each have at least three nodes. Let $`N=\mathrm{max}\{|U_1|,|U_2|\}`$ and $`n=\mathrm{max}\{|W|,|X|\}`$. Our algorithm first finds a separator $`y`$ of $`X`$ and computes $`\text{mast}(W,X)`$ for the following two cases. The output is the larger of the two cases. Figure 4 outlines our algorithm.

Case 1: $`\text{mast}(W,X)=\text{mast}(W,X^y)`$. We root $`X`$ at $`y`$ and evaluate $`\text{mast}(W,X^y)`$. By Lemma 9, this takes $`O(n^{1.5}\mathrm{log}N)`$ time.

Case 2: $`\text{mast}(W,X)=\text{mast}(W,X^z)`$ for some internal node $`zy`$. We compute $`\mathrm{max}\{\text{mast}(W,X^z)z\text{ is an internal node and }zy\}`$ by solving a set of subproblems $`\{\text{mast}(W_1,X_1)`$, $`\mathrm{}`$, $`\text{mast}(W_b,X_b)\}`$ where their total size is $`n`$ and $`\mathrm{max}\{\text{mast}(W,X^z)z\text{ is an internal node and }zy\}=`$ $`\mathrm{max}\{\text{mast}(W_i,X_i)i[1,b]\}`$. Moreover, our algorithm enforces the following properties.

* If $`X`$ contains at most one shrunk leaf, every subproblem generated has size at most half that of $`X`$.
* If $`X`$ has two shrunk leaves, at most one subproblem $`(W_{i_o},X_{i_o})`$ has size greater than half that of $`X`$, but $`X_{i_o}`$ contains only one shrunk leaf. Thus, in the next recursion level, every subproblem spawned by $`(W_{i_o},X_{i_o})`$ has size at most half that of $`X`$.

To summarize, whenever the recursion gets down by two levels, the size of a subproblem reduces by half.

The subproblems $`\text{mast}(W_1,X_1),\mathrm{}`$, $`\text{mast}(W_b,X_b)`$ are formally defined as follows. Assume that the separator $`y`$ has $`b`$ neighbors in $`X`$, namely, $`v_1,\mathrm{},v_b`$. For each $`i[1,b]`$, let $`C_i`$ be the connected component in $`X\{y\}`$ that contains $`v_i`$. The size of $`C_i`$ is at most half that of $`X`$. Intuitively, we would like to shrink the subtree $`X^{v_iy}`$ into a leaf, producing a smaller unrooted tree $`X_i`$. We first consider the simple case where $`X`$ has at most one shrunk leaf. Then no $`C_i`$ contains more than one shrunk leaf.

If $`C_i`$ contains no shrunk leaf, then $`X_i`$ contains only one shrunk leaf representing the subtree $`X^{v_iy}`$. Note that $`X^{v_iy}`$ corresponds to the subtree $`U_2^{v_iy}`$ in $`U_2`$ and $`X_i=U_2U_2^{v_iy}`$. Let $`W_i=U_1^xU_2^{v_iy}`$.

If $`C_i`$ contains one shrunk leaf $`\gamma _1`$ then $`X_i`$ contains $`\gamma _1`$ as well as a new shrunk leaf representing the subtrees $`X^{v_iy}`$. The two subtrees are label-disjoint. Again, $`X^{v_iy}`$ corresponds to the subtree $`U_2^{v_iy}`$ in $`U_2`$. Assume that $`\gamma _1`$ corresponds to a subtree $`U_2^{v^{}y^{}}`$ in $`U_2`$. Then $`X_i=U_2(U_2^{v^{}y^{}},U_2^{v_iy})`$. Let $`W_i=U_1^x(U_2^{v^{}y^{}},U_2^{v_iy})`$.

We now consider the case where $`X`$ itself already has two shrunk leaves $`\gamma _1`$ and $`\gamma _2`$. If $`y`$ lies on the path between $`\gamma _1`$ and $`\gamma _2`$, then no $`C_i`$ contains more than one shrunk leaf and we define the smaller problem instances $`(W_i,X_i)`$ as above. Otherwise, there is a $`C_i`$ containing both $`\gamma _1`$ and $`\gamma _2`$. $`X_i`$ as defined contains three compressed leaves, violating our requirement. In this case, we replace $`y`$ with the node $`y^{}`$ on the path between $`\gamma _1`$ and $`\gamma _2`$, which is the closest to $`y`$. Now, to compute $`\text{mast}(W,X)`$, we consider the two cases depending on whether the root of $`W`$ is mapped to $`y^{}`$ or not. Again, we first compute $`\text{mast}(W,X^y^{})`$. Then, we define the connected components $`C_i`$ and the smaller problem instances $`(W_i,X_i)`$ with respect to $`y^{}`$. Every $`X_i`$ has at most two compressed leaves, but $`y^{}`$ may not be a separator and we cannot guarantee that the size of every subproblem is reduced by half. However, there can exist only one connected component $`C_{i_o}`$ with size larger than half that of $`X`$. Indeed, $`C_{i_o}`$ is the component containing $`y`$. In this case, both $`\gamma _1`$ and $`\gamma _2`$ are not inside $`C_{i_o}`$, and $`X_{i_o}`$ as defined contains only one compressed leaf. Thus, the subproblems that $`\text{mast}(W_{i_0},X_{i_0})`$ spawns in the next recursion level each have size of at most half that of $`(W,X)`$.

With respect to $`y`$ or $`y^{}`$, computing the topology of all $`X_i`$ and $`W_i`$ from $`X`$ and $`W`$ is straightforward; see §5.2. Computing the auxiliary information in all $`W_i`$ efficiently requires some intricate techniques, which are detailed in §6 and §7.

### 5.2 Computing the topology of compressed trees

The topology of $`X_i`$ can be constructed from $`X`$ by replacing the subtree $`X^{v_iy}`$ of $`X`$ with a shrunk leaf. Let $`J`$ and $`J_i`$ be the sets of labels in $`X`$ and $`X_i`$, respectively. For the trees $`W_i`$, recall that the definitions of $`W`$ and the trees $`W_i`$ are based on affixing some nodes to the trees $`U_1^x|J`$ and $`U_1^x|J_i`$, respectively. Observe that $`W|J`$ and $`U_1^x|J`$ have the same topology. Moreover, $`W|J_i=(W|J)|J_i`$ and $`U_1^x|J_i=(U_1^x|J)|J_i`$. Thus, $`W|J_i`$ and $`U_1^x|J_i`$ have the same topology. We can obtain $`U_1^x|J_i`$ by constructing $`W|J_i`$. Note that $`J=_{1ib}J_i`$ and all the label sets $`J_i`$ are disjoint. We can construct all the trees $`W|J_i`$ from $`W`$ in $`O(n)`$ time . Next, we show how to construct $`W_i`$ from $`W|J_i`$ in time linear in the size of $`W|J_i`$. We only detail the case where $`X_i`$ consists of two shrunk leaves. The case for one shrunk leaf is similar. The following procedure is derived directly from the definition of the compression of $`U_1^x`$ with respect to two subtrees.

Let $`v`$ be any node of $`W|J_i`$. If $`v`$ is not the root, let $`u`$ be the parent of $`v`$ in $`W|J_i`$.

* If $`𝒜(U_1^x,LJ_i,v)`$ is non-empty or equivalently the degree of $`v`$ in $`U_1^x`$ is different from its degree in $`W|J_i`$, then attach auxiliary leaves $`z`$ and $`\overline{z}`$ to $`v`$.
* If $`(U_1^x,LJ_i,v)`$ is non-empty or equivalently $`u`$ is not the parent of $`v`$ in $`U_1^x`$, then create a path between $`u`$ and $`v`$ consisting of two nodes $`p`$ and $`\overline{p}`$, attach auxiliary leaves $`z`$ and $`\overline{z}`$ to $`p`$, and attach $`\overline{\overline{z}}`$ to $`\overline{p}`$.

### 5.3 Time complexity of computing mast($`𝑾\mathbf{,}𝑿`$)

###### Lemma 10.

We can compute $`\text{mast}(W,X)`$ in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Proof.

Let $`T(n)`$ be the computation time of $`\text{mast}(W,X)`$. The computation is divided into two cases. Case 1 of §5.1 takes $`O(n^{1.5}\mathrm{log}N)`$ time. For Case 2, a set of subproblems $`\{\text{mast}(W_i,X_i)i[1,b]\}`$ are generated. As to be shown in $`\mathrm{\S }`$6 and $`\mathrm{\S }`$7, the time to prepare all these subproblems is also $`O(n^{1.5}\mathrm{log}N)`$. These subproblems, except possibly one, are each of size less than $`n/2`$. For the exceptional subproblem, say, $`\text{mast}(W_l,X_l)`$, its computation is again divided into two cases. One case takes $`O(n^{1.5}\mathrm{log}N)`$ time. For the other case, another set of subproblems are generated in $`O(n^{1.5}\mathrm{log}N)`$ time. This time every such subproblem has size less than $`n/2`$. Let $`\mathrm{\Sigma }`$ be the set of all the subproblems generated in both steps. The total size of the subproblems in $`\mathrm{\Sigma }`$ is at most $`n`$, and

$$T(n)=O(n^{1.5}\mathrm{log}N)+\underset{\text{mast}(W^{},X^{})\mathrm{\Sigma }}{}T(|X^{}|).$$

It follows that $`T(n)=`$ $`O(n^{1.5}\mathrm{log}N)`$. ∎

By letting $`W=U_1^x`$ and $`X=U_2`$, we have proved Lemma 5. What remains is to show how to compute the auxiliary information stored in all $`W_i`$ from $`(W,X)`$ in $`O(n^{1.5}\mathrm{log}N)`$ time. Note that $`X`$ contains at most two shrunk leaves. Depending on the number of shrunk leaves in $`X`$, we divide our discussion into §6 and §7.

## 6 Auxiliary information for $`𝑿`$ with no shrunk leaf

The case of $`X`$ containing no shrunk leaf occurs only when the algorithm starts, i.e., $`W=U_1^x`$, $`X=U_2`$. and $`N=n`$. The subproblems $`\text{mast}(W_1,X_1),`$ $`\mathrm{}`$, $`\text{mast}(W_b,X_b)`$ spawned from $`(W,X)`$ are defined by an internal node $`y`$ in $`X`$, which is adjacent to the nodes $`v_1,\mathrm{},v_b`$. Let $`R_i`$ and $`\overline{R}_i`$ denote the rooted subtrees $`X^{v_iy}`$ and $`X^{yv_i}`$, respectively. Note that the rooted tree $`X^y`$ is composed of the subtrees $`\overline{R}_1,\mathrm{},\overline{R}_b`$. Also, $`W_i=WR_i`$ and $`X_i=WR_i`$.

The total size of all $`\overline{R}_i`$ is at most $`n`$. Furthermore, each $`R_i`$ is $`X^y`$ with $`\overline{R}_i`$ removed; see Figure 5. This section discusses how to compute the auxiliary information required by each $`W_i`$ in $`O(n^{1.5}\mathrm{log}N)`$ time.

### 6.1 Auxiliary information in the compressed leaves of $`𝑾_𝒊`$

Consider any compressed leaf $`v`$ in $`W_i`$. Let $`S_v`$ denote the set of subtrees from which $`v`$ is compressed. Then, the auxiliary information to be stored in $`v`$ is

$$\alpha (v)=\mathrm{max}\{\text{mast}(W^z,R_i)W^zS_v\}.$$
(4)
Observe that for any $`W^zS_v`$, $`W^z`$ contains no labels outside $`R_i`$. So $`\text{mast}(W^z,R_i)=`$ $`\text{mast}(W^z,X^y)`$ and we can rewrite Equation (4) as

$$\alpha (v)=\mathrm{max}\{\text{mast}(W^z,X^y)W^zS_v\}.$$

We use the rooted subtree algorithm of  to compute $`\text{mast}(W,X^y)`$ in $`O(n^{1.5}\mathrm{log}N)`$ time. Then, we can retrieve the value of $`\text{mast}(W^z,X^y)`$ for any node $`zW`$ in $`O(1)`$ time. To compute $`\mathrm{max}\{\text{mast}(W^z,X^y)W^zS_v\}`$ efficiently, we assume that for any node $`uW`$, the subtrees attached to $`u`$ are numbered consecutively, starting from 1. We consider a preprocessing for efficient retrieval of the following types of values:

* for some node $`uW`$ and some interval $`[a,b]`$, $`\mathrm{max}\{\text{mast}(W^z,X^y)W^z`$ is a subtree attached to $`u`$ and its number falls in $`[a,b]\}`$;
* for some path $`P`$ of $`W`$, $`\mathrm{max}\{\text{mast}(W^z,X^y)W^z`$ is a subtree attached to some node in $`P\}`$.

###### Lemma 11.

Assume that we can retrieve $`\text{mast}(W^z,X^y)`$ for any $`zW`$ in $`O(1)`$ time. Then we can preprocess $`W`$ and $`X`$ and construct additional data structures in $`O(n\mathrm{log}^{}n)`$ time so that any value of the above types can be retrieved in $`O(1)`$ time.

###### Proof.

We adapt preprocessing techniques for on-line product queries in . ∎

With the preprocessing stated in Lemma 11, we can determine $`\alpha (v)`$ as follows. Note that $`S_v`$ is either a subset of the subtrees attached to a node $`u`$ in $`W`$ or the set of subtrees attached to nodes on a particular path in $`W`$. In the former case, $`u`$ is also a parent of $`v`$ and $`S_v`$ is partitioned into at most $`d_u+1`$ intervals where $`d_u`$ is the degree of $`u`$ in $`W_i`$. From Lemma 11, $`\alpha (v)`$ can be found in $`O(d_u+1)`$ time. Similarly, for the latter case, $`\alpha (v)`$ can be found in $`O(1)`$ time. Thus, the compressed leaves in $`W_i`$ are processed in $`O(|W_i|)`$ time. Summing over all $`W_i`$, the time complexity is $`O(n)`$. Therefore, the overall computation time for preprocessing and finding auxiliary information in the leaves of all $`W_i`$ is $`O(n^{1.5}\mathrm{log}N)`$.

### 6.2 Auxiliary information in the internal nodes of $`𝑾_𝒊`$

Consider any internal node $`v`$ in $`W_i`$ with $`i[1,b]`$. Our goal is to compute the auxiliary information $`\alpha (v)=\text{mast}(W^v,R_i)`$. Note that $`R_i`$ may be of size $`\mathrm{\Theta }(n)`$, and even computing one particular $`\text{mast}(W^v,R_i)`$ already takes $`O(n^{1.5}\mathrm{log}N)`$ time. Fortunately, these $`R_i`$ are very similar. Each $`R_i`$ is $`X^y`$ with $`\overline{R}_i`$ removed. Exploiting this similarity and using the algorithm in §2 for all-cavity matchings, we can perform an $`O(n^{1.5}\mathrm{log}N)`$-time preprocessing so that we can retrieve $`\text{mast}(W^v,R_i)`$ for any internal node $`v`$ in $`W`$ and $`i[1,b]`$ in $`O(\mathrm{log}^2n)`$ time. Therefore, it takes $`O(|W_i|\mathrm{log}^2n)`$ time to compute $`\alpha (v)`$ for all internal nodes $`v`$ of one particular $`W_i`$, and $`O(n\mathrm{log}^2n)`$ time for all $`W_i`$. The $`O(n^{1.5}\mathrm{log}N)`$-time preprocessing is detailed as follows.

First, note that if we remove $`y`$ from $`X^y`$, the tree would decompose into the subtrees $`\overline{R}_1,\mathrm{},\overline{R}_b`$. Thus, the total size of all $`\overline{R}_i`$ is at most $`n`$. The next lemma suggests a way to retrieve efficiently $`\text{mast}(W^v,\overline{R}_i)`$ and $`\mathrm{max}\{\text{mast}(W^v,\overline{R}_j)jI\}`$ for any $`vW`$ and and $`I[1,b]`$.

###### Lemma 12.

We can compute $`\text{mast}(W,\overline{R}_i)`$ for all $`i[1,b]`$ in $`O(n^{1.5}\mathrm{log}N)`$ time. Then, we can retrieve $`\text{mast}(W^v,\overline{R}_i)`$ for any node $`v`$ in $`W`$ and $`i[1,b]`$ in $`O(\mathrm{log}n)`$ time. Furthermore, we can build a data structure to retrieve $`\mathrm{max}\{\text{mast}(W^v,\overline{R}_j)jI\}`$ for any $`vW`$ and $`I[1,b]`$ in $`O(\mathrm{log}^2n)`$ time.

###### Proof.

This lemma follows from the rooted subtree algorithm and related data structures in . ∎

Below, we give a formula to compute $`\text{mast}(W^v,R_i)`$ efficiently. For any $`zW`$ and $`i[1,b]`$, let $`\text{r-mast}(W^z,R_i)`$ denote the maximum size among all the agreement subtrees of $`W^z`$ and $`R_i`$ in which $`z`$ is mapped to the root of $`R_i`$.

###### Lemma 13.

$$\text{mast}(W^v,R_i)=\mathrm{max}\{\begin{array}{c}\mathrm{max}\{\text{mast}(W^v,\overline{R}_j)j[1,b],ji\};\hfill \\ \mathrm{max}\{\text{r-mast}(W^z,R_i)zW^v\}.\hfill \end{array}$$

###### Proof.

Observe that $`\text{mast}(W^v,R_i)=\text{mast}(W^z,R_i)=\text{r-mast}(W^z,R_i)`$ if in some maximum agreement subtree of $`W^v`$ and $`R_i`$, the root of $`R_i`$ is mapped to some node $`z`$ in $`W^v`$. On the other hand, $`\text{mast}(W^v,R_i)=\text{mast}(W^v,\overline{R}_j)`$ for some $`ji`$ if in some maximum agreement subtree of $`W^v`$ and $`R_i`$, the root of $`R_i`$ is not mapped to any node $`z`$ in $`W^v`$. ∎

By Lemma 13, we decompose the computation of $`\text{mast}(W^v,R_i)`$ into two parts. The value $`\mathrm{max}\{\text{mast}(W^v,\overline{R}_j)j[1,b],ji\}`$ is determined by answering two queries $`\mathrm{max}\{\text{mast}(W^v,\overline{R_j})j[1,i1]\}`$ and $`\mathrm{max}\{\text{mast}(W^v,\overline{R_j})j[i+1,b]\}`$ in $`O(\mathrm{log}^2n)`$ time by Lemma 12. The computation of $`\mathrm{max}\{\text{r-mast}(W^z,R_i)zW^v\}`$ makes use of a maximum weight matching of some bipartite graph as follows.

Let $`\mathrm{Ch}(z)`$ denote the set of children of a node $`z`$ in a tree. Let $`G_{z,i}\mathrm{Ch}(z)\times \{\overline{R}_1,\mathrm{},\overline{R}_{i1},\overline{R}_{i+1}`$, $`\mathrm{},\overline{R}_b\}`$ be a bipartite graph where $`w\mathrm{Ch}(z)`$ is connected to $`\overline{R}_j`$ if and only if $`\text{mast}(W^w,\overline{R}_j)>0`$. Such an edge has weight $`\text{mast}(W^w,\overline{R}_j)N`$.

###### Fact 14 (see ).

If the root of $`R_i`$ is mapped to $`z`$ in some maximum agreement subtree of $`W^z`$ and $`R_i`$, then a maximum weight matching of $`G_{z,i}`$ consists of at least two edges, and $`\text{mwm}(G_{z,i})=\text{r-mast}(W^z,R_i)`$.

Note that if a maximum weight matching of $`G_{z,i}`$ consists of one edge, it corresponds to an agreement subtree of $`W^z`$ and $`R_i`$ in which the root of $`R_i`$ is not mapped to any node in $`W^z`$. Thus, it is possible that $`\text{mwm}(G_{z,i})>\text{r-mast}(W^z,R_i)`$. Nevertheless, in this case we are no longer interested in the exact value of $`\text{r-mast}(W^z,R_i)`$ since in a maximum agreement subtree of $`W^z`$ and $`R_i`$, the root of $`R_i`$ is not mapped to any node in $`W^z`$. In fact, Lemma 13 can be rewritten with the $`\text{r-mast}(W^z,R_i)`$ replaced by $`\text{mwm}(G_{z,i})`$. Furthermore, since $`G_{z,1}`$, $`G_{z,2}`$, $`\mathrm{}`$, $`G_{z,b}`$ are very similar, the weights of a maximum weight matching cannot be all distinct.

###### Lemma 15.

At least $`bd_z`$ of $`\text{mwm}(G_{z,1})`$, $`\text{mwm}(G_{z,2})`$, $`\mathrm{}`$, $`\text{mwm}(G_{z,b})`$ have the same value, where $`d_z`$ denotes the degree of $`z`$ in $`W`$.

###### Proof.

Consider the bipartite graph $`K\mathrm{Ch}(z)\times \{\overline{R}_1,\mathrm{},\overline{R}_b\}`$ in which a node $`w\mathrm{Ch}(z)`$ is connected to $`\overline{R}_i`$ if and only if $`\text{mast}(W^w,\overline{R}_i)>0`$. This edge is given a weight of $`\text{mast}(W^w,\overline{R}_i)`$. Then, every $`G_{z,i}`$ is a subgraph of $`K`$. Let $`M`$ be a maximum weight matching of $`K`$. Observe that if an $`R_i`$ is not adjacent to any edge in $`M`$, then $`M`$ is also a maximum weight matching of $`G_{z,i}`$. Since $`M`$ contains at most $`d_z`$ edges, there are at least $`bd_z`$ trees $`R_i`$ not adjacent to any edge in $`M`$ and the corresponding $`\text{mwm}(G_{z,i})`$ have the same value. ∎

We next use $`O(n^{1.5}\mathrm{log}N)`$ time to find for all $`z`$ in $`W`$, $`\text{mwm}(G_{z,1}),\mathrm{},\text{mwm}(G_{z,b})`$. The results are to be stored in an array $`A_z`$ of dimension $`b`$ for each node $`z`$, i.e., $`A_z[i]=\text{mwm}(G_{z,i})`$. Note that if we represent each $`A_z`$ as an ordinary array, then filling these arrays entry by entry for all $`zW`$ would cost $`\mathrm{\Omega }(bn)`$ time. Nevertheless, by Lemma 15, most of the weights $`\text{mwm}(G_{z,i})`$ have the same value. Thus, we store these values in sparse arrays. Like an ordinary array, any entry in a sparse array $`A`$ can be read and modified in $`O(1)`$ time. In addition, we require that all the entries in $`A`$ can be initialized to a fixed value in $`O(1)`$ time and that all the distinct values stored in $`A`$ can be retrieved in $`O(m)`$ time, where $`m`$ denotes the number of distinct values in $`A`$. For an implementation of sparse array, see Exercise 2.12, page 71 of .

Before showing how to build these sparse arrays, we illustrate how they support the computation of

$$\mathrm{max}\{\text{mwm}(G_{z,i})zW^v\}=\mathrm{max}\{A_z[i]zW^v\}.$$
(5)
An efficient data structure for answering such a query is given in §B. Let $`m_z`$ be the number of distinct values in $`A_z`$, and $`m=_{zW}(m_z+1)`$. Let $`\alpha (n)`$ denote the inverse Ackermann function. Appendix B shows how to construct a data structure on top of the sparse arrays $`A_z`$ in $`O(m\alpha (|W|))`$ time such that we can retrieve for any $`vW`$ and $`i[1,b]`$ the value of $`\mathrm{max}\{A_z[i]zW^v\}`$ in $`O(\mathrm{log}|W|)`$ time. From Lemma 15, $`m_zd_z+1`$ for all $`zW`$; thus, $`m=O(|W|)`$. Therefore, the data structure can be built in time $`O(m\alpha (|W|))=O(|W|\alpha (|W|))=O(n\mathrm{log}n)`$ and the retrieval time of Equation (5) is $`O(\mathrm{log}|W|)=O(\mathrm{log}n)`$.

To summarize, after building all the necessary data structures, we can retrieve $`\mathrm{max}\{\text{mast}(W^v,\overline{R}_j)j[1,b],ji\}`$ in $`O(\mathrm{log}^2n)`$ time and $`\mathrm{max}\{\text{r-mast}(W^z,R_i)zW^v\}`$ in $`O(\mathrm{log}n)`$ time. Hence, for any $`vW`$ and $`i[1,b]`$, $`\text{mast}(W^v,R_i)`$ can be computed in $`O(\mathrm{log}^2n)`$ time.

To complete our discussion, we show below how to construct a sparse array $`A_z`$ or equivalently compute the weights $`\{\text{mwm}(G_{z,i})i[1,b]\}`$ efficiently. We cannot afford to examine every $`G_{z,i}`$ and compute $`\text{mwm}(G_{z,i})`$ separately. Instead we build only one weighted graph $`G_z\mathrm{Ch}(z)\times \{\overline{R}_1,\mathrm{},\overline{R}_b\}`$ as follows.

For a node $`z`$ in $`W`$, the max-child $`z^{}`$ of $`z`$ is a child of $`z`$ such that the subtree rooted at $`z^{}`$ contains the maximum number of atomic leaves among all the subtrees attached to $`z`$. Let $`\kappa (z)`$ denote the total number of atomic leaves that are in $`W^z`$ but not in $`W^z^{}`$. The edges of $`G_z`$ are specified as follows.

* For any non-max-child $`u`$ of $`z`$, $`G_z`$ contains an edge between $`u`$ and some $`\overline{R}_i`$ if and only if $`\text{mast}(W^u,\overline{R}_i)>0`$. There are at most $`\kappa (z)`$ such edges.
* Regarding the max-child $`z^{}`$ of $`z`$, we only put into $`G_z`$ a limited number of edges between $`z^{}`$ and $`\{\overline{R}_1,\mathrm{},\overline{R}_b\}`$. For each $`\overline{R}_i`$ already connected to some non-max-child of $`z`$, $`G_z`$ has an edge between $`z^{}`$ and $`\overline{R}_i`$ if $`\text{mast}(W^z^{},\overline{R}_i)>0`$. Among all other $`\overline{R}_i`$, we pick $`\overline{R}_i^{}`$ and $`\overline{R}_{i^{\prime \prime }}`$ such that $`\text{mast}(W^z^{},\overline{R}_i^{})`$ and $`\text{mast}(W^z^{},\overline{R}_{i^{\prime \prime }})`$ are the first and second largest.
* Every edge $`(u,\overline{R}_i)`$ in $`G_z`$ is given a weight of $`\text{mast}(W^u,\overline{R}_i)`$.

###### Lemma 16.

For all $`i[1,b]`$, $`\text{mwm}(G_z\{\overline{R}_i\})=\text{mwm}(G_{z,i})`$. Furthermore, $`G_z`$ can be built in $`O((\kappa (z)+1)\mathrm{log}^2n)`$ time.

###### Proof.

The fact that $`\text{mwm}(G_z\{\overline{R}_i\})=\text{mwm}(G_{z,i})`$ follows from the construction of $`G_z`$. Note that $`G_z`$ contains $`O(\kappa (z)+1)`$ edges. All edges in $`G_z`$, except $`(z^{},\overline{R}_i^{})`$ and $`(z^{},\overline{R}_{i^{\prime \prime }})`$, can be found using $`O(\kappa (z))`$ time. The weight of these edges can be found in $`O(\kappa (z)\mathrm{log}n)`$ time using Lemma 12. To identify $`(z^{},\overline{R}_i^{})`$ and $`(z^{},\overline{R}_{i^{\prime \prime }})`$, note that at most $`\kappa (z)`$ instances of $`\overline{R}_i`$ are connected to some non-max-child of $`z`$. All other $`\overline{R}_i`$ are partitioned into at most $`\kappa (z)+1`$ intervals. For each interval, say $`I[1,b]`$, by Lemma 12, the corresponding $`\text{mast}(W^z^{},\overline{R}_i)`$ which attains the maximum in the set $`\{\text{mast}(W^z^{},\overline{R}_j)jI\}`$ can be found in $`O(\mathrm{log}^2n)`$ time. Thus, by scanning all the $`\kappa (z)+1`$ intervals, $`\overline{R}_i^{}`$ can be found in $`O((\kappa (z)+1)\mathrm{log}^2n)`$ time. $`\overline{R}_{i^{\prime \prime }}`$ can be found similarly. ∎

Since $`G_z`$ contains $`O(\kappa (z)+1)`$ edges, and each edge has weight at most $`N`$, we use the Gabow-Tarjan algorithm  to compute $`\text{mwm}(G_z)`$ in $`O(\sqrt{\kappa (z)+1}(\kappa (z)+1)\mathrm{log}N)`$ time. Then, using our algorithm for all-cavity maximum weight matching, we can compute $`\text{mwm}(G_z\{\overline{R}_i\})`$ for all $`i[1,b]`$, and store the results in a sparse array $`A_z`$ in the same amount of time.

Thus, all $`G_z`$ with $`zW`$ can be constructed in time $`_{zW}O((\kappa (z)+1)\mathrm{log}^2n)`$, which is $`O(n^{1.5}\mathrm{log}N)`$ as $`_{zW}\kappa (z)=O(n\mathrm{log}n)`$ . Given all $`G_z`$, the time for computing $`A_z`$ for all $`zW`$ is $`O(_{zW}(\kappa (z)+1)^{1.5}\mathrm{log}N)`$.

###### Lemma 17.

$`_{zW}(\kappa (z)+1)^{1.5}\mathrm{log}N=O(n^{1.5}\mathrm{log}N)`$.

###### Proof.

Let $`T(W)=_{zW}(\kappa (z)+1)^{1.5}\mathrm{log}N`$. Let $`P`$ be a path starting from the root of $`W`$ such that every next node is the max-child of its predecessor. Then $`_{zP}\kappa (z)|W|n`$. Let $`\chi (P)`$ denote the set of subtrees attached to some node on $`P`$. The subtrees in $`\chi (P)`$ are label-disjoint and each has size at most $`n/2`$. Thus,

$`T(W)`$ $``$ $`{\displaystyle \underset{zP}{}}(\kappa (z)+1)^{1.5}\mathrm{log}N+{\displaystyle \underset{W^{}\chi (P)}{}}T(W^{})`$
$``$ $`n^{1.5}\mathrm{log}N+{\displaystyle \underset{W^{}\chi (P)}{}}T(W^{})`$
$`=`$ $`O(n^{1.5}\mathrm{log}N).`$
## 7 Auxiliary information for $`𝑿`$ with one or two shrunk leaves

### 7.1 $`𝑿`$ has one shrunk leaf

Consider the computation of $`\text{mast}(W,X)`$. According to the algorithm, $`\text{mast}(W,X)`$ will spawn $`b`$ subproblems $`\text{mast}(W_1,X_1)`$,$`\mathrm{}`$, $`\text{mast}(W_b,X_b)`$, which are defined by an internal node $`y`$ in $`X`$ adjacent to the nodes $`v_1,\mathrm{},v_b`$. Also, for every $`i[1,b]`$, $`R_i`$ and $`\overline{R}_i`$ denote the subtrees $`X^{v_iy}`$ and $`X^{yv_i}`$, respectively. Suppose that $`X`$ has one shrunk leaf and without loss of generality, assume that the shrunk leaf of $`X`$ is in $`\overline{R}_b`$, i.e., $`X_b`$ has two shrunk leaves and all the other $`X_i`$ have one shrunk leaf each. This section shows how to find the auxiliary information required by $`W_1,\mathrm{},W_b`$ in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Lemma 18.

The auxiliary information required by $`W_1,\mathrm{},W_{b1}`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Proof.

Note that $`\text{mast}(W_1,X_1),\mathrm{},\text{mast}(W_{b1},X_{b1})`$ are almost identical to the subproblems considered in §6 in that all the $`X_i`$ have exactly one shrunk leaf each. Using exactly the same approach, we can compute the auxiliary information in $`W_1`$, $`\mathrm{}`$, $`W_{b1}`$. ∎

The remaining section focuses on computing the auxiliary information in $`W_b`$. Let $`\gamma _1`$ and $`\gamma _2`$ be the two shrunk leaves of $`X_b`$. Assume that $`\gamma _1`$ is also a shrunk leaf in $`X`$, and $`\gamma _2`$ represents $`R_b`$. Let $`Q^+`$ be the subtree obtained by connecting $`\gamma _1`$ and $`R_b`$ together with a node. To compute the auxiliary information in $`W_b`$, we require the values $`\text{mast}(W^v,\gamma _1)`$, $`\text{mast}(W^v,R_b)`$, and $`\text{mast}(W^v,Q^+)`$ for all nodes $`vW`$. These values are computed based on the following lemma.

###### Lemma 19.

$`\text{mast}(W^v,\gamma _1)`$, $`\text{mast}(W^v,R_b)`$, and $`\text{mast}(W^v,Q^+)`$ for all nodes $`vW`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Proof.

By Lemma 7, $`\text{mast}(W,R_b)`$ and $`\text{mast}(W,Q^+)`$ can be computed in time $`O(n^{1.5}\mathrm{log}N)`$ and afterwards, for each node $`vW`$, $`\text{mast}(W^v,R_b)`$ and $`\text{mast}(W^v,Q^+)`$ can be retrieved in $`O(1)`$ time. For each node $`vW`$, $`\text{mast}(W^v,\gamma _1)`$ is the auxiliary information stored at $`v`$ in $`W`$ and can be retrieved in $`O(1)`$ time. ∎

Now, we are ready to compute the auxiliary information stored at each node $`vW_b`$. No auxiliary information is required for atomic leaves. Below, Lemma 20 and Lemma 21 show that using $`O(n)`$ additional time, we can compute the auxiliary information in internal nodes and in compressed leaves, respectively. In summary, the auxiliary information in $`W_1,\mathrm{},W_b`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Lemma 20.

Given $`\text{mast}(W^v,\gamma _1)`$, $`\text{mast}(W^v,R_b)`$, and $`\text{mast}(W^v,Q^+)`$ for all nodes $`vW`$, the auxiliary information stored at the internal nodes in $`W_b`$ can be found in $`O(n)`$ time.

###### Proof.

Let $`J_b`$ be the set of labels of the atomic leaves of $`W_b`$. An internal node $`v`$ can be either an auxiliary node, a compressed node, or a node of $`W|J_b`$. If $`v`$$`W|J_b`$, then $`vW`$. Thus, $`\alpha _1(v)=\text{mast}(W^v,\gamma _1)`$ and $`\alpha _2(v)=\text{mast}(W^v,R_b)`$.

If $`v`$ is a compressed node, then we need to compute $`\alpha _1(v),\alpha _2(v)`$ and $`\alpha _+(v)`$. Recall that $`v`$ represents some tree path $`\sigma =v_1,\mathrm{},v_k`$ of $`W`$, where $`v_1`$ is the closest to the root, i.e., $`v=v_1`$. Thus, $`\alpha _1(v)=\text{mast}(W^{v_1},\gamma _1)`$, $`\alpha _2(v)=\text{mast}(W^{v_1},R_b)`$, and $`\alpha _+(v)=\text{mast}(W^{v_1},Q^+)`$.

Thus, $`O(n)`$ time is sufficient for finding the auxiliary information stored at every internal node of $`W_b`$. ∎

###### Lemma 21.

Given $`\text{mast}(W^v,\gamma _1)`$, $`\text{mast}(W^v,R_b)`$, and $`\text{mast}(W^v,Q^+)`$ for all nodes $`vW`$, the auxiliary information stored at the compressed leaves in $`W_b`$ can be found in $`O(n)`$ time.

###### Proof.

If $`v`$ is a compressed leaf in $`W`$, $`v`$’s parent $`u`$ must not be an auxiliary node. Depending on whether $`u`$ is a compressed node, we have two cases.

Case A: $`u`$ is not a compressed node. We must compute $`\alpha _1(v),\alpha _2(v),\alpha _+(v),\beta (v)`$. Note that $`u`$ is also in $`W`$. When $`W_b`$ is constructed from $`W`$, some of the subtrees of $`W`$ attached to $`u`$ are replaced by $`v`$ and no longer exist in $`W_b`$. Let $`W^{p_1},\mathrm{},W^{p_k}`$ be these subtrees. Observe that both $`v`$ and $`W^{p_1},\mathrm{},W^{p_k}`$ represent the same set of subtrees in $`T_1`$. Thus,

* $`\alpha _1(v)=\mathrm{max}\{\text{mast}(W^{p_i},\gamma _1)1ik\}`$;
* $`\alpha _2(v)=\mathrm{max}\{\text{mast}(W^{p_i},R_b)1ik\}`$;
* $`\alpha _+(v)=\mathrm{max}\{\text{mast}(W^{p_i},Q^+)1ik\}`$;
* $`\beta (v)=\mathrm{max}\{\text{mast}(W^{p_i},\gamma _1)+\text{mast}(W^{p_j},R_b)1ijk\}`$.

These four values can be found in $`O(k)`$ time. Since $`W^{p_1},\mathrm{},W^{p_k}`$ are subtrees attached to $`u`$ in $`W`$, $`k`$ is at most the degree of $`u`$ in $`W`$. Moreover, the sum of the degrees of all internal nodes of $`W`$ is $`O(n)`$. Therefore, $`O(n)`$ time suffices to compute the auxiliary information for all the compressed leaves in $`W_b`$ whose parents are not compressed node.

Case B: $`u`$ is a compressed node. We need to compute $`\alpha _1(v)`$, $`\alpha _2(v)`$, $`\alpha _+(v)`$, $`\beta (v)`$, $`\beta _{12}(v)`$ and $`\beta _{12}(v)`$. Note that $`u`$ is compressed from a tree path $`p_1,\mathrm{},p_k`$ in $`W`$ where $`p_1`$ is the closest to the root. Moreover, $`v`$ is compressed from the subtrees hanging between $`p_1`$ and $`p_k`$. For every $`i[1,k]`$, let $`𝒯_i`$ be the set of subtrees of $`W`$ attached to $`p_i`$ that are compressed into $`v`$. Both $`v`$ and the subtrees in $`_{1ik}𝒯_i`$ represent the same set of subtrees in $`T_1`$. The auxiliary information stored at $`v`$ can be expressed as follows.

* $`\alpha _1(v)=\mathrm{max}\{\text{mast}(W^q,\gamma _1)W^q𝒯_i\text{ for some }i[1,k]\}`$.
* $`\alpha _2(v)=\mathrm{max}\{\text{mast}(W^q,R_b)W^q𝒯_i`$ for some $`i[1,k]\}`$.
* $`\alpha _+(v)=\mathrm{max}\{\text{mast}(W^q,Q^+)W^q𝒯_i`$ for some $`i[1,k]\}`$.
* $`\beta (v)=\mathrm{max}_{1ik}[\mathrm{max}\{\text{mast}(W^q,\gamma _1)+\text{mast}(W^q^{},R_b)W^q,W^q^{}𝒯_i\}]`$.
* $`\beta _{12}(v)=\mathrm{max}_{1j<ik}[\mathrm{max}\{\text{mast}(W^q,\gamma _1)W^q𝒯_i\}+\mathrm{max}\{\text{mast}(W^q^{},R_b)W^q^{}𝒯_j\}]`$.
* $`\beta _{21}(v)=\mathrm{max}_{1j<ik}[\mathrm{max}\{\text{mast}(W^q,R_b)W^q𝒯_i\}+\mathrm{max}\{\text{mast}(W^q^{},\gamma _1)W^q^{}𝒯_j\}]`$.

These values can be found in $`O(_{1ik}d_{p_i})`$ time, where $`d_{p_i}`$ is the degree of $`p_i`$ in $`W`$. Thus, the auxiliary information for every compressed leaf of $`W_b`$, whose parents are compressed nodes, can be computed in $`O(n)`$ time. ∎

### 7.2 $`𝑿`$ has two shrunk leaves

Recall that the subproblems $`\text{mast}(W_1,X_1)`$, $`\mathrm{}`$, $`\text{mast}(W_b,X_b)`$ are spawned from $`\text{mast}(W,X)`$. This section considers the case where $`X`$ has two shrunk leaves. Without loss of generality, assume that the two shrunk leaves are in $`\overline{R}_{b1}`$ and $`\overline{R}_b`$, respectively. Then, $`X_1,\mathrm{}`$, $`X_{b2}`$ each have one shrunk leaf. $`X_{b1}`$ and $`X_b`$ each have two shrunk leaves. Below, we show how to compute the auxiliary informations of $`W_1,\mathrm{},W_b`$ in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Lemma 22.

The auxiliary information required by $`W_1,\mathrm{},W_{b2}`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Proof.

The proof of this lemma is the same as that of Lemma 18. ∎

For the remaining subproblems $`\text{mast}(W_{b1},X_{b1})`$ and $`\text{mast}(W_b,X_b)`$, both $`X_{b1}`$ and $`X_b`$ have two shrunk leaves. By symmetry, it suffices to discuss the computation of $`\text{mast}(W_b,X_b)`$ only. Lemma 23 shows that the auxiliary information in $`W_b`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time. Therefore, the auxiliary information in $`W_1,\mathrm{},W_b`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Lemma 23.

The auxiliary information in $`W_b`$ can be computed in $`O(n^{1.5}\mathrm{log}N)`$ time.

###### Proof.

Let $`\gamma _1`$ and $`\gamma _2`$ be the two shrunk leaves of $`X_b`$. Assume that $`\gamma _1`$ is also a shrunk leaf in $`X`$ and $`\gamma _2`$ represents $`R_b`$, i.e., $`\gamma _2`$ represents the subtree $`U_2^{v_by}`$ of $`T_1`$. Let $`Q^+`$ be the subtree obtained by connecting $`\gamma _1`$ and $`R_b`$. By the same argument as in Lemma 20 and 21, the auxiliary information in $`W_b`$ can be computed based on the values $`\text{mast}(W^v,\gamma _1)`$, $`\text{mast}(W^v,R_b)`$ and $`\text{mast}(W^v,Q^+)`$ for all $`vW`$. The value $`\text{mast}(W^v,\gamma _1)`$ can be found in $`W`$. The values $`\text{mast}(W^v,R_b)`$ and $`\text{mast}(W^v,Q^+)`$ for all $`vW`$ can be retrieved in $`O(1)`$ time after $`\text{mast}(W,R_b)`$ and $`\text{mast}(W,Q^+)`$ are computed in $`O(n^{1.5}\mathrm{log}N)`$ time based on Lemma 9. Then the auxiliary information in $`W_b`$ can be computed in $`O(n)`$ time. ∎

## 8 Extension

We have presented an $`O(N^{1.5}\mathrm{log}N)`$-time algorithm for computing a maximum agreement subtree of two unrooted evolutionary trees of at most $`N`$ nodes each. This algorithm can be modified slightly to compute a maximum agreement subtree for two mixed trees $`M_1`$ and $`M_2`$.

For a mixed tree $`M`$, a node $`\mathrm{}`$ is consistent with a node $`u`$ if the directed edges on the path between $`u`$ and $`\mathrm{}`$ all point away from $`u`$. Let $`M^u`$ be the rooted tree constructed by assigning $`u`$ in $`M`$ as the root and removing the nodes of $`M`$ inconsistent with $`u`$. Given two mixed tree $`M_1`$ and $`M_2`$, we define a maximum agreement subtree of $`M_1`$ and $`M_2`$ to be the one with the largest number of labels among the maximum agreement subtree of $`M_1^u`$ and $`M_2^v`$ over all nodes $`uM_1`$ and $`vM_2`$. That is,

$$\text{mast}(M_1,M_2)=\mathrm{max}\{\text{mast}(M_1^u,M_2^v)uM_1,vM_2\}.$$

As in the unrooted case, to compute $`\text{mast}(M_1,M_2)`$, we find a separator $`y`$ of $`M_1`$ and compute $`\text{mast}(M_1^y,M_2)`$. However, we need to delete the nodes of $`M_1`$ not in $`M_1^y`$. When computing $`\text{mast}(M_1^y,M_2)`$, we construct some rooted subtrees of $`M_2`$. Again, we delete the nodes of $`M_2`$ not in these rooted subtrees. Such deletions are straightforward and do not increase the time complexity of computing $`\text{mast}(M_1,M_2)`$. Thus, $`\text{mast}(M_1,M_2)`$ can be computed in $`O(N^{1.5}\mathrm{log}N)`$ time.

## Acknowledgments

The authors thank the referees for helpful comments.

## Appendix A Computing mast($`𝑾_\mathrm{𝟏}\mathbf{,}𝑾_\mathrm{𝟐}`$)

Let $`T_1`$ and $`T_2`$ be rooted evolutionary trees. Let $`R_1`$ and $`R_2`$ be two label-disjoint rooted subtrees of $`T_2`$. Let $`W_1=T_1(R_1,R_2)`$ and $`W_2=T_2(R_1,R_2)`$. This section shows that $`\text{mast}(W_1,W_2)`$ can be computed as if $`W_1`$ and $`W_2`$ were ordinary rooted evolutionary trees  with some special procedures on handling compressed and shrunk leaves. Note that the case where $`W_1`$ and $`W_2`$ are compressed and shrunk with respect to a subtree can be treated as the special case where $`R_1`$ is empty.

###### Lemma 24.

We can compute $`\text{mast}(W_1,W_2)`$ in $`O(n^{1.5}\mathrm{log}N)`$ time, where $`n=\mathrm{max}\{|W_1|,|W_2|\}`$ and $`N=\mathrm{max}\{|T_1|,|T_2|\}`$. Then, we can retrieve $`\text{mast}(W_1^u,W_2)`$ for any node $`u`$ of $`W_1`$ in $`O(1)`$ time.

###### Proof.

We adopt the framework of Farach and Thorup’s algorithm , which is essentially a sparsified dynamic programming based on the following formula. For any internal nodes $`u`$ of $`W_1`$ and $`v`$ of $`W_2`$,

$`\text{mast}(W_1^u,W_2^v)=\mathrm{max}\{\begin{array}{c}\mathrm{max}\{\text{mast}(W_1^x,W_2^v)x\text{ is a child of }u\};\hfill \\ \mathrm{max}\{\text{mast}(W_1^u,W_2^y)y\text{ is a child of }v\};\hfill \\ \text{r-mast}(W_1^u,W_2^v),\hfill \end{array}`$ (9)
where $`\text{r-mast}(W_1^u,W_2^v)`$ denotes the maximum size of all the agreement subtrees of $`W_1^u`$ and $`W_2^v`$ in which $`u`$ is mapped to $`v`$.

Our algorithm differs from Farach and Thorup’s algorithm in the way how each individual $`\text{mast}(W_1^u,W_2^v)`$ is computed. When $`W_1`$ and $`W_2`$ are ordinary evolutionary trees, each $`\text{mast}(W_1^u,W_2^v)`$ is found by computing a maximum weight matching of some bipartite graph, and it takes $`O(n^{1.5}\mathrm{log}n)`$ time to compute $`\text{mast}(W_1,W_2)`$. Below, we show that when $`W_1`$ and $`W_2`$ have compressed and shrunk leaves, each $`\text{mast}(W_1^u,W_2^v)`$ can be found either in constant time or by computing at most two maximum weight bipartite matchings of similar graphs but with edge weights bounded by $`N`$ instead of $`n`$. Thus, we can compute $`\text{mast}(W_1,W_2)`$ using the same sparsified dynamic programming in ; as a by-product, we can afterwards retrieve $`\text{mast}(W_1^u,W_2)`$ for any node $`u`$ of $`W_1`$ in $`O(1)`$ time. The enlarged upper bound of edge weights increases the time complexity to $`O(n^{1.5}\mathrm{log}N)`$, though.

In the rest of this section, we show how each $`\text{mast}(W_1^u,W_2^v)`$ is computed. First, we consider the case when $`u`$ is a leaf. The following case analysis shows that $`O(1)`$ time suffices to compute $`\text{mast}(W_1^u,W_2^v)`$.

Case 1: $`u`$ is an atomic leaf. If $`W_2^v`$ contains a leaf with the same label as that of $`u`$, then $`\text{mast}(W_1^u,W_2^v)=1`$; otherwise it equals zero.

Case 2: $`u`$ is an auxiliary leaf. Then, $`\text{mast}(W_1^u,W_2^v)=0`$.

Case 3: $`u`$ is a compressed leaf. By definition, $`u`$ can only be mapped to $`\gamma _1`$, $`\gamma _2`$ or the least common ancestor $`y_c`$ of $`\gamma _1`$ and $`\gamma _2`$. If $`W_2^v`$ has no shrunk leaves, then $`\text{mast}(W_1^u,W_2^v)=0`$. If $`W_2^v`$ has only one shrunk leaf, say $`\gamma _1`$, then $`\text{mast}(W_1^u,W_2^v)=\alpha _1(u)`$. If $`W_2^v`$ has two shrunk leaves, $`W_2^v`$ must also contain $`y_c`$ and $`\text{mast}(W_1^u,W_2^v)=\mathrm{max}\{\alpha _1(u),\alpha _2(u),\alpha _+(u)\}`$.

Next, we consider the case when $`u`$ is an internal node. Assume that $`v`$ is an atomic leaf. Then $`\text{mast}(W_1^u,W_2^v)=1`$ if $`W_1^u`$ contains a leaf with the same label as that of $`v`$, and zero otherwise. If $`v`$ is a shrunk leaf, say $`\gamma _1`$, then $`\text{mast}(W_1^u,W_2^v)=\alpha _1(u)`$. It remains to consider the case when $`v`$ is an internal node. Due to the nature of dynamic programming, we only need to compute $`\text{r-mast}(W_1^u,W_2^v)`$, then we can apply the Equation (9) to compute $`\text{mast}(W_1^u,W_2^v)`$. We further divide our discussion into the following three cases.

Case 1: $`u`$ is an auxiliary internal node. In such case, $`u`$ has only two children, one of them is an auxiliary leaf. From definition, an auxiliary leaf will not be mapped to any node in any agreement subtree of $`W_1^u`$ and $`W_2^v`$; thus, there is no agreement subtree in which $`u`$ is mapped to $`v`$ and $`\text{r-mast}(W_1^u,W_2^v)=0`$.

Case 2: $`u`$ is an ordinary internal node. As in , we first construct the bipartite graph defined as follows: Let $`A`$ and $`B`$ be the set of children of $`u`$ and $`v`$, respectively; define $`G[A,B]`$ to be the bipartite graph formed by the edges $`(x,y)A\times B`$ with $`\text{mast}(W_1^x,W_2^y)>0`$, and $`(x,y)`$ is given a weight $`\text{mast}(W_1^x,W_2^y)`$.

If none of $`u`$’s children is an auxiliary leaf, then $`\text{r-mast}(W_1^u,W_2^v)=\text{mwm}(G[A,B])`$. Otherwise, let $`\overline{z}`$ be the child of $`u`$ which is an auxiliary. In this case, $`u`$ also has a compressed child $`z`$. Other than $`z`$ and $`\overline{z}`$, no other child of $`u`$ is a compressed and auxiliary leaf. On the other hand, consider the rooted subtrees $`W_2^x`$ rooted at the children $`x`$ of $`v`$. If the shrunk leaves appear together in one of such subtrees, then by definition, $`\overline{z}`$ cannot be mapped to any shrunk leaf in any agreement subtree of $`W_1^u`$ and $`W_2^v`$, and $`\text{r-mast}(W_1^u,W_2^v)=\text{mwm}(G[A,B]\{\overline{z}\})`$. If the shrunk leaves appear in two different subtrees rooted at two children $`y_1`$ and $`y_2`$ of $`v`$, then

$$\text{r-mast}(W_1^u,W_2^v)=\mathrm{max}\{\text{mwm}(G[A,B]\{\overline{z}\}),\text{mwm}(G[A,B]\{z,\overline{z},y_1,y_2\})+\beta (z)\}.$$

Case 3: $`u`$ is a compressed internal node. By definition of a compressed node, the structure of $`W_1^u`$ is very restrictive—$`u`$ has exactly three children $`z`$, $`\overline{z}`$, and an auxiliary internal node $`\overline{u}`$; $`\overline{u}`$ has two children, an auxiliary leaf $`\overline{\overline{z}}`$ and an uncompressed internal node $`w`$; see Figure 6. To find

$`\text{r-mast}(W_1^u,W_2^v)`$, we note that there are only six possible ways on how the $`z,\overline{z},\overline{\overline{z}}`$ are mapped to $`\gamma _1`$ and $`\gamma _2`$. We consider each of these cases and $`\text{r-mast}(W_1^u,W_2^v)`$ is the maximum of the values found. We only discuss the case where $`\gamma _1`$ and $`\gamma _2`$ are mapped to $`z`$ and $`\overline{\overline{z}}`$, respectively. The other cases can be handled similarly. Let $`P`$ be the path between $`\gamma _2`$ and $`y_c`$. Let $`\text{S}(P)`$ denote the set of subtrees hanged on $`P`$. The size of the largest agreement subtrees of $`W_1^u`$ and $`W_2^v`$ in which $`\gamma _1`$ and $`\gamma _2`$ are mapped to $`z`$ and $`\overline{z}`$, respectively, equals

$$\beta _{12}(z)+\mathrm{max}\{\text{mast}(W_1^w,\tau )\tau \text{S}(P)\}.$$
(10)
Note that using the technique in , we can precompute $`\mathrm{max}\{\text{mast}(W_1^x,\tau )\tau \text{S}(P)\}`$ for all $`xW_1`$ in $`O(n^{1.5}\mathrm{log}N)`$ time. Afterwards, (10) can be found in constant time. ∎

## Appendix B Preprocessing for finding $`\mathrm{𝐦𝐚𝐱}\mathbf{\{}𝑨_𝒛\mathbf{[}𝒊\mathbf{]}\mathbf{|}𝒛\mathbf{}𝑾^𝒗\mathbf{\}}`$

Let $`h`$ be the number of nodes in $`W`$. Consider the $`h`$ arrays $`A_z`$ of dimension $`b`$ where $`zW`$. Recall that $`m_z`$ is the number of distinct values in $`A_z`$, and $`m=_{zW}(m_z+1)`$. This section describes an $`O(m\alpha (h))`$-time preprocessing, which supports finding $`\mathrm{max}\{A_z[i]zW^v\}`$, for any $`i[1,b]`$ and any node $`v`$ of $`W`$, in $`O(\mathrm{log}h)`$ time.

By definition, each $`A_z`$ has at least $`bm_z`$ entries storing some common value $`c_z`$. For every $`i[1,b]`$, let $`\mathrm{\Gamma }_i`$ be the set of nodes $`z`$ where $`A_z[i]`$ stores a value different from $`c_z`$. Note that $`m_z=_{1ib}|\mathrm{\Gamma }_i|`$. We assume that each node of $`W`$ is identified uniquely by an integer in $`[1,h]`$ assigned by a preorder tree traversal . For any node $`vW`$, let $`\beta (v)`$ be the number of proper descendents of $`v`$ in $`W`$.

Based on Lemma 25, $`\mathrm{max}\{A_z[i]zW^v\}=\mathrm{max}\{A_z[i]z[v,v+\beta (v)]\}`$ for any $`vW`$, $`i[1,b]`$. Therefore, to solve our problem, it is sufficient to give an $`O(m\alpha (h))`$-time preprocessing to support finding $`\mathrm{max}\{A_z[i]zH\}`$ for any $`i[1,b]`$ and any interval $`H[1,h]`$ in $`O(\mathrm{log}h)`$ time.

###### Lemma 25.

For any $`vW`$ and $`i[1,b]`$, $`\mathrm{max}\{A_z[i]zW^v\}=\mathrm{max}\{A_z[i]z[v,v+\beta (v)]\}.`$

###### Proof.

Straightforward. ∎

Our preprocessing does not work on each sequence $`A_1[i],A_2[i],\mathrm{},A_h[i]`$ directly. Instead, it first draws out useful information about the common values $`c_z`$ stored in the sequences and applies a contraction technique to shorten each sequence. Then, it executes Fact 26 on these shortened sequences.

###### Fact 26 (see ).

Given any sequence $`a_1,\mathrm{},a_h`$ of real numbers, we can preprocess these $`h`$ numbers in $`O(h)`$ time so that we can find the maximum of any subsequence $`a_x,a_{x+1},\mathrm{},a_y`$ in $`O(\alpha (h))`$ time.

Our preprocessing is detailed as follows. Its time complexity is $`O(m\alpha (h))`$ as shown in Lemma 27.

1. For each $`i[1,b]`$, find $`\mathrm{\Gamma }_i`$ and arrange the integers in $`\mathrm{\Gamma }_i`$ in ascending order.
2. Apply Fact 26 to the sequence $`c_1,\mathrm{},c_h`$.
3. For every non-empty $`\mathrm{\Gamma }_i=\{x_1<\mathrm{}<x_d\}`$, compute $`\beta _{\mathrm{}}=\mathrm{max}\{A_x[i]x(x_{\mathrm{}},x_{\mathrm{}+1})\}`$ for every $`\mathrm{}[1,d1]`$, and then apply Fact 26 to the sequence $`A_{x_1}[i],\beta _1,A_{x_2}[i],\mathrm{},\beta _{d1},A_{x_d}[i].`$

###### Lemma 27.

The preprocessing requires $`O(m\alpha (h))`$ time.

###### Proof.

We can examine all the entries of $`A_z`$ whose values differ from $`c_z`$ in $`O(m_z)`$ time. By examining all such entries of $`A_1,\mathrm{},A_h`$, we can construct $`\mathrm{\Gamma }_i`$ and arrange the integers in $`\mathrm{\Gamma }_i`$ in ascending order. Thus, Step 1 takes $`O(m)`$ time. Step 2 takes $`O(h)=O(m)`$ time. For Step 3, we first analyze the time required to process one nonempty $`\mathrm{\Gamma }_i=\{x_1<\mathrm{}<x_d\}`$. Note that $`(x_{\mathrm{}},x_{\mathrm{}+1})\mathrm{\Gamma }_i=\varphi `$ for every $`\mathrm{}[1,d1]`$. Thus, $`\beta _{\mathrm{}}=\mathrm{max}\{c_xx(x_{\mathrm{}},x_{\mathrm{}+1})\}`$ can be computed in $`O(\alpha (n))`$ time using the result of Step 2. Summing over all $`\mathrm{}[1,d1]`$, computing all $`\beta _{\mathrm{}}`$ takes $`O(|\mathrm{\Gamma }_i|\alpha (h))`$ time. Applying Fact 26 to the sequence $`A_{x_1}[i]`$, $`\beta _1`$, $`A_{x_2}[i]`$, $`\mathrm{}`$ $`\beta _{d1}`$, $`A_{x_d}[i]`$ takes $`O(|\mathrm{\Gamma }_i|)`$ time. In total, it takes $`O(|\mathrm{\Gamma }_i|\alpha (h))`$ time to process one $`\mathrm{\Gamma }_i`$, and Step 3 takes $`O(|\mathrm{\Gamma }_i|\alpha (h))=O(m\alpha (h))`$ time. Thus, the total time of our preprocessing is $`O(m\alpha (h))`$. ∎

After the preprocessing, each query can be answered in $`O(\mathrm{log}h)`$ time as stated in the following lemma.

###### Lemma 28.

After the preprocessing, $`\mathrm{max}\{A_z[i]zH\}`$ can be found in $`O(\mathrm{log}n)`$ time for any $`i[1,b]`$ and any interval $`H[1,h]`$.

###### Proof.

Let $`H=[p,q]`$. A crucial step is to find $`[p,q]\mathrm{\Gamma }_i`$. Without loss of generality, assume $`\mathrm{\Gamma }_i\varphi `$. To find $`[p,q]\mathrm{\Gamma }_i`$, we first find the smallest integer $`x_s`$ in $`\mathrm{\Gamma }_i`$ that is greater than $`p`$, and the largest integer $`x_t`$ in $`\mathrm{\Gamma }_i`$ that is smaller than $`q`$. Since $`\mathrm{\Gamma }_i`$ is sorted, we can find $`x_s`$ and $`x_t`$ in $`O(\mathrm{log}|\mathrm{\Gamma }_i|)=O(\mathrm{log}h)`$ time. If $`x_s>x_t`$, then $`[p,q]\mathrm{\Gamma }_i=\varphi `$; otherwise, $`[p,q]\mathrm{\Gamma }_i`$ is the set of integers between $`x_s`$ and $`x_t`$ in $`\mathrm{\Gamma }_j`$.

If $`[p,q]\mathrm{\Gamma }_i=\varphi `$, then $`\mathrm{max}\{A_x[i]x[p,q]\}`$ $`=`$ $`\mathrm{max}\{c_xx[p,q]\}`$. Because of Step 1 of our preprocessing, we can find $`\mathrm{max}\{c_xx[p,q]\}`$ in $`O(\alpha (h))`$ time.

If $`[p,q]\mathrm{\Gamma }_i=\{x_s<x_{s+1}<\mathrm{}<x_t\}`$, then $`[p,q]=[p,x_s1]\{x_s\}(x_s,x_{s+1})`$ $`\mathrm{}\{x_t\}[x_t+1,q]`$ and $`\mathrm{max}\{A_z[i]z[p,q]\}`$ equals the maximum of

1. $`\mathrm{max}\{A_x[i]x[p,x_s1]\}`$,
2. $`\mathrm{max}\{A_x[i]x\{x_s\}(x_s,x_{s+1})\mathrm{}(x_{t1},x_t)\{x_t\}\}`$,
3. $`\mathrm{max}\{A_x[i]x[x_t+1,q]\}`$.

Note that Item 2 equals the maximum of $`A_{x_s}[i],\beta _s,\mathrm{},\beta _{t1},A_{x_t}[i]`$, which can be computed in $`O(\alpha (h))`$ time after Step 3 of our preprocessing. Since $`\mathrm{\Gamma }_i[p,x_s1]=\varphi `$ and $`\mathrm{\Gamma }_i[x_t+1,q]=\varphi `$, Step 2 enables us to compute Items 1 and 3 in $`O(\alpha (h))`$ time. As a result, $`\mathrm{max}\{A_z[i]z[p,q]\}`$ can be answered in $`O(\mathrm{log}h)`$ time. ∎