Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lowest common ancestors. Write an Euler tour of the tree 3 4 5 1 6 7 2 4171356533123 1 2 3 4 6 9 10 85 7 1112 0 LCA(1,5) = 3 Shallowest node.

Similar presentations


Presentation on theme: "Lowest common ancestors. Write an Euler tour of the tree 3 4 5 1 6 7 2 4171356533123 1 2 3 4 6 9 10 85 7 1112 0 LCA(1,5) = 3 Shallowest node."— Presentation transcript:

1 Lowest common ancestors

2 Write an Euler tour of the tree 3 4 5 1 6 7 2 4171356533123 1 2 3 4 6 9 10 85 7 1112 0 LCA(1,5) = 3 Shallowest node

3 3 4 5 1 6 7 2 4171356533123 1 2 3 4 6 9 10 85 7 1112 0 2121012100110 minimum

4 Range minimum 3 4 5 1 6 7 2 4171356533123 1 2 3 4 6 9 10 85 7 1112 0 2121012100110 minimum Preprocess an array, such that given i,j you can find the minimum in [i,j] fast Reduction takes linear time

5 Trivial algorithms for RMQ

6 Less trivial algorithms to RMQ Try to use O(nlog(n)) space to do a query in O(1) time

7 ... …... B[0]B[2n/logn]B[i] …... A’[0]A’[2n/logn]A’[i] Rememeber the min in each block (A’) and its position (B) Optimal solution

8 10337263492 12 2522 021934567 8 101112131415 24435111927 n=16 A[] : 1025 227349 … = 8 1079 021 A’[] : … 035 021 B[] : … Example

9 Preprocess A’ for RMQ using the O(nlog(n)) space algorithm. Since the size of A’ is this would take Space, preprocessing time

10 i and j might be in the same block, we need some mechanism to answer inside blocks i < j on different blocks, answer the query as follows: 1.Compute minima from i to end of its block. 2.Compute minima of all blocks in between i’s and j’s blocks. 3.Compute minima from the beginning of j’s block to j. Return the index of the minimum of these 3 values. How do we answer queries ?

11 So what do we do inside blocks?

12 We need to solve a special case of RMQ 3 4 5 1 6 7 2 4171356533123 1 2 3 4 6 9 10 85 7 1112 0 2121012100110 restriction

13 0132 2 1 2 3 1 2 +1 +1 +1 Each subproblem can be described by the first entry and a vector of ±1 Two subproblems with the same vector of ±1 are equivalent 34 6 5 5 4 5 6 4 5

14 The bottom line There aren’t too many different subproblems, only For each such subproblem prepare all answers in advance 

15 Pick a subproblem: Find the solution using i,j:

16 ... …... B[0]B[2n/logn]B[i] …... A’[0]A’[2n/logn]A’[i] Summary Each block know its subproblem (A’’) …... A’’[0]A’’[2n/logn]A’’[i]


Download ppt "Lowest common ancestors. Write an Euler tour of the tree 3 4 5 1 6 7 2 4171356533123 1 2 3 4 6 9 10 85 7 1112 0 LCA(1,5) = 3 Shallowest node."

Similar presentations


Ads by Google