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 of or , is the subtree of , rooted at : and all its descendants. Write when there is a bijection from the vertices of onto those of that maps to and edges to edges (a rooted isomorphism). Let 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 , if and only if there is a bijection from the children of to the children of with for each child .
Proof. If is a rooted isomorphism, then , and maps the neighbors of in , which are its children, bijectively onto the children of . For a child , maps , the part of reached from without passing , onto the part of reached from without passing , which is . So restricted to the children works. Conversely, given and rooted isomorphisms , the map that sends to and agrees with on each is a bijection, because the subtrees of the children partition and . It maps the edges to and the edges inside each by ; these are all the edges.
Lemma 2 (invariant). After line L7 has run for level , for all vertices of level (each in or in ): .
Proof. By induction on .
Base, . All vertices of level 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 , and let be at level . The children of and are at level . By line L7, if and only if , 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 if and only if the multisets of children's codes are equal, that is, if and only if there is a bijection between the children with for each . By the induction hypothesis, this is a bijection with . By Lemma 1, such a exists if and only if .
Theorem. AHU-ISOMORPHIC terminates and returns true if and only if by a map with .
Proof. Termination: the loop of line L4 runs times, and each step works on finite lists.
If the trees are isomorphic. Let 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 , maps bijectively onto , and its restriction to is a rooted isomorphism onto . By Lemma 2, . So the multisets of codes of and 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 in and only in , so . By Lemma 2, , and , .
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.
Free trees. Every isomorphism of free trees maps the set of centers onto the set of centers, because it keeps eccentricities. If is a center of , then is a rooted isomorphism from to , and is a center of . So trying rooted at one center against rooted at each center of finds an isomorphism when one exists. Conversely, a rooted isomorphism is in particular an isomorphism of the free trees.