1 / 39

The LCA Problem Revisited

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

Download Presentation

The LCA Problem Revisited

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

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

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

  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). 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: j i 2kelements an a1 ... ... 2kelements

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

  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[j] A[i] ... 1 2 3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related