Determining the Hausdorff Distance Between Trees in Polynomial Time

The Hausdorff distance is a relatively new measure of similarity of graphs. The notion of the Hausdorff distance considers a special kind of a common subgraph of the compared graphs and depends on the structural properties outside of the common subgraph. There was no known efficient algorithm for the problem of determining the Hausdorff distance between two trees, and in this paper we present a polynomial-time algorithm for it. The algorithm is recursive and it utilizes the divide and conquer technique. As a subtask it also uses the procedure that is based on the well known graph algorithm of finding the maximum bipartite matching.


Introduction
Comparing the structure of objects is a popular task in several scientific fields. The scientists want to know if the compared objects are identical or similar in some way. For the study of similarity of molecular structures in chemistry many algorithmic approaches have been developed. The so-called structure searching mostly uses a graph isomorphism algorithm to determine whether two molecular compounds are identical; substructure searching involves the subgraph isomorphism problem and involves determining whether any of the sample structures (usually saved in a database) contains a given structure.
Closely related to the topic of this paper is the problem known in chemistry as similarity searching: given a molecule of interest find in a database its nearest neighbours -those molecules which are most similar to the given sample -using some measure of inter-molecular similarity [6]. To have a measure of similarity one has to model the compared objects with an appropriate tool. Graphs are often used for this purpose. Determining the distance between two graphs is related to the study of similarity of molecular structures [14].
A graph can be transformed into another one by a finite sequence of graph edit operations such as vertex insertion, vertex deletion, vertex substitution, edge insertion, edge deletion and edge substitution. Therefore, the distance between the two graphs can be defined by the shortest (or least-cost) edit operation sequence and it is called the graph edit distance [8]. The graph edit distance is a general approach of inexact graph matching and by restricting to some special operations we get special measures. For example, assume that the compared graphs are of the same order and size, the possible operations defined are edge move [2], edge rotation [5] and edge slide [2,10].
A graph G is said to be a common subgraph of the graphs G 1 and G 2 if it holds that H 1 ⊆ G 1 and H 2 ⊆ G 2 , where H 1 and H 2 are both isomorphic to G. We say that a common subgraph G of G 1 and G 2 is a maximum common subgraph if there does not exist a common subgraph H with |V (H)| > |V (G)|. The problem of determining maximum common subgraph is also a special case of graph edit distance computation. It was shown [3] that under a particular cost function the graph edit distance computation is equivalent to the maximum common subgraph problem.
In [4] the authors introduced a graph distance metric based on the maximum common subgraph. The metric they define uses only the order of a maximum common subgraph and the order of the graphs compared. A measure of similarity of graphs based on a maximum common subgraph is often used in chemical graph theory to search for molecules that are measured to be close to each other. In [7,12] the authors described the maximum common subgraph algorithms and their applications to cheminformatics tasks.
The Hausdorff distance of two graphs was introduced in [1]. The Hausdorff distance considers a special kind of a common subgraph of the compared graphs which depends on the structural properties outside of the common subgraph. The Hausdorff distance of graphs is more useful than the graph distance metric based on the maximum common subgraph when the measure of similarity of graphs has to be correlated with the distances from a subgraph (isomorphic to a common subgraph of the compared graphs) to the vertices that are outside of that subgraph. In the Example 1.1 there are graphs G 1 and G 2 that have the same number of vertices in the maximum common subgraphs but different Hausdorff distances regarding to the graph G. Example 1.1. Graphs G 1 and G 2 from Figure 1 are both subgraphs of graph G, therefore, there are six vertices in the maximum common subgraph of G and G 1 , and six vertices in the maximum common subgraph of G and G 2 . However, the Hausdorff distance of G and G 1 is two and the Hausdorff distance of G and G 2 is one. This means that graphs G and G 2 are more similar than graphs G and G 1 with respect to the Hausdorff distance of graphs.
Authors of the paper [11] have further studied the Hausdorff distance on common families of chemical graphs, namely paths, cycles and trees. They have presented an open problem of existence of a polynomial-time algorithm for the Hausdorff distance between two trees.
In this paper we give the answer to this open problem. We present a polynomial-time algorithm for the Hausdorff distance between two trees. The algorithm is based on the divide and conquer technique. We proceed as follows. In the next section we state some basic definitions. Section 3 deals with some known results that are used in the algorithm. In section 4 we present the polynomial-time algorithm for Hausdorff distance between two trees and an example of how this algorithm works.

Basic definitions and notations
Let G = (V (G), E(G)) be a graph with the vertex set V (G) and the edge set E(G), where an edge is an unordered pair of vertices {u, v} . A short notation uv is used for an All graphs considered in the paper are simple graphs, i.e. the are no multiple edges and no loops (uu ∈ E(G) for any u ∈ V (G)).
Let G be a graph and let S ⊆ V (G). By S we denote the subgraph of G induced by the set S, i.e. for all u, v ∈ S, uv ∈ E( S ) if and only if uv ∈ E(G).
Two graphs are isomorphic, if there is a bijective correspondence between their vertex sets which preserves adjacency and non-adjacency of the vertices.
A path P from a vertex x to a vertex y in a graph G is a sequence The vertices x and y are called the endpoints of the path. The length of a path P , denoted by ℓ(P ), is the number of edges in P . If we add the edge xy to the path, then we get a cycle.
The distance between vertices x and y is the length of a shortest path between x and y in G and is denoted by d G (x, y). A graph G is connected if for each pair of vertices u, v ∈ V (G) there is a path in G from u to v. A connected subgraph H of a graph G is convex in G if for any pair of vertices u, v ∈ V (H), any shortest path P from u to v in graph G lies entirely in H (P ⊆ H).
A graph T = (V (T ), E(T )) is a tree if it is connected and has no cycles. A tree T = (V (T ), E(T )) is rooted if there is a distinguished vertex r ∈ T (G) called the root of the tree. Note, there is a unique path from the root to any other vertex v ∈ V (T ). The root is at the top and the other vertices can be partitioned in the levels according to their distance to the root of the tree. The depth of vertex v ∈ V (T ), denoted by depth [v], is the length of the path from the root node to the vertex v. The depth of T is a maximum depth among the all vertices. Vertex v ∈ V (T ) is called ancestor of vertex u ∈ V (T ) if vertex v lies on the unique path from u to the root and Example 2.1. In Figure 2 there is a rooted tree T with the root vertex v 10 . Tree T is drawn twice. On the left side, T is drawn with regard to the depth of the vertices, and on the right side, T is drawn with regard to the height of the vertices. Let G be a graph and v be a vertex of G. The eccentricity of the vertex v, denoted e(v) is the maximum distance from v to any vertex of V (G). That is, e(v) = max{d G (v, u) | u ∈ V (G)}. The radius of the graph G, denoted rad(G), is the minimum eccentricity among the vertices of G, i.e. rad(G) = min{e(v) | v ∈ V (G)}. The diameter of G, denoted diam(G), is the maximum eccentricity among the vertices of G, i.e. diam(G) = max{e(v) | v ∈ V (G)}. The center of G is the set of vertices with minimum eccentricity, i.e. center(G) = {v ∈ V (G) | e(v) = rad(G)}. A vertex v ∈ center(G) is called a central vertex of G. For an arbitrary graph G it holds that rad(G) ≤ diam(G) ≤ 2 · rad(G).
A graph G = (V (G), E(G)) is bipartite if the set of vertices V (G) can be partitioned into two sets A and B such that any edge from E(G) has one endpoint in the set A and the other in the set B. A matching M ⊆ E(G) is a collection of edges such that every vertex of V (G) is incident to at most one edge of M . A vertex is matched if it is an endpoint of an edge from the set M . A maximum matching is a matching that contains the largest possible number of edges. A matching is called perfect or 1-factor if every vertex of a graph G is matched.
To introduce the Hausdorff distance in graphs we will need the following definitions.  Figure 3 for reference. We denote the set of all amalgams of the graphs G 1 and G 2 by X (G 1 , G 2 ). Remark 2.3. Let A be an amalgam of G 1 and G 2 obtained from G 1 and G 2 by identifying their convex subgraphs H 1 and H 2 .
Let G be the family of all simple connected graphs. Figure 3: An amalgam A of G1 and G2.

Remark 2.5. In [1] authors introduced the Hausdorff graph 2 A of the graph A and defined h
as the distance between the vertices G 1 and G 2 in the Hausdorff graph 2 A , where those two vertices correspond to the subgraphs G 1 and G 2 of the graph A. However, it was shown in [11] For the sake of simplicity we define the distance between the covers G A 1 and G A 2 of the amalgam A in this way. Given G 1 , G 2 ∈ G and an amalgam A of G 1 and G 2 , Definition 2.4 says that to determine ). The Hausdorff distance H : G × G → R on G is defined as follows: We call H the Hausdorff distance on G.
From the Definition 2.6 follows that the Hausdorff distance between two graphs is zero if and only if they are isomorphic. If two graphs are not isomorphic then there is at least one vertex outside of the intersection G A 1 ∩ G A 2 of any amalgam and therefore the Hausdorff distance is at least one. Note, Definition 2.6 is equivalent to definition of the Hausdorff distance in [1,Definition 4.18], where it is proven that H is a metric on the class of all simple connected pairwise non-isomorphic graphs. An amalgam A of two simple connected graphs G 1 and To determine the Hausdorff distance between the graphs G 1 and G 2 from G one has to find an optimal amalgam. Having a convex common subgraph of G 1 and G 2 an amalgam of graphs G 1 and G 2 can be constructed. Therefore, the task is to find a convex common subgraph of G 1 and G 2 such that the distance between the covers G A 1 and G A 2 of the corresponding amalgam A is minimized. In [11] the Hausdorff distance between the families of some chemical graphs were considered. The exact formulae for the Hausdorff distance between paths and cycles were given. Trees were also considered and the exact exponential time algorithm for trees was introduced. The authors stated the following open problem: Problem 2.7. [11] Is there a polynomial algorithm that determines the Haudsorff distance between two arbitrary trees?
In the next sections we give an affirmative answer to Problem 2.7 and present such an algorithm.

Preparation for the algorithm
The main procedure of the algorithm is working on the so called top-down common subtrees and therefore we need the following definitions summarized in [13].
) and the rooted tree Example 3.4. In Figure 4 there are two trees T 1 and T 2 . A subtree S 1 induced on the vertex set A subtree of T 1 , induced with grey vertices, is a subtree rooted at vertex v 5 and it is not a top-down subtree since, for example v 5 is not the root and parent[v 5 ] is not in the subtree.
, (v 11 , u 8 )} be a rooted tree isomorphism of S 1 and S 2 . The structure (S 1 , S 2 , M ) is a top-down common subtree of rooted trees T 1 and T 2 .
We proceed with some general properties of the Hausdorff distance between two simple connected graphs and some properties of the Hausdorff distance between two trees.
For a convex common subgraph of two simple connected graphs one can take a trivial subgraph on one vertex from each factor. If central vertices from the both factors are taken as a convex common subgraph then we get a natural upper bound on the Hausdorff distance between the two graphs: Theorem 3.5. [11] Let G 1 and G 2 be two arbitrary simple, connected graphs. Then Figure 4: Illustration of the concepts defined above.
Any tree has either one central vertex or two adjacent central vertices. If |center(T )| = 1 then we say that a tree T is central. Otherwise it is bicentral. The next theorem states that in the tree with the larger diameter there always exists at least one central vertex that is in every optimal amalgam. Theorem 3.6. [11] Let T 1 and T 2 be two arbitrary non-trivial trees, with diam( On the other hand, an example was presented in [11] showing that this may not hold for the tree with a smaller diameter. We will also need to find maximum matchings in bipartite graphs. A maximum matching in bipartite graph G = (V (G), E(G)) is called a maximum bipartite matching. The problem of finding a maximum bipartite matching can be solved in polynomial time. The Hopcroft-Karp algorithm [9] finds a maximum bipartite matching in O( |V (G)||E(G)|) time.
Recall, to determine the Hausdorff distance between two trees, one has to find a convex common subgraph (a subtree) of the input trees such that the distance between the covers of the corresponding amalgam is minimized (an optimal amalgam). Note, a subtree of a tree is always a convex subgraph.
An amalgam of trees T 1 and T 2 is a tree. If we root an amalgam A at a vertex from the intersection , then the intersection of the amalgam is a top-down subtree of the amalgam A. The subtrees of T 1 and T 2 that give rise to the rooted amalgam A are top-down subtrees of the trees T 1 and T 2 rooted in the vertices corresponding to the vertex v A . We can get any optimal amalgam by finding the appropriate top-down subtrees of the input trees, so the procedure of the algorithm works on top-down common subtrees, and therefore, we have to root both input trees. Optimal top-down amalgam is an amalgam optimal with respect to the rooted structure; meaning that the corresponding isomorphism is a rooted tree isomorphism. We call a top-down common subtree optimal if the corresponding amalgam is an optimal top-down amalgam. Note, both root vertices of an optimal top-down common subtree have to be in the intersection of the corresponding amalgam, since the corresponding isomorphism is a rooted tree isomorphism.
Example 3.7. We can see that in Figure 5 there are two non-isomorphic rooted trees T 1 and T 2 . Since the top-down common subtree labeled with black vertices gives rise to an amalgam in which the distance between the covers is equal to one, it follows that this is an optimal top-down common subtree. Figure 5: An optimal top-down common subtree of trees T1 (rooted at v11) and T2 (rooted at u8). It is labeled with black vertices in both trees.
As the input of the algorithm we get two non-rooted trees . Since a central vertex of T 1 is in the intersection of any optimal amalgam (Theorem 3.6) we can root T 1 in a central vertex. For T 2 we have no such property. In the example below we can see that an optimal top-down amalgam is not necessarily an optimal amalgam (non-rooted). This depends on the choice of the root vertices of the input trees T 1 and T 2 . If we root tree T 2 in each vertex v ∈ V (T 2 ) and run the procedure for each such case, then we are guaranteed that the algorithm is able to find a common subtree of the input trees such that the distance between the covers of the corresponding amalgam is minimized. In other words, this way the algorithm finds an optimal top-down amalgam that is also an optimal amalgam. Example 3.8. Figure 6 shows an optimal top-down common subtree of the non isomorphic rooted trees T 1 and T 2 . Trees T 1 and T 2 are almost the same to those in Figure 5, with the difference that tree T 2 here is rooted in the vertex u 7 . An optimal top-down common subtree is induced by black vertices and it gives rise to an amalgam in which the distance between the covers is equal to two. Therefore, this common subtree does not minimize the distance between the covers of the corresponding amalgam of non-rooted trees. The minimum distance is one, see Figure 5.

The Algorithm
Now, we are ready to present the Algorithm 1 that determines the Hausdorff distance between two arbitrary trees T 1 and T 2 in polynomial time. The corresponding common subtree structure is also determined by the algorithm.
The algorithm uses two procedures. With respect to Definition 3.3, an optimal top-down common subtree is a structure (S 1 , S 2 , M ) and therefore, we have to find a mapping M from T 1 to T 2 . The procedure OptimalTopDownCommonSubtree is for determining the distance between the covers of Figure 6: An optimal top-down common subtree of trees T1 (rooted at v11) and T2 (rooted at u7), induced on black vetrices in both trees.

Algorithm 1:
HausdorffDistanceBetweenTrees input : Arbitrary trees T 1 and T 2 , where diam(T 1 ) ≥ diam(T 2 ). output: The Hausdorff distance between T 1 and T 2 stored in hd, and the corresponding common subtree structure stored in M . the optimal top-down amalgam of two rooted trees and the procedure ReconstructionOfMapping is for the reconstruction of the subtree isomorphism that corresponds to the optimal amalgam. Notice that the first procedure is called many times with different rooted trees as input, while the second one (for the reconstruction of solution) is called just once, at the end of the algorithm. First, let us describe the procedure OptimalTopDownCommonSubtree. The result of the pro-cedure is the distance between the covers of the optimal top-down amalgam of the input rooted trees. Remember, an optimal top-down common subtree gives rise to an optimal top-down amalgam. An optimal top-down common subtree of the rooted input trees T 1 and T 2 can be constructed with breaking down the original rooted trees to rooted subtrees and finding optimal top-down common subtrees of those smaller rooted trees. We start with the root vertices r 1 and r 2 , and traverse both trees recursively. At each step we are in the vertices v ∈ V (T 1 ) and u ∈ V (T 2 ). We break down each rooted tree into rooted subtrees, such that the rooted subtrees of T 1 are rooted in the children of v and the rooted subtrees of T 2 are rooted in the children of u. We consider optimal top-down common subtrees for all possible pairs of those smaller subtrees. After we get all optimal top-down common subtrees for the children of v and children of u we can combine some of them and determine an optimal top-down common subtree of the subtree rooted at v and the subtree rooted at u. When we combine optimal top-down common subtrees of children of v and children of u, we have to be careful that we do not combine one subtree with more than one other subtree.
We can easily determine an optimal top-down common subtree if one of the root vertices is a leaf of original input tree (subtree rooted at this root is a trivial graph). If a vertex v ∈ V (T 1 ) is a leaf (or a vertex u ∈ V (T 2 ) is a leaf) then mapping v to u gives an optimal top-down common subtree. The distance between the covers of the corresponding amalgam is determined by the farthest vertex from the root in the other subtree. The farthest vertex from the root is always at the distance equal to height[u] (or height[v]), respectively. Therefore, one of the root vertices being a leaf is our stopping condition for the recursion.
Otherwise . For technical reasons related to the reconstruction of an optimal top-down common subtree, the edges (v i , u j ) ∈ E of graph G vu are ordered pairs of vertices. The first vertex is from T 1 and the second is from T 2 . Each edge of G vu is assigned a non-negative weight. We want that from the weights of the edges of the graph G vu we are able to determine the distance between the covers of an optimal top-down amalgam of a subtree rooted at v and a subtree rooted at u. The weight of an edge (v i , u j ) ∈ E is equal to the distance between the covers in an optimal top-down amalgam of a subtree (of T 1 ) rooted at v i and a subtree (of T 2 ) rooted at u j . Therefore, we will recursively call the same procedure with different root vertices. If v i ∈ V (T 1 ) is a leaf (or u j ∈ V (T 2 ) is a leaf) then the recursive call hits the stop condition and returns the distance height[u] (or height[v]), respectively. A dummy vertex d k represents an empty subtree and no such top-down common subtree exists. If we want that the weight of the edge (v i , d k ) ∈ E can possibly give rise to the distance between the covers of an optimal top-down amalgam of a subtree rooted at v and a subtree rooted at u, then the edge (v i , d k ) must get the weight that is equal to the distance of the farthest vertex from the v i plus 1 (height[v] + 1), i.e. vertices v and u are in the intersection of such optimal topdown amalgam while the whole subtree rooted at v i is not in the intersection of such optimal top-down amalgam. When all the weights of the graph G vu are determined we need to get the best possible combination of the corresponding optimal top-down amalgams to combine them into an optimal top-down amalgam A of a subtree rooted at v and a subtree rooted at u. We have to minimize the distance between the covers of an optimal top-down amalgam A. To do this we need the following concept. Let M vu be a perfect matching of the complete bipartite graph G vu that minimizes the value of the largest weight (we will call it an optimal perfect matching).
Lemma 4.1. The distance between the covers of an optimal top-down amalgam of a subtree (of T 1 ) rooted at v and a subtree (of T 2 ) rooted at u is equal to the largest weight in an optimal perfect matching M vu .
Proof: Every perfect matching of the graph G vu corresponds to a bijective mapping between partitions of the graph G vu . Therefore, a perfect matching gives rise to a combination of optimal top-down amalgams between the subtrees rooted at children [v] and subtrees rooted at children[u] together with the dummy vertices. Every subtree rooted at some vertex from the set children[v] is combined either with exactly one subtree rooted at some vertex from the children[u] or exactly one dummy vertex. Such a combination of optimal top-down amalgams induces an amalgam A of a subtree rooted at v and a subtree rooted at u. The distance between the covers of the amalgam A is equal to the largest weight in a perfect matching, since the weights of edges in the graph G vu are the distances between the covers of the corresponding optimal top-down amalgams.
Let M vu be an optimal perfect matching of the graph G vu . From the construction of the graph G vu it follows that the distance between the covers of an optimal top-down amalgam is at most the largest weight in an optimal perfect matching M vu . For the converse suppose, that the distance between the covers of an optimal top-down amalgam is less than the largest weight in an optimal perfect matching M vu . Using the corresponding subtree isomorphism M of the optimal top-down common subtree we can construct the complete bipartite graph G ′ vu which has an optimal perfect matching with the largest weight that is smaller than the largest weight in M vu , a contradiction with the construction of G vu . Therefore, the distance between the covers of an optimal top-down amalgam is equal to where M is a perfect matching of the complete bipartite graph G vu and w(e) represents the weight of the edge e.
When all the recursive calls are completed, we get back to the root vertices and the largest weight of the optimal perfect matching M r1u is the distance between the covers of an optimal top-down amalgam of the rooted trees T 1 and T 2 .
Procedure OptimalTopDownCommonSubtree(T 1 ,v,T 2 ,u,M ′ ) input : Rooted tree T 1 and its root vertex v, rooted tree T 2 and its root vertex u, and the union set of solutions to the optimal perfect matching problems M ′ . output: Distance between the subtree of T 1 rooted at v and subtree of T 2 rooted at u, and the union set of solutions to all optimal perfect matchings solved during the procedure saved in M ′ . The described procedure uses the sub-procedure SolveOptimalPerfectMatching that finds a perfect matching of the complete bipartite graph G vu that minimizes the value of the largest weight (an optimal perfect matching) and returns it. For the sake of clarity we will describe this sub-procedure briefly.
Given a complete bipartite graph G vu = (V (G vu ), E(G vu )) with |V (G vu )| = 2p, we first sort the edges in the ascending order of the edge weights and make an array of all different edge weights. Then we find an optimal perfect matching of G vu by using binary search on the array of the edge weights where on each step we run Hopcroft-Karp algorithm for finding a maximum bipartite matching. In each iteration we have an edge weight w in the middle of the current array. Take the spanning subgraph G ′ vu of the graph G vu with all the edges having the weights smaller or equal than w. Find a maximum bipartite matching M ′ vu of the graph G ′ vu using the Hopcroft-Karp algorithm. If |M ′ vu | = p, then the optimal perfect matching M vu has the largest weight smaller of equal than w. We save current M ′ vu into M vu and we can continue the binary search in the left half of the current array. Otherwise, the optimal perfect matching M vu has the largest weight greater than w and we can continue the binary search in the right half of the current array. Since the graph G vu is a finite complete bipartite graph there will be at least one maximum bipartite matching M ′ vu with cardinality p during the described binary search. In the end, return the matching M vu .
Let us take a look at an example of executing the procedure OptimalTopDownCommonSubtree on the input rooted trees T 1 (rooted at v 11 ) and T 2 (rooted at u 8 ), both depicted in Figure 5.
Example 4.2. We start with the tree T 1 rooted at v 11 and tree T 2 rooted at u 8 . Since none of the root vertices is a leaf we build the following complete bipartite graph with edge weights table shown on the right hand-side: We know the weights of edges if one of the endpoints is a leaf or a dummy vertex. To get the missing weights we have to proceed recursively down the trees. First, we want to determine the weight of the edge v 6 u 4 . In order to find the optimal top-down common subtree of the subtree rooted at v 6 and subtree rooted at u 4 we get complete bipartite graph G v6u4 : Since the vertices u 1 , u 2 and u 3 are leaves, all the weights are known. Therefore, we get an optimal perfect matching M v6u4 = {(v 2 , u 3 ), (v 5 , u 2 ), (d 2 , u 1 )} of the complete bipartite graph (drawn with bold edges and encircled weights). The largest weight of M v6u4 is 1, therefore the weight of the edge v 6 u 4 from graph G v11u8 is 1.
Next, we want to determine the weight of the edge v 6 u 7 . In order to find the optimal top-down common subtree of the subtree rooted at v 6 and the subtree rooted at u 7 we get complete bipartite graph G v6u7 : For the weights of edges v 2 u 6 and v 5 u 6 we have to find the optimal top-down common subtrees of the following two pairs of rooted subtrees. The first pair with the subtree rooted at v 2 and subtree rooted at u 6 yields the trivial weighted complete bipartite graph G v2u6 with the optimal perfect matching M v2u6 = The second one with the subtree rooted at v 5 and subtree rooted at u 6 yields the complete bipartite graph Therefore, the weights of edges v 2 u 6 and v 5 u 6 from graph G v6u7 are 0 and 1, respectively. We have all the weights of the graph G v6u7 to find the optimal top-down common subtree of the subtree rooted at v 6 and the subtree rooted at u 7 : From the largest weight of optimal perfect matching M v6u7 = {(v 2 , u 6 ), (v 5 , d 3 )} it follows that the weight of the edge v 6 u 7 from graph G v11u8 is equal to 2. Proceeding in the same way, we have to determine the weight of the edge v 9 u 4 . In order to find the optimal top-down common subtree of the subtree rooted at v 9 and the subtree rooted at u 4 we get the complete bipartite graph G v9u4 with the optimal perfect matching M v9u4 = {(v 8 , u 1 ), (d 5 , u 2 ), (d 6 , u 3 )}: The largest weight of the optimal perfect matching M v9u4 is equal to 1 so the weight of the edge v 9 u 4 from graph G v11u8 is 1.
To get the last missing weight, namely the weight of the edge v 9 u 7 , from the graph G v11u8 we have to find the optimal top-down common subtree of the subtree rooted at v 9 and the subtree rooted at u 7 . We get the trivial weighted complete bipartite graph G v9u7 : The perfect matching is trivial but we still need the weight of the edge v 8 u 6 . To get the weight of the edge v 8 u 6 we get another trivial complete bipartite graph G v8u6 with optimal perfect matching M v8u6 = Since the largest weight of the matching M v8u6 is equal 0 also the largest weight of the previous trivial matching M v9u7 = {(v 8 , u 6 )} is equal to 0. Therefore, the weight of the edge v 9 u 7 is equal to 0 and now we have all the weights to find the perfect matching of the complete bipartite graph G v11u8 : After finding the optimal perfect matching M v11u8 = {(v 6 , u 4 ), (v 9 , u 7 ), (v 10 , d 1 )} we get the optimal top-down common subtree of the input rooted trees T 1 (rooted at v 11 ) and T 2 (rooted at u 8 ). The largest weight of the optimal perfect matching M v11u8 is equal to 1 so the distance between the covers of the corresponding amalgam is equal to 1.
The procedure ReconstructionOfMapping is used to construct an actual optimal top-down common subtree isomorphism mapping M of the input rooted trees. The construction is based on the Lemma 4.3. First, recall some properties of optimal perfect matchings.
At a fixed step during the procedure OptimalTopDownCommonSubtree we are in the vertices v ∈ T 1 and u ∈ T 2 . Let S 1 = (V (S 1 ), E(S 1 )) be the subtree of T 1 rooted at v and S 2 = (V (S 2 ), E(S 2 )) the subtree of T 2 rooted at u. The solution to an optimal perfect matching M vu of the complete bipartite graph G vu is a set of weighted edges. Notice, the endpoints of those edges are from the vertex sets V (S 1 ), V (S 2 ) or dummy vertices D. If we remove from set M vu all the edges with a dummy vertex as an endpoint, then we get a set of ordered pairs of vertices . Lemma 4.3. Let T 1 = (V (T 1 ), E(T 1 )) and T 2 = (V (T 2 ), E(T 2 )) be input rooted trees for the procedure OptimalTopDownCommonSubtree and let M ′ ⊆ V (T 1 ) × V (T 2 ) be the union set of solutions to all optimal perfect matching problems solved during the procedure without the edges incident with dummy vertices. There is a unique optimal top-down common subtree isomorphism M ⊆ V (T 1 ) × V (T 2 ) such that M ⊆ M ′ .

Proof:
Let T 1 = (V (T 1 ), E(T 1 )) and T 2 = (V (T 2 ), E(T 2 )) be the input rooted trees for the procedure OptimalTopDownCommonSubtree and let M ′ be the corresponding union set of solutions to optimal perfect matching problems without the edges incident with dummy vertices. If we show that for each vertex v ∈ V (T 1 ) with (parent(v), z) ∈ M ′ , for some vertex z ∈ V (T 2 ), there is at most one pair (v, w) ∈ M ′ such that parent(w) = z, then we can reconstruct the unique optimal top-down common subtree isomorphism M ⊆ M ′ of T 1 and T 2 in the order of non-decreasing depth of the vertices in the tree T 1 .
Let (v, w 1 ), (v, w 2 ) ∈ M ′ with w 1 = w 2 . Suppose that vertices w 1 and w 2 are siblings. Both of them appear in the bipartite graph G pz in the same partition set, where p = parent(v). Two edges in a (bipartite) matching cannot share a common vertex. Only one pair, either (v, w 1 ) or (v, w 2 ), can be part of an optimal perfect matching for G pz , a contradiction. It follows that vertices w 1 and w 2 are not siblings. Therefore, parent(w 1 ) = parent(w 2 ).
We will reconstruct an optimal top-down common subtree isomorphism mapping M ⊆ V (T 1 )× V (T 2 ) from the set M ′ ⊆ V (T 1 ) × V (T 2 ) as follows. Begin with M = {(r 1 , r 2 )} and for all the remaining vertices v ∈ V (T 1 ) in preorder traversal (i) of the tree T 1 , add the pair (v, w) to the set M if it holds that (v, w) ∈ M ′ and (parent(v), parent(w)) ∈ M .
Procedure ReconstructionOfMapping(T 1 ,r 1 ,r 2 ,M ′ ,M ) input : Rooted tree T 1 and its root vertex r 1 , root vertex r 2 of T 2 , the union set of solutions to the optimal perfect matching problems M ′ and mapping M . output: Optimal top-down common subtree isomorphism mapping M from the subtree of T 1 rooted at r 1 to subtree of T 2 rooted at r 2 reconstructed from the union set of solutions to all optimal perfect matchings saved in M ′ .  (i) In this case, preorder traversal means that we start in the root vertex and, the parent vertices have to be visited before their child vertices. The visiting order of children of a vertex is not important.
Lemma 4.6. Let T 1 = (V (T 1 ), E(T 1 )) and T 2 = (V (T 2 ), E(T 2 )) be rooted input trees of the procedure OptimalTopDownCommonSubtree and let G vu be the complete bipartite graph on 2p vertices considered during the procedure. The sub-procedure of finding an optimal perfect matching of graph G vu runs in O log (|V (T 1 )|) · p 5 2 .
Proof: Graph G vu has p 2 edges. First we sort all the edges in O(p 2 · log (p 2 )) time. Then we do a binary search on the array of all different edge weights. There are at most |V (T 1 )| different edge weights in the graph G vu since the edge weights in graph G vu are made from heights of the vertices of the input trees. Therefore, the binary search takes at most log (|V (T 1 )|) iterations. At each iteration we run the Hopcroft-Karp algorithm for maximum bipartite matching. Hopcroft-Karp algorithm runs in O( |V (G)||E(G)|) time [9]. This gives us the O log (|V (T 1 )|) · p 5 2 overall time complexity.
Proof: If one of the root vertices is a leaf then the complexity of the procedure is constant. Therefore, the total effort spent on leaves is bounded by O (|V (T 1 )| + |V (T 2 )|).
If both of the root vertices are non-leaves then the most (time) consuming part of the procedure is the sub-procedure SolveOptimalPerfectMatching and is bounded by time complexity O log (|V (T 1 )|) · p 5 2 due to the Lemma 4.6, where p = max {|children[v]|, |children[u]|}. Until the end of the proof we will denote |children[v]| with c(v). If we sum the time complexities of all the possible pairs of vertices such that one is from V (T 1 ) and the other is from V (T 2 ) then we get an upper bound for the time complexity. Therefore, using the following equalities and inequalities