Algorithm Search⌘K

Proofs: Zhang-Shasha algorithm

These are the human proofs of Zhang-Shasha algorithm, at full length.

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′u, u' of a forest, uu is an ancestor of u′u' if it lies on the path from u′u' to its root (a node is not its own proper ancestor), and uu is to the left of u′u' if neither is an ancestor of the other and uu comes first in postorder. A mapping MM between forests FF and GG is a set of pairs (v,w)∈F×G(v, w) \in F \times G in which each node occurs at most once, and such that for all (v1,w1),(v2,w2)∈M(v_1, w_1), (v_2, w_2) \in M: v1v_1 is an ancestor of v2v_2 exactly when w1w_1 is an ancestor of w2w_2, and v1v_1 is to the left of v2v_2 exactly when w1w_1 is to the left of w2w_2. Its cost γ(M)\gamma(M) is ∑(v,w)∈Mγ(v→w)+∑v unmappedγ(v→ε)+∑w unmappedγ(ε→w)\sum_{(v, w) \in M} \gamma(v \to w) + \sum_{v \text{ unmapped}} \gamma(v \to \varepsilon) + \sum_{w \text{ unmapped}} \gamma(\varepsilon \to w), with non-negative costs. Let δ(F,G)\delta(F, G) be the least cost of a mapping; the empty set is a mapping, so δ\delta is well defined. For a node vv, T(v)T(v) is its subtree; F−vF - v is FF with vv removed and its children put in its place; F−T(v)F - T(v) is FF without the subtree of vv.

Termination. All loops of lines L3 to L9 have fixed bounds, so the algorithm stops.

Lemma 1 (empty forests). δ(∅,∅)=0\delta(\emptyset, \emptyset) = 0, δ(F,∅)\delta(F, \emptyset) is the sum of the deletion costs of FF, and δ(∅,G)\delta(\emptyset, G) is the sum of the insertion costs of GG.

Proof. The only mapping with an empty side is the empty mapping. □\square

Lemma 2 (forest recurrence). Let FF and GG be non-empty forests with rightmost roots vv and ww. Then

δ(F,G)=min⁡(δ(F−v,G)+γ(v→ε), δ(F,G−w)+γ(ε→w), δ(F−T(v),G−T(w))+δ(T(v),T(w))).\delta(F, G) = \min\big(\delta(F - v, G) + \gamma(v \to \varepsilon),\ \delta(F, G - w) + \gamma(\varepsilon \to w),\ \delta(F - T(v), G - T(w)) + \delta(T(v), T(w))\big).

If moreover F=T(v)F = T(v) and G=T(w)G = T(w), then

δ(F,G)=min⁡(δ(F−v,G)+γ(v→ε), δ(F,G−w)+γ(ε→w), δ(T(v)−v,T(w)−w)+γ(v→w)).\delta(F, G) = \min\big(\delta(F - v, G) + \gamma(v \to \varepsilon),\ \delta(F, G - w) + \gamma(\varepsilon \to w),\ \delta(T(v) - v, T(w) - w) + \gamma(v \to w)\big).

Proof. Each term is the cost of a mapping (upper bound). A mapping of F−vF - v and GG is a mapping of FF and GG in which vv is unmapped: removing vv keeps the ancestor and left relations among the other nodes. So δ(F,G)≤δ(F−v,G)+γ(v→ε)\delta(F, G) \le \delta(F - v, G) + \gamma(v \to \varepsilon), and the same for ww. Let M1M_1 be a mapping of F−T(v)F - T(v) and G−T(w)G - T(w) and M2M_2 a mapping of T(v)T(v) and T(w)T(w). Every node of F−T(v)F - T(v) is to the left of every node of T(v)T(v), because vv is the rightmost root, and the same holds in GG. So M1∪M2M_1 \cup M_2 keeps both relations, and its cost is the sum of the two costs. In the tree case, a mapping M2′M_2' of T(v)−vT(v) - v and T(w)−wT(w) - w together with the pair (v,w)(v, w) is a mapping: vv and ww are ancestors of all other nodes of their trees.

An optimal mapping has one of these forms (lower bound). Let MM be a mapping of least cost. If vv is unmapped, MM is a mapping of F−vF - v and GG with γ(M)=γ(M on F−v)+γ(v→ε)\gamma(M) = \gamma(M \text{ on } F - v) + \gamma(v \to \varepsilon), so δ(F,G)≥\delta(F, G) \ge the first term. The same holds if ww is unmapped. Otherwise (v,w′)∈M(v, w') \in M and (v′,w)∈M(v', w) \in M for some w′w', v′v'. Suppose w′≠ww' \ne w. Then v′≠vv' \ne v. As vv is the rightmost root, v′v' is either a descendant of vv or to the left of vv. If v′v' is a descendant of vv, then w′w' is an ancestor of ww, which is impossible since ww is a root. If v′v' is to the left of vv, then ww is to the left of w′w', so ww comes before w′w' in postorder. This is impossible, since ww is the rightmost root and so the last node of GG in postorder. So (v,w)∈M(v, w) \in M. Now a pair (x,y)∈M(x, y) \in M with x∈T(v)x \in T(v), x≠vx \ne v, has vv as an ancestor of xx, so ww is an ancestor of yy and y∈T(w)y \in T(w); symmetrically for y∈T(w)y \in T(w). So MM splits into M1M_1 on F−T(v)F - T(v) and G−T(w)G - T(w) and M2M_2 on T(v)T(v) and T(w)T(w), with (v,w)∈M2(v, w) \in M_2, and γ(M)=γ(M1)+γ(M2)≥δ(F−T(v),G−T(w))+δ(T(v),T(w))\gamma(M) = \gamma(M_1) + \gamma(M_2) \ge \delta(F - T(v), G - T(w)) + \delta(T(v), T(w)). In the tree case, F−T(v)F - T(v) and G−T(w)G - T(w) are empty, and M2∖{(v,w)}M_2 \setminus \{(v, w)\} is a mapping of T(v)−vT(v) - v and T(w)−wT(w) - w, so γ(M)≥δ(T(v)−v,T(w)−w)+γ(v→w)\gamma(M) \ge \delta(T(v) - v, T(w) - w) + \gamma(v \to w). □\square

Postorder intervals. Number each tree 1,…,n1, \ldots, n in postorder and let l(i)l(i) be the leftmost leaf of T(i)T(i). The subtree T(i)T(i) is the interval [l(i),i][l(i), i]. For l(i)≤i1≤il(i) \le i_1 \le i, the nodes l(i),…,i1l(i), \ldots, i_1 form a forest T[l(i)..i1]T[l(i)..i_1] whose rightmost root is i1i_1; removing i1i_1 gives T[l(i)..i1−1]T[l(i)..i_1 - 1], and removing T(i1)T(i_1) gives T[l(i)..l(i1)−1]T[l(i)..l(i_1) - 1], where an interval with its end below its start is the empty forest. If l(i1)=l(i)l(i_1) = l(i), then T[l(i)..i1]=T(i1)T[l(i)..i_1] = T(i_1).

Lemma 3 (the table). Fix a pair of keyroots (i,j)(i, j) handled at lines L5 to L13, and assume that every entry td[i1][j1]\mathit{td}[i_1][j_1] read at line L13 already holds δ(T1(i1),T2(j1))\delta(T_1(i_1), T_2(j_1)). Then after the pair, fd[i1][j1]=δ(T1[l1(i)..i1],T2[l2(j)..j1])\mathit{fd}[i_1][j_1] = \delta(T_1[l_1(i)..i_1], T_2[l_2(j)..j_1]) for all l1(i)−1≤i1≤il_1(i) - 1 \le i_1 \le i and l2(j)−1≤j1≤jl_2(j) - 1 \le j_1 \le j, and line L12 sets td[i1][j1]=δ(T1(i1),T2(j1))\mathit{td}[i_1][j_1] = \delta(T_1(i_1), T_2(j_1)) for every i1i_1 with l1(i1)=l1(i)l_1(i_1) = l_1(i) and j1j_1 with l2(j1)=l2(j)l_2(j_1) = l_2(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<i1l_1(i_1) - 1 < i_1 and the column l2(j1)−1<j1l_2(j_1) - 1 < j_1, and all these indices are at least l1(i)−1l_1(i) - 1 and l2(j)−1l_2(j) - 1, since l(i1)≥l(i)l(i_1) \ge l(i) for i1i_1 in T(i)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\mathit{td}. Line L12 copies a value that, in the tree case, is δ(T1(i1),T2(j1))\delta(T_1(i_1), T_2(j_1)). □\square

Lemma 4 (order of the keyroots). For each node i1i_1, let k(i1)k(i_1) be the highest node with the leftmost leaf l(i1)l(i_1). Then k(i1)k(i_1) is a keyroot, l(i1)≤i1≤k(i1)l(i_1) \le i_1 \le k(i_1), and td[i1][j1]\mathit{td}[i_1][j_1] is written at line L12 during the pair (k1(i1),k2(j1))(k_1(i_1), k_2(j_1)). If i1i_1 lies in T1(i)T_1(i) and l1(i1)≠l1(i)l_1(i_1) \ne l_1(i), then k1(i1)<ik_1(i_1) < i; if l1(i1)=l1(i)l_1(i_1) = l_1(i) for a keyroot ii, then k1(i1)=ik_1(i_1) = i. The same holds in T2T_2.

Proof. The nodes with the leftmost leaf l(i1)l(i_1) form a path upward from that leaf, and k(i1)k(i_1) is the top of this path, so no later node has the same leftmost leaf: it is a keyroot, and i1i_1 is on the path, so i1≤k(i1)i_1 \le k(i_1). In the pair (k1(i1),k2(j1))(k_1(i_1), k_2(j_1)), i1i_1 and j1j_1 satisfy the test of line L10, so line L12 writes td[i1][j1]\mathit{td}[i_1][j_1]. If ii is a keyroot and l(i1)=l(i)l(i_1) = l(i), both are on the same path, whose top is ii. If i1∈T(i)i_1 \in T(i) and l(i1)≠l(i)l(i_1) \ne l(i), the path of l(i1)l(i_1) is inside T(i)T(i) and does not reach ii, so its top is a proper descendant of ii, and comes before ii in postorder. □\square

Theorem. ZHANG-SHASHA returns δ(T1,T2)\delta(T_1, T_2).

Proof. Process the pairs of keyroots in the order of lines L3 and L4: by increasing ii, and for each ii by increasing jj. 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)(i, j), i1i_1 and j1j_1 do not both have the leftmost leaves of ii and jj. By Lemma 4, td[i1][j1]\mathit{td}[i_1][j_1] was written in the pair (k1(i1),k2(j1))(k_1(i_1), k_2(j_1)), where either k1(i1)<ik_1(i_1) < i, or k1(i1)=ik_1(i_1) = i and k2(j1)<jk_2(j_1) < 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))\delta(T_1(i_1), T_2(j_1)). Later pairs may overwrite td[i1][j1]\mathit{td}[i_1][j_1] only with the same value, by Lemma 3 again. So Lemma 3 applies to every pair. The roots n1n_1 and n2n_2 are keyroots with l(nt)=1l(n_t) = 1, and the last pair (n1,n2)(n_1, n_2) writes td[n1][n2]=δ(T1,T2)\mathit{td}[n_1][n_2] = \delta(T_1, T_2) at line L12, which line L14 returns. ■\blacksquare

Back to Zhang-Shasha algorithm