K-D Trees and Quad Trees

K-D Trees and Quad Trees

K-D Trees and Quad Trees James Fogarty Autumn 2007 Lecture 12 Range Queries • Think of a range query. – “Give me all customers aged 45-55.” – “Give me all accounts worth $5m to $15m” • Can be done in time ________. • What if we want both: – “Give me all customers aged 45-55 with accounts worth between $5m and $15m.” 2 Geometric Data Structures • Organization of points, lines, planes, etc in support of faster processing • Applications – Map information – Graphics - computing object intersections – Data compression - nearest neighbor search – Decision Trees - machine learning 3 k-d Trees • Jon Bentley, 1975, while an undergraduate • Tree used to store spatial data. – Nearest neighbor search. – Range queries. – Fast look-up • k-d tree are guaranteed log2 n depth where n is the number of points in the set. – Traditionally, k-d trees store points in d-dimensional space which are equivalent to vectors in d-dimensional space. 4 Range Queries i i g g h h e e y d f y d f b b a c a c x x Rectangular query Circular query 5 Nearest Neighbor Search i g h e y d f query b a c x Nearest neighbor is e. 6 k-d Tree Construction • If there is just one point, form a leaf with that point. • Otherwise, divide the points in half by a line perpendicular to one of the axes. • Recursively construct k-d trees for the two sets of points. • Division strategies – divide points perpendicular to the axis with widest spread. – divide in a round-robin fashion (book does it this way) 7 k-d Tree Construction (1) i g h e y d f b a c x divide perpendicular to the widest spread. 8 k-d Tree Construction (2) x i s1 g h e y d f b a c s1 x 9 k-d Tree Construction (3) x i s1 g y h s2 e y d f s2 b a c s1 x 10 k-d Tree Construction (4) x i s1 g y h s2 e y d f x s3 s2 b a c s3 s1 x 11 k-d Tree Construction (5) x i s1 g y h s2 e y d f x s3 s2 b a c a s3 s1 x 12 k-d Tree Construction (6) x i s1 g y h s2 e y d f x s3 s2 b a c a b s3 s1 x 13 k-d Tree Construction (7) x i s1 g y h s2 s4 e y d f x y s3 s4 s2 b a c a b s3 s1 x 14 k-d Tree Construction (8) x i s1 g y h s2 s4 e y d f x y s5 s3 s4 s2 b a c a b x s5 s3 s1 x 15 k-d Tree Construction (9) x i s1 g y h s2 s4 e y d f x y s5 s3 s4 s2 b a c a b x s5 s3 s1 x d 16 k-d Tree Construction (10) x i s1 g y h s2 s4 e y d f x y s5 s3 s4 s2 b a c a b x s5 s3 s1 x d e 17 k-d Tree Construction (11) x i s1 g y h s2 s4 e y d f x y s5 s3 s4 s2 b a c a b x g s5 s3 s1 x d e 18 k-d Tree Construction (12) x i s1 g y y h s2 s6 s4 e s6 y d f x y s5 s3 s4 s2 b a c a b x g s5 s3 s1 x d e 19 k-d Tree Construction (13) x i s1 g y y h s2 s6 s4 e s6 y d f x y y s5 s3 s4 s7 s2 b s7 a c a b x g s5 s3 s1 x d e 20 k-d Tree Construction (14) x i s1 g y y h s2 s6 s4 e s6 y d f x y y s5 s3 s4 s7 s2 b s7 a c a b x g c s5 s3 s1 x d e 21 k-d Tree Construction (15) x i s1 g y y h s2 s6 s4 e s6 y d f x y y s5 s3 s4 s7 s2 b s7 a c a b x g c f s5 s3 s1 x d e 22 k-d Tree Construction (16) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f s5 s3 s1 x d e 23 k-d Tree Construction (17) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h s5 s3 s1 x d e 24 k-d Tree Construction (18) k-d tree cell x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 25 2-d Tree Decomposition 2 1 3 26 k-d Tree Splitting sorted points in each dimension 1 2 3 4 5 6 7 8 9 i x a d g b e i c h f y a c b d f e h g i g h • max spread is the max of e fx -ax and iy -ay. y d f • In the selected dimension the middle point in the list splits the b data. a c • To build the sorted lists for the x other dimensions scan the sorted list adding each point to one of two sorted lists. 27 k-d Tree Splitting sorted points in each dimension 1 2 3 4 5 6 7 8 9 i x a d g b e i c h f y a c b d f e h g i g h indicator for each set y d e f abcdef gh i 0 0 1 0 0 1 0 1 1 b a c scan sorted points in y dimension and add to correct set x y a b d e g c f h i 28 k-d Tree Construction Complexity • First sort the points in each dimension. – O(dn log n) time and dn storage. – These are stored in A[1..d,1..n] • Finding the widest spread and equally divide into two subsets can be done in O(dn) time. • We have the recurrence – T(n,d) < 2T(n/2,d) + O(dn) • Constructing the k-d tree can be done in O(dn log n) and dn storage 29 Node Structure for k-d Trees • A node has 5 fields – axis (splitting axis) – value (splitting value) – left (left subtree) – right (right subtree) – point (holds a point if left and right children are null) 30 Rectangular Range Query • Recursively search every cell that intersects the rectangle. 31 Rectangular Range Query (1) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 32 Rectangular Range Query (2) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 33 Rectangular Range Query (3) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 34 Rectangular Range Query (4) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 35 Rectangular Range Query (5) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 36 Rectangular Range Query (6) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 37 Rectangular Range Query (7) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 38 Rectangular Range Query (8) x i s1 y y g s8 h s2 s6 s4 e s6 y d f x y y y s5 s3 s4 s7 s8 s2 b s7 a c a b x g c f h i s5 s3 s1 x d e 39 Rectangular Range Query print_range(xlow, xhigh, ylow, yhigh :integer, root: node pointer) { Case { root = null: return; root.left = null: if xlow < root.point.x and root.point.x < xhigh and ylow < root.point.y and root.point.y < yhigh then print(root); else if(root.axis = “x” and xlow < root.value ) or (root.axis = “y” and ylow < root.value ) then print_range(xlow, xhigh, ylow, yhigh, root.left); if (root.axis = “x” and xlow > root.value ) or (root.axis = “y” and ylow > root.value ) then print_range(xlow, xhigh, ylow, yhigh, root.right); }} 40 k-d Tree Nearest Neighbor Search • Search recursively to find the point in the same cell as the query.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    63 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us