Fast Algorithms for Finding Nearest Common Ancestors I: A Lower Bound for Pointer MachinesResearch Paper
Motivation
The nearest common ancestor problem asks, for a rooted tree and two of its vertices and , for the deepest vertex that is an ancestor of both, written . It appears as a subroutine in string algorithms (suffix trees), in graph algorithms (path queries, dominators) and in the analysis of set-union structures. Aho, Hopcroft and Ullman (On finding lowest common ancestors in trees, SIAM J. Comput. 5, 1976) posed it in several versions, differing in how much the tree changes while the queries are answered.
Harel and Tarjan (Fast Algorithms for Finding Nearest Common Ancestors, SIAM J. Comput. 13, 1984) study how the answer depends on the machine model. On a random-access machine, where addresses can be computed arithmetically, they preprocess a static tree in linear time and then answer each query in constant time. On a pointer machine, where memory can only be traversed by following pointers, their §2 shows that no representation of the tree allows constant-time queries: steps are needed in the worst case. This mission formalizes that lower bound.
Timeline.
- 1976: Aho, Hopcroft and Ullman give an -per-query random-access algorithm for static trees.
- 1976: van Leeuwen (Finding lowest common ancestors in less than logarithmic time, unpublished report, reference [14] of Harel–Tarjan) gives an algorithm for static trees that runs on a pointer machine.
- 1984: Harel and Tarjan prove Theorem 1, the matching lower bound for pointer machines, and the -per-query random-access algorithm.
Setting
A pointer machine stores its data as a collection of nodes. Each node has a fixed number of fields, and a pointer field holds either a node or nil. The machine can follow a pointer from a node it holds, but it cannot compute an address. Following Harel and Tarjan (p. 340), a static tree is represented by a list structure: each tree vertex is represented by a single node , distinct vertices by distinct nodes, and the structure may contain further nodes that represent no vertex. Each node has two pointer fields; the paper reduces any fixed number of pointers to two "without loss of generality". To answer a query on and , the machine is given pointers to and and must return a pointer to .
The node is accessible from in steps or less if it can be reached from by following at most pointers. Write for the set of such nodes. A run of steps from input nodes and is a sequence in which each is the content of a pointer field of a node among . A query with answer is answered in steps if some run of at most steps holds .
The tree is the complete binary tree of height , with leaves. Its vertices are the words (the root-to-vertex path, = left), the ancestors of are its prefixes, the depth of is and its height is . Then is the longest common prefix of and . Logarithms are binary: .
Formalization targets
Goal: Theorem 1 in the explicit form of its proof
For every , every node type, every list structure with two pointers per node and every injective representation of the complete binary tree with leaves: if every nca query on two leaves is answered in steps, then
This is the last display of the proof (p. 341), which is what the paper's means. The representation is arbitrary and is quantified before the query bound, so the bound holds for every representation.
Milestones: the claims of the proof
- A query answered in steps reaches only nodes in .
- for every node .
- With the set of vertices whose nodes are accessible from in steps or less: for a nonleaf with children , either for every leaf below , or for every leaf below .
- A vertex of height lies in for at least leaves .
where is the set of leaves.
Significance
The result. Theorem 1 shows that van Leeuwen's pointer-machine algorithm for static trees is optimal up to a constant factor, and that the constant-time queries of the paper's §§3–5 depend on address arithmetic. It is an early nontrivial lower bound for pointer machines on a natural problem; the paper compares it with Tarjan's lower bound for disjoint-set union on a pointer machine (J. Comput. System Sci. 18, 1979).
Formalizing it. The theorem is proved in the paper; as far as could be determined no machine-checked version exists, and Mathlib has no pointer-machine model. The mission produces an explicit, reusable definition of pointer-machine runs and accessibility together with a complete proof of the explicit bound. A formal model of this kind is the precondition for stating any other pointer-machine lower bound.
Difficulty
The statement must hold for every representation, including structures with many auxiliary nodes and arbitrary pointers between tree nodes. Arguing about one natural representation, such as parent pointers, where a leaf is far from its ancestors, says nothing about other representations: a structure with shortcut pointers or auxiliary nodes may bring some ancestors close to some leaves, and the bound must survive every such choice. In the formal setting the counting also has to handle overlaps: nodes reachable from several leaves, nodes that represent no vertex, and pointer cycles.
Formalization scope
- Model. Nodes form an arbitrary type
N, not necessarily finite.ptr : N → Fin 2 → Option Ngives the two pointer fields (none= nil), andrep : Vertex h → Nis required to be injective.acc ptr j ais defined recursively.Run ptr a b t heldis an inductive predicate for runs of steps,AnsweredInasks for some run of at most steps holding the answer, andAnswersLeafQueriesIn ptr rep krequires this for every pair of leaves. - Conventions.
- Two pointer fields per node, as the paper's "without loss of generality" reduction allows; the reduction itself is not formalized.
- Only queries on two leaves are assumed answerable. This is weaker than all queries, so the theorem is at least as strong as the paper's.
- Time is counted as pointer-following steps. Mutation of the structure during a query and non-pointer fields are not modelled: neither lets the machine hold a node it has not reached by following pointers. The clause "the algorithm remembers nothing between queries" is built into the static structure.
Vertex his{s : List Bool // s.length ≤ h},ncais the longest common prefix, and a separate theorem identifies it with the Appendix's deepest common ancestor. counts leaves, not vertices.- is
Real.logb 2. For Lean's gives the true statement ; for , . - Cardinalities in the milestones are
Set.encardin , so finiteness is part of each claim. Divisions are cleared: .
- Ruling out trivial formalizations. The hypothesis
AnswersLeafQueriesInis satisfiable: the parent-pointer representation answers every leaf query in steps. Ifrepwere not injective, a constantrepwould answer every query in zero steps, so injectivity is kept in the goal. The milestones do not need it and do not assume it. - Infrastructure. The goal needs finite-set counting over the leaves of the complete binary tree and a double count over heights. The run and accessibility definitions are reusable for other pointer-machine arguments. Proofs of the milestones, and of the ℕ form that the goal reduces to, are welcome.
Selected references
- D. Harel, R. E. Tarjan, Fast Algorithms for Finding Nearest Common Ancestors, SIAM J. Comput. 13(2):338–355, 1984. https://doi.org/10.1137/0213024
- A. V. Aho, J. E. Hopcroft, J. D. Ullman, On finding lowest common ancestors in trees, SIAM J. Comput. 5(1):115–132, 1976. https://doi.org/10.1137/0205011
- A. Schönhage, Storage modification machines, SIAM J. Comput. 9(3):490–508, 1980. https://doi.org/10.1137/0209036
- R. E. Tarjan, A class of algorithms which require nonlinear time to maintain disjoint sets, J. Comput. System Sci. 18(2):110–127, 1979. https://doi.org/10.1016/0022-0000(79)90042-4