CMPS 3130/6130 Computational Geometry Spring 2015 Orthogonal Range Searching II Carola Wenk 4/13/15 CMPS 3130/6130 Computational Geometry 1
Orthogonal range searching Input: A set P of n points in d dimensions Task: Process P into a data structure that allows fast orthogonal range queries. Given an axis-aligned box (in 2D, a rectangle) • Report on the points inside the box: • Are there any points? • How many are there? • List the points. 4/13/15 2 CMPS 3130/6130 Computational Geometry
Orthogonal range searching: KD-trees Let us start in 2D: Input: A set P of n points in 2 dimensions Task: Process P into a data structure that allows fast 2D orthogonal range queries: Report all points in P that lie in the query rectangle [ x,x’ ] [ y,y’ ] y' y x' x 4/13/15 3 CMPS 3130/6130 Computational Geometry
KD trees Idea: Recursively split P into two sets of the same size, alternatingly along a vertical or horizontal line through the median in x - or y -coordinates. l 1 1 l 5 {p 1 , p 2 , p 3 , p 4 ,p 5 } {p 6 , p 7 , p 8 , p 9 ,p 10 } l 1 l 7 l 2 l 3 2 3 p 4 p 9 p 5 {p 4 ,p 5 } {p 6 , p 7 , p 8 } {p 9 ,p 10 } {p 1 , p 2 , p 3 } p 10 l 4 l 5 l 6 l 7 4 5 6 7 l 2 p 2 l 3 {p 6 , p 7 } {p 1 , p 2 } p 7 l 8 l 8 p 3 p 4 l 9 p 1 p 5 p 8 p 9 p 8 8 p 10 9 p 3 l 9 p 6 p 1 p 2 p 7 p 6 l 4 l 6 4/13/15 4 CMPS 3130/6130 Computational Geometry
BuildKDTree Idea: Recursively split P into two sets of the same size, alternatingly along a vertical or horizontal line through the median in x - or y -coordinates. l 5 l 1 l 7 p 4 p 9 p 5 p 10 l 2 p 2 l 3 p 7 l 8 p 1 p 8 p 3 l 9 p 6 l 4 l 6 l 1 1 l 2 l 3 2 3 l 4 l 5 l 6 l 7 4 5 6 7 l 8 p 3 p 4 l 9 p 5 p 8 p 9 8 p 10 9 p 1 p 6 p 2 p 7 4/13/15 5 CMPS 3130/6130 Computational Geometry
BuildKDTree Analysis • Sort P separately by x - and y -coordinate in advance • Use these two sorted lists to find the median • Pass sorted lists into the recursive calls • Runtime: Storage: O( n ), because it is a binary tree on n leaves • 4/13/15 6 CMPS 3130/6130 Computational Geometry
Regions l 1 l 5 l 1 l 7 p 4 l 2 l 3 p 9 p 5 p 10 l 2 l 4 l 5 l 6 l 7 p 2 l 3 p 7 l 8 p 1 p 8 =v l 8 p 3 p 4 l 9 p 5 p 8 p 9 p 10 p 3 l 9 p 6 p 6 p 1 p 7 p 2 l 4 l 6 region(v) • lc(v)=left_child(v) • region(lc(v)) = region(v) l(v) left Can be computed on the fly in constant time 4/13/15 7 CMPS 3130/6130 Computational Geometry
l 1 SearchKDTree l 2 l 3 l 4 l 5 l 7 l 6 l 8 p 3 l 9 p 4 p 5 p 8 p 9 p 10 p 1 p 6 l 10 p 2 10 l 11 l 12 11 12 l 5 l 1 l 7 p 11 p 12 p 7 p 13 p 4 p 9 p 5 p 10 l 2 p 7 p 2 l 3 l 8 p 13 l 11 How many nodes p 1 p 8 l 12 p 11 p 12 p 3 l 10 does a search touch? p 6 l 9 l 4 l 6 4/13/15 8 CMPS 3130/6130 Computational Geometry
SearchKDTree Analysis Theorem: A kd-tree for a set of n points in the plane can be constructed in O( n log n ) time and uses O( n ) space. A rectangular range query can be answered in time, where k = # reported points. (Generalization to d dimensions: Also O( n log n ) �� � construction time and O( n ) space, but � query time.) 4/13/15 9 CMPS 3130/6130 Computational Geometry
SearchKDTree Analysis Proof Sketch: Sum of # visited vertices in ReportSubtree is O( k ) • # visited vertices that are not in one of the reported • subtrees = O(# regions(v) intersected by a query line) Consider intersections with a vertical line only. Let Q ( n ) = # intersected regions in kd-tree of n points whose root contains a vertical splitting line Q (n) = 2 + 2 Q ( n /4), for n >1 l 3 l 1 l 1 Q (n) = O( ) l 2 l 2 l 3 l 4 l 4 4/13/15 10 CMPS 3130/6130 Computational Geometry
Summary Orthogonal Range Searching Range trees Query time: O( k + log d-1 n ) to report k points (uses fractional cascading in the last dimension) Space: O( n log d – 1 n ) Preprocessing time: O( n log d – 1 n ) KD-trees �� � Query time: � to report k points Space: O( n ) Preprocessing time: O( n log n ) 4/13/15 11 CMPS 3130/6130 Computational Geometry
Recommend
More recommend