Computational Geometry Lecture 8: Range trees 1 Computational - - PowerPoint PPT Presentation

computational geometry
SMART_READER_LITE
LIVE PREVIEW

Computational Geometry Lecture 8: Range trees 1 Computational - - PowerPoint PPT Presentation

Introduction 2D Range trees Degenerate cases Range trees Computational Geometry Lecture 8: Range trees 1 Computational Geometry Lecture 8: Range trees Introduction 2D Range trees Range queries Degenerate cases Database queries G.


slide-1
SLIDE 1

Introduction 2D Range trees Degenerate cases

Range trees

Computational Geometry

Lecture 8: Range trees

Computational Geometry Lecture 8: Range trees

1

slide-2
SLIDE 2

Introduction 2D Range trees Degenerate cases Range queries

Database queries

A database query may ask for all employees with age between a1 and a2, and salary between s1 and s2

date of birth salary 19,500,000 19,559,999

  • G. Ometer

born: Aug 16, 1954 salary: $3,500

Computational Geometry Lecture 8: Range trees

2

slide-3
SLIDE 3

Introduction 2D Range trees Degenerate cases Range queries

Result

Theorem: A set of n points on the real line can be preprocessed in O(nlogn) time into a data structure of O(n) size so that any 1D range [counting] query can be answered in O(logn [+k]) time

Computational Geometry Lecture 8: Range trees

3

slide-4
SLIDE 4

Introduction 2D Range trees Degenerate cases Range queries

Result

Theorem: A set of n points in the plane can be preprocessed in O(nlogn) time into a data structure of O(n) size so that any 2D range query can be answered in O(√n+k) time, where k is the number of answers reported For range counting queries, we need O(√n) time

Computational Geometry Lecture 8: Range trees

4

slide-5
SLIDE 5

Introduction 2D Range trees Degenerate cases Range queries

Faster queries

Can we achieve O(logn [+k]) query time?

Computational Geometry Lecture 8: Range trees

5

slide-6
SLIDE 6

Introduction 2D Range trees Degenerate cases Range queries

Faster queries

Can we achieve O(logn [+k]) query time?

Computational Geometry Lecture 8: Range trees

6

slide-7
SLIDE 7

Introduction 2D Range trees Degenerate cases Range queries

Faster queries

If the corners of the query rectangle fall in specific cells of the grid, the answer is fixed (even for lower left and upper right corner)

Computational Geometry Lecture 8: Range trees

7

slide-8
SLIDE 8

Introduction 2D Range trees Degenerate cases Range queries

Faster queries

Build a tree so that the leaves correspond to the different possible query rectangle types (corners in same cells of grid), and with each leaf, store all answers (points) [or: the count] Build a tree on the different x-coordinates (to search with left side of R), in each of the leaves, build a tree on the different x-coordinates (to search with the right side of R), in each of the leaves, ...

Computational Geometry Lecture 8: Range trees

8

slide-9
SLIDE 9

Introduction 2D Range trees Degenerate cases Range queries

Faster queries

n n n n

Computational Geometry Lecture 8: Range trees

9

slide-10
SLIDE 10

Introduction 2D Range trees Degenerate cases Range queries

Faster queries

Question: What are the storage requirements of this structure, and what is the query time?

Computational Geometry Lecture 8: Range trees

10

slide-11
SLIDE 11

Introduction 2D Range trees Degenerate cases Range queries

Faster queries

Recall the 1D range tree and range query: Two search paths (grey nodes) Subtrees in between have answers exclusively (black)

Computational Geometry Lecture 8: Range trees

11

slide-12
SLIDE 12

Introduction 2D Range trees Degenerate cases Range queries

Example 1D range query

A 1-dimensional range query with [25, 90]

3 10 19 23 30 37 59 62 70 80 89 3 19 10 30 59 70 62 93 89 80 23 49 93 97 37 49

Computational Geometry Lecture 8: Range trees

12

slide-13
SLIDE 13

Introduction 2D Range trees Degenerate cases Range queries

Example 1D range query

A 1-dimensional range query with [61, 90]

3 10 19 23 30 37 59 62 70 80 89 3 19 10 30 59 70 62 93 89 80 23 49 93 97 37 49 split node

Computational Geometry Lecture 8: Range trees

13

slide-14
SLIDE 14

Introduction 2D Range trees Degenerate cases Range queries

Examining 1D range queries

Observation: Ignoring the search path leaves, all answers are jointly represented by the highest nodes strictly between the two search paths Question: How many highest nodes between the search paths can there be?

Computational Geometry Lecture 8: Range trees

14

slide-15
SLIDE 15

Introduction 2D Range trees Degenerate cases Range queries

Examining 1D range queries

For any 1D range query, we can identify O(logn) nodes that together represent all answers to a 1D range query

Computational Geometry Lecture 8: Range trees

15

slide-16
SLIDE 16

Introduction 2D Range trees Degenerate cases Range queries

Toward 2D range queries

For any 2d range query, we can identify O(logn) nodes that together represent all points that have a correct first coordinate

Computational Geometry Lecture 8: Range trees

16

slide-17
SLIDE 17

Introduction 2D Range trees Degenerate cases Range queries

Toward 2D range queries

(3, 8) (1, 5) (4, 2) (5, 9) (6, 7) (8, 1) (7, 3) (9, 4)

Computational Geometry Lecture 8: Range trees

17

slide-18
SLIDE 18

Introduction 2D Range trees Degenerate cases Range queries

Toward 2D range queries

(3, 8) (1, 5) (4, 2) (5, 9) (6, 7) (8, 1) (7, 3) (9, 4)

Computational Geometry Lecture 8: Range trees

18

slide-19
SLIDE 19

Introduction 2D Range trees Degenerate cases Range queries

Toward 2D range queries

(3, 8) (1, 5) (4, 2) (5, 9) (6, 7) (8, 1) (7, 3) (9, 4) data structure for searching on y-coordinate

Computational Geometry Lecture 8: Range trees

19

slide-20
SLIDE 20

Introduction 2D Range trees Degenerate cases Range queries

Toward 2D range queries

(3, 8) (1, 5) (4, 2) (5, 9) (6, 7) (8, 1) (7, 3) (9, 4) (3, 8) (1, 5) (4, 2) (5, 9) (6, 7) (8, 1) (7, 3) (9, 4)

Computational Geometry Lecture 8: Range trees

20

slide-21
SLIDE 21

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range trees

Every internal node stores a whole tree in an associated structure, on y-coordinate Question: How much storage does this take?

Computational Geometry Lecture 8: Range trees

21

slide-22
SLIDE 22

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Storage of 2D range trees

To analyze storage, two arguments can be used: By level: On each level, any point is stored exactly once. So all associated trees on one level together have O(n) size By point: For any point, it is stored in the associated structures of its search path. So it is stored in O(logn) of them

Computational Geometry Lecture 8: Range trees

22

slide-23
SLIDE 23

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Construction algorithm

Algorithm Build2DRangeTree(P) 1. Construct the associated structure: Build a binary search tree Tassoc on the set Py of y-coordinates in P 2. if P contains only one point 3. then Create a leaf ν storing this point, and make Tassoc the associated structure of ν. 4. else Split P into Pleft and Pright, the subsets ≤ and > the median x-coordinate xmid 5. νleft ← Build2DRangeTree(Pleft) 6. νright ← Build2DRangeTree(Pright) 7. Create a node ν storing xmid, make νleft the left child of ν, make νright the right child of ν, and make Tassoc the associated structure of ν 8. return ν

Computational Geometry Lecture 8: Range trees

23

slide-24
SLIDE 24

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Efficiency of construction

The construction algorithm takes O(nlog2 n) time T(1) = O(1) T(n) = 2·T(n/2)+O(nlogn) which solves to O(nlog2 n) time

Computational Geometry Lecture 8: Range trees

24

slide-25
SLIDE 25

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Efficiency of construction

Suppose we pre-sort P on y-coordinate, and whenever we split P into Pleft and Pright, we keep the y-order in both subsets For a sorted set, the associated structure can be built in linear time

Computational Geometry Lecture 8: Range trees

25

slide-26
SLIDE 26

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Efficiency of construction

The adapted construction algorithm takes O(nlogn) time T(1) = O(1) T(n) = 2·T(n/2)+O(n) which solves to O(nlogn) time

Computational Geometry Lecture 8: Range trees

26

slide-27
SLIDE 27

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range queries

How are queries performed and why are they correct? Are we sure that each answer is found? Are we sure that the same point is found only once?

Computational Geometry Lecture 8: Range trees

27

slide-28
SLIDE 28

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range queries

ν µ µ′ p p p p

Computational Geometry Lecture 8: Range trees

28

slide-29
SLIDE 29

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Query algorithm

Algorithm 2DRangeQuery(T,[x : x′]×[y : y′]) 1. νsplit ←FindSplitNode(T,x,x′) 2. if νsplit is a leaf 3. then report the point stored at νsplit, if an answer 4. else ν ← lc(νsplit) 5. while ν is not a leaf 6. do if x ≤ xν 7. then 1DRangeQ(Tassoc(rc(ν)),[y : y′]) 8. ν ← lc(ν) 9. else ν ← rc(ν) 10. Check if the point stored at ν must be reported. 11. Similarly, follow the path from rc(νsplit) to x′ ...

Computational Geometry Lecture 8: Range trees

29

slide-30
SLIDE 30

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range query time

Question: How much time does a 2D range query take? Subquestions: In how many associated structures do we search? How much time does each such search take?

Computational Geometry Lecture 8: Range trees

30

slide-31
SLIDE 31

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range queries

ν µ µ′

Computational Geometry Lecture 8: Range trees

31

slide-32
SLIDE 32

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range query efficiency

We search in O(logn) associated structures to perform a 1D range query; at most two per level of the main tree The query time is O(logn)×O(logm+k′), or

ν

O(lognν +kν) where ∑kν = k the number of points reported

Computational Geometry Lecture 8: Range trees

32

slide-33
SLIDE 33

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range query efficiency

Use the concept of grey and black nodes again:

Computational Geometry Lecture 8: Range trees

33

slide-34
SLIDE 34

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range query efficiency

The number of grey nodes is O(log2 n) The number of black nodes is O(k) if k points are reported The query time is O(log2 n+k), where k is the size of the

  • utput

Computational Geometry Lecture 8: Range trees

34

slide-35
SLIDE 35

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Result

Theorem: A set of n points in the plane can be preprocessed in O(nlogn) time into a data structure of O(nlogn) size so that any 2D range query can be answered in O(log2 n+k) time, where k is the number of answers reported Recall that a kd-tree has O(n) size and answers queries in O(√n+k) time

Computational Geometry Lecture 8: Range trees

35

slide-36
SLIDE 36

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Efficiency

n logn log2 n √n 16 4 16 4 64 6 36 8 256 8 64 16 1024 10 100 32 4096 12 144 64 16384 14 196 128 65536 16 256 256 1M 20 400 1K 16M 24 576 4K

Computational Geometry Lecture 8: Range trees

36

slide-37
SLIDE 37

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

2D range query efficiency

Question: How about range counting queries?

Computational Geometry Lecture 8: Range trees

37

slide-38
SLIDE 38

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Higher dimensional range trees

A d-dimensional range tree has a main tree which is a

  • ne-dimensional balanced

binary search tree on the first coordinate, where every node has a pointer to an associated structure that is a (d −1)-dimensional range tree

  • n the other coordinates

Computational Geometry Lecture 8: Range trees

38

slide-39
SLIDE 39

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Storage

The size Sd(n) of a d-dimensional range tree satisfies: S1(n) = O(n) for all n Sd(1) = O(1) for all d Sd(n) ≤ 2·Sd(n/2)+Sd−1(n) for d ≥ 2 This solves to Sd(n) = O(nlogd−1 n)

Computational Geometry Lecture 8: Range trees

39

slide-40
SLIDE 40

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Query time

The number of grey nodes Gd(n) satisfies: G1(n) = O(logn) for all n Gd(1) = O(1) for all d Gd(n) ≤ 2·logn+2·logn·Gd−1(n) for d ≥ 2 This solves to Gd(n) = O(logd n)

Computational Geometry Lecture 8: Range trees

40

slide-41
SLIDE 41

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Result

Theorem: A set of n points in d-dimensional space can be preprocessed in O(nlogd−1 n) time into a data structure of O(nlogd−1 n) size so that any d-dimensional range query can be answered in O(logd n+k) time, where k is the number of answers reported Recall that a kd-tree has O(n) size and answers queries in O(n1−1/d +k) time

Computational Geometry Lecture 8: Range trees

41

slide-42
SLIDE 42

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Comparison for d = 4

n logn log4 n n3/4 1024 10 10,000 181 65,536 16 65,536 4096 1M 20 160,000 32,768 1G 30 810,000 5,931,641 1T 40 2,560,000 1G

Computational Geometry Lecture 8: Range trees

42

slide-43
SLIDE 43

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

We can improve the query time of a 2D range tree from O(log2 n) to O(logn) by a technique called fractional cascading This automatically lowers the query time in d dimensions to O(logd−1 n) time

Computational Geometry Lecture 8: Range trees

43

slide-44
SLIDE 44

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

The idea illustrated best by a different query problem: Suppose that we have a collection of sets S1,...,Sm, where |S1| = n and where Si+1 ⊆ Si We want a data structure that can report for a query number x, the smallest value ≥ x in all sets S1,...,Sm

Computational Geometry Lecture 8: Range trees

44

slide-45
SLIDE 45

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

1 2 3 5 8 13 21 34 55 1 3 5 8 13 21 34 55 1 3 13 34 55 3 34 55 21

S1 S2 S3 S4

Computational Geometry Lecture 8: Range trees

45

slide-46
SLIDE 46

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

1 2 3 5 8 13 21 34 55 1 3 5 8 13 21 34 55 1 3 13 34 55 3 34 55 21

S1 S2 S3 S4

Computational Geometry Lecture 8: Range trees

46

slide-47
SLIDE 47

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

1 2 3 5 8 13 21 34 55 1 3 5 8 13 21 34 55 1 3 13 34 55 3 34 55 21

S1 S2 S3 S4

Computational Geometry Lecture 8: Range trees

47

slide-48
SLIDE 48

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

Suppose that we have a collection of sets S1,...,Sm, where |S1| = n and where Si+1 ⊆ Si We want a data structure that can report for a query number x, the smallest value ≥ x in all sets S1,...,Sm This query problem can be solved in O(logn+m) time instead

  • f O(m·logn) time

Computational Geometry Lecture 8: Range trees

48

slide-49
SLIDE 49

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

Can we do something similar for m 1-dimensional range queries on m sets S1,...,Sm? We hope to get a query time of O(logn+m+k) with k the total number of points reported

Computational Geometry Lecture 8: Range trees

49

slide-50
SLIDE 50

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

1 2 3 5 8 13 21 34 55 1 3 5 8 13 21 34 55 1 3 13 34 55 3 34 55 21

S1 S2 S3 S4

Computational Geometry Lecture 8: Range trees

50

slide-51
SLIDE 51

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

1 2 3 5 8 13 21 34 55 1 3 5 8 13 21 34 55 1 3 13 34 55 3 34 55 21

S1 S2 S3 S4

Computational Geometry Lecture 8: Range trees

51

slide-52
SLIDE 52

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Improving the query time

1 2 3 5 8 13 21 34 55 1 3 5 8 13 21 34 55 1 3 13 34 55 3 34 55 21 [6,35]

S1 S2 S3 S4

Computational Geometry Lecture 8: Range trees

52

slide-53
SLIDE 53

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

Now we do “the same” on the associated structures of a 2-dimensional range tree Note that in every associated structure, we search with the same values y and y′ Replace all associated structures except for the one of the root by a linked list For every list element (and leaf of the associated structure of the root), store two pointers to the appropriate list elements in the lists of the left child and

  • f the right child

Computational Geometry Lecture 8: Range trees

53

slide-54
SLIDE 54

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

Computational Geometry Lecture 8: Range trees

54

slide-55
SLIDE 55

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

Computational Geometry Lecture 8: Range trees

55

slide-56
SLIDE 56

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

(2, 19) (5, 80) (7, 10) (8, 37) (12, 3) (15, 99) (17, 62) (21, 49) (33, 30) (41, 95) (52, 23) (58, 59) (67, 89) (93, 70) 2 5 7 8 12 15 17 21 33 41 52 58 67 17 8 15 5 7 12 21 41 33 52 58 67 93 2

Computational Geometry Lecture 8: Range trees

56

slide-57
SLIDE 57

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

3 99 10 19 37 80 30 49 62 3 10 19 23 30 37 49 59 62 70 80 89 95 99 89 70 59 23 95 30 49 80 3 99 62 37 19 10 3 99 62 89 70 59 23 95 30 49 89 70 23 95 10 37 19 80 70 89 23 95 37 10 19 59 49 80 3 30 99

Computational Geometry Lecture 8: Range trees

57

slide-58
SLIDE 58

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

(2, 19) (5, 80) (7, 10) (8, 37) (12, 3) (15, 99) (17, 62) (21, 49) (33, 30) (41, 95) (52, 23) (58, 59) (67, 89) (93, 70) 2 5 7 8 12 15 17 21 33 41 52 58 67 17 8 15 5 7 12 21 41 33 52 58 67 93 2

[4, 58] × [19, 65]

Computational Geometry Lecture 8: Range trees

58

slide-59
SLIDE 59

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

3 99 10 19 37 80 30 49 62 3 10 19 23 30 37 49 59 62 70 80 89 95 99 89 70 59 23 95 30 49 80 3 99 62 37 19 10 3 99 62 89 70 59 23 95 30 49 89 70 23 95 10 37 19 80 70 89 23 95 37 10 19 59 49 80 3 30 99

Computational Geometry Lecture 8: Range trees

59

slide-60
SLIDE 60

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

3 99 10 19 37 80 30 49 62 3 10 19 23 30 37 49 59 62 70 80 89 95 99 89 70 59 23 95 30 49 80 3 99 62 37 19 10 3 99 62 89 70 59 23 95 30 49 89 70 23 95 10 37 19 80 70 89 23 95 37 10 19 59 49 80 3 30 99

Computational Geometry Lecture 8: Range trees

60

slide-61
SLIDE 61

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Fractional cascading

Instead of doing a 1D range query on the associated structure

  • f some node ν, we find the leaf where the search to y would

end in O(1) time via the direct pointer in the associated structure in the parent of ν The number of grey nodes reduces to O(logn)

Computational Geometry Lecture 8: Range trees

61

slide-62
SLIDE 62

Introduction 2D Range trees Degenerate cases Construction Querying Higher dimensions Fractional cascading

Result

Theorem: A set of n points in d-dimensional space can be preprocessed in O(nlogd−1 n) time into a data structure of O(nlogd−1 n) size so that any d-dimensional range query can be answered in O(logd−1 n+k) time, where k is the number of answers reported Recall that a kd-tree has O(n) size and answers queries in O(n1−1/d +k) time

Computational Geometry Lecture 8: Range trees

62

slide-63
SLIDE 63

Introduction 2D Range trees Degenerate cases

Degenerate cases

Both for kd-trees and for range trees we have to take care of multiple points with the same x- or y-coordinate

Computational Geometry Lecture 8: Range trees

63

slide-64
SLIDE 64

Introduction 2D Range trees Degenerate cases

Degenerate cases

Both for kd-trees and for range trees we have to take care of multiple points with the same x- or y-coordinate

Computational Geometry Lecture 8: Range trees

64

slide-65
SLIDE 65

Introduction 2D Range trees Degenerate cases

Degenerate cases

Treat a point p = (px,py) with two reals as coordinates as a point with two composite numbers as coordinates A composite number is a pair of reals, denoted (a|b) We let (a|b) < (c|d) iff a < c or ( a = c and b < d ); this defines a total order on composite numbers

Computational Geometry Lecture 8: Range trees

65

slide-66
SLIDE 66

Introduction 2D Range trees Degenerate cases

Degenerate cases

The point p = (px,py) becomes ((px|py), (py|px)). Then no two points have the same first or second coordinate The median x-coordinate or y-coordinate is a composite number The query range [x : x′]×[y : y′] becomes [(x|−∞) : (x′|+∞)]×[(y|−∞) : (y′|+∞)] We have (px,py) ∈ [x : x′]×[y : y′] iff ((px|py), (py|px)) ∈ [(x|−∞) : (x′|+∞)]×[(y|−∞) : (y′|+∞)]

Computational Geometry Lecture 8: Range trees

66