Presentation on theme: "The LCA Problem Revisited Michael A. BenderMartin Farach-Colton Latin American Theoretical Informatics Symposium, pages 8894, 2000. Speaker:"— Presentation transcript:
The LCA Problem Revisited Michael A. BenderMartin Farach-Colton Latin American Theoretical Informatics Symposium, pages 8894, Speaker:
Lowest Common Ancestor After a O(n) time preprocessing, any LCA query can then be answered in O(1) time. Introduction
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:
Level 1 Level 2 Level 3 Level 4 Reduce LCA to 1RMQ Compute the level array while visiting the tree in an Euler Tour
An O(nlogn) Preprocessing for RMQ An O(n 2 ) 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)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 ij RMQ(i, j) is the minima of the two blocks (one start at i and one end at j) covering the range [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 is block and js block. min(R3): The minimum from the beginning of js block to j. The preprocessing of A can be used to compute min(R2). How to compute min(R1) and min(R3)? ½logn 123
An O(n) Preprocessing for 1RMQ Normalize each block of length ½logn to a binary string of length ½logn-1 Ex: => 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)Example The total preprocessing time for all normalized block is O( nlognloglogn) = O(n).
There are logn such queries started at position i at most. i