130 likes | 297 Views
The LCA Problem Revisited Michael A. Bender Martin Farach-Colton Latin American Theoretical Informatics Symposium , pages 88—94, 2000. Speaker: 吳展碩. Introduction. L owest C ommon A ncestor After a O ( n ) time preprocessing , any LCA query can then be answered in O (1) time. Outline.
E N D
The LCA Problem RevisitedMichael A. Bender Martin Farach-ColtonLatin American Theoretical Informatics Symposium, pages 88—94, 2000. Speaker: 吳展碩
Introduction • Lowest Common Ancestor • After a O(n) time preprocessing, any LCA query can then be answered in O(1) time.
Outline • Range Minimum Query (RMQ) Problem • Reduce LCA to 1RMQ • An Algorithm for 1RMQ
RMQ Problem Structure to Preprocess: An array A of numbers. Query: RMQ(i, j) returns the index of the smallest element in the subarray A[i...j]. RMQ(4, 8) = 5.
1RMQ Problem • A restricted case of RMQ such that all adjacent elements in the array A differ by +1 or -1. • Example:
1 2 2 3 3 3 3 3 4 4 Reduce LCA to 1RMQ • Compute the level array while visiting the tree in an Euler Tour. Level 1 Level 2 Level 3 Level 4
An O(nlogn) Preprocessing for RMQ • An O(n2) brute-force preprocessing: • precompute every query RMQ(i, j) • An O(nlogn) preprocessing: • only precompute the queries whose length is a power of 2 (there are nlogn such queries at most) • the preprocessing table can be computed in O(nlogn) by using dynamic programming. • RMQ(i, j) = min(RMQ(i, (j-i+1)/2), RMQ((j-i+1)/2+1, j))
Constant Time RMQ • RMQ(i, j) is the minima of the two blocks (one start at i and one end at j) covering the range [i, ...,j]. i j
An O(n) Preprocessing for 1RMQ • Partition A into blocks of size ½logn • Define an array A’[1, ..., n/½logn], where A’[k] is the minimum element in the kth block of A. The RMQ preprocessing of A’ spends O(n/½logn x log(n/½logn)) = O(n) time and space. n ½logn
How to compute RMQ(i, j)? • The answer is minimum of min(R1): The minimum from i forward to the end of its block. min(R2): The minimum of all the blocks in between i’s block and j’s block. min(R3): The minimum from the beginning of j’s block to j. • The preprocessing of A’ can be used to compute min(R2). • How to compute min(R1) and min(R3)? 1 2 3 ½logn
An O(n) Preprocessing for 1RMQ • Normalize each block of length ½logn to a binary string of length ½logn-1 Ex: 5676545 => 110001 • If two blocks normalize to the same binary string, the RMQ answers of these two blocks will be the same. • There are only O(n) kinds of normalized blocks. There are only 2(½logn)-1 = O(n) kinds of binary strings of length (½logn)-1. (Example) • The total preprocessing time for all normalized block is O(nlognloglogn) = O(n).
i • There are logn such queries started at position i at most.