Algorithm Search⌘K

Proofs: Aho-Hopcroft-Ullman tree isomorphism algorithm

These are the human proofs of Aho-Hopcroft-Ullman tree isomorphism algorithm, at full length.

AHU-ISOMORPHIC decides rooted tree isomorphism full

This proof shows: correctness, termination.

Notation. For a vertex vv of T1T_1 or T2T_2, T(v)T(v) is the subtree of vv, rooted at vv: vv and all its descendants. Write T(u)≅T(v)T(u) \cong T(v) when there is a bijection from the vertices of T(u)T(u) onto those of T(v)T(v) that maps uu to vv and edges to edges (a rooted isomorphism). Let h=h(T1)=h(T2)h = h(T_1) = h(T_2) after line L3. A rooted isomorphism maps the root to the root and keeps distances to the root, so it maps each level onto the level of the same number.

Lemma 1 (children). For vertices u,vu, v, T(u)≅T(v)T(u) \cong T(v) if and only if there is a bijection β\beta from the children of uu to the children of vv with T(c)≅T(β(c))T(c) \cong T(\beta(c)) for each child cc.

Proof. If φ:T(u)→T(v)\varphi : T(u) \to T(v) is a rooted isomorphism, then φ(u)=v\varphi(u) = v, and φ\varphi maps the neighbors of uu in T(u)T(u), which are its children, bijectively onto the children of vv. For a child cc, φ\varphi maps T(c)T(c), the part of T(u)T(u) reached from cc without passing uu, onto the part of T(v)T(v) reached from φ(c)\varphi(c) without passing vv, which is T(φ(c))T(\varphi(c)). So β=φ\beta = \varphi restricted to the children works. Conversely, given β\beta and rooted isomorphisms φc:T(c)→T(β(c))\varphi_c : T(c) \to T(\beta(c)), the map that sends uu to vv and agrees with φc\varphi_c on each T(c)T(c) is a bijection, because the subtrees of the children partition T(u)∖{u}T(u) \setminus \{u\} and T(v)∖{v}T(v) \setminus \{v\}. It maps the edges {u,c}\{u, c\} to {v,β(c)}\{v, \beta(c)\} and the edges inside each T(c)T(c) by φc\varphi_c; these are all the edges. □\square

Lemma 2 (invariant). After line L7 has run for level ii, for all vertices u,vu, v of level ii (each in T1T_1 or in T2T_2): code(u)=code(v)  ⟺  T(u)≅T(v)\mathrm{code}(u) = \mathrm{code}(v) \iff T(u) \cong T(v).

Proof. By induction on h−ih - i.

Base, i=hi = h. All vertices of level hh are leaves, so all keys are the empty list and all codes are 0. Every subtree is a single vertex, and any two single vertices are isomorphic.

Step. Assume the claim for level i+1i + 1, and let u,vu, v be at level ii. The children of uu and vv are at level i+1i + 1. By line L7, code(u)=code(v)\mathrm{code}(u) = \mathrm{code}(v) if and only if key(u)=key(v)\mathrm{key}(u) = \mathrm{key}(v), because the rank in a list without copies is injective on its entries. The key is the sorted list of the children's codes, so key(u)=key(v)\mathrm{key}(u) = \mathrm{key}(v) if and only if the multisets of children's codes are equal, that is, if and only if there is a bijection β\beta between the children with code(c)=code(β(c))\mathrm{code}(c) = \mathrm{code}(\beta(c)) for each cc. By the induction hypothesis, this is a bijection with T(c)≅T(β(c))T(c) \cong T(\beta(c)). By Lemma 1, such a β\beta exists if and only if T(u)≅T(v)T(u) \cong T(v). □\square

Theorem. AHU-ISOMORPHIC(T1,r1,T2,r2)(T_1, r_1, T_2, r_2) terminates and returns true if and only if T1≅T2T_1 \cong T_2 by a map with r1↦r2r_1 \mapsto r_2.

Proof. Termination: the loop of line L4 runs h+1h + 1 times, and each step works on finite lists.

If the trees are isomorphic. Let φ\varphi be a rooted isomorphism. It is a bijection, so line L1 passes. It keeps levels, so both trees have the same height and line L3 passes. For each level ii, φ\varphi maps Li(T1)L_i(T_1) bijectively onto Li(T2)L_i(T_2), and its restriction to T(v)T(v) is a rooted isomorphism onto T(φ(v))T(\varphi(v)). By Lemma 2, code(v)=code(φ(v))\mathrm{code}(v) = \mathrm{code}(\varphi(v)). So the multisets of codes of Li(T1)L_i(T_1) and Li(T2)L_i(T_2) are equal, line L8 never returns false, and line L9 returns true.

If the algorithm returns true. Then line L8 passed at level 0. Level 0 holds only r1r_1 in T1T_1 and only r2r_2 in T2T_2, so code(r1)=code(r2)\mathrm{code}(r_1) = \mathrm{code}(r_2). By Lemma 2, T(r1)≅T(r2)T(r_1) \cong T(r_2), and T(r1)=T1T(r_1) = T_1, T(r2)=T2T(r_2) = T_2.

So a return of false at lines L1, L3 or L8 happens only when no rooted isomorphism exists, and a return of true happens only when one exists. ■\blacksquare

Free trees. Every isomorphism ψ:T1→T2\psi : T_1 \to T_2 of free trees maps the set of centers onto the set of centers, because it keeps eccentricities. If cc is a center of T1T_1, then ψ\psi is a rooted isomorphism from (T1,c)(T_1, c) to (T2,ψ(c))(T_2, \psi(c)), and ψ(c)\psi(c) is a center of T2T_2. So trying T1T_1 rooted at one center against T2T_2 rooted at each center of T2T_2 finds an isomorphism when one exists. Conversely, a rooted isomorphism is in particular an isomorphism of the free trees.

Back to Aho-Hopcroft-Ullman tree isomorphism algorithm