lowest common ancestors
DESCRIPTION
1. 2. 8. 8. 3. 4. 5. 6. 7. Lowest Common Ancestors. Two vertices (u, v) Lowest common ancestors, lca (u, v). Example. 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 - PowerPoint PPT PresentationTRANSCRIPT
Lowest Common Ancestors
Two vertices (u, v)Lowest common ancestors, lca (u, v)
Example
1
2 8 8
3 4
5 6 7
lca (5, 6) = 4lca (3, 7) = 2lca (7, 8) = 1
l(v): left most appearance of vr(v): right most appearance of vIf r(u) < l(v) thenlca (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 ProblemLet {s1,s2,s3,s4,s5,s6,s7,s8} = {5,10,3,4,7,1,8,2}
Given i, j, how to find min {ai...aj} in constant time?
Approach:Prefix minSuffix 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]
5 10 3 4 7 1 8 2
p=[5,5,3,3,3,1,1,1]s=[1,1,1,1,1,1,2,2]
min {ak..al} 1. Find lca w of ak and al
in a complete binary tree
Let x: left child, y: right child of w2. suffix min of k in x
3. prefix min of l in y 4. take min of 2 and 3
Example {a3..a5}w = v1
suffix min of 3rd in v2 = 3
prefix min of 5th in v3 =7
min {a3..a5} = 3
v1
v2 v3
v4 v5 v6 v7
Applications: Computing the minimum of its descendants
Example12
2 3
7 9
6 4
4
51For 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
Complexity of Range Minima
O ( log n ) timewith O ( n ) PEs
Merging P [ i.. j]P [ j+1.. k]
P’ [ i .. k] parentcopy
min (, p[j+1])Each level of three O (1)total time O (log n) timefor constructing the P, S lists
Two part: • Initial construction• Searching
Construction:
Searching:Constant time
Breadth First Traversal
BFS: 1, 2, 8, 9, 3, 4, 5, 6, 7
1
2 8 9
3 4
5 6 7
level 0
level 1
Remove the left most edges, and right most edges from ETET’ = (v1,v2), (v2,v3),(v1,v2), (v2,v3), (v3,v2), (v2,v4), ), (v4,v5),(v4,v5), (v5,v4), (v4,v6), (v6,v4), (v4,v7), (v7,v4),v7,v4), (v4,v2),(v4,v2), (v2,v1), (v1,v8), (v8,v1), (v1,v9), (v9,v1)(v9,v1)How to detect?
Prefix maxima, and suffix maxia of edge levelIf 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 definedIf (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
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)
Generalized Prefix Computation
• Next class: read section 4.9