Download presentation
Presentation is loading. Please wait.
Published byEleanor Reed Modified over 9 years ago
1
1
2
2
3
3 Segment Trees - motivation. When one wants to inspect a small portion of a large and complex objects. Example: –GIS: windowing query in a map - given a detailed map contains enormous amount of data, the system has to determine efficiently the part of the map corresponding to a specified region (window).
4
4 Semi dynamic segment trees. Let I = { [x 1, x 1 ’ ], [x 2, x 2 ’ ], , [x n, x n ’ ] } be a set of n intervals. Let p 1, p 2, , p m be the list of distinct intervals endpoints, sorted from left to right. The elementary intervals are defined to be : (- , p 1 ), [p 1, p 1 ], (p 1, p 2 ), [p 2, p 2 ], (p m, ) p1p1 p2p2 pmpm
5
5 Constructing a segment tree A balanced binary tree T. The leaves of T correspond to the elementary intervals (ordered from left to right). The elementary interval in a leaf is denoted Int( ) The internal nodes of T correspond to the intervals that are the union of elementary intervals: Int(v) is the union of intervals of its two children.
6
6 Constructing a segment tree- cont. Each node or leaf v in T stores the interval Int(v) and a canonical set I(v) I of intervals. This set contains the intervals [x, x’] I s.t Int(v) [x, x’] and Inv(Parent(v)) [x, x’]. Lemma: A segment tree on a set of n intervals uses O(nlogn) storage.
7
7 Segment tree- example s1s1 s2s2 s5s5 s3s3 s4s4 S5S5 S 2, S 5 S1S1 S1S1 S1S1 S3S3 S4S4 S3S3 S4, S3S4, S3 p1p1 p2p2 p3p3 p4p4 p5p5 p6p6 p7p7
8
8 Lemma - proof T is a balances binary tree its height is O(logn). Claim: any interval [x, x’] is stored in the set I(v) for at most two nodes at the same depth of T. proof: Let v1, v2, v3 be three nodes from left to right in same depth. Suppose [x, x’] is at v1 and v3. [x, x’] spans the interval from left point of Int(v1) to right point of Int(v3), v2 lies between v1, v3 Int(parent(v2)) [x, x’].
9
9 Lemma - proof. Continue Any interval is stored at most twice at a given depth of T. The total amount of storage is O(nlogn). v1v1 v2v2 v3v3
10
10 Algorithm: Query Segment Tree(V,q x ) Input: the root of a segment tree and a query point q x. Output: All intervals in the tree containing q x. Report all intervals in I(v). If v is not a leaf –then if q x Int(lc(v)) –then QuerySegmentTree(lc(v), qx) –else QuerySegmentTree(rc(v), qx)
11
11 Algorithm: Query Segment Tree(V,q x ) - Complexity. Visit O(logn) nodes in total. At each node v spend O(1+k v ) time. The intervals containing a query point q x are reported in O(logn+k) time, where k is the number of reported intervals.
12
12 Algorithm: Constructing a segment tree. Sort the endpoints of the intervals and get the elementary intervals. Construct a balanced binary tree on the elementary intervals and determine Int(v) for each v. (bottom-up manner). Determine the canonical subsets : insert the intervals one by one to T.
13
13 Algorithm: Insert Segment Tree(V,[x,x’]) Input: the root of a segment tree and an interval. Output: The interval will be stored in tree. If Inv(v) [x, x’] then store [x, x’] at v. else if Int(lc(v)) [x, x’] then InsertSegmentTree(lc(v), [x, x’]) if Inv(rc(v)) [x, x’] then InsertSegmentTree(rc(v), [x, x’])
14
14 Algorithm: Insert Segment Tree(V,[x,x’]) - Complexity. An interval is stored at most twice at each level. There is at most one node at every level whose interval contains x (the same for x’). Hence, We visit at most 4 nodes per level. The insertion time is O(logn). Construction time is O(nlogn).
15
15
16
16 Introduction Generalization of union-find problem: copy operation is also supported. The structure: A bipartite graph with two node sets V 1, V 2 and edges between them. V 1 form the sets and V 2 form the elements. An edge between v 1 V 1 and v 2 V 2 indicates that v 2 is a member element of the set v 1.
17
17 Notations Let = {S 1, S m } be a collection of sets. = {x 1, ,x n } be a collection of elements. The sets in are subsets of . Let x be the collection of sets that contain an element x.
18
18 The structure Consists four ingredients: Set nodes - . Element nodes - . Normal nodes - connect sets to elements. Reversed nodes - connect elements to sets. Whenever there is a path from a set node to an element node, the corresponding element is in the corresponding set.
19
19 The structure - continue. The following invariant are maintained: A set node has one outgoing edge. An element node has one incoming edge. A normal node has one incoming edge, and at least two outgoing edges. A reversed node has one outgoing edge and at least two incoming edges. No edge may go from a reversed node to another reversed node.
20
20 The structure - continue. No edge may go from a normal node to another normal node. There is one unique path from a set node to an element node iff the element is in the set. Any path in the union-copy structure from a set node to an element node consists of an alternating sequence of normal and reversed nodes. The union copy structure is acyclic.
21
21 The structure - example S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x3x3 x4x4 x5x5 x6x6 x7x7 x8x8 set normal reversed element
22
22 Defining the operations. Set-create(S) - create a new empty set in with name S. Set-insert(S i,x j ) - (defined when x j S i ). make S i S i {x j }. Set-destroy(S i ) - remove S i from . Set-find(S i ) - report all elements in S i. Set-union(S i,S j ) - (defined when S i, S j disjoint). S i S i S j and S j . Set-copy(S i,S j ) - (defined when S j is empty). S j S i. Element-create(x) - create an element x in and x .
23
23 Defining the operations. Continue Element-insert(x i, S j ) - (define when x i S j ) xi xi {S j }. Element-destroy(x i ) - remove x i from and from all its sets. Element-find(x i ) - report all sets that contain x i. Element-union(x i,x j ) - (defined when no set contains them both). S xj, S S {x i }\{x j }, hence xi becomes xi xj and xj . Element-copy(x i,x j ) - (defined when x j in no set). Puts x j in all sets containing x i.
24
24 Set-insert(S 4,x 2 ) S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x3x3 x4x4 x5x5 x6x6 x7x7 x8x8
25
25 S3S3 Set-destroy(S 3 ) S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x3x3 x4x4 x5x5 x6x6 x7x7 x8x8
26
26 Set-union(S 4,S 1 ) S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x3x3 x4x4 x5x5 x6x6 x7x7 x8x8
27
27 Set-copy(S 5,S 6 ) S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x3x3 x4x4 x5x5 x6x6 x7x7 x8x8 S6S6
28
28 The structure - notations. N-UF is a union-find structure represents the normal nodes. Sets in N-UF normal nodes. Elements in N-UF outgoing edges. R-UF is a union-find structure represents the reversed nodes. Sets in N-UF reversed nodes. Elements in R-UF incoming edges.
29
29 Operations N-UF structure supports N-union(v,w) - Unite the edges of the v and w to become the edges of v. w looses its edges. N-find(e i ) - Return the normal node of e i. N-add(v,e i ) - Add edge e i to node v. N-delete(e i ) - Delete e i from its origin. N-enumerate(v) - Return all outgoing edges of v.
30
30 The operations. Set-find(S i ) if out(S i ) nil then Traverse(out(S i ) ) Traverse(e) case type(dest(e)) of –element: report the element as an answer. –normal: for all e’ N-enumerate(dest(e)) do Traverse(e’ ) –reversed: Traverse(out(R-find(e)))
31
31 Set-union(S i, S j ) if out(Si) nil or type(child(S i )) normal then exchange S i and S j. if out(Sj) nil then ready. else if type(child(S i )) normal and type(child(S j )) normal then N-union(child(S i ), child(S j )) else if type(child(S i )) normal then N-add(child(S i ), out(S j )) else // both are reversed or elements. make a normal node v. N-add(v, out(S i ) ) ; N-add(v, out(S j ) ) child(S i ) v ; out(S j ) nil
32
32 Set-union - examples. SiSi SjSj SiSi SjSj SiSi SjSj SiSi SjSj SiSi SiSi SjSj SjSj v
33
33 Set-copy(S i, S j ) if out(S i ) nil then ready. else if type(child(S i ))=reversed then R-add(R-find(out(S i )), out(S j )) else // type(child(S i ))=normal or element. make a reversed node v. child(v) child(S i ) R-add(v, out(S i )) R-add(v, out(S j ))
34
34 Set-copy - examples. SiSi SiSi SjSj SiSi SiSi SjSj v
35
35 Set-insert(S i, x j ) Make a new edge e if out(S i ) nil then out(S i ) e else if type(child(S i )) normal then N-add(child((S i ), e) else // type(child(S i )) is reversed or element. Make a normal node v N-add(v, out(S i )) ; N-add(v, e) child(S i ) = v // now e has an origin. if in(x j ) nil then in(x j ) e else if type(parent(x j )) reversed then R-add(parent(x j ), e)
36
36 Set-insert(S i, x j ) - continue else // type(parent(x j )) is normal or set. Make a reversed node w. R-add(w, in(x j ) ); R-add(w, e) parent(xj) w // now e has a destination. SiSi SiSi xjxj e v w xjxj
37
37 Set-destroy(S i ) if out(S i ) nil then case type(child(S i )) of element: in(child(Si)) nil reversed : Restore(out(S i ) ) normal: for all e N-enumerate(child(S i )) do if type(dest(e)) element then in(dest(e)) nil else //type(dest(e)) is reversed. Restore(e) remove S i
38
38 Set-destroy(S i ) - Restore(e) v R-find(e) R-delete(e) from v if v has only one incoming edge e’ then if type(org(e’)) set or type(child(v)) element then out(v) e’ else // org(e’) and child(v) types are normal. w N-find(e’) for all e’’ N-enumerate(child(v)) do N-add(w, e’’ ) remove child(v) remove v
39
39 Set-destroy(S i ) - examples SiSi v SiSi v e’ w
40
40 The analysis Theorem Given a collection of m sets and collection of n elements, a union copy structure has the following performance: set-create O(1)elm-create O(1) set-insert O(1)elm-insert O(1) set-destroy O(1+k (F N (n)+F R (m)))elm-destroy O(1+k (F R (m)+F N (n))) set-findO(1+k F R (m))elm-findO(1+k F N (n)) set-unionO(U N (n))elm-unionO(U R (m)) set-copyO(F R (m)) elm-copyO(F N (n))
41
41 Theorem - proof Any normal node has out degree n. Any reversed node has in degree m. The operations N-find (R ), N-enumerate (R ) operate on sets of size O(n) ( O(m) ). Proof for set-find: If there are k answers, then there are O(k) normal nodes and reversed nodes that have been visited. N-enumerate is linear with the degree of the node the total time spent on normal nodes is O(k). The time to visit a reversed node is O(F R (m)). At most O(1+k F R (m)) time is taken.
42
42
43
43 Dynamic segments trees - motivation With semi-dynamic data structures, new segments may only inserted if their endpoints are chosen from a restricted universe. When using dynamic segments trees segments may be inserted and deleted freely. Split and concatenate operations are also provided.
44
44 Defining the operations. Create(T) - (defined if T does not exist yet). Creates an empty tree T. Insert(T,[x 1,x 2 ]) - (defined if [x 1,x 2 ] T). [x 1,x 2 ] is inserted to T. Delete(T,[x 1,x 2 ]) - (defined if [x 1,x 2 ] T). [x 1,x 2 ] is deleted from T. Stabbing-query(T,y) - all segments [x 1,x 2 ] for which x 1 y x 2 are reported.
45
45 Defining the operations. Continue Concatenate(T 1,T 2,T) - (defined if the right endpoint of segment in T 1 is less than the left endpoint of segment in T 2, and T is empty. It makes T T 1 T 2.T 1 and T 2 are returned empty. Split(T,T 1,T 2,y) - (Defined if all segments [x 1,x 2 ] in T either y x 1 or x 2 y, and T 1, T 2 are empty). It makes T 1 {[x 1,x 2 ]; x 2 y},T 2 {[x 1,x 2 ]; y x 1 } T is returned empty.
46
46 Weak segment tree - definition. A w.s.t for a set S of segments consists of a binary tree T, with ordered elementary intervals in its leaves. Each node represents a subset of S, s.t s S we have: –s is represented exactly once on every path from the root to a leaf, of which the elementary interval in contained in s. –s is not represented on any other path from the root to a leaf. A traditional segment tree is a w.s.t.
47
47 Weak segment tree - example. I 1,I 2 I 1 =[1,3] I 2 =[2,3] (-,1) [1,1] (1,2) [2,2] (2,3) [3,3] (3,-) I1I1 I2I2 I1I1 I2I2 I2I2 I 1,I 2 I1I1 I1I1 I1I1
48
48 The union copy structure for dynamic segment trees Every node in T corresponds to a set in the union-copy structure. Every segment corresponds to an element. The union-copy structure has O(n) sets and O(n) elements.
49
49 The union copy structure for dynamic segment trees. Continue We take for the N-UF a union-find structure. (O(1) time for N-union). For R-UF we take a structure in which R-find takes O(1) (UF(i) structure of La Poutre). Allows : R-find in O(1), R-union in O( (n)) amortized.
50
50 The structure. The dynamic segment tree consists of: A week segment tree noted ST T (RB tree). Every node is augmented with an extra pointer to a set node of the union-copy structure. A union-copy structure. A dictionary tree (RB tree) noted D T, storing the set S of segments in its leaves order on increasing (lexicography) left endpoint. Every leaf stores a segment which is an element node of the union-copy structure.
51
51 Why do we use a w.s.t? S2S2 S2S2 S1S1 S1S1 v u After rotation right at v node u holds S 2 – contradicts the definition of a w.s.t! u v S1S1 S2S2 S2S2 S1S1 S1S2S1S2 u v S2S2 S1S1 S1S2S1S2 S1S2S1S2
52
52 The solution The auxiliary operation Down( ). Empty a set S by shifting ’s elements to both its children: Set-create(S’) Set-copy (S ,S’) Set-union(S lchild( ),S’) Set-union(S rchild( ),S ) Set-destroy(S’). // remove the empty set S’. This operation will take O(1)!
53
53 Insert ([x 1,x 2 ],T) Add [x 1,x 2 ] to D T. NewEndpoint(x 1,[x 1,x 2 ]) NewEndpoint(x 2,[x 1,x 2 ]) The node in ST T where the paths to x 1 and to x 2 are split. for 1 on path from lchild( ) to leaf of x 1 do if x 1 is in left subtree of 1 then Set-insert(S rchild( 1), [x 1,x 2 ]) for 2 on path from rchild( ) to leaf of x 2 do if x 2 is in left subtree of 2 then Set-insert(S lchild( 2), [x 1,x 2 ])
54
54 NewEndpoint (x,[x 1,x 2 ]) The leaf that contains x in ST T. if Int( ) is an open interval (a,b) then Int( ) [x,x] add (a,x) as a leaf ’ to ST T add (x,b) as a leaf ’’ to ST T Set-create(S ’ ) ; Set-create(S ’’ ) Set-copy(S , S ’ ) ; Set-copy(S , S ’’ ) m( ) 0 ; Set-insert(S ,[x 1,x 2 ]) ; m( ) m( ) +1
55
55 Insert ([x 1,x 2 ],T) - example I 1,I 2 I 1 =[1,3] I 2 =[2,3] (-,1) [1,1] (3,-) I1I1 I2I2 Insert ( [1.5,2.5] ) [2,2] (1.5,2) (1,1.5) [1.5,1.5] [3,3] (2.5,3) (2,2.5) [2.5,2.5] I 3 =[1.5,2.5] I1I1 I3I3 I3I3 I3I3 I3I3 m( )=1 (1,2) (2,3)
56
56 Delete([x 1,x 2 ],T) The leaf in D T that contains [x 1,x 2 ] Element-destroy( ) delete [x 1,x 2 ] from D T Remove-endpoint(x 1 ) Remove-endpoint(x 2 )
57
57 Remove-endpoint(x) The leaf in ST T that contains x. m( ) m( ) - 1 if m( ) = 0 then // let Int(leaf left )=(a,x) // let Int(leaf right )=(x,b) Int( ) (a,b) delete from ST T the leaf left of delete from ST T the leaf right of
58
58 [1.5,1.5] Delete ([x 1,x 2 ],T) - example I 1,I 2 I 1 =[1,3] I 2 =[2,3] (-,1) [1,1] (3,-) I1I1 I2I2 Delete ( [1.5,2.5] ) (1.5,2) (1,1.5) (2.5,3) [2.5,2.5] I 3 =[1.5,2.5] I1I1 I3I3 I3I3 I3I3 I3I3 m( )=0 (1,1.5) (1.5,2) I3I3 I3I3 I3I3 I3I3 [2.5,2.5] [1.5,1.5] (1,2) (2.5,3) [2,2] (2,3) [3,3] (2,2.5)
59
59 Concatenate(T 1,T 2,T) if T 1 is empty then T T 2 ; ready if T 2 is empty then T T 1 ; ready 1 rightmost leaf of ST T1 // let Int( 1 ) = (x, ) 2 leftmost leaf of ST T2 // let Int( 2 ) = (- ,y) Int( 1 ) = (x,y) delete 2 from ST T2 concatenate ST T1 and ST T2 to ST T concatenate D T1 and D T2 to D T
60
60 Stabbing-query(T,y) for all nodes on the path ST T to y do Set-find(S )
61
61 The analysis Lemma1: The operations insert and concatenate take O(logn) time on a dynamic segment tree that stores n segments, and this operations increase the space of the structure by at most O(logn).
62
62 The analysis - Continue. Lemma2: Suppose the union copy-structure (as we chose) has O(nlogn) edges. Then O(n) Element-destroy operations take O(nlogn (n)) time.
63
63 The analysis - Continue. Theorem: The operations insert and concatenate on the dynamic segment tree as described above for storing a set of n segments each take O(logn) amortized time. The delete operations take O(logn (n)) amortized time, and stabbing queries take O(logn+k) time. The structure requires O(nlogn) space and can be constructed in O(nlogn) time.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.