union copy structures and dynamic segments tress
DESCRIPTION
Union copy structures and dynamic segments tress. Traditional segment trees. Segment Trees - motivation. When one wants to inspect a small portion of a large and complex objects . Example: - PowerPoint PPT PresentationTRANSCRIPT
1
2
3
Segment Trees - motivation.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
Semi dynamic segment Semi dynamic segment trees.trees.
Let I = { [x1, x1’ ], [x2, x2’ ],, [xn, xn’ ] } be a set of n intervals.
Let p1, p2,, pm be the list of distinct intervals endpoints, sorted from left to right. The elementary intervals are defined to be : (-, p1), [p1, p1], (p1, p2), [p2, p2],(pm,)
p1 p2 pm
5
Constructing a segment treeConstructing 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
Constructing a segment tree- Constructing a segment tree- cont.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
Segment tree- exampleSegment tree- example
s1 s2 s5
s3
s4
S5
S2, S5
S1
S1
S2, S5
S1S3
S4S3
S4, S3
p1 p2p3 p4
p5
p6p7
8
Lemma - proofLemma - proof
T is a balances binary tree its height isO(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
Lemma - proof. ContinueLemma - proof. Continue
Any interval is stored at most twice at a given depth of T.
The total amount of storage is O(nlogn).
v1 v2 v3
10
Algorithm: Algorithm: Query Segment Query Segment Tree(V,qTree(V,qxx))
Input: the root of a segment tree and a query point qx.
Output: All intervals in the tree containing qx .
Report all intervals in I(v). If v is not a leaf
– then if qx Int(lc(v))
– then QuerySegmentTree(lc(v), qx)–else QuerySegmentTree(rc(v), qx)
11
Algorithm: Algorithm: Query Segment Query Segment Tree(V,qTree(V,qxx) - Complexity.) - Complexity.
Visit O(logn) nodes in total. At each node v spend O(1+kv) time.
The intervals containing a query point qx
are reported in O(logn+k) time, where k is
the number of reported intervals.
12
Algorithm:Algorithm:Constructing a Constructing a segment tree.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
Algorithm:Algorithm:Insert Segment Tree(V,Insert Segment Tree(V,[x,x’])[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
Algorithm: Algorithm: Insert Segment Insert Segment Tree(V,[x,x’]) - Complexity.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
16
IntroductionIntroduction Generalization of union-find problem:
copy operation is also supported.
The structure: A bipartite graph with two node sets V1, V2 and edges between them.
V1 form the sets and V2 form the elements. An edge between v1V1 and v2V2 indicates that v2 is a member element of the set v1.
17
NotationsNotations
Let = {S1,Sm} be a collection of sets.
= {x1,,xn} be a collection of elements.
The sets in are subsets of .
Let x be the collection of sets that contain an element x.
18
The structureThe 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
The structure - continue. 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
The structure - continue.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
The structure - exampleThe structure - exampleS1 S2 S3 S4 S5
x1 x2 x3x4 x5
x6 x7 x8
set
normal
reversed
element
22
Defining the operationsDefining the operations..
Set-create(S) - create a new empty set in with name S.
Set-insert(Si,xj) - (defined when xj Si). make Si Si{xj}.
Set-destroy(Si) - remove Si from . Set-find(Si) - report all elements in Si. Set-union(Si,Sj) - (defined when Si, Sj disjoint).
Si SiSj and Sj. Set-copy(Si,Sj) - (defined when Sj is empty).
Sj Si. Element-create(x) - create an element x in and
x .
23
Defining the operationsDefining the operations. . ContinueContinue
Element-insert(xi, Sj) - (define when xiSj) xi xi {Sj}.
Element-destroy(xi) - remove xi from and from all its sets.
Element-find(xi) - report all sets that contain xi.
Element-union(xi,xj) - (defined when no set contains them both). Sxj, SS{xi}\{xj}, hence xi becomes xixj and xj.
Element-copy(xi,xj) - (defined when xj in no set). Puts xj in all sets containing xi.
24
Set-insert(SSet-insert(S44,x,x22))S1 S2 S3 S4 S5
x1 x2 x3x4 x5
x6 x7 x8
25
S3
Set-destroy(SSet-destroy(S33))S1 S2
S3
S4 S5
x1 x2 x3x4 x5
x6 x7 x8
26
Set-union(SSet-union(S44,S,S11))S1 S2 S3 S4 S5
x1 x2 x3x4 x5
x6 x7 x8
27
Set-copy(SSet-copy(S55,S,S66))S1 S2 S3 S4 S5
x1 x2 x3x4 x5
x6 x7 x8
S6
28
The structure - notations.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
Operations N-UF structure Operations N-UF structure supportssupports N-union(v,w) - Unite the edges of the
v and w to become the edges of v. w looses its edges.
N-find(ei) - Return the normal node of ei.
N-add(v,ei) - Add edge ei to node v.
N-delete(ei) - Delete ei from its origin.
N-enumerate(v) - Return all outgoing edges of v.
30
The operations.The operations.
Set-find(Si)
if out(Si) nil then Traverse(out(Si) )
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
Set-union(Set-union(SSii, S, Sj j ))
if out(Si) nil or type(child(Si)) normal then exchange Si and Sj.
if out(Sj) nil then ready. else if type(child(Si)) normal and type(child(Sj))
normal then N-union(child(Si), child(Sj))
else if type(child(Si)) normal then N-add(child(Si), out(Sj))
else // both are reversed or elements.make a normal node v.N-add(v, out(Si) ) ; N-add(v, out(Sj) )child(Si) v ; out(Sj) nil
32
Set-union - examples.Set-union - examples.
Si SjSi Sj
Si Sj SiSj
SiSi Sj
Sj
v
33
Set-copy(Set-copy(SSii, S, Sjj ))
if out(Si) nil then ready.
else if type(child(Si))=reversed then R-add(R-find(out(Si)), out(Sj))
else // type(child(Si))=normal or element.make a reversed node v.child(v) child(Si)R-add(v, out(Si)) R-add(v, out(Sj))
34
Set-copy - examples.Set-copy - examples.
SiSi
Sj
Si SiSj
v
35
Set-insert(SSet-insert(Sii, x, xjj))
Make a new edge e if out(Si) nil then out(Si) e
else if type(child(Si)) normal then N-add(child((Si), e)
else // type(child(Si)) is reversed or element.Make a normal node vN-add(v, out(Si)) ; N-add(v, e)child(Si) = v
// now e has an origin. if in(xj) nil then in(xj) e
else if type(parent(xj)) reversed then R-add(parent(xj), e)
36
Set-insert(SSet-insert(Sii, x, xjj) - continue) - continue
else // type(parent(xj)) is normal or set.Make a reversed node w.R-add(w, in(xj) ); R-add(w, e)parent(xj) w
// now e has a destination.
SiSi
xj
ev
wxj
37
Set-destroy(SSet-destroy(Sii))
if out(Si) nil then case type(child(Si)) of
element: in(child(Si)) nil
reversed : Restore(out(Si) )
normal: for all eN-enumerate(child(Si)) do
if type(dest(e))element then in(dest(e)) nilelse //type(dest(e)) is reversed.Restore(e)
remove Si
38
Set-destroy(SSet-destroy(Sii) - Restore(e)) - 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
thenout(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
Set-destroy(SSet-destroy(Sii) - examples) - examples
Si
v
Si
v
e’
w
40
The analysisThe analysisTheoremGiven 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(FN(n)+FR(m))) elm-destroy O(1+k(FR(m)
+FN(n)))
set-find O(1+kFR(m)) elm-find O(1+kFN(n))
set-union O(UN(n)) elm-union O(UR(m))
set-copy O(FR(m)) elm-copy O(FN(n))
41
Theorem - proofTheorem - 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(FR(m)).
At most O(1+kFR(m)) time is taken.
42
43
Dynamic segments trees - Dynamic segments trees - motivationmotivation
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
Defining the operations.Defining the operations. Create(T) - (defined if T does not exist yet).
Creates an empty tree T.
Insert(T,[x1,x2]) - (defined if [x1,x2]T). [x1,x2] is inserted to T.
Delete(T,[x1,x2]) - (defined if [x1,x2]T). [x1,x2] is deleted from T.
Stabbing-query(T,y) - all segments [x1,x2] for which x1 y x2 are reported.
45
Defining the operations. Defining the operations. ContinueContinue
Concatenate(T1,T2,T) - (defined if the right endpoint of segment in T1 is less than the left endpoint of segment in T2 , and T is empty. It makes T T1T2 .T1 and T2 are returned empty.
Split(T,T1,T2,y) - (Defined if all segments [x1,x2] in T either y x1 or x2 y, and T1, T2 are empty). It makes T1 {[x1,x2]; x2 y},T2 {[x1,x2]; y x1} T is returned empty.
46
Weak segment tree - Weak segment tree - definition.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 sS 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
Weak segment tree - Weak segment tree - example.example.
I1,I2
I1=[1,3] I2=[2,3]
(-,1) [1,1] (1,2) [2,2] (2,3) [3,3] (3,-)
I1
I2
I1
I2
I2I1,I2I1
I1
I1
48
The union copy structure for The union copy structure for dynamic segment treesdynamic 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
The union copy structure for The union copy structure for dynamic segment trees. dynamic segment trees. ContinueContinue 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
The structure.The structure.
The dynamic segment tree consists of: A week segment tree noted STT (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 DT, 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
Why do we use a w.s.t?Why do we use a w.s.t?
S2
S2S1
S1v
u
After rotation right at v node u holds S2 –
contradicts the definition
of a w.s.t!
u
vS1
S2
S2S1
S1S2
u
v
S2S1
S1S2
S1S2
52
The solutionThe 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(Slchild(),S’) Set-union(Srchild(),S) Set-destroy(S’). // remove the empty set S’.
This operation will take O(1)!
53
Insert ([xInsert ([x11,x,x22],T)],T) Add [x1,x2] to DT .
NewEndpoint(x1,[x1,x2])
NewEndpoint(x2,[x1,x2])
The node in STT where the paths to x1 and to x2 are split.
for 1 on path from lchild() to leaf of x1 doif x1 is in left subtree of 1 then
Set-insert(Srchild(1), [x1,x2])
for 2 on path from rchild() to leaf of x2 doif x2 is in left subtree of 2 then
Set-insert(Slchild(2), [x1,x2])
54
NewEndpoint (x,[xNewEndpoint (x,[x11,x,x22])])
The leaf that contains x in STT . if Int() is an open interval (a,b) then
Int() [x,x]add (a,x) as a leaf ’ to STT
add (x,b) as a leaf ’’ to STT
Set-create(S’) ; Set-create(S’’) Set-copy(S, S’ ) ; Set-copy(S, S’’ )m() 0 ;
Set-insert(S ,[x1,x2]) ; m() m() +1
55
Insert ([xInsert ([x11,x,x22],T) - example],T) - example
I1,I2
I1=[1,3] I2=[2,3]
(-,1) [1,1] (3,-)
I1
I2
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]
I3=[1.5,2.5]
I1
I3 I3
I3
I3
m()=1 m()=1
(1,2)(2,3)
56
Delete([xDelete([x11,x,x22],T) ],T)
The leaf in DT that contains [x1,x2]
Element-destroy() delete [x1,x2] from DT
Remove-endpoint(x1)
Remove-endpoint(x2)
57
Remove-endpoint(x)Remove-endpoint(x)
The leaf in STT 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 STT the leaf left of delete from STT the leaf right of
58
[1.5,1.5]
Delete ([xDelete ([x11,x,x22],T) - example],T) - example
I1,I2
I1=[1,3] I2=[2,3]
(-,1) [1,1] (3,-)
I1
I2
Delete ( [1.5,2.5] )
(1.5,2)(1,1.5) (2.5,3)[2.5,2.5]
I3=[1.5,2.5]
I1I3 I3
I3
I3
m()=0 m()=0
(1,1.5) (1.5,2)
I3 I3
I3
I3
[2.5,2.5][1.5,1.5]
(1,2)(2.5,3)[2,2] (2,3)
[3,3](2,2.5)(2,2.5)
59
Concatenate(TConcatenate(T11,T,T22,T),T)
if T1 is empty then T T2; ready
if T2 is empty then T T1; ready
1 rightmost leaf of STT1 // let Int(1) = (x,)
2 leftmost leaf of STT2 // let Int(2) = (-,y)
Int(1) = (x,y)
delete 2 from STT2
concatenate STT1 and STT2 to STT
concatenate DT1 and DT2 to DT
60
Stabbing-query(T,y)Stabbing-query(T,y)
for all nodes on the path STT to y do Set-find(S)
61
The analysisThe 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
The analysis - Continue.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
The analysis - Continue.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.