Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example 1 288 34 567 lca (5, 6) = 4 lca (3, 7) = 2 lca (7, 8) = 1 l(v):

Similar presentations


Presentation on theme: "Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example 1 288 34 567 lca (5, 6) = 4 lca (3, 7) = 2 lca (7, 8) = 1 l(v):"— Presentation transcript:

1 Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example 1 288 34 567 lca (5, 6) = 4 lca (3, 7) = 2 lca (7, 8) = 1 l(v): left most appearance of v r(v): right most appearance of v If r(u) < l(v) then lca (u,v) is the vertex with minimum level over the interval [r(u), l(v)] How to find [r(u), l(v)] efficiently?

2 Range Minima Problem Let {s 1,s 2,s 3,s 4,s 5,s 6,s 7,s 8 } = {5,10,3,4,7,1,8,2} Given i, j, how to find min {a i...a j } in constant time? Approach: Prefix min Suffix min p=[5,5] s=[5,10] p=[7,1] s=[1,1] p=[5,5,3,3] s=[3,3,3,4] p=[7,1,1,1] s=[1,1,2,2] p=[3,3] s=[3,4] p=[8,2] s=[2,2] 510347182 p=[5,5,3,3,3,1,1,1] s=[1,1,1,1,1,1,2,2] min {a k..a l } 1. Find lca w of a k and a l in a complete binary tree Let x: left child, y: right child of w 2. suffix min of k in x 3. prefix min of l in y 4. take min of 2 and 3 Example {a 3..a 5 } w = v 1 suffix min of 3rd in v 2 = 3 prefix min of 5th in v 3 =7 min {a 3..a 5 } = 3 v1 v2 v3 v4 v5 v6v7

3 Applications: Computing the minimum of its descendants Example 12 23 79 64 4 51 For each v i, compute x i = minimum a j among all its descendants v j of v i Then x i = min {a k.. a j }, where k is preorder (v i ) and j = k + descendants of (v i ) -1

4 Complexity of Range Minima O ( log n ) time with O ( n ) PEs MergingP [ i.. j] P [ j+1.. k] P’ [ i.. k] parent copy  min ( , p[j+1]) Each level of three O (1) total time O (log n) time for constructing the P, S lists Two part: Initial construction Searching Construction: Searching: Constant time

5 Breadth First Traversal BFS: 1, 2, 8, 9, 3, 4, 5, 6, 7 1 289 34 567 level 0 level 1 Eulerian Tour ET = (v1,v2), (v2,v3), (v3,v2), (v2,v4), (v4,v5), (v5,v4), (v4,v6 ), (v6,v4), (v4,v7), (v7,v4), (v4,v2), (v2,v1), (v1,v8), (v8,v1), (v1,v9), (v9,v1)

6 Generalized Prefix Computation Next class: read section 4.9


Download ppt "Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example 1 288 34 567 lca (5, 6) = 4 lca (3, 7) = 2 lca (7, 8) = 1 l(v):"

Similar presentations


Ads by Google