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. Ometer born: Aug 16, 1954 salary salary: $3,500 A database query may ask for all employees with age between a 1 and a 2 , and salary between s 1 and s 2 19,500,000 19,559,999 date of birth 2 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Result Theorem: A set of n points on the real line can be preprocessed in O ( n log n ) time into a data structure of O ( n ) size so that any 1D range [counting] query can be answered in O ( log n [+ k ]) time 3 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Result Theorem: A set of n points in the plane can be preprocessed in O ( n log n ) 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 4 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Faster queries Can we achieve O ( log n [+ k ]) query time? 5 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Faster queries Can we achieve O ( log n [+ k ]) query time? 6 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases 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) 7 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases 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, ... 8 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Faster queries n n n n 9 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Faster queries Question: What are the storage requirements of this structure, and what is the query time? 10 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Faster queries Recall the 1D range tree and range query: Two search paths (grey nodes) Subtrees in between have answers exclusively (black) 11 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Example 1D range query A 1-dimensional range query with [ 25 , 90 ] 49 23 80 10 37 62 89 3 19 30 59 70 93 49 89 3 10 19 23 30 37 59 62 70 80 93 97 12 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Example 1D range query A 1-dimensional range query with [ 61 , 90 ] 49 split node 23 80 10 37 62 89 3 19 30 59 70 93 49 89 3 10 19 23 30 37 59 62 70 80 93 97 13 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases 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? 14 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Examining 1D range queries For any 1D range query, we can identify O ( log n ) nodes that together represent all answers to a 1D range query 15 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Toward 2D range queries For any 2d range query, we can identify O ( log n ) nodes that together represent all points that have a correct first coordinate 16 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Toward 2D range queries (1 , 5) (3 , 8) (4 , 2) (5 , 9) (6 , 7) (7 , 3) (8 , 1) (9 , 4) 17 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Toward 2D range queries (1 , 5) (3 , 8) (4 , 2) (5 , 9) (6 , 7) (7 , 3) (8 , 1) (9 , 4) 18 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Toward 2D range queries data structure for searching on y -coordinate (1 , 5) (3 , 8) (4 , 2) (5 , 9) (6 , 7) (7 , 3) (8 , 1) (9 , 4) 19 Computational Geometry Lecture 8: Range trees
Introduction 2D Range trees Range queries Degenerate cases Toward 2D range queries (5 , 9) (3 , 8) (6 , 7) (1 , 5) (9 , 4) (7 , 3) (4 , 2) (8 , 1) (3 , 8) (1 , 5) (4 , 2) (5 , 9) (6 , 7) (7 , 3) (8 , 1) (9 , 4) 20 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases 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? 21 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases 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 ( log n ) of them 22 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases Fractional cascading Construction algorithm Algorithm Build2DRangeTree ( P ) 1. Construct the associated structure: Build a binary search tree T assoc on the set P y of y -coordinates in P 2. if P contains only one point 3. then Create a leaf ν storing this point, and make T assoc the associated structure of ν . 4. else Split P into P left and P right , the subsets ≤ and > the median x -coordinate x mid 5. ν left ← Build2DRangeTree ( P left ) 6. ν right ← Build2DRangeTree ( P right ) 7. Create a node ν storing x mid , make ν left the left child of ν , make ν right the right child of ν , and make T assoc the associated structure of ν 8. return ν 23 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases Fractional cascading Efficiency of construction The construction algorithm takes O ( n log 2 n ) time T ( 1 ) = O ( 1 ) T ( n ) = 2 · T ( n / 2 )+ O ( n log n ) which solves to O ( n log 2 n ) time 24 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases Fractional cascading Efficiency of construction Suppose we pre-sort P on y -coordinate, and whenever we split P into P left and P right , we keep the y -order in both subsets For a sorted set, the associated structure can be built in linear time 25 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases Fractional cascading Efficiency of construction The adapted construction algorithm takes O ( n log n ) time T ( 1 ) = O ( 1 ) T ( n ) = 2 · T ( n / 2 )+ O ( n ) which solves to O ( n log n ) time 26 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases 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? 27 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases Fractional cascading 2D range queries ν p p µ ′ p µ p 28 Computational Geometry Lecture 8: Range trees
Construction Introduction Querying 2D Range trees Higher dimensions Degenerate cases Fractional cascading Query algorithm Algorithm 2DRangeQuery ( T , [ x : x ′ ] × [ y : y ′ ] ) ν split ← FindSplitNode ( T , x , x ′ ) 1. 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 ν then 1DRangeQ ( T assoc ( rc ( ν )) , [ y : y ′ ] ) 7. 8. ν ← lc ( ν ) 9. else ν ← rc ( ν ) 10. Check if the point stored at ν must be reported. Similarly, follow the path from rc ( ν split ) to x ′ ... 11. 29 Computational Geometry Lecture 8: Range trees
Recommend
More recommend