text:
# An Even Faster and More Unifying Algorithm for Comparing Trees via Unbalanced Bipartite Matchings11footnote 1This paper combines results of three conference papers: (1) A faster and unifying algorithm for comparing trees. In Proceedings of the 11th Symposium on Combinatorial Pattern Matching, pages 129-142, 2000. (2) Unbalanced and hierarchical bipartite matchings with applications to labeled tree comparison. In Proceedings of the 11th International Symposium on Algorithms and Computation, pages 479-490, 2000. (3) A decomposition theorem for maximum weight bipartite matchings with applications to evolutionary trees. In Proceedings of the 8th Annual European Symposium on Algorithms, pages 438-449, 1999.

## 1 Introduction

A labeled tree is a rooted tree with an arbitrary subset of nodes labeled with symbols. In recent years, many algorithms for comparing such trees have been developed for diverse application areas including biology , chemistry , linguistics , computer vision , and structured text databases .

A widely used measure of the similarity of two labeled trees is the notion of a maximum agreement subtree defined as follows. A labeled tree $`R`$ is a label-preserving homeomorphic subtree of another labeled tree $`T`$ if there exists a one-to-one mapping $`f`$ from the nodes of $`R`$ to those of $`T`$ such that for any nodes $`u,v,w`$ of $`R`$, (1) $`u`$ and $`f(u)`$ have the same label; and (2) $`w`$ is the least common ancestor of $`u`$ and $`v`$ if and only if $`f(w)`$ is the least common ancestor of $`f(u)`$ and $`f(v)`$. Let $`T_1`$ and $`T_2`$ be two labeled trees. An agreement subtree of $`T_1`$ and $`T_2`$ is a labeled tree which is also a label-preserving homeomorphic subtree of the two trees. A maximum agreement subtree is one which maximizes the number of labeled nodes. Let mast$`(T_1,T_2)`$ denote the number of labeled nodes in a maximum agreement subtree of $`T_1`$ and $`T_2`$.

In the literature, many algorithms for computing a maximum agreement subtree have been developed. These algorithms focus on the special cases where $`T_1`$ and $`T_2`$ are either (1) uniformly labeled trees, i.e., trees with all their nodes unlabeled, or equivalently, labeled with the same symbol or (2) evolutionary trees , i.e., leaf-labeled trees with distinct symbols for distinct leaves.

We denote $`n`$ as the number of nodes in the labeled trees $`T_1`$ and $`T_2`$, and $`d`$ as the maximum degree of $`T_1`$ and $`T_2`$. For uniformly labeled trees $`T_1`$ and $`T_2`$, Chung  gave an algorithm to determine whether $`T_1`$ is a label-preserving homeomorphic subtree of $`T_2`$ using $`O(n^{2.5})`$ time. Gupta and Nishimura  gave an algorithm which actually computes a maximum agreement subtree of $`T_1`$ and $`T_2`$ in $`O(n^{2.5}\mathrm{log}n)`$ time. For evolutionary trees, Steel and Warnow  gave the first polynomial-time algorithm for computing a maximum agreement subtree. Farach and Thorup  improved the time complexity from $`O(n^{4.5}\mathrm{log}n)`$ to $`O(n^{1.5}\mathrm{log}n)`$. Faster algorithms for the case $`d=O(1)`$ were also discovered. The algorithm of Farach, Przytycka and Thorup  runs in $`O(\sqrt{d}n\mathrm{log}^3n)`$ time, and that of Kao  takes $`O(nd^2\mathrm{log}^2n\mathrm{log}d)`$ time. Cole et al.  gave an $`O(n\mathrm{log}n)`$-time algorithm for the case where $`T_1`$ and $`T_2`$ are binary trees. Przytycka  attempted to generalize the algorithm of Cole et al. so that the degree-2 restriction could be removed with the running time being $`O(\sqrt{d}n\mathrm{log}n)`$.

For unrestricted labeled trees (i.e., trees where labels are not restricted to leaves and may not be distinct), little work has been reported, but they have applications in several contexts . For example, labeled trees are used to represent sentences in a structural text database  and querying such a database involves comparison of trees; an XML document can also be represented by a labeled tree . Instead of solving special cases, this paper gives an algorithm to compute $`\text{mast}(T_1,T_2)`$ where $`T_1`$ and $`T_2`$ are unrestricted labeled trees. As detailed below, our algorithm not only is more general but also uniformly improves or matches the previously best algorithms for subtree homeomorphism and evolutionary tree comparison.

Let $`\mathrm{\Delta }_{T_1,T_2}`$ (or simply $`\mathrm{\Delta }`$ when the context is clear) $`=`$ $`_{uT_1}_{vT_2}\delta (u,v)`$ where $`\delta (u,v)=1`$ if nodes $`u`$ and $`v`$ are labeled with the same symbol, and $`0`$ otherwise. Our algorithm computes $`\text{mast}(T_1,T_2)`$ in $`O(\sqrt{d}\mathrm{\Delta }\mathrm{log}\frac{2n}{d})`$ time. Thus, if $`T_1`$ and $`T_2`$ are uniformly labeled trees, then $`\mathrm{\Delta }n^2`$ and the time complexity of our algorithm is $`O(\sqrt{d}n^2\mathrm{log}\frac{2n}{d})`$, which is faster than the Gupta-Nishimura algorithm  for any $`d`$. If $`T_1`$ and $`T_2`$ are evolutionary trees, then $`\mathrm{\Delta }n`$ and the time complexity of our algorithm is $`O(\sqrt{d}n\mathrm{log}\frac{2n}{d})`$, which is better than the $`O(\sqrt{d}n\mathrm{log}n)`$ bound claimed by Przytycka . In particular, our algorithm can attain the $`O(n\mathrm{log}n)`$ bound for binary trees . Also for general evolutionary trees, our algorithm runs in $`O(n^{1.5})`$ time since $`\sqrt{d}n\mathrm{log}\frac{2n}{d}=O(n^{1.5})`$ for any degree $`d`$. This is faster than the $`O(n^{1.5}\mathrm{log}n)`$ time of the Farach-Thorup algorithm .

The efficiency achieved by our mast algorithm is based on improved algorithms for computing maximum weight matchings of bipartite graphs that satisfy some structural properties. Let $`G=(X,Y,E)`$ be a bipartite graph with positive integer weights on its edges. Denote by $`n`$, $`m`$, $`N`$, and $`W`$ the number of nodes, the number of edges, the maximum edge weight, and the total edge weight of $`G`$, respectively. The best known algorithm for computing maximum weight bipartite matchings was given by Gabow and Tarjan , which takes $`O(\sqrt{n}m\mathrm{log}nN)`$ time. For some applications where the total edge weight is small (say, $`W=O(m)`$), Kao et al.  gave a slightly faster algorithm that runs in $`O(\sqrt{n}W)`$ time. Intuitively, a bipartite graph is node-unbalanced if there are much fewer nodes on one side than the other. It is weight-unbalanced if its total weight is dominated by the edges incident to a few nodes; we call these nodes the dominating nodes. In this paper, we show how to enhance these two matching algorithms when the input graphs are either node-unbalanced or weight-unbalanced.

The node-unbalanced property has many practical applications (see, e.g., ) and has been exploited to improve various graph algorithms. For example, Ahuja et al.  adapted several bipartite network flow algorithms such that the running times depend on the number of nodes in the smaller side of the input bipartite graph instead of the total number of nodes. Tokuyama and Nakano used this property to reduce the time complexity of the minimum cost assignment problem  and the Hitchcock transportation problem . This paper presents similar improvements for maximum weight matching. Specifically, we show that the running time of the matching algorithms of Gabow and Tarjan  and Kao et al.  can be improved to $`O(\mathrm{min}\{\sqrt{n_s}m\mathrm{log}n_sN,m+n_s^{2.5}\mathrm{log}n_sN\})`$ and $`O(\sqrt{n_s}W)`$, respectively, where $`n_s`$ is the number of nodes in the smaller side of the input bipartite graph.

The weight-unbalanced property is exploited in another way. Given a weight-unbalanced bipartite graph $`G`$, let $`G^{}`$ be the subgraph of $`G`$ with its dominating nodes removed. Note that $`G^{}`$ has a total weight much smaller than $`G`$ does. Based on the $`O(\sqrt{n}W)`$-time matching algorithm of Kao et al. , finding the maximum weight matching of $`G^{}`$ is much faster than finding one of $`G`$. To take advantage of this fact, we design an efficient algorithm that finds a maximum weight matching of $`G`$ from that of $`G^{}`$. This algorithm is substantially faster than applying directly the $`O(\sqrt{n}W)`$-time matching algorithm on $`G`$.

These results for unbalanced graphs provide a basis for solving a new matching problem called the hierarchical bipartite matching problem. This matching problem is at the core of our mast algorithm and is defined as follows. Let $`T`$ be a rooted tree. Denote $`r`$ as the root of $`T`$. Let $`C(u)`$ denote the set of children of node $`u`$. Every node $`u`$ of $`T`$ is associated with a positive integer $`w(u)`$ and a weighted bipartite graph $`G_u`$ satisfying the following properties:

* $`w(u)_{vC(u)}w(v)`$.
* $`G_u=(X_u,Y_u,E_u)`$ where $`X_u=C(u)`$. Each edge of $`G_u`$ has a positive integer weight, and there is no isolated node. For any node $`vX_u`$, the total weight of all the edges incident to $`v`$ is at most $`w(v)`$. Thus, the total weight of the edges in $`G_u`$ is at most $`w(u)`$.

See Figure 1 for an example. For any weighted bipartite graph $`G`$, let $`\text{mwm}(G)`$ denote a maximum weight matching of $`G`$. The hierarchical matching problem is to compute $`\text{mwm}(G_u)`$ for all internal nodes $`u`$ of $`T`$. Let $`b=\mathrm{max}_{uT}\{\mathrm{min}\{|X_u|,|Y_u|\}\}`$ and $`e=_{uT}|E_u|`$. The problem can be solved by applying directly our results for node-unbalanced graphs; for example, it can be solved in $`O(_{uT}\sqrt{b}|E_u|\mathrm{log}bw(u))=O(\sqrt{b}e\mathrm{log}w(r))`$ time using the enhanced Gabow-Tarjan algorithm. However, this time complexity is not yet satisfactory. When comparing labeled trees, we often encounter instances of the hierarchical bipartite matching problem with $`e`$ being very large; in particular, $`e`$ is asymptotically much greater than $`w(r)`$. We further improve the running time to $`O(\sqrt{b}w(r)+e)`$ by making additional use of our technique for weight-unbalanced graphs and exploiting trade-offs between the size of the bipartite graphs involved and their total edge weight.

The rest of the paper is organized as follows. Section 2 details our techniques of speeding up the existing algorithms for unbalanced graphs. Section 3 gives an efficient algorithm for solving the hierarchical matching problem. Finally, Section 4 describes our algorithm for computing maximum agreement subtrees.

## 2 Maximum weight matching of unbalanced graphs

Throughout this section, let $`G=(X,Y,E)`$ be a weighted bipartite graph with no isolated nodes. Let $`n=|X|+|Y|`$, $`n_s=\mathrm{min}\{|X|,|Y|\}`$, $`m=|E|`$, $`N`$ be the largest edge weight, and $`W`$ be the total edge weight.

Suppose every edge of $`G`$ has a positive integer weight. Gabow and Tarjan  and Kao et al.  gave an $`O(\sqrt{n}m\mathrm{log}(nN))`$-time algorithm and an $`O(\sqrt{n}W)`$-time one to compute $`\text{mwm}(G)`$, respectively.

### 2.1 Matchings of node-unbalanced graphs

The following theorem speeds up the computation of $`\text{mwm}(G)`$ if $`G`$ is node-unbalanced.

###### Theorem 1.

1. $`\text{mwm}(G)`$ can be computed in $`O(\sqrt{n_s}m\mathrm{log}(n_sN))`$ time.
2. $`\text{mwm}(G)`$ can be computed in $`O(m+n_s^{2.5}\mathrm{log}(n_sN))`$ time.
3. $`\text{mwm}(G)`$ can be computed in $`O(\sqrt{n_s}W)`$ time.

###### Proof.

Without loss of generality, we assume $`n_s=|X||Y|`$. The statements are proved as follows.

Statement 1. For any node $`v`$ in $`G`$, let $`\alpha (v)`$ be the number of edges incident to $`v`$. Suppose $`Y=\{y_1,y_2,\mathrm{},y_{kn_s+r}\}`$ where $`k1`$, $`0r<n_s`$, and $`\alpha (y_1)`$ $``$ $`\alpha (y_2)\mathrm{}\alpha (y_{kn_s+r})`$. We partition $`Y`$ into $`Y_0=\{y_1,\mathrm{},y_r\},Y_1=\{y_{r+1},\mathrm{},y_{r+n_s}\},\mathrm{},`$ and $`Y_k=\{y_{r+(k1)n_s+1},\mathrm{},y_{r+kn_s}\}`$. Note that except $`Y_0`$, every set has $`n_s`$ nodes.

For any $`Y^{}Y`$, denote $`G(Y^{})`$ as the subgraph of $`G`$ induced by all the edges incident to $`Y^{}`$. Suppose that $`M_i`$ is a maximum weight matching of $`G(Y_0Y_1\mathrm{}Y_i)`$. Let $`Y_{M_i}=\{y(x,y)M_i\}`$. Note that a maximum weight matching of $`G(Y_{M_i}Y_{i+1})`$ is also one of $`G(Y_0Y_1\mathrm{}Y_{i+1})`$. Therefore, we can compute $`\text{mwm}(G)`$ using the following algorithm:

* Step 1. Compute a maximum weight matching $`M_0`$ of $`G(Y_0)`$.
* Step 2. For $`i=1`$ to $`k`$,

+ let $`Y_{M_{i1}}`$ = $`\{y(x,y)M_{i1}\}`$;
    + compute a maximum weight matching $`M_i`$ of $`G(Y_{M_{i1}}Y_i)`$.
* Step 3. Return $`M_k`$.

The running time is analyzed below. Let $`\alpha (Y^{})`$ be the total number of edges in $`G(Y^{})`$. For $`1ik`$, $`\alpha (Y_{M_{i1}})\alpha (Y_i)`$, and $`\alpha (Y_{M_{i1}}Y_i)2\alpha (Y_i)`$. Using the matching algorithm by Gabow and Tarjan , we can compute $`\text{mwm}(G(Y_{M_{i1}}Y_i))`$ in $`O(\sqrt{|Y_{M_{i1}}Y_i|}\alpha (Y_i)\mathrm{log}(|Y_{M_{i1}}Y_i|N))`$ time. Note that $`|Y_{M_{i1}}|n_s`$ and $`|Y_i|=n_s`$. Hence, the whole algorithm uses $`O(_{i=1}^k\sqrt{n_s}\alpha (Y_i)\mathrm{log}(n_sN))`$ $`=`$ $`O(\sqrt{n_s}m\mathrm{log}(n_sN))`$ time.

Statement 2. Since we suppose $`|X||Y|`$, any matching of $`G`$ contains at most $`|X|=n_s`$ edges. Thus, for every $`uX`$, we can discard the edges incident to $`u`$ that are not among the $`n_s`$ heaviest ones; the remaining $`n_s^2`$ edges must still contain a maximum weight matching of $`G`$. Note that we can find these $`n_s^2`$ edges in $`O(m)`$ time, and from Statement 1, we can compute $`\text{mwm}(G)`$ from them in $`O(\sqrt{n_s}n_s^2\mathrm{log}(n_sN))`$ time. The total time taken is $`O(m+n_s^{2.5}\mathrm{log}(n_sN))`$.

Statement 3. The algorithm in the proof of Statement 1 can be adapted to find $`\text{mwm}(G)`$ in $`O(\sqrt{n_s}W)`$ time by using the $`O(\sqrt{n}W)`$-time matching algorithm of Kao et al.  to compute each $`M_i`$. For any $`Y^{}Y`$, we redefine $`\alpha (Y^{})`$ to be the total weight of edges incident to $`Y`$. Then we can use the same analysis to show that the adapted algorithm runs in $`O(_{i=1}^k\sqrt{n_s}\alpha (Y_i))`$ = $`O(\sqrt{n_s}W)`$ time. ∎

### 2.2 Matchings of weight-unbalanced graphs

We show how to speed up the matching algorithm of Kao et al.  when the input graph $`G`$ is weight-unbalanced. The key technique is stated in Lemma 2 below. The following example illustrates how this lemma can help. Suppose that $`G`$ has $`O(1)`$ dominating nodes. Let $`G^{}`$ be the subgraph of $`G`$ with the dominating nodes removed. Let $`W^{}`$ be the total edge weight of $`G^{}`$. Since $`G`$ is weight-unbalanced, we further assume $`W^{}=o(W)`$. To compute $`\text{mwm}(G)`$, we can first use Theorem 1(3) to compute $`\text{mwm}(G^{})`$ in $`O(\sqrt{n_s}W^{})`$ time and then use Lemma 2 to compute $`\text{mwm}(G)`$ from $`\text{mwm}(G^{})`$ in $`O(m\mathrm{log}n_s)`$ time. The total running time is $`O(m\mathrm{log}n_s+\sqrt{n_s}W^{})=o(\sqrt{n_s}W)`$, which is smaller than the running time of using the algorithm of Kao et al.  to find $`\text{mwm}(G)`$ directly.

###### Lemma 2.

Let $`H=\{x_1,x_2,\mathrm{},x_h\}`$ be a subset of $`h`$ nodes of $`X`$. Let $`GH`$ be the subgraph of $`G`$ constructed by removing the nodes in $`H`$. Denote by $`E^{}`$ the set of edges in $`GH`$. Given $`\text{mwm}(GH)`$, we can compute $`\text{mwm}(G)`$ in $`O(|E|+(h^2|E^{}|+h^3)\mathrm{log}n_s)`$ time.

###### Proof.

First, we show that using $`O(|E|)`$ time, we can find a set $`\mathrm{{\rm Y}}`$ of only $`O(\mathrm{min}\{h|E^{}|+h^2,n_s^2\})`$ edges such that $`\mathrm{{\rm Y}}`$ still contains a maximum weight matching of $`G`$. In the proof of Theorem 1(2), it has already been shown that we can find in $`O(|E|)`$ time a set of $`O(n_s^2)`$ edges that contains $`\text{mwm}(G)`$. Thus, it suffices to find in $`O(|E|)`$ time another set of $`O(h|E^{}|+h^2)`$ edges that contains $`\text{mwm}(G)`$; $`\mathrm{{\rm Y}}`$ is just the smaller of these two sets. Let $`Y^{}`$ be the subset of nodes of $`Y`$ that are endpoints of $`E^{}`$. For any $`x_iH`$, we select, among the edges incident to $`x_i`$, a subset of edges $`E_i`$, which is the union of the following two sets:

* $`\{(x_i,y)yY^{}\}`$;
* $`\{(x_i,y)(x_i,y)`$ is among the $`h`$ heaviest edges with $`yY^{}\}`$.

Observe that $`E^{}E_1\mathrm{}E_h`$ must contain a maximum weight matching of $`G`$, and these $`|E^{}E_1\mathrm{}E_h|=O(h|E^{}|+h^2)`$ edges can be found in $`O(|E|)`$ time.

By discarding all unnecessary edges (i.e., edges neither in $`\mathrm{{\rm Y}}`$ nor in $`\text{mwm}(GH)`$), we can assume that $`G`$ has only $`O(\mathrm{min}\{h|E^{}|+h^2,n_s^2\})`$ edges, while still containing $`\text{mwm}(G)`$ and $`\text{mwm}(GH)`$. This preprocessing requires an extra $`O(|E|)`$ time for finding $`\text{mwm}(G)`$.

Below, we describe a procedure which, given any bipartite graph $`D`$ and any node $`x`$ of $`D`$, finds $`\text{mwm}(D)`$ from $`\text{mwm}(D\{x\})`$ in $`O(m_D\mathrm{log}m_D)`$ time, where $`m_D`$ is the number edges of $`D`$. Then, starting from $`GH`$, we can apply this procedure repeatedly $`h`$ times to find $`\text{mwm}(G)`$ from $`\text{mwm}(GH)`$. Since $`G`$ is assumed to have only $`O(\mathrm{min}\{h|E^{}|+h^2,n_s^2\})`$ edges, this process takes $`O(h((h|E^{}|+h^2)\mathrm{log}n_s))`$ time. This lemma follows.

Let $`M`$ and $`M_x`$ be a maximum weight matching of $`D`$ and $`D\{x\}`$, respectively; denote by $`S`$ the set of augmenting paths and cycles formed in $`MM_xMM_x`$, and let $`\sigma `$ be the augmenting path in $`S`$ starting from $`x`$. Note that the augmenting paths and cycles in $`S\{\sigma \}`$ cannot improve the matching $`M_x`$; otherwise, $`M_x`$ is not a maximum weight matching of $`D\{x\}`$. Thus, we can transform $`M_x`$ to $`M`$ using $`\sigma `$. Note that $`\sigma `$ is indeed a maximum augmenting path starting from $`x`$, which can be found in $`O(m_D\mathrm{log}m_D)`$ time . ∎

## 3 Hierarchical bipartite matching

Throughout this section, let $`T`$ be a rooted tree as defined in the definition of the hierarchical bipartite matching problem in §1. The root of $`T`$ is denoted by $`r`$. For each node $`u`$ of $`T`$, $`w(u)`$ and $`G_u=(X_u,Y_u,E_u)`$ denote the weight and the bipartite graph associated with $`u`$, respectively. Furthermore, let $`b=\mathrm{max}_{uT}\left\{\mathrm{min}\{|X_u|,|Y_u|\}\right\}`$, and $`e=_{uT}|E_u|`$.

In this section, we describe an algorithm for computing $`\text{mwm}(G_u)`$ for all $`uT`$ in $`O(\sqrt{b}w(r)+e)`$ time. Our algorithm is based on two crucial observations. One is that for any value $`x`$, there are at most $`w(r)/x`$ graphs with its second maximum edge weight greater than $`x`$. The other is that most of these graphs have their total weight dominated by edges incident to a few nodes. For those graphs with a large second maximum edge weight, we compute their maximum weight matchings using a less weight-sensitive algorithm. As there are not many of them, the computation is efficient. For the other graphs, their weights are dominated by the edges incident to a few nodes. Thus, using Lemma 2 and a weight-efficient matching algorithm, we can compute the maximum weight matchings for these graphs efficiently. Details are as follows.

Consider any subset $`B`$ of nodes of $`T`$. Let $`\delta =\mathrm{min}_{uB}w(u)`$. We say that $`B`$ has a critical degree $`h`$ if for every $`uB`$, $`u`$ has at most $`h`$ children with weight at least $`\delta `$. For any internal node $`u`$, let $`\text{s}\text{ec}\text{w}(u)`$ $`=`$ $`\text{2nd-}\mathrm{max}\{w(v)vC(u)\}`$, i.e., the value of the second largest $`w(v)`$ over all the children $`v`$ of $`u`$. Lemma 3 below shows the importance of $`\text{s}\text{ec}\text{w}(u)`$ and critical degrees. Lemma 3(1) shows that there are not many nodes $`u`$ with large $`\text{s}\text{ec}\text{w}(u)`$; for those nodes $`u`$ with small $`\text{s}\text{ec}\text{w}(u)`$, they should not have a large critical degree, and Lemma 3(2) states that the maximum weight matchings associated with these nodes can be computed efficiently.

###### Lemma 3.

1. Let $`x`$ be any positive number. Let $`A`$ be the set of nodes $`u`$ of $`T`$ with $`\text{s}\text{ec}\text{w}(u)>x`$. Then $`|A|<w(r)/x`$.
2. Let $`B`$ be any set of nodes of $`T`$. If $`B`$ has critical degree $`h`$, then we can compute $`\text{mwm}(G_u)`$ for all $`uB`$ in $`O((\sqrt{b}+h^3\mathrm{log}b)w(r)+_{uB}|E_u|)`$ time.

###### Proof.

The statement are proved as follows.

Statement 1. Let $`L`$ be the set of nodes $`u`$ in $`T`$ such that (1) $`w(u)>x`$ and (2) either $`u`$ is a leaf or $`w(v)x`$ for all children $`v`$ of $`u`$. Since the subtrees rooted at the nodes of $`L`$ are disjoint, $`w(r)_{uL}w(u)>x|L|`$. Thus, $`|L|<w(r)/x`$. Let $`T^{}`$ be the tree in $`T`$ induced by $`L`$, i.e., $`T^{}`$ contains exactly the nodes of $`L`$ and the least common ancestor of every two nodes of $`L`$. Note that $`T^{}`$ has at most $`|L|`$ leaves and at most $`|L|`$ internal nodes. On the other hand, every node of $`A`$ is an internal node of $`T^{}`$; thus, $`|A||L|<w(r)/x`$.

Statement 2. For every node $`uB`$, let $`H(u)`$ be the set of $`u`$’s children that have a weight at least $`\delta =\mathrm{min}_{uB}w(u)`$ each. Let $`L(u)`$ be the set of the rest of $`u`$’s children. Note that $`|H(u)|h`$ because $`B`$ has critical degree $`h`$. Since the weight of $`G_uH(u)`$ is at most $`_{xL(u)}w(x)`$ and $`b\mathrm{min}\{|X_u|,|Y_u|\}`$, by Theorem 1 we can compute $`\text{mwm}(G_uH(u))`$ in time

$$O(\sqrt{b}_{xL(u)}w(x)+|E_u|).$$
(1)
Since $`G_uH(u)`$ has at most $`_{xL(u)}w(x)`$ edges and $`|H(u)|h`$, by Lemma 2, we can compute $`\text{mwm}(G_u)`$ from $`\text{mwm}(G_uH(u))`$ in time

$$O(|E_u|+(h^2_{xL(u)}w(x)+h^3)\mathrm{log}b)=O(h^3\mathrm{log}b_{xL(u)}w(x)+|E_u|).$$
(2)
From Equations (1) and (2), we can compute $`\text{mwm}(G_u)`$ for all $`uB`$ in time

$$O\left(_{uB}\left((\sqrt{b}+h^3\mathrm{log}b)_{xL(u)}w(x)+|E_u|\right)\right).$$

Since the subtrees rooted at some node in $`_{uB}L(u)`$ are disjoint, $`_{uB}_{xL(u)}w(x)w(r)`$. This statement follows. ∎

We are now ready to compute $`\text{mwm}(G_u)`$ for all nodes $`u`$ of $`T`$. We divide all the nodes in $`T`$ into two sets: $`\mathrm{\Phi }=\{uT\text{s}\text{ec}\text{w}(u)>b^3\}`$ and $`\mathrm{\Pi }=\{uT\text{s}\text{ec}\text{w}(u)b^3\}`$.

Every node $`u\mathrm{\Phi }`$ has $`\text{s}\text{ec}\text{w}(u)>b^3`$; by Lemma 3(1), $`|\mathrm{\Phi }|w(r)/b^3`$. Furthermore, by Theorem 1(2), the time for computing $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Phi }`$ is $`O(_{u\mathrm{\Phi }}(b^{2.5}\mathrm{log}(bw(u))+|E_u|))`$ = $`O(\frac{w(r)}{b^3}b^{2.5}\mathrm{log}w(r)+_{u\mathrm{\Phi }}|E_u|)`$ = $`O(w(r)\frac{\mathrm{log}w(r)}{\sqrt{b}}+_{u\mathrm{\Phi }}|E_u|)`$. This time complexity is still far from our goal as $`\mathrm{log}w(r)`$ may be much larger than $`\sqrt{b}`$. To improve the time complexity, we first note that using the technique for proving Lemma 2, we can compute $`\text{mwm}(G_u)`$ in time depending only on $`\text{s}\text{ec}\text{w}(u)`$. Then, with a better estimation of $`\text{s}\text{ec}\text{w}(u)`$, we can reduce the time complexity to $`O(w(r)+_{u\mathrm{\Phi }}|E_u|)`$. Details are given in Lemma 4(1).

For $`\mathrm{\Pi }`$, we can handle the nodes $`u\mathrm{\Pi }`$ with $`w(u)>b^3`$ easily. For nodes with $`w(u)<b^3`$, we apply Lemma 3(2) to compute $`\text{mwm}(G_u)`$. The basic idea is to partition the nodes $`u\mathrm{\Pi }`$ into a constant number of sets according to $`w(u)`$ such that every set has critical degree $`b^{\frac{1}{7}}`$. This can ensure that the total time to compute all the $`\text{mwm}(G_u)`$ is $`O(\sqrt{b}w(r)+_{u\mathrm{\Pi }}|E_u|)`$. Details are given in Lemma 4(2).

###### Lemma 4.

1. We can compute $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Phi }`$ in $`O(w(r)+_{u\mathrm{\Phi }}|E_u|)`$ time.
2. We can compute $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Pi }`$ in $`O(\sqrt{b}w(r)+_{u\mathrm{\Pi }}|E_u|)`$ time.

###### Proof.

The two statements are proved as follows.

Statement 1. Observe that for any $`u\mathrm{\Phi }`$, $`G_u`$ has at most $`b^2`$ edges relevant to the computation of $`\text{mwm}(G_u)`$, and they can be found in $`O(|E_u|)`$ time. Let $`E_u^{}`$ be this set of edges. Below, we assume that, for every $`u\mathrm{\Phi }`$, $`G_u`$ has only edges in $`E_u^{}`$. Otherwise, it costs $`O(_{u\mathrm{\Phi }}|E_u|)`$ extra time to find all $`E_u^{}`$ and the assumption holds.

For every $`k1`$, let $`\mathrm{\Phi }_k=\{u\mathrm{\Phi }2^{k1}b^3<\text{s}\text{ec}\text{w}(u)2^kb^3\}`$. Obviously, the nonempty sets $`\mathrm{\Phi }_k`$ form a partition of $`\mathrm{\Phi }`$. Below, we show that for any nonempty $`\mathrm{\Phi }_k`$, we can compute $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Phi }_k`$ in $`O\left(w(r)k/2^k+_{u\mathrm{\Phi }_k}|E_u^{}|\mathrm{log}b\right)`$ time. Thus, the time for computing $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Phi }`$ is $`O(_{k1}w(r)k/2^k+_{u\mathrm{\Phi }}|E_u^{}|\mathrm{log}b)`$ $`=`$ $`O(w(r)+_{u\mathrm{\Phi }}|E_u^{}|\mathrm{log}b)`$ $`=`$ $`O(w(r)+(w(r)/b^3)b^2\mathrm{log}b)`$ $`=`$ $`O(w(r))`$, and Statement 1 follows.

We now give the details of computing $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Phi }_k`$. Let $`u^{}`$ be the child of $`u`$ where $`w(u^{})`$ is the largest over all children of $`u`$. Since $`\text{s}\text{ec}\text{w}(u)2^kb^3`$, every edge of $`G_u\{u^{}\}`$ has weight at most $`2^kb^3`$. By Theorem 1(2) and Lemma 2, and the fact $`b\mathrm{min}\{|X_u|,|Y_u|\}`$, we can find $`\text{mwm}(G_u)`$ in $`O(\sqrt{b}b^2\mathrm{log}(b2^kb^3)+|E_u^{}|\mathrm{log}b)`$ time. By Lemma 3(1), $`|\mathrm{\Phi }_k|\frac{w(r)}{2^kb^3}`$. Thus, we can compute $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Phi }_k`$ in time

$`O\left(_{u\mathrm{\Phi }_k}\sqrt{b}b^2\mathrm{log}(b2^kb^3)+|E_u^{}|\mathrm{log}b\right)`$ $`=`$ $`O\left(\frac{w(r)b^{2.5}}{2^kb^3}(k+\mathrm{log}b)+_{u\mathrm{\Phi }_k}|E_u^{}|\mathrm{log}b\right)`$
$`=`$ $`O\left(w(r)k/2^k+_{u\mathrm{\Phi }_k}|E_u^{}|\mathrm{log}b\right).`$
Statement 2. We partition $`\mathrm{\Pi }`$ as follows. Let $`\mathrm{\Pi }^{}`$ be the set of nodes in $`\mathrm{\Pi }`$ with weight greater than $`b^3`$. For any $`0k20`$, let $`\mathrm{\Pi }_k=\{uu\mathrm{\Pi }\text{ and }b^{\frac{k}{7}}<w(u)b^{\frac{k+1}{7}}\}.`$ Obviously, $`\mathrm{\Pi }=\mathrm{\Pi }^{}\mathrm{\Pi }_0\mathrm{}\mathrm{\Pi }_{20}`$.

Since $`\text{s}\text{ec}\text{w}(u)b^3`$ and $`w(u)>b^3`$ for all nodes $`u`$ in $`\mathrm{\Pi }^{}`$, $`\mathrm{\Pi }^{}`$ has critical degree one. By Lemma 3(2), we can compute $`\text{mwm}(G_u)`$ for all nodes in $`\mathrm{\Pi }^{}`$ using $`O(\sqrt{b}w(r)+_{u\mathrm{\Pi }^{}}|E_u|)`$ time.

Each $`\mathrm{\Pi }_k`$ is handled as follows. For every node $`u\mathrm{\Pi }_k`$, $`u`$ has at most $`b^{\frac{1}{7}}`$ children with weight at least $`b^{\frac{k}{7}}`$; otherwise $`w(u)>b^{\frac{k+1}{7}}`$ and $`u\mathrm{\Pi }_k`$. Thus, $`\mathrm{\Pi }_k`$ has critical degree $`b^{\frac{1}{7}}`$. By Lemma 3(2), we can compute $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Pi }_k`$ in $`O((\sqrt{b}+b^{\frac{3}{7}}\mathrm{log}b)w(r)+_{u\mathrm{\Pi }_k}|E_u|)`$ $`=`$ $`O(\sqrt{b}w(r)+_{u\mathrm{\Pi }_k}|E_u|)`$ time. In summary, we can compute $`\text{mwm}(G_u)`$ for all $`u\mathrm{\Pi }`$ in $`O(\sqrt{b}w(r)+_{u\mathrm{\Pi }}|E_u|)`$ time. ∎

###### Theorem 5.

We can compute $`\text{mwm}(G_u)`$ for all nodes $`uT`$ in $`O(\sqrt{b}w(r)+e)`$ time.

###### Proof.

It follows from Lemma 4 and the fact that $`T=\mathrm{\Phi }\mathrm{\Pi }`$ and $`_{uT}|E_u|=e`$. ∎

## 4 Computing maximum agreement subtrees

By generalizing the work of Cole et al.  on binary evolutionary trees, we can easily derive an algorithm to compute a maximum agreement subtree of two labeled trees. There is, however, a bottleneck of computing the maximum weight matchings of a large number of bipartite graphs with nonconstant degrees. By using our result on the hierarchical bipartite matchings, we can eliminate this bottleneck and obtain the fastest known mast algorithm. Section 4.1 introduces basics of labeled trees. Section 4.2 uses our results on the hierarchical bipartite matchings to remove the bottleneck in our mast algorithm. Section 4.3 details our mast algorithm and analyzes its time complexity. Section 4.4 discusses the generalization of the work of Cole et al. .

Throughout this section, $`T_1`$ and $`T_2`$ denote two labeled tress with $`n`$ nodes and of degree $`d2`$. Let $`\mathrm{\Delta }_{T_1,T_2}=_{uT_1}_{vT_2}\delta (u,v)`$ where $`\delta (u,v)=1`$ if nodes $`u`$ and $`v`$ are labeled with the same symbol, and $`0`$ otherwise. Also, let $`\mathrm{\Delta }`$ denote $`\mathrm{\Delta }_{T_1,T_2}`$.

### 4.1 Basics

For a rooted tree $`T`$ and any node $`u`$ of $`T`$, let $`T^u`$ denote the subtree of $`T`$ that is rooted at $`u`$. For any set $`L`$ of symbols, the restricted subtree of $`T`$ with respect to $`L`$, denoted by $`TL`$, is the subtree of $`T`$ (1) whose nodes are the nodes with labels from $`L`$ and the least common ancestors of any two nodes with labels from $`L`$ and (2) whose edges preserve the ancestor-descendant relationship of $`T`$. Figure 2 gives an example. Note that $`TL`$ may contain nodes with labels outside $`L`$. For any labeled tree $`T^{}`$, let $`TT^{}`$ denote the restricted subtree of $`T`$ with respect to the set of symbols used in $`T^{}`$.

A centroid path decomposition  of a rooted tree $`T`$ is a partition of its nodes into disjoint paths as follows. For each internal node $`u`$ in $`T`$, let $`C(u)`$ denote the set of children of $`u`$. Among the children of $`u`$, one is chosen as the heavy child, denoted by $`\mathrm{hvy}(u)`$, if the subtree of $`T`$ rooted at $`\mathrm{hvy}(u)`$ contains the largest number of nodes; the other children of $`u`$ are side children. We call the edge from $`u`$ to its heavy child a heavy edge. A centroid path is a maximal path formed by heavy edges; the root centroid path is the centroid path that contains the root of $`T`$. See Figure 3 for an example.

Let $`𝒟(T)`$ denote the set of the centroid paths of $`T`$. Note that $`𝒟(T)`$ can be constructed in $`O(|T|)`$ time. For every $`P𝒟(T)`$, the root of $`P`$, denoted $`r(P)`$, refers to the node on $`P`$ that is the closest to the root of $`T`$, and $`𝒜(P)`$ denotes the set of the side children of the nodes on $`P`$. For any node $`u`$ on $`P`$, a subtree rooted at some side child of $`u`$ is called a side tree of $`u`$, as well as a side tree of $`P`$. Let $`\text{side-tree}(P)`$ be the set of side trees of $`P`$. Note that for every $`R\text{side-tree}(P)`$, $`|R||T^{r\left(P\right)}|/2`$.

The following lemma states two useful properties of the centroid path decomposition.

###### Lemma 6.

Let $`T_1`$ and $`T_2`$ be two labeled trees.

1. $`_{P𝒟(T_1)}\mathrm{\Delta }_{T_1^{{}_{}{}^{r}(P)},T_2}`$ $`\mathrm{\Delta }_{T_1,T_2}\mathrm{log}n`$.
2. $`_{P𝒟(T_1)}\sqrt{\mathrm{min}(d,|T_1^{r\left(P\right)}|)}\mathrm{\Delta }_{T_1^{r(P)},T_2}`$ $`\sqrt{d}\mathrm{\Delta }_{T_1,T_2}\mathrm{log}\frac{2n}{d}`$.

###### Proof.

The two statements are proved as follows.

Statement 1. A centroid path $`P`$ is attached to another centroid path $`P^{}`$ if the root of $`P`$ is the child of a node on $`P^{}`$. We define the level of a centroid path as follows. The root centroid path has level zero. A centroid path has level $`i`$ if it is attached to some centroid path with level $`i1`$. Note that any subtree attached to a centroid path with level $`i`$ has size at most $`n/2^{i+1}`$. Thus, there are at most $`\mathrm{log}n`$ different levels. Moreover, subtrees attached to centroid paths with the same level are all disjoint.

For any $`0i<\mathrm{log}n`$, denote by $`D_i`$ the set of all centroid paths in $`𝒟(T_1)`$ with level $`i`$. Then $`_{P𝒟(T_1)}\mathrm{\Delta }_{T_1^{r(P)},T_2}`$ $`=_{0i<\mathrm{log}n}_{PD_i}\mathrm{\Delta }_{T_1^{r(P)},T_2}`$ $`\mathrm{log}n_{PD_i}\mathrm{\Delta }_{T_1^{r(P)},T_2}`$ $`\mathrm{\Delta }_{T_1,T_2}\mathrm{log}n`$.

Statement 2. We divide the centroid paths into 2 groups. We first consider the centroid paths on level $`i`$ where $`0i<\mathrm{log}\frac{2n}{d}`$. For any such $`i`$,

$$\underset{PD_i}{}\sqrt{\mathrm{min}\{d,|T_1^{r\left(P\right)}|\}}\mathrm{\Delta }_{T_1^{r(P)},T_2}\underset{PD_i}{}\sqrt{d}\mathrm{\Delta }_{T_1^{r(P)},T_2}\sqrt{d}\mathrm{\Delta }_{T_1,T_2}.$$

Thus, $`_{0i<\mathrm{log}\frac{2n}{d}}_{PD_i}\sqrt{\mathrm{min}\{d,|T_1^{r\left(P\right)}|\}}\mathrm{\Delta }_{T_1^{r(P)},T_2}\sqrt{d}\mathrm{\Delta }_{T_1,T_2}\mathrm{log}\frac{2n}{d}`$.

Next, we consider the centroid paths on level $`\mathrm{log}\frac{2n}{d}+i`$ where $`i0`$. Note that for a path $`P`$ on level $`\mathrm{log}\frac{2n}{d}+i`$, $`|T_1^{r\left(P\right)}|d/2^{i+1}`$. Thus, $`_{i0}_{PD_{\mathrm{log}\frac{2n}{d}+i}}\sqrt{\mathrm{min}\{d,|T_1^{r\left(P\right)}|\}}\mathrm{\Delta }_{T_1^{r(P)},T_2}`$ is at most

$$\underset{i0}{}\underset{PD_{\mathrm{log}\frac{2n}{d}+i}}{}\sqrt{d/2^{i+1}}\mathrm{\Delta }_{T_1^{r(P)},T_2}\underset{i0}{}\sqrt{d/2^{i+1}}\mathrm{\Delta }_{T_1,T_2}=O(\sqrt{d}\mathrm{\Delta }_{T_1,T_2}).$$

The following notion captures which pairs of nodes of two labeled trees $`T_1`$ and $`T_2`$ are important. Consider any centroid paths $`P𝒟(T_1)`$ and $`Q𝒟(T_2)`$. For any node $`xP`$ or $`Q`$, let $`(x)`$ be the set of symbols labeling $`x`$ and the nodes in the side trees of $`x`$. Let $`\text{inp}(P,Q)`$ be the set of node pairs $`(u,v)P\times Q`$ with $`(u)(v)\mathrm{}`$. Let $`\text{inp}(P,T_2)=_{Q𝒟(T_2)}\text{inp}(P,Q)`$.

### 4.2 Matchings

As explained later in §4.3, we can easily generalize the dynamic programming approach in  to compute $`\text{mast}(T_1,T_2)`$ for any two labeled trees $`T_1`$ and $`T_2`$, but there is a bottleneck of computing the maximum weight matchings of a large number of bipartite graphs with nonconstant degrees. This section uses our results on hierarchical bipartite matchings to remove this bottleneck.

First of all, we identify the bipartite graphs for which maximum weight matchings are required. For any nodes $`uT_1`$ and $`vT_2`$, define $`G_{uv}`$ as the weighted bipartite graph between $`C(u)`$ and $`C(v)`$ where edge $`(x,y)`$ has weight $`\text{mast}(T_1^x,T_2^y)`$. Furthermore, define $`H_{uv}`$ as the graph constructed from $`G_{uv}`$ by removing all the zero-weight edges and all the edges adjacent to the heavy child of $`u`$ or $`v`$. Note that the total edge weight of $`H_{uv}`$ can be significantly smaller than that of $`G_{uv}`$. Yet by Lemma 2, we can recover $`\text{mwm}(G_{uv})`$ from $`\text{mwm}(H_{uv})`$ efficiently.

This section shows that for any centroid path $`P𝒟(T_1)`$, we can efficiently compute $`\text{mwm}(H_{uv})`$ for all $`(u,v)\text{inp}(P,T_2)`$. More precisely, let $`𝒯_P`$ denote the required time; the key result of this section is that $`_{P𝒟(T_1)}𝒯_P`$ $`\sqrt{d}\mathrm{\Delta }\mathrm{log}\frac{2n}{d}`$ (see Lemma 8).

To derive an upper bound on $`𝒯_P`$, we need an estimate of the number of edges in the graphs $`H_{uv}`$ for all $`(u,v)\text{inp}(P,T_2)`$. For any centroid path $`P𝒟(T_1)`$, let $`\text{tnoe}(P)`$ be the total number of edges in the graphs $`H_{uv}`$ for all $`(u,v)\text{inp}(P,T_2)`$. Furthermore, let $`\text{tnoe}=`$ $`_{P𝒟(T_1)}\text{tnoe}(P)`$.

###### Lemma 7.

1. For any $`P𝒟(T_1)`$, $`\text{tnoe}(P)=`$ $`O\left(_{w𝒜(P)}|T_2T_1^w|\mathrm{log}\frac{2|T_2T_1^{r(P)}|}{|T_2T_1^w|}\right)`$.
2. $`\text{tnoe}=O(\mathrm{\Delta }\mathrm{log}n)`$.

###### Proof.

The two statements are proved as follows.

Statement 1. Let $`r`$ be the root of $`P`$. By definition, every edge in $`H_{uv}`$ for any $`(u,v)\text{inp}(P,T_2)`$ corresponds to a pair of side trees $`(\mathrm{{\rm Y}},\mathrm{\Pi })`$ where $`\mathrm{{\rm Y}}\text{side-tree}(P)`$ and $`\mathrm{\Pi }\text{side-tree}(Q)`$ for some $`Q𝒟(T_2T_1^r)`$ such that $`\mathrm{{\rm Y}}`$ and $`\mathrm{\Pi }`$ contain some common labels. We call $`(\mathrm{{\rm Y}},\mathrm{\Pi })`$ an intersecting side tree pair. Thus, $`\text{tnoe}(P)`$ is at most the total number of intersecting side tree pairs in $`\text{side-tree}(P)\times \{\text{side-tree}(Q)Q𝒟(T_2T_1^r)\}`$.

To simplify our discussion, let $`R=T_2T_1^r`$ and $`\text{side-tree}(R)=\{\text{side-tree}(Q)Q𝒟(R)\}`$. Consider any node $`w𝒜(P)`$. $`T_1^w`$ is a side tree in $`\text{side-tree}(P)`$. Let $`R_w`$ be $`T_2T_1^w`$. Note that each path in $`R_w`$ starting from a node $`x`$ to its descendant $`y`$ corresponds to a simple path $`Q_{xy}`$ in $`R`$ from $`x`$ to $`y`$. Let $`1^{st}(x,y)`$ be the node on $`Q_{xy}`$ which is the child of $`x`$. By the definition of side trees, among all the side trees in $`\text{side-tree}(R)`$, at most $`\mathrm{log}|R^{1^{st}(x,y)}|+1`$ have roots on $`Q_{xy}`$.

For all side trees $`R^v\text{side-tree}(R)`$, $`(T_1^w,R^v)`$ is an intersecting side tree pair if and only if either (1) $`v`$ is a node on the path from the root of $`R`$ to the root $`R_w`$; or (2) $`v`$ is a node on some path $`Q_{xy}`$ on $`R`$ where $`(x,y)`$ is an edge in $`R_w`$. The number of side trees $`R^v\text{side-tree}(R)`$ in case (1) is less than $`\mathrm{log}|R|`$. The number of side trees $`R^v\text{side-tree}(R)`$ in case (2) is less than $`_{(x,y)R_w}\left(\mathrm{log}|R^{1^{st}(x,y)}|+1\right)`$. Let sum$`(R_w)`$ denote $`_{(x,y)R_w}\mathrm{log}|R^{1^{st}(x,y)}|`$. Below we prove sum$`(R_w)`$ = $`O\left(|R_w|\mathrm{log}\frac{2|R|}{|R_w|}\right)`$. In total, $`\text{tnoe}(P)=`$ $`O\left(_{w𝒜(P)}\left\{|R_w|\mathrm{log}\frac{2|R|}{|R_w|}\right\}\right)`$, as claimed in this statement.

It remains to prove sum$`(R_w)=O\left(|R_w|\mathrm{log}\frac{2|R|}{|R_w|}\right)`$. For any leaf $`y`$ of $`R`$, let $`p(y)`$ be the maximal path in $`R`$ ending at $`y`$ such that every node on $`p(y)`$ has at most one child; denote $`r_p(y)`$ as the root of $`p(y)`$. Let $`Z_{R_w}=\{p(y)y`$ is a leaf of $`R_w\}`$. As $`\{R^{1^{st}(r_p(y),y)}p(y)Z_{R_w}\}`$ is a set of disjoint subtrees of $`R`$, $`_{p(y)Z_{R_w}}|R^{1^{st}(r_p(y),y)}||R|`$. Note that $`|Z_{R_w}||R_w|`$. Thus, $`_{p(y)Z_{R_w}}\mathrm{log}|R^{1^{st}(r_p(y),y)}|`$ $`|R_w|\mathrm{log}\frac{2|R|}{|R_w|}`$.<sup>2</sup><sup>2</sup>2This follows from the fact that for any sequence of positive numbers $`a_1,a_2,\mathrm{},a_k`$ with the sum equal to $`s`$, $`_{i=1}^k\mathrm{log}a_i\mathrm{}\mathrm{log}\frac{2s}{\mathrm{}}`$, where $`k\mathrm{}s`$.  Let $`\widehat{R}_w`$ be the tree obtained by removing all the paths in $`Z_{R_w}`$. We have sum$`(R_w)=|R_w|\mathrm{log}\frac{2|R|}{|R_w|}+`$ sum$`(\widehat{R}_w)`$. Note that $`\widehat{R}_w`$ contains at most half the leaves of $`R_w`$. Hence, sum$`(R_w)=O\left(|R_w|\mathrm{log}\frac{2|R|}{|R_w|}\right)`$.

Statement 2. By Statement 1, tnoe is in the order of

$`{\displaystyle \underset{P𝒟(T_1)}{}}\left[{\displaystyle \underset{w𝒜(P)}{}}|T_2T_1^w|\mathrm{log}{\displaystyle \frac{2|T_2T_1^{r\left(P\right)}|}{|T_2T_1^w|}}\right]`$
$``$ $`{\displaystyle \underset{P𝒟(T_1)}{}}\left[{\displaystyle \underset{w𝒜(P)}{}}\mathrm{\Delta }_{T_1^w,T_2}\mathrm{log}{\displaystyle \frac{2|T_2T_1^{r\left(P\right)}|}{\mathrm{log}|T_2T_1^w|}}\right]`$
$`=`$ $`{\displaystyle \underset{P𝒟(T_1)}{}}\left[{\displaystyle \underset{w𝒜(P)}{}}\mathrm{\Delta }_{T_1^w,T_2}(1+\mathrm{log}|T_2T_1^{r\left(P\right)}|\mathrm{log}|T_2T_1^w|)\right]`$
$``$ $`{\displaystyle \underset{P𝒟(T_1)}{}}\left[\mathrm{\Delta }_{T_1^{r(P)},T_2}+\mathrm{\Delta }_{T_1^{r(P)},T_2}\mathrm{log}|T_2T_1^{r\left(P\right)}|{\displaystyle \underset{w𝒜(P)}{}}\mathrm{\Delta }_{T_1^w,T_2}\mathrm{log}|T_2T_1^w|\right]`$
$``$ $`{\displaystyle \underset{P𝒟(T_1)}{}}\mathrm{\Delta }_{T_1^{r(P)},T_2}+{\displaystyle \underset{P𝒟(T_1)}{}}\left[\mathrm{\Delta }_{T_1^{r(P)},T_2}\mathrm{log}|T_2T_1^{r\left(P\right)}|{\displaystyle \underset{w𝒜(P)}{}}\mathrm{\Delta }_{T_1^w,T_2}\mathrm{log}|T_2T_1^w|\right]`$
$`=`$ $`{\displaystyle \underset{P𝒟(T_1)}{}}\mathrm{\Delta }_{T_1^{r(P)},T_2}+\mathrm{\Delta }_{T_1^{r_o},T_2}\mathrm{log}|T_2T_1^{r_o}|\text{, where }r_o\text{ is the root of }T_1`$
$``$ $`{\displaystyle \underset{P𝒟(T_1)}{}}\mathrm{\Delta }_{T_1^{r(P)},T_2}+\mathrm{\Delta }_{T_1,T_2}\mathrm{log}|T_2|`$
$``$ $`\mathrm{\Delta }_{T_1,T_2}\mathrm{log}|T_2|+\mathrm{\Delta }_{T_1,T_2}\mathrm{log}|T_2|\text{ by Lemma }\text{6}\text{(1)}`$
$`=`$ $`2\mathrm{\Delta }\mathrm{log}n.`$
We proceed to detail the computing of $`\text{mwm}(H_{uv})`$ for all $`(u,v)_{P𝒟(T_1)}\text{inp}(P,T_2)`$. A bipartite graph is nontrivial if both node sets have at least two nodes. Computing $`\text{mwm}(H_{uv})`$ for all trivial $`H_{uv}`$ takes only linear time, i.e., $`O(\text{tnoe})`$ = $`O(\mathrm{\Delta }\mathrm{log}n)`$ time. Thus, we focus on those nontrivial $`H_{uv}`$.

Consider any centroid path $`P`$ in $`𝒟(T_1)`$ and fix a node $`u`$ of $`P`$. Let $`_u`$ be the set of all nontrivial graphs $`H_{uv}`$ where $`(u,v)\text{inp}(P,T_2)`$. Let $`𝒯_u`$ be the time for finding $`\text{mwm}(H_{uv})`$ for all the graphs in $`_u`$. Let $`\text{tnoe}(u)`$ be the number of edges of all the graphs in $`_u`$. In the next lemma, we first derive an upper bound of $`𝒯_u`$, and then we show $`_{P𝒟(T_1)}𝒯_P`$ = $`O(\sqrt{d}\mathrm{\Delta }\mathrm{log}\frac{2n}{d})`$.

###### Lemma 8.

1. $`𝒯_u=O(\sqrt{\mathrm{min}(d,|T_1^u|)}\mathrm{\Delta }_{S_u,T_2}+\text{tnoe}(u))`$, where $`S_u`$ is the set of side trees of $`u`$ in $`T_1`$ and $`\mathrm{\Delta }_{S_u,T_2}=_{\mathrm{{\rm Y}}S_u}\mathrm{\Delta }_{\mathrm{{\rm Y}},T_2}`$.
2. $`_{P𝒟(T_1)}𝒯_P`$ = $`O(\sqrt{d}\mathrm{\Delta }\mathrm{log}\frac{2n}{d})`$.

###### Proof.

The two statements are proved as follows.

Statement 1. Let $`B_u`$ be the set of labels used in the side trees in $`S_u`$. First, we show that for all $`vT_2B_u`$, $`\text{mwm}(H_{uv})`$ can be computed in $`O\left(\sqrt{\mathrm{min}(d,|T_1^u|)}\mathrm{\Delta }_{S_u,T_2}+\text{tnoe}(u)\right)`$ time. Second, we recover $`\text{mwm}(H_{uv})`$ for all nontrivial $`H_{uv}`$ where $`(u,v)\text{inp}(P,T_2)`$ in $`O(\mathrm{\Delta }_{S_u,T_2})`$ time. Then this statement follows.

To compute $`\text{mwm}(H_{uv})`$ for all $`vT_2B_u`$, we apply the hierarchical bipartite matching algorithm of §3. Let $`T=T_2B_u`$. For every node $`vT`$, we associate with $`v`$ the bipartite graph $`H_{uv}`$ and let $`w(v)=\mathrm{\Delta }_{S_u,T_2^v}`$. Observe that $`w(v)=\mathrm{\Delta }_{S_u,T_2^v}_{xC(v)}\mathrm{\Delta }_{S_u,T_2^x}=_{xC(v)}w(x)`$. In addition, for every node $`xC(v)`$, the total weight of all the edges incident to $`x`$ in $`H_{uv}`$ is at most $`w(x)=\mathrm{\Delta }_{S_u,T_2^x}`$. Hence, $`T`$ and the associated bipartite graphs $`H_{uv}`$ satisfy the conditions for the hierarchical bipartite matching problem. For the time complexity, note that, for every $`vT`$, the two node sets of $`H_{uv}`$ have size bounded by $`d`$ and $`\mathrm{min}(d,|T_1^u|)`$, respectively. Thus, by Theorem 5, we can find $`\text{mwm}(H_{uv})`$ for all nodes $`v`$ of $`T=T_2B_u`$ in $`O(\sqrt{\mathrm{min}(d,|T_1^u|)}\mathrm{\Delta }_{S_u,T_2}+\text{tnoe}(u))`$ time.

Next, we show how to recover $`\text{mwm}(H_{uv})`$ for all $`vL`$, where $`L`$ denotes the set of nodes $`v`$ of $`T_2`$ such that $`(u,v)\text{inp}(P,T_2)`$ and $`H_{uv}`$ is nontrivial. Note that every node $`x`$ of $`T_2B_u`$ is also a node in $`T_2`$ and every edge $`(x,y)`$ of $`T_2B_u`$ corresponds to a path in $`T_2`$. Also observe that every node $`vL`$ must be a node in $`T_2B_u`$; otherwise, $`v`$ lies on a path corresponding to an edge $`(x,y)`$ of $`T_2B_u`$, and $`H_{uv}`$ contains a singleton node set and is trivial because $`vL`$. Therefore, we can compute $`\text{mwm}(H_{uv})`$ for all $`vL`$ by traversing $`T_2B_u`$ once using $`O(|(T_2B_u)|)=O(\mathrm{\Delta }_{S_u,T_2})`$ time.

Statement 2. By Statement 1,

$`{\displaystyle \underset{P𝒟(T_1)}{}}𝒯_P`$ $`=`$ $`O\left({\displaystyle \underset{P𝒟(T_1)}{}}{\displaystyle \underset{uP}{}}𝒯_u\right)`$
$`=`$ $`O\left({\displaystyle \underset{P𝒟(T_1)}{}}{\displaystyle \underset{uP}{}}(\sqrt{\mathrm{min}(d,|T_1^u|)}\mathrm{\Delta }_{S_u,T_2}+\text{tnoe}(u))\right)`$
$`=`$ $`O\left(\text{tnoe}+{\displaystyle \underset{P𝒟(T_1)}{}}{\displaystyle \underset{uP}{}}\sqrt{\mathrm{min}(d,|T_1^u|)}\mathrm{\Delta }_{S_u,T_2}\right)`$
$`=`$ $`O\left(\text{tnoe}+{\displaystyle \underset{P𝒟(T_1)}{}}\sqrt{\mathrm{min}(d,|T_1^{r\left(P\right)}|)}\mathrm{\Delta }_{T_1^{r(P)},T_2}\right)`$
$`=`$ $`O\left(\text{tnoe}+\sqrt{d}\mathrm{\Delta }_{T_1,T_2}\mathrm{log}{\displaystyle \frac{2n}{d}}\right)\text{ by Lemma }\text{6}`$
$`=`$ $`O\left(\sqrt{d}\mathrm{\Delta }\mathrm{log}{\displaystyle \frac{2n}{d}}\right)\text{ by Lemma }\text{7}`$
### 4.3 The MAST algorithm

Our algorithm is based on the following recurrence, which generalizes the one given in  to handle labeled trees.

$$\text{mast}(T_1^u,T_2^v)=\mathrm{max}\{\begin{array}{c}\mathrm{max}\{\text{mast}(T_1^u,T_2^x)xC(v)\},\hfill \\ \mathrm{max}\{\text{mast}(T_1^x,T_2^v)xC(u)\},\hfill \\ \text{mwm}(G_{uv})\text{ if }u\text{ and }v\text{ are both unlabeled},\hfill \\ \text{mwm}(G_{uv})+1\text{ for }u,v\text{ labeled with the same symbol,}\hfill \end{array}$$
(3)
where $`\text{mwm}(G_{uv})`$ denotes the total weight of the matching.

Equation (3) suggests a bottom-up dynamic programming approach to computing mast$`(T_1,T_2)`$. The following lemma generalizes the technique of Cole et al.  for speeding up the dynamic programming. Basically, it states that the time complexity is dominated by the time for finding maximum weight matchings of some graphs $`H_{uv}`$.

###### Lemma 9.

Let $`P𝒟(T_1)`$ be a centroid path and $`r=r(P)`$. Given the values $`\text{mast}(T_1^u,(T_2T_1^u)^v)`$ for all nodes $`u𝒜(P)`$ and $`vT_2T_1^u`$, we can compute $`\text{mast}(T_1^r,(T_2T_1^r)^v)`$ for all $`vT_2T_1^r`$ in $`O\left(\left(\gamma (T_1^r)_{u𝒜(P)}\gamma (T_1^u)+\mathrm{\Delta }_{T_1^r,T_2}\right)\mathrm{log}d+𝒯_P\right)`$ time, where $`\gamma (R)`$ denotes $`\mathrm{\Delta }_{R,T_2}\mathrm{log}|(T_2R)|`$.

Cole et al.  proved Lemma 9 for the special case where $`T_1`$ and $`T_2`$ are binary evolutionary trees. For a better flow of discussion, we postpone the proof of Lemma 9 to §4.4. Here, Lemma 9 immediately suggests that $`\text{mast}(T_1,T_2)`$ can be computed in a bottom-up fashion as follows:

* Step 1. Let $``$ denote the ordering on $`𝒟(T)`$ where $`P_1P_2`$ if the root of $`P_1`$ is a descendant of the root $`P_2`$.
* Step 2. For every $`P𝒟(T_1)`$ in increasing order according to $``$, let $`r`$ denote the root of $`P`$; apply Lemma 9 to find $`(T_1^r,(T_2T_1^r)^v)`$ for every node $`vT_2T_1^r`$.

The above algorithm at the end computes $`\text{mast}(T_1^{r(P_o)},(T_2T_1^{r(P_o)}))`$, where $`P_o`$ is root centroid path of $`T_1`$. Since $`r(P_o)`$ is also the root of $`T`$, we have $`T_1^{r(P_o)}=T_1`$ and $`\text{mast}(T_1^{r(P_o)},(T_2T_1^{r(P_o)}))=\text{mast}(T_1,T_2)`$. As stated in the following lemma, the running time is dominated by the time for computing the maximum weight matchings, i.e., $`_{P𝒟(T_1)}𝒯_P`$.

###### Lemma 10.

We can compute $`\text{mast}(T_1,T_2)`$ in $`O(\mathrm{\Delta }\mathrm{log}n\mathrm{log}d+_{P𝒟(T_1)}𝒯_P)`$ time, where $`\mathrm{\Delta }=\mathrm{\Delta }_{T_1,T_2}`$.

###### Proof.

To derive the time for computing $`\text{mast}(T_1,T_2)`$, we simply sum the time bound stated in Lemma 9 over all centroid paths of $`T_1`$. Observe that

$`{\displaystyle \underset{P𝒟(T_1)}{}}\left[\gamma (T_1^{r\left(P\right)}){\displaystyle \underset{u𝒜(P)}{}}\gamma (T_1^u)\right]`$ $`=`$ $`\gamma (T_1^{r_o})\text{, where }r_o\text{ is the root of of }T_1`$
$`=`$ $`\mathrm{\Delta }_{T_1,T_2}\mathrm{log}|T_2T_1|`$
$`=`$ $`\mathrm{\Delta }_{T_1,T_2}\mathrm{log}|T_2|`$
$`=`$ $`\mathrm{\Delta }\mathrm{log}n.`$
Thus, we can compute $`\text{mast}(T_1,T_2)`$ in $`O(\mathrm{\Delta }\mathrm{log}n\mathrm{log}d+_{P𝒟(T_1)}𝒯_P`$ $`+_{P𝒟(T_1)}\mathrm{\Delta }_{T_1^{r(P)},T_2}\mathrm{log}d)`$ time. By Lemma 6, $`_{P𝒟(T_1)}\mathrm{\Delta }_{T_1^{r(P)},T_2}\mathrm{\Delta }\mathrm{log}n`$. Thus, this lemma follows. ∎

###### Theorem 11.

$`\text{mast}(T_1,T_2)`$ can be computed in $`O(\sqrt{d}\mathrm{\Delta }_{T_1,T_2}\mathrm{log}\frac{2n}{d})`$ time.

###### Proof.

By Lemma 8, $`_{P𝒟(T_1)}𝒯_P`$ = $`O(\sqrt{d}\mathrm{\Delta }\mathrm{log}\frac{2n}{d})`$. Thus, by Lemma 10, mast$`(T_1,T_2)`$ can be computed in $`O(\mathrm{\Delta }(\mathrm{log}n\mathrm{log}d+\sqrt{d}\mathrm{log}\frac{2n}{d}))`$ time. Since $`\mathrm{log}n\mathrm{log}d\sqrt{d}\mathrm{log}\frac{2n}{d}`$, this theorem follows. ∎

### 4.4 Proof of Lemma 9

This section provides the details for adapting the techniques of Cole et al.  to prove Lemma 9. Consider any centroid path $`P𝒟(T_1)`$. Let $`r`$ be the root of $`P`$. Lemma 9 states that if we are given, for every $`u𝒜(P)`$,

$$\text{mast}(T_1^u,(T_2T_1^u)^v)\text{ for all }vT_2T_1^u,$$

then we can compute

$$\text{mast}(T_1^r,(T_2T_1^r)^v)\text{ for all }vT_2T_1^r$$
(4)
in $`O\left((\gamma (T_1^r)_{u𝒜(P)}\gamma (T_1^u)+\mathrm{\Delta }_{T_1^r,T_2})\mathrm{log}d+𝒯_P\right)`$ time.

The centroid paths in $`𝒟(T_2T_1^r)`$ partition the set of nodes of $`T_2T_1^r`$ and define an ordering on the nodes of $`T_2T_1^r`$. Precisely, the set of values in Equation (4) are partitioned into the following sets

$$\{\text{mast}(T_1^r,(T_2T_1^r)^v)vQ\}\text{ where }Q𝒟(T_2T_1^r).$$

We focus on computing $`\{\text{mast}(T_1^r,(T_2T_1^r)^v)vQ\}`$ for each $`Q𝒟(T_2T_1^r)`$. Cole et al.  dealt with the special case where $`T_1`$ and $`T_2`$ are binary evolutionary trees. They introduced the maximum agreement matching (MAM) problem and showed that $`\{\text{mast}(T_1^r,(T_2T_1^r)^v)vQ\}`$ can be computed by solving the MAM problem on some weighted bipartite multigraph. In , Przytycka observed that this technique can be generalized to evolutionary trees of arbitrary degrees; basically, it suffices to use a more complicated bipartite multigraph. We observe that this can be further generalized to labeled trees with arbitrary degrees by adding more edges to the multigraph.

In the rest of this section, we define the maximum agreement matching problem and the weighted bipartite multigraph $`𝒢_{PQ}`$ for handling labeled trees with general degrees.

#### The maximum agreement matching problem.

Let $`𝒢=(X,Y,E)`$ be a weighted bipartite multigraph. Suppose that $`X=\{u_1,u_2,\mathrm{},u_p\}`$, $`Y=\{v_1,v_2,\mathrm{},v_q\}`$, and every pair of nodes are connected by at most four edges. Every edge is colored by either gray, green, red, or white. We say that edge $`(u_i,v_j)`$ is below edge $`(u_k,v_{\mathrm{}})`$ if $`i<k`$ and $`j<\mathrm{}`$; and that $`(u_i,v_j)`$ crosses $`(u_k,v_{\mathrm{}})`$ if $`i<k`$ and $`j>\mathrm{}`$. A matching of $`𝒢`$ is an agreement matching if it satisfies all the following properties:

* No white edge crosses another white edge.
* There is at most one gray edge. If a gray edge is present, it must be below all the white edges.
* There are at most one pair of red and green edges. If such a pair is present, then this pair of edges are below all white edges, and the red edge crosses the green edge.
* A gray edge cannot coexist with a pair of red and green edges.

The weight of an agreement matching of $`𝒢`$ is the total weight of the edges in the matching. A maximum agreement matching is one with the maximum weight, and we denote this weight as $`\text{mam}(𝒢)`$.

For any nodes $`u_iX`$ and $`v_jY`$, let $`𝒢(u_i,v_j)`$ denote the subgraph of $`𝒢`$ induced by the nodes $`u_i,u_{i+1},\mathrm{},u_p`$ and $`v_j,v_{j+1},\mathrm{},v_q`$. The maximum agreement matching problem asks for $`\text{mam}(𝒢(u_i,v_j))`$ for all pairs of $`(u_i,v_j)`$ such that either (1) $`u_i=u_1`$ and $`v_j`$ is adjacent to some edges of $`𝒢`$; or (2) $`v_j=v_1`$ and $`u_i`$ is adjacent to some edges of $`𝒢`$.

#### The weighted bipartite multigraph $`𝓖_{𝑷𝑸}`$.

Roughly speaking, $`𝒢_{PQ}`$ is constructed by adding suitable colored edges between $`P`$ and $`Q`$. Our aim is that by solving the MAM problem on $`𝒢_{PQ}`$, all the values in Equation (4) are found automatically.

First, we define a new graph $`H_{uv}^{}`$ from $`G_{uv}`$ and $`H_{uv}`$ as follows. $`H_{uv}^{}`$ has all the edges of $`H_{uv}`$, as well as some other edges from $`G_{uv}`$. Among all the edges of $`G_{uv}`$ adjacent to $`\mathrm{hvy}(u)`$, we add into $`H_{uv}^{}`$ those edges $`(\mathrm{hvy}(u),y)`$ where $`y`$ is adjacent to some edges of $`H_{uv}`$. Among the rest of the edges adjacent to $`\mathrm{hvy}(u)`$, we add into $`H_{uv}^{}`$ the one with the heaviest weight. Similarly, among all edges adjacent to $`\mathrm{hvy}(v)`$, we choose some edges to add into $`H_{uv}^{}`$.

We are now ready to define $`𝒢_{PQ}`$. Suppose that $`P=(u_1,u_2,\mathrm{},u_p)`$ and $`Q=(v_1,v_2,\mathrm{},v_q)`$. There is one or more edges between nodes $`u_i`$ and $`v_j`$ if and only if $`(u_i,v_j)\text{inp}(P,Q)`$. The number, color, and weight of edges between $`u_i`$ and $`v_j`$ are determined in the three cases below. Let $`\text{max}_R=\mathrm{max}\{\text{mast}(T_1^{u_i},\mathrm{\Gamma })\mathrm{\Gamma }`$ is a side tree of $`v_j\}`$. Let $`\text{max}_L=\mathrm{max}\{\text{mast}(\mathrm{\Gamma },T_2^{v_j})\mathrm{\Gamma }`$ is a side tree of $`u_i\}`$.

Case 1: $`u_i`$ and $`v_j`$ are both unlabeled. There are a white edge, a gray edge, a green edge and a red edge connecting $`u_i`$ and $`v_j`$, with weights $`\text{mwm}(H_{u_iv_j})`$, $`\text{mwm}(H_{u_iv_j}^{})`$, $`\text{max}_R`$, and $`\text{max}_L`$, respectively.

Case 2: $`u_i`$ and $`v_j`$ are labeled by the same symbol $`z`$. There are a white edge and a gray edge connecting them. The weight of the white edge is $`\text{mwm}(H_{u_iv_j})+\mu (z)`$. The weight of the gray edge equals the maximum of $`\text{mwm}(H_{u_iv_j}^{})`$ $`+\mu (z)`$, $`\text{max}_R`$, and $`\text{max}_L`$.

Case 3: either $`u_i`$ and $`v_j`$ are labeled by different symbols, or only one of them is labeled. There is only one gray edge connecting them. Its weight equals the larger of $`\text{max}_R`$ and $`\text{max}_L`$.

Note that when the input is evolutionary trees, $`𝒢_{PQ}`$ is reduced to the multigraph defined in , in which most of the edges are from Case 1, and there are edges $`(u_i,v_j)`$ from Cases 2 and 3 only when $`u_i`$ and $`v_j`$ are leaves. For labeled trees, we simply add extra edges in Cases 2 and 3 when $`u_i`$ or $`v_j`$ is a labeled internal node. By construction, we have the following fact.

###### Fact 12.

For any $`u_iP`$ and $`v_jQ`$, $`\text{mam}(𝒢_{PQ}(u_i,v_j))=\text{mast}(T_1^{u_i},(T_2T_1^r)^{v_j})`$. Thus, solving the mam problem on $`𝒢_{PQ}`$ gives $`\text{mast}(T_1^r,(T_2T_1^r)^v)`$ for all $`vQ`$.

Using the techniques of Cole et al. , we can construct $`𝒢_{PQ}`$ for all $`Q𝒟(T_2T_1^r)`$ and solve the corresponding MAM problems in $`O\left(\left(\gamma (T_1^r)_{u𝒜(P)}\gamma (T_1^u)+\mathrm{\Delta }_{T_1^r,T_2}\right)\mathrm{log}d+𝒯_P\right)`$ total time. Therefore, Lemma 9 follows.

## Acknowledgments

We wish to thank anonymous referees for extremely helpful suggestions.