Fast Algorithms for Finding Nearest Common Ancestors

We consider the following problem: Given a collection of rooted trees, answer on-line queries of the form, “What is the nearest common ancester of vertices x and y?” We show that any pointer machine that solves this problem requires $\Omega (\log \log n)$ time per query in the worst case, where n is the total number of vertices in the trees. On the other hand, we present an algorithm for a random access machine with uniform cost measure (and a bound of $\Omega (\log n)$ on the number of bits per word) that requires $O(1)$ time per query and $O(n)$ preprocessing time, assuming that the collection of trees is static. For a version of the problem in which the trees can change between queries, we obtain an almost-linear-time (and linear-space) algorithm.

  • [1]  Alfred V. Aho, John E. Hopcroft and , Jeffrey D. Ullman, The design and analysis of computer algorithms, Addison-Wesley Publishing Co., Reading, Mass.-London-Amsterdam, 1975x+470 54:1706 0326.68005 Google Scholar

  • [2]  A. V. Aho, J. E. Hopcroft and , J. D. Ullman, On finding lowest common ancestors in trees, SIAM J. Comput., 5 (1976), 115–132 10.1137/0205011 53:2007 0325.68018 LinkGoogle Scholar

  • [3]  Michael L. Fredman, Two applications of a probabilistic search technique: sorting X+Y and building balanced search trees, Seventh Annual ACM Symposium on Theory of Computing (Albuquerque, N. M., 1975), Assoc. Comput. Mach., New York, 1975, 240–244 55:4812 0391.68035 Google Scholar

  • [4]  Harold N. Gabow and , Robert Endre Tarjan, A linear-time algorithm for a special case of disjoint set union, J. Comput. System Sci., 30 (1985), 209–221 10.1016/0022-0000(85)90014-5 87f:68031 0572.68058 CrossrefISIGoogle Scholar

  • [5]  D. Harel, A linear time time algorithm for the lowest common ancestors problem, Proc. 21st IEEE Symposium on Foundations of Computer Science, 1980, 308–319 Google Scholar

  • [6]  D. E. Knuth, The Art of Computer Programming, Volume I: Fundamental Algorithms, Addison-Wesley, Reading, MA, 1973 Google Scholar

  • [7]  David Maier, An efficient method for storing ancestor information in trees, SIAM J. Comput., 8 (1979), 599–618 10.1137/0208048 81k:68034 0421.68089 LinkISIGoogle Scholar

  • [8]  A. Schönhage, Storage modification machines, SIAM J. Comput., 9 (1980), 490–508 10.1137/0209036 82b:68040 0454.68034 LinkISIGoogle Scholar

  • [9]  D. Sleator and , Robert Endre Tarjan, A data structure for dynamic trees, J. Comput. System Sci., 26 (1983), 362–391 10.1016/0022-0000(83)90006-5 85c:68007 0509.68058 CrossrefISIGoogle Scholar

  • [10]  Robert Endre Tarjan, Efficiency of a good but not linear set union algorithm, J. Assoc. Comput. Mach., 22 (1975), 215–225 10.1145/321879.321884 56:17194 0307.68029 CrossrefISIGoogle Scholar

  • [11]  Robert Endre Tarjan, Applications of path compression on balanced trees, J. Assoc. Comput. Mach., 26 (1979), 690–715 10.1145/322154.322161 81c:68052 0413.68063 CrossrefISIGoogle Scholar

  • [12]  Robert Endre Tarjan, A class of algorithms which require nonlinear time to maintain disjoint sets, J. Comput. System Sci., 18 (1979), 110–127 10.1016/0022-0000(79)90042-4 80e:68079 0413.68039 CrossrefISIGoogle Scholar

  • [13]  Robert E. Tarjan and , Jan van Leeuwen, Worst-case analysis of set union algorithms, J. Assoc. Comput. Mach., 31 (1984), 245–281 10.1145/62.2160 819 138 0632.68043 CrossrefISIGoogle Scholar

  • [14]  J. van Leeuwen, Finding lowest common ancestors in less than logarithmic time, 1976, unpublished report Google Scholar