390 likes | 561 Views
The LCA Problem Revisited. Michael A.Bender & Martin Farach-Colton. Outline. Definitions. Reduction from LCA to RMQ. Trivial Solutions for RMQ Faster solution for sub-problem of RMQ. Solution for general RMQ. LCA - Lowest Common Ancestor. LCA T ( u,v ).
E N D
The LCA Problem Revisited Michael A.Bender & Martin Farach-Colton
Outline • Definitions. • Reduction from LCA to RMQ. • Trivial Solutions for RMQ • Faster solution for sub-problem of RMQ. • Solution for general RMQ.
LCA - Lowest Common Ancestor LCAT(u,v) The LCA of nodes u and v in a tree T is the shared ancestor of u and v that is located farthest from the root. v u
RMQ - Range Minimum Query RMQA(i,j) For indices i and j between 1 and n, query RMQA(i,j) returns the index of the smallest element in the sub array A[i…j]. A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 0 1 2 34 7 19 10 12 13 16 RMQA(3,7) = 4
Complexity Notation Preprocessing Time Query Time
LCA - Trivial Algorithm • For each pair of vertices, follow both paths toward the root until the first shared vertex is found. Complexity =
Reduction From LCA to RMQ • If there is an -time solution for RMQ, then there is an -time solution for LCA. • The reduction - We build three array from the input tree T. 1. E[1,…,2n-1] stores the nodes visited in an Euler Tour of T. 2. L[1,…,2n-1], where L[i] is the level of E[i] in T. 3. R[1,…,n], where R[i] is the index of the first occurrence of the node i in the array E.
Example 0 1 4 5 6 7 9 2 3 8 E: 0 1 2 1 3 1 0 4 0 5 6 5 7 8 7 5 9 5 0 L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 R: 0 1 2 4 7 9 10 12 13 16
Computing LCAT(u,v) • The nodes in the Euler Tour between the first visits to u and to v are E[R[u],…,R[v]]. • The shallowest node is at index RMQL(R[u],R[v]). • The node E[RMQL(R[u],R[v])] is LCAT(u,v).
Example - LCAT(6,9) 0 1 4 5 6 7 9 2 3 8 E[10,…,16] E: 0 1 2 1 3 1 0 4 0 5 6 5 7 8 7 5 9 5 0 L: 0 1 2 1 2 1 0 1 0 1 2 1 2 3 2 1 2 1 0 R: 0 1 2 4 7 9 10 12 13 16 RMQL(10,16) = 11 LCAT(6,9) = E[11]=5 R[6] R[9]
Complexity Analysis Preprocessing • Array Construction : O(n). • Preprocessing of the array L : f(2n-1). Query • Three Array references : O(1). • RMQ query in L : g(2n-1). Overall :
Naive Solutions for RMQ • From now on, we will focus on the RMQ problem. • Computing the RMQ for every pair of indices - • Trivial dynamic programming -
ST Algorithm for RMQ Preprocessing • Time: O(nlogn). i i+2j-1-1 i+2j-1 an a1 ... ...
ST Algorithm for RMQ Cont. Arbitrary RMQ(i,j) Query • Time: O(1). • ST Algorithm Complexity: j i 2kelements an a1 ... ... 2kelements
RMQ Solution A’[2n/logn] A’[0] A’[i] A’: block min value … ... B[2n/logn] B[0] B[i] B : block min index … ... ... ... ... ... ... A
RMQ Solution Cont. • Size(A’) = 2n / logn. • ST_Preprocessing(A’) • ST(A’)=
RMQ Solution Cont. Arbitrary RMQ(i,j) Query • We should compute the following values: 1. The minimum from i to the end of its block. 2. The minimum of all the blocks in between i’s block and j’s block. 3. The minimum from the beginning of j’s block to j. A[j] A[i] ... 1 2 3
RMQ Solution Cont. ST Preprocessing of a block Per Block All Blocks A[j] A[i] ... 1 2 3
Observation Let two arrays X & Y such that A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] A[8] 3 4 5 6 5 4 5 6 5 4 A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 0 1 2 3 2 1 2 3 2 1 +1 +1 +1 -1 -1 +1 +1 -1 -1
1RMQ • Block size = • 1 sequence block size = • Number of possible sequences = • Preprocessing all possible 1 blocks: • Determining which table to use for each block: O(n). • Overall time complexity =
General RMQ Solution • Reduction from RMQ to LCA. Claim: • If there is an -time solution for LCA, then there is an -time solution for RMQ.
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 • Given an array, we can build a Cartesian tree. • The root of a Cartesian tree is the minimum element of the array. • The root is labeled with the position of this minimum.
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 7 10 25 22 34 19 10 12 26 16 4
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 6
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 6 7
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 6 7 9
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 6 7 9 8
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 6 5 7 9 8
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 0 6 5 7 9 8
Cartesian Tree A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 0 6 2 5 7 1 3 9 8
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 • Suppose Ci is the Cartesian tree of A[0,…,i]. • The i+1 node belongs to the rightmost path of Ci+1. • We climb up the rightmost path of Ci until finding the position where i+1 node belongs.
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 10 0
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 10 0 25 1
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 10 0 22 2 25 1
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 34 7 10 25 22 34 7 19 10 12 26 16 34 10 0 22 2 34 25 1 3 3
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 7 4 10 0 22 2 34 25 1 3
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 0 6 2 5 7 1 3 9 8
Reduction from RMQ to LCA • If there is an -time solution for LCA, then there is an -time solution for RMQ. • Let A be the input array to the RMQ problem. • Let C be the Cartesian tree of A. • RMQA(i,j) = LCAC(i,j)
Linear Cartesian Tree Construction A[8] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[9] 10 25 22 34 7 19 10 12 26 16 4 0 6 2 5 7 1 3 9 8