The Zhang-Shasha algorithm computes the tree edit distance full
This proof shows: correctness, termination.
Setting. A forest is an ordered list of ordered rooted trees. For two nodes u,u′ of a forest, u is an ancestor of u′ if it lies on the path from u′ to its root (a node is not its own proper ancestor), and u is to the left of u′ if neither is an ancestor of the other and u comes first in postorder. A mapping M between forests F and G is a set of pairs (v,w)∈F×G in which each node occurs at most once, and such that for all (v1,w1),(v2,w2)∈M: v1 is an ancestor of v2 exactly when w1 is an ancestor of w2, and v1 is to the left of v2 exactly when w1 is to the left of w2. Its cost γ(M) is ∑(v,w)∈Mγ(v→w)+∑v unmappedγ(v→ε)+∑w unmappedγ(ε→w), with non-negative costs. Let δ(F,G) be the least cost of a mapping; the empty set is a mapping, so δ is well defined. For a node v, T(v) is its subtree; F−v is F with v removed and its children put in its place; F−T(v) is F without the subtree of v.
Termination. All loops of lines L3 to L9 have fixed bounds, so the algorithm stops.
Lemma 1 (empty forests). δ(∅,∅)=0, δ(F,∅) is the sum of the deletion costs of F, and δ(∅,G) is the sum of the insertion costs of G.
Proof. The only mapping with an empty side is the empty mapping. □
Lemma 2 (forest recurrence). Let F and G be non-empty forests with rightmost roots v and w. Then
δ(F,G)=min(δ(F−v,G)+γ(v→ε), δ(F,G−w)+γ(ε→w), δ(F−T(v),G−T(w))+δ(T(v),T(w))). If moreover F=T(v) and G=T(w), then
δ(F,G)=min(δ(F−v,G)+γ(v→ε), δ(F,G−w)+γ(ε→w), δ(T(v)−v,T(w)−w)+γ(v→w)). Proof. Each term is the cost of a mapping (upper bound). A mapping of F−v and G is a mapping of F and G in which v is unmapped: removing v keeps the ancestor and left relations among the other nodes. So δ(F,G)≤δ(F−v,G)+γ(v→ε), and the same for w. Let M1 be a mapping of F−T(v) and G−T(w) and M2 a mapping of T(v) and T(w). Every node of F−T(v) is to the left of every node of T(v), because v is the rightmost root, and the same holds in G. So M1∪M2 keeps both relations, and its cost is the sum of the two costs. In the tree case, a mapping M2′ of T(v)−v and T(w)−w together with the pair (v,w) is a mapping: v and w are ancestors of all other nodes of their trees.
An optimal mapping has one of these forms (lower bound). Let M be a mapping of least cost. If v is unmapped, M is a mapping of F−v and G with γ(M)=γ(M on F−v)+γ(v→ε), so δ(F,G)≥ the first term. The same holds if w is unmapped. Otherwise (v,w′)∈M and (v′,w)∈M for some w′, v′. Suppose w′=w. Then v′=v. As v is the rightmost root, v′ is either a descendant of v or to the left of v. If v′ is a descendant of v, then w′ is an ancestor of w, which is impossible since w is a root. If v′ is to the left of v, then w is to the left of w′, so w comes before w′ in postorder. This is impossible, since w is the rightmost root and so the last node of G in postorder. So (v,w)∈M. Now a pair (x,y)∈M with x∈T(v), x=v, has v as an ancestor of x, so w is an ancestor of y and y∈T(w); symmetrically for y∈T(w). So M splits into M1 on F−T(v) and G−T(w) and M2 on T(v) and T(w), with (v,w)∈M2, and γ(M)=γ(M1)+γ(M2)≥δ(F−T(v),G−T(w))+δ(T(v),T(w)). In the tree case, F−T(v) and G−T(w) are empty, and M2∖{(v,w)} is a mapping of T(v)−v and T(w)−w, so γ(M)≥δ(T(v)−v,T(w)−w)+γ(v→w). □
Postorder intervals. Number each tree 1,…,n in postorder and let l(i) be the leftmost leaf of T(i). The subtree T(i) is the interval [l(i),i]. For l(i)≤i1≤i, the nodes l(i),…,i1 form a forest T[l(i)..i1] whose rightmost root is i1; removing i1 gives T[l(i)..i1−1], and removing T(i1) gives T[l(i)..l(i1)−1], where an interval with its end below its start is the empty forest. If l(i1)=l(i), then T[l(i)..i1]=T(i1).
Lemma 3 (the table). Fix a pair of keyroots (i,j) handled at lines L5 to L13, and assume that every entry td[i1][j1] read at line L13 already holds δ(T1(i1),T2(j1)). Then after the pair, fd[i1][j1]=δ(T1[l1(i)..i1],T2[l2(j)..j1]) for all l1(i)−1≤i1≤i and l2(j)−1≤j1≤j, and line L12 sets td[i1][j1]=δ(T1(i1),T2(j1)) for every i1 with l1(i1)=l1(i) and j1 with l2(j1)=l2(j).
Proof. Lines L5 to L7 fill the row and the column of the empty forests, correctly by Lemma 1. The loops of lines L8 and L9 go through the other entries row by row. Each read entry has a smaller row, or the same row and a smaller column, or the row l1(i1)−1<i1 and the column l2(j1)−1<j1, and all these indices are at least l1(i)−1 and l2(j)−1, since l(i1)≥l(i) for i1 in T(i). So each read entry was written earlier in this pair. By the postorder intervals, line L11 is the tree case of Lemma 2 and line L13 is its forest case, with the assumed values of td. Line L12 copies a value that, in the tree case, is δ(T1(i1),T2(j1)). □
Lemma 4 (order of the keyroots). For each node i1, let k(i1) be the highest node with the leftmost leaf l(i1). Then k(i1) is a keyroot, l(i1)≤i1≤k(i1), and td[i1][j1] is written at line L12 during the pair (k1(i1),k2(j1)). If i1 lies in T1(i) and l1(i1)=l1(i), then k1(i1)<i; if l1(i1)=l1(i) for a keyroot i, then k1(i1)=i. The same holds in T2.
Proof. The nodes with the leftmost leaf l(i1) form a path upward from that leaf, and k(i1) is the top of this path, so no later node has the same leftmost leaf: it is a keyroot, and i1 is on the path, so i1≤k(i1). In the pair (k1(i1),k2(j1)), i1 and j1 satisfy the test of line L10, so line L12 writes td[i1][j1]. If i is a keyroot and l(i1)=l(i), both are on the same path, whose top is i. If i1∈T(i) and l(i1)=l(i), the path of l(i1) is inside T(i) and does not reach i, so its top is a proper descendant of i, and comes before i in postorder. □
Theorem. ZHANG-SHASHA returns δ(T1,T2).
Proof. Process the pairs of keyroots in the order of lines L3 and L4: by increasing i, and for each i by increasing j. We show by induction over this order that the assumption of Lemma 3 holds for each pair. At line L13 for the pair (i,j), i1 and j1 do not both have the leftmost leaves of i and j. By Lemma 4, td[i1][j1] was written in the pair (k1(i1),k2(j1)), where either k1(i1)<i, or k1(i1)=i and k2(j1)<j. In both cases this pair comes earlier, and by the induction hypothesis and Lemma 3 the value written there is δ(T1(i1),T2(j1)). Later pairs may overwrite td[i1][j1] only with the same value, by Lemma 3 again. So Lemma 3 applies to every pair. The roots n1 and n2 are keyroots with l(nt)=1, and the last pair (n1,n2) writes td[n1][n2]=δ(T1,T2) at line L12, which line L14 returns. ■