64
Dynamic Segment Trees and Union Copy Stuctures Computational Geometry, WS 2006/07 Lecture 19 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für Angewandte Wissenschaften Albert-Ludwigs-Universität Freiburg

Dynamic Segment Trees and Union Copy Stuctures

Embed Size (px)

DESCRIPTION

Dynamic Segment Trees and Union Copy Stuctures. Computational Geometry, WS 2006/07 Lecture 19 Prof. Dr. Thomas Ottmann. Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für Angewandte Wissenschaften Albert-Ludwigs-Universität Freiburg. Traditional Segment Trees. - PowerPoint PPT Presentation

Citation preview

Page 1: Dynamic Segment Trees and Union Copy Stuctures

Dynamic Segment Trees and Union Copy Stuctures

Computational Geometry, WS 2006/07Lecture 19

Prof. Dr. Thomas Ottmann

Algorithmen & Datenstrukturen, Institut für InformatikFakultät für Angewandte WissenschaftenAlbert-Ludwigs-Universität Freiburg

Page 2: Dynamic Segment Trees and Union Copy Stuctures

2

Traditional Segment Trees

Page 3: Dynamic Segment Trees and Union Copy Stuctures

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).

Page 4: Dynamic Segment Trees and Union Copy Stuctures

4

Semi dynamic segment 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 p2pm

Page 5: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 6: Dynamic Segment Trees and Union Copy Stuctures

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(n log n) storage.

Page 7: Dynamic Segment Trees and Union Copy Stuctures

7

Segment tree- example

s1 s2s5

s3

s4

S5

S2, S5

S1

S1

S2, S5

S1S3

S4S3

S4, S3

p1 p2

p3 p4

p5

p6p7

Page 8: Dynamic Segment Trees and Union Copy Stuctures

8

Lemma - proof

T is a balances binary tree its height is O(log n).

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’] .

Page 9: Dynamic Segment Trees and Union Copy Stuctures

9

Lemma - proof. Continue

Any interval is stored at most twice at a given depth of T.

The total amount of storage is O(n log n).

v1 v2 v3

Page 10: Dynamic Segment Trees and Union Copy Stuctures

10

Algorithm: Query Segment Tree(V,qx)

• 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)

Page 11: Dynamic Segment Trees and Union Copy Stuctures

11

Algorithm: Query Segment Tree(V,qx) - Complexity.

• Visit O(log n) nodes in total.

• At each node v spend O(1 + kv) time.

The intervals containing a query point qx are reported in O(log n + k) time, where k is the number of reported intervals.

Page 12: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 13: Dynamic Segment Trees and Union Copy Stuctures

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’])

Page 14: Dynamic Segment Trees and Union Copy Stuctures

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(log n). Construction time is

O(n log n).

Page 15: Dynamic Segment Trees and Union Copy Stuctures

15

Union copy structures

Page 16: Dynamic Segment Trees and Union Copy Stuctures

16

Introduction

• 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 v2 V2 indicates that v2 is a member element of the set v1.

Page 17: Dynamic Segment Trees and Union Copy Stuctures

17

Notations

• 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.

Page 18: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 19: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 20: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 21: Dynamic Segment Trees and Union Copy Stuctures

21

The structure - example

set

normal

reversed

element

S1S2 S3 S4

S5

x1 x2 x3x4 x5

x6 x7 x8

Page 22: Dynamic Segment Trees and Union Copy Stuctures

22

Defining 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 Si Sj and Sj.

• Set-copy(Si, Sj) - (defined when Sj is empty). Sj Si.

Page 23: Dynamic Segment Trees and Union Copy Stuctures

23

Defining the operations. Continue

• Element-create(x) - create an element x in and x .

• Element-insert(xi, Sj) - (define when xi Sj) 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, S S {xi}\{xj}, hence xi becomes xi xj and xj .

• Element-copy(xi, xj) - (defined when xj in no set). Puts xj in all sets containing xi.

Page 24: Dynamic Segment Trees and Union Copy Stuctures

24

Set-insert(S4,x2)

S1 S2 S3 S4 S5

x1 x2 x3x4 x5

x6 x7 x8

Page 25: Dynamic Segment Trees and Union Copy Stuctures

25

Set-destroy(S3)

x8

S1 S2 S4S5

x1 x2 x3x4 x5

x6 x7

S3

Page 26: Dynamic Segment Trees and Union Copy Stuctures

26

Set-union(S4,S1)

x8

S1 S2 S4S5

x1 x2 x3x4 x5

x6 x7

S3

Page 27: Dynamic Segment Trees and Union Copy Stuctures

27

Set-copy(S5, S6)

S1 S2 S3 S4S5

x1 x2 x3x4 x5

x6 x7 x8

S6

Page 28: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 29: Dynamic Segment Trees and Union Copy Stuctures

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(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.

Page 30: Dynamic Segment Trees and Union Copy Stuctures

30

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)))

Page 31: Dynamic Segment Trees and Union Copy Stuctures

31

Set-union(Si, Sj )

• 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

Page 32: Dynamic Segment Trees and Union Copy Stuctures

32

Set-union - examples.

Si SjSi Sj

Si Sj SiSj

SiSi Sj

Sj

v

Page 33: Dynamic Segment Trees and Union Copy Stuctures

33

Set-copy(Si, Sj )

• 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))

Page 34: Dynamic Segment Trees and Union Copy Stuctures

34

Set-copy - examples.

SiSi

Sj

Si SiSj

v

Page 35: Dynamic Segment Trees and Union Copy Stuctures

35

Set-insert(Si, xj)

• 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)

Page 36: Dynamic Segment Trees and Union Copy Stuctures

36

Set-insert(Si, xj) - 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

e

v

wxj

Page 37: Dynamic Segment Trees and Union Copy Stuctures

37

Set-destroy(Si)

• 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)) doif type(dest(e))element then

in(dest(e)) nilelse //type(dest(e)) is

reversed.Restore(e)

• remove Si

Page 38: Dynamic Segment Trees and Union Copy Stuctures

38

Set-destroy(Si) - 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

Page 39: Dynamic Segment Trees and Union Copy Stuctures

39

Set-destroy(Si) - examples

Si

v

Si

v

e’

w

Page 40: Dynamic Segment Trees and Union Copy Stuctures

40

The analysis

TheoremGiven 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+ k FR(m)) elm-find O(1+ k FN(n))

set-union O(UN(n)) elm-union O(UR(m))

set-copy O(FR(m)) elm-copy O(FN(n))

Page 41: Dynamic Segment Trees and Union Copy Stuctures

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(FR(m)).

At most O(1+kFR(m)) time is taken.

Page 42: Dynamic Segment Trees and Union Copy Stuctures

42

Dynamic Segment Trees

Page 43: Dynamic Segment Trees and Union Copy Stuctures

43

Dynamic segments trees - motivation

• With semi-dynamic data structures, new segments may be 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.

Page 44: Dynamic Segment Trees and Union Copy Stuctures

44

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.

Page 45: Dynamic Segment Trees and Union Copy Stuctures

45

Defining the operations. Continue

• Concatenate(T1, T2, T) - (defined if the rightmost endpoint of a segment in T1 is less than the leftmost endpoint of a segment in T2 , and T is empty. It makes T T1 T2 . T1 and T2 are returned empty.

• Split(T, T1, T2, y) - (Defined if for 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.

Page 46: Dynamic Segment Trees and Union Copy Stuctures

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 sS we have:

– s is represented exactly once on every path from the root to a leaf, of which the elementary interval is 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.

Page 47: Dynamic Segment Trees and Union Copy Stuctures

47

Weak segment tree - 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

Page 48: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 49: Dynamic Segment Trees and Union Copy Stuctures

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.

Page 50: Dynamic Segment Trees and Union Copy Stuctures

50

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, ordered on increasing (lexicography) left endpoint. Every leaf stores a segment which is an element node of the union-copy structure.

Page 51: Dynamic Segment Trees and Union Copy Stuctures

51

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

S1

u

v

S2S1

S1S2

S1S2

Page 52: Dynamic Segment Trees and Union Copy Stuctures

52

Why do we use a w.s.t?

S2

S1v

u

u

v

S5S4

v

u

S3

Page 53: Dynamic Segment Trees and Union Copy Stuctures

53

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(Slchild(), S’)

• Set-union(Srchild(), S)

• Set-destroy(S’). // remove the empty set S’.

This operation will take O(1)!

Page 54: Dynamic Segment Trees and Union Copy Stuctures

54

Insert ([x1,x2],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])

Page 55: Dynamic Segment Trees and Union Copy Stuctures

55

NewEndpoint (x, [x1,x2])

The leaf that contains x in STT.

• if Int() is an open interval (a,b) thenInt() [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

Page 56: Dynamic Segment Trees and Union Copy Stuctures

56

Insert ([x1,x2],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)

Page 57: Dynamic Segment Trees and Union Copy Stuctures

57

Delete([x1,x2],T)

The leaf in DT that contains [x1,x2]

• Element-destroy()

• delete [x1,x2] from DT

• Remove-endpoint(x1)

• Remove-endpoint(x2)

Page 58: Dynamic Segment Trees and Union Copy Stuctures

58

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

Page 59: Dynamic Segment Trees and Union Copy Stuctures

59

[1.5,1.5]

Delete ([x1,x2],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)

Page 60: Dynamic Segment Trees and Union Copy Stuctures

60

Concatenate(T1, T2 , 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

Page 61: Dynamic Segment Trees and Union Copy Stuctures

61

Stabbing-query(T,y)

• for all nodes on the path STT to y do

Set-find(S)

Page 62: Dynamic Segment Trees and Union Copy Stuctures

62

The analysis

Lemma1: The operations insert and concatenate take O(log n) time on a dynamic segment tree that stores n segments, and this operations increase the space of the structure by at most O(log n).

Page 63: Dynamic Segment Trees and Union Copy Stuctures

63

The analysis - Continue.

Lemma2: Suppose the union copy-structure (as we chose) has O(nlog n) edges. Then O(n) Element-destroy operations take O(nlogn(n)) time.

Page 64: Dynamic Segment Trees and Union Copy Stuctures

64

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(log n) amortized time. The delete operations take O(log n (n)) amortized time, and stabbing queries take O(log n + k) time.The structure requires O(nlog n) space and can be constructed in O(nlog n) time.