Lowest Common Ancestors Two vertices u v Lowest

  • Slides: 6
Download presentation
Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example

Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example 1 2 8 3 4 5 6 8 lca (5, 6) = 4 lca (3, 7) = 2 lca (7, 8) = 1 7 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?

Range Minima Problem Let {s 1, s 2, s 3, s 4, s 5,

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 {ai. . . aj} in constant time? Approach: Prefix min Suffix min {ak. . al} 1. Find lca w of ak and al 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 3 rd in v 2 = 3 prefix min of 5 th in v 3 =7 min {a 3. . a 5} = 3 v 1 p=[5, 5, 3, 3, 3, 1, 1, 1] s=[1, 1, 1, 2, 2] p=[5, 5, 3, 3] s=[3, 3, 3, 4] v 2 v 4 p=[5, 5] s=[5, 10] 5 10 p=[3, 3] s=[3, 4] 3 v 3 p=[7, 1, 1, 1] s=[1, 1, 2, 2] v 6 v 5 4 v 7 p=[7, 1] s=[1, 1] 7 p=[8, 2] s=[2, 2] 1 8 2

Applications: Computing the minimum of its descendants 12 Example 4 7 2 9 6

Applications: Computing the minimum of its descendants 12 Example 4 7 2 9 6 3 1 For each vi, compute xi = minimum aj among all its descendants vj of vi Then xi = min {ak. . aj }, where k is preorder (vi) and j = k + descendants of (vi) -1 4 5

Complexity of Range Minima Two part: • Initial construction • Searching Construction: O (

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

Breadth First Traversal level 0 level 1 1 2 8 3 4 5 6

Breadth First Traversal level 0 level 1 1 2 8 3 4 5 6 9 7 BFS: 1, 2, 8, 9, 3, 4, 5, 6, 7 Eulerian Tour ET = (v 1, v 2), (v 2, v 3), (v 3, v 2), (v 2, v 4), (v 4, v 5), (v 5, v 4), (v 4, v 6), (v 6, v 4), (v 4, v 7), (v 7, v 4), (v 4, v 2), (v 2, v 1), (v 1, v 8), (v 8, v 1), (v 1, v 9), (v 9, v 1) Remove the left most edges, and right most edges from ET ET’ = (v 1, v 2), (v 2, v 3), (v 3, v 2), (v 2, v 4), (v 4, v 5), (v 5, v 4), (v 4, v 6), (v 6, v 4), (v 4, v 7), (v 7, v 4), (v 4, v 2), (v 2, v 1), (v 1, v 8), (v 8, v 1), (v 1, v 9), (v 9, v 1) How to detect? Prefix maxima, and suffix maxia of edge level If prefix max has ai - ai-1 > 0 then i-th edge is a leftmost edge Back Edge: “( “ Advance edge: “)” find mate of each parenthesis Mostly well defined If (vi, vj) is the right most edge at level k, then its mate is leftmost edge at level k+1 (if it exists) (vk, vl) is a mate of (vi, vj) then make a link vk -> vi (vi, vj) is the first edge, vi -> vj

Generalized Prefix Computation • Next class: read section 4. 9

Generalized Prefix Computation • Next class: read section 4. 9