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, 2000. 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]. 123456789101112 931524886723 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: 1232321232323434321
Level 1 Level 2 Level 3 Level 4 Reduce LCA to 1RMQ 1 2 33 2 333 44 Compute the level array while visiting the tree in an Euler Tour. 12345678910111213141516171819 1232321232323434321
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)) 1232321232323434321 1232321232323434321
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: 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)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