Presentation is loading. Please wait.

Presentation is loading. Please wait.

The LCA Problem Revisited

Similar presentations


Presentation on theme: "The LCA Problem Revisited"— Presentation transcript:

1 The LCA Problem Revisited
Michael A.Bender & Martin Farach-Colton

2 Outline Definitions. Reduction from LCA to RMQ.
Trivial Solutions for RMQ Faster solution for sub-problem of RMQ. Solution for general RMQ.

3 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. u v

4 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[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 1 2 34 7 19 10 12 13 16 RMQA(3,7) = 4

5 Complexity Notation Preprocessing Time Query Time

6 LCA - Trivial Algorithm
For each pair of vertices, follow both paths toward the root until the first shared vertex is found. Complexity =

7 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.

8 Example 1 4 5 6 7 9 2 3 8 E: L: R:

9 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).

10 Example - LCAT(6,9) 1 4 5 6 7 9 2 3 8 E[10,…,16] E: L: R: RMQL(10,16) = 11 LCAT(6,9) = E[11]=5 R[6] R[9]

11 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 :

12 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 -

13 ST Algorithm for RMQ Preprocessing Time: O(nlogn). an a1 ... ... i
i+2j-1-1 i+2j-1 an a1 ... ...

14 ST Algorithm for RMQ Cont.
Arbitrary RMQ(i,j) Query Time: O(1). ST Algorithm Complexity: i j 2k elements an a1 ... ... 2k elements

15 RMQ Solution A’: block min value … ... B : block min index … ... ...
A’[i] A’[2n/logn] A’: block min value B[0] B[i] B[2n/logn] B : block min index ... ... ... ... ... A

16 RMQ Solution Cont. Size(A’) = 2n / logn. ST_Preprocessing(A’) ST(A’)=

17 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[i] A[j] ...

18 RMQ Solution Cont. ST Preprocessing of a block Per Block All Blocks
A[i] A[j] ...

19 Observation Let two arrays X & Y such that 3 4 5 6 5 4 5 6 5 4 1 2 3 2
1 2 3 2 1 2 3 2 1 +1 +1 +1 -1 -1 +1 +1 -1 -1

20  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 =

21 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.

22 Cartesian Tree A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 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.

23 Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 A[0] A[1] A[2] A[3] A[4]

24 Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 A[0] A[1] A[2] A[3]

25 Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 7 A[0] A[1] A[2] A[3]

26 Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 7 9 A[0] A[1] A[2]

27 Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 7 9 8 A[0] A[1] A[2]

28 Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 5 7 9 8 A[0] A[1] A[2]

29 Cartesian Tree 10 25 22 34 7 19 10 12 26 16 4 6 5 7 9 8 A[0] A[1] A[2]
6 5 7 9 8

30 Cartesian Tree A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 10 25 22 34 7 19 10 12 26 16 4 6 2 5 7 1 3 9 8

31 Linear Cartesian Tree Construction
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.

32 Linear Cartesian Tree Construction
10 25 22 34 7 19 10 12 26 16 10

33 Linear Cartesian Tree Construction
10 25 22 34 7 19 10 12 26 16 10 25 1

34 Linear Cartesian Tree Construction
10 25 22 34 7 19 10 12 26 16 10 22 2 25 1

35 Linear Cartesian Tree Construction
10 25 22 34 34 34 7 7 19 10 12 26 16 10 22 2 34 25 1 3 3

36 Linear Cartesian Tree Construction
10 25 22 34 7 19 10 12 26 16 7 4 10 22 2 34 25 1 3

37 Linear Cartesian Tree Construction
10 25 22 34 7 19 10 12 26 16 4 6 2 5 7 1 3 9 8

38 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)

39 Linear Cartesian Tree Construction
10 25 22 34 7 19 10 12 26 16 4 6 2 5 7 1 3 9 8


Download ppt "The LCA Problem Revisited"

Similar presentations


Ads by Google