#include "muscle.h" #include "tree.h" #define TRACE 0 /*** Algorithm to compare two trees, X and Y. A node x in X and node y in Y are defined to be similar iff the set of leaves in the subtree under x is identical to the set of leaves under y. A node is defined to be dissimilar iff it is not similar to any node in the other tree. Nodes x and y are defined to be married iff every node in the subtree under x is similar to a node in the subtree under y. Married nodes are considered to be equal. The subtrees under two married nodes can at most differ by exchanges of left and right branches, which we do not consider to be significant here. A node is defined to be a bachelor iff it is not married. If a node is a bachelor, then it has a dissimilar node in its subtree, and it follows immediately from the definition of marriage that its parent is also a bachelor. Hence all nodes on the path from a bachelor node to the root are bachelors. We assume the trees have the same set of leaves, so every leaf is trivially both similar and married to the same leaf in the opposite tree. Bachelor nodes are therefore always internal (i.e., non-leaf) nodes. A node is defined to be a diff iff (a) it is married and (b) its parent is a bachelor. The subtree under a diff is maximally similar to the other tree. (In other words, you cannot extend the subtree without adding a bachelor). The set of diffs is the subset of the two trees that we consider to be identical. Example: -----A -----k ----j -----B --i -----C ------D -----A -----p ----n -----B --m -----D ------C The following pairs of internal nodes are similar. Nodes Set of leaves ----- ------------- k,p A,B i,m A,B,C,D Bachelors in the first tree are i and j, bachelors in the second tree are m and n. Node k and p are married, but i and m are not (because j and n are bachelors). The diffs are C, D and k. The set of bachelor nodes can be viewed as the internal nodes of a tree, the leaves of which are diffs. (To see that there can't be disjoint subtrees, note that the path from a diff to a root is all bachelor nodes, so there is always a path between two diffs that goes through the root). We call this tree the "diffs tree". There is a simple O(N) algorithm to build the diffs tree. To achieve O(N) we avoid traversing a given subtree multiple times and also avoid comparing lists of leaves. We visit nodes in depth-first order (i.e., a node is visited before its parent). If either child of a node is a bachelor, we flag it as a bachelor. If both children of the node we are visiting are married, we check whether the spouses of those children have the same parent in the other tree. If the parents are different, the current node is a bachelor. If they have the same parent, then the node we are visiting is the spouse of that parent. We assign this newly identified married couple a unique integer id. The id of a node is in one-to-one correspondence with the set of leaves in its subtree. Two nodes have the same set of leaves iff they have the same id. Bachelor nodes do not get an id. ***/ static void BuildDiffs(const Tree &tree, unsigned uTreeNodeIndex, const bool bIsDiff[], Tree &Diffs, unsigned uDiffsNodeIndex, unsigned IdToDiffsLeafNodeIndex[]) { #if TRACE Log("BuildDiffs(TreeNode=%u IsDiff=%d IsLeaf=%d)\n", uTreeNodeIndex, bIsDiff[uTreeNodeIndex], tree.IsLeaf(uTreeNodeIndex)); #endif if (bIsDiff[uTreeNodeIndex]) { unsigned uLeafCount = tree.GetLeafCount(); unsigned *Leaves = new unsigned[uLeafCount]; GetLeaves(tree, uTreeNodeIndex, Leaves, &uLeafCount); for (unsigned n = 0; n < uLeafCount; ++n) { const unsigned uLeafNodeIndex = Leaves[n]; const unsigned uId = tree.GetLeafId(uLeafNodeIndex); if (uId >= tree.GetLeafCount()) Quit("BuildDiffs, id out of range"); IdToDiffsLeafNodeIndex[uId] = uDiffsNodeIndex; #if TRACE Log(" Leaf id=%u DiffsNode=%u\n", uId, uDiffsNodeIndex); #endif } delete[] Leaves; return; } if (tree.IsLeaf(uTreeNodeIndex)) Quit("BuildDiffs: should never reach leaf"); const unsigned uTreeLeft = tree.GetLeft(uTreeNodeIndex); const unsigned uTreeRight = tree.GetRight(uTreeNodeIndex); const unsigned uDiffsLeft = Diffs.AppendBranch(uDiffsNodeIndex); const unsigned uDiffsRight = uDiffsLeft + 1; BuildDiffs(tree, uTreeLeft, bIsDiff, Diffs, uDiffsLeft, IdToDiffsLeafNodeIndex); BuildDiffs(tree, uTreeRight, bIsDiff, Diffs, uDiffsRight, IdToDiffsLeafNodeIndex); } void DiffTrees(const Tree &Tree1, const Tree &Tree2, Tree &Diffs, unsigned IdToDiffsLeafNodeIndex[]) { #if TRACE Log("Tree1:\n"); Tree1.LogMe(); Log("\n"); Log("Tree2:\n"); Tree2.LogMe(); #endif if (!Tree1.IsRooted() || !Tree2.IsRooted()) Quit("DiffTrees: requires rooted trees"); const unsigned uNodeCount = Tree1.GetNodeCount(); const unsigned uNodeCount2 = Tree2.GetNodeCount(); const unsigned uLeafCount = Tree1.GetLeafCount(); const unsigned uLeafCount2 = Tree2.GetLeafCount(); assert(uLeafCount == uLeafCount2); if (uNodeCount != uNodeCount2) Quit("DiffTrees: different node counts"); // Allocate tables so we can convert tree node index to // and from the unique id with a O(1) lookup. unsigned *NodeIndexToId1 = new unsigned[uNodeCount]; unsigned *IdToNodeIndex2 = new unsigned[uNodeCount]; bool *bIsBachelor1 = new bool[uNodeCount]; bool *bIsDiff1 = new bool[uNodeCount]; for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex) { NodeIndexToId1[uNodeIndex] = uNodeCount; bIsBachelor1[uNodeIndex] = false; bIsDiff1[uNodeIndex] = false; // Use uNodeCount as value meaning "not set". IdToNodeIndex2[uNodeIndex] = uNodeCount; } // Initialize node index <-> id lookup tables for (unsigned uNodeIndex = 0; uNodeIndex < uNodeCount; ++uNodeIndex) { if (Tree1.IsLeaf(uNodeIndex)) { const unsigned uId = Tree1.GetLeafId(uNodeIndex); if (uId >= uNodeCount) Quit("Diff trees requires existing leaf ids in range 0 .. (N-1)"); NodeIndexToId1[uNodeIndex] = uId; } if (Tree2.IsLeaf(uNodeIndex)) { const unsigned uId = Tree2.GetLeafId(uNodeIndex); if (uId >= uNodeCount) Quit("Diff trees requires existing leaf ids in range 0 .. (N-1)"); IdToNodeIndex2[uId] = uNodeIndex; } } // Validity check. This verifies that the ids // pre-assigned to the leaves in Tree1 are unique // (note that the id