K-D Trees! Binary Space Partitioning Trees! L! N! Nearest-Neighbor Search!
Total Page:16
File Type:pdf, Size:1020Kb
BST Range Search! T: Instead of finding an exact match, find all items whose keys fall k! between a range of values, e.g., between m and z, inclusive! d! p! ! Example applications:! Lecture 7: BST Range Search! a! f! m! z! k-d Trees! Binary Space Partitioning Trees! l! n! Nearest-Neighbor Search! BST Range Search: Algorithm! BST Range Search: Example! results:! z! void! searchrange, subtreerange! rangesearch(T, [m-z], [a-z], results)! n! rangesearch(Link root, Key searchrange[], (T’s range is “whole universe”)! m! Key subtreerange[], List results)! is k in [m-z]?! [a-z] ! p! does [a-j] overlap [m-z]?! T: k! 1. if root is in search range," does [l-z] overlap [m-z]?! T: [a-j] [l-z] add root to results! k! search p’s subtree! 2. compute range of left subtree! is p in [m-z]? results p! d! p! does [l-o] overlap [m-z]?! 3. if search range covers all or" d! p! search m’s subtree! [l-o] [q-z] part of left subtree, search left! is m in [m-z]? results m! 4. compute range of right subtree! does [l-l] overlap [m-z]?! a! f! m! z! 5. if search range covers all or" a! f! m! z! does [n-o] overlap [m-z]?! [l-l] [n-o] part of right subtree, search right! search n’s subtree! is n in [m-z]? results n! l! n! 6. return results! l! n! does [q-z] overlap [m-z]?! ! search z’s subtree! (Other traversal orders are also ok)! is z in [m-z]? results z! BST Range Search: Support Functions! BST Range Search: Other Details ! 1. if root is in search range, i.e.,! root->key <= searchrange[MAX], and! How to express range when the keys are floats?! root->key >= searchrange[MIN]! • be careful with numerical precision and floating point add node to results! errors [one of this week’s discussion topic]! ! 2. compute subtree’s range: replace upper (lower) bound How to support duplicate keys?! of left (right) subtree’s range by root->key-1 (+1)! • be consistent about using ≤ or <" • if ≤, the range for the left subtree would be closed, e.g., 3. if search range covers all or part of subtree’s range," [–∞, 0], and the range for the right subtree half open, search subtree! e.g., (0, +∞]! • each subtree covers a range of key values! • compute overlap between subtree’s range and search range! • no overlap if either" searchrange[MAX] < subtreerange[MIN] or searchrange[MIN] > subtreerange[MAX]! Multidimensional Search! k-d Trees! A k-d tree is a binary search tree (not covered in Example applications:! textbook, link to original 1975 paper on syllabus)! ! At each level of the k-d tree, keys from a different search ! dimension is used as the discriminator! ! • keys for any given level are all from the same dimension indicated by ! the discriminator! • nodes on the left subtree of a node have keys with value < the ! node’s key value along this dimension! A k-d tree can handle all these queries with O(log n) • nodes on the right subtree have keys with value > the node’s key value along this dimension! insert and search times (it can also handle partial, range, and approximate matches)! We cycle through the dimensions as we go down the tree! • for example, given keys consisting of x- and y-coordinates, level 0 could discriminate by the x-coordinate, level 1 by the y-coordinate, level 2 again by the x-coordinate, etc.! k-d Trees: Example! k-d Tree Insert! void kdTree::! Given points in a Cartesian plane on the left," insert(Link &root, Item newitem, int disc)! {! a corresponding k-d tree is shown on the right! if (root == NULL) {! disc! y root = new Node(newitem);! T: x! return;! E(40, 85)! G(70, 85)! A! ! }! if (newitem.key[disc] < root->item.key[disc]) // or <=! B(10, 70)! y! insert(root->left, newitem, (disc+1)%dim);! B! C! else if (newitem.key[disc] > root->item.key[disc])! H(30, 60)! ! insert(root->right, newitem, (disc+1)%dim);! A(50, 50)! }! D! E! F! G! x! ! If new item’s key is smaller than root’s along the dimension indicated by the discriminator, recursive call on left subtree! C(80, 15)! ! D(25, 20)! H! y! else recursive call on right subtree! F(60, 10)! x In both cases, switch the discriminator before traversing" 0 100 the next level of the tree, cycling through the dimensions! k-d Tree Search! k-d Tree Remove! To remove a node on a level with Search works similarly to insert, using a discriminator along dimension j:! discriminator to cycle through the dimensions as • if the node is a leaf, remove it! one recurses down the levels: O(log n) time! • else if node has right subtree," find the j-minimum node in the right subtree! The tree we built was nicely balanced! • replace node with j-minimum node and repeat • if nodes are inserted randomly, on average we will get a until you reach a leaf, then remove the leaf! balanced tree: O(log n) time! • else find the j-maximum node in the left subtree, • if nodes inserted are sorted, recursively insert the median replace, repeat, remove! of the range to build a balanced tree: O(n log n) time! • j-minimum means minimum along the j dimension, analogously j-maximum! • O(log n) time! k-d Tree Remove Example! k-d Tree Remove Example! delete (35,60)! 35,60! x! 50,30! x! 50,30! x! 50,30! x! 20,45! 60,80! y! 20,45! 60,80! y! 20,45! 60,80! y! 20,45! 60,80! y! 10,35! 80,40! x! 10,35! 80,40! x! 10,35! 80,40! x! 10,35! 80,40! x! 20,20! 50,30! 90,60! y! 20,20! 50,30! 90,60! y! 20,20! 50,30! 90,60! y! 20,20! 60,20! 90,60! y! 60,20! x! replacement! x! 60,20! replacement! x! 60,20! replacement! x! 60,20! replacement! x-minimum! y-maximum! y-maximum! x-minimum! 70,10! y! y! 70,10! y! 70,10! y! 70,10! delete (50,30)! k-d Tree Remove Example! k-d Tree Remove Summary! delete (35,60)! 50,30! x! 50,30! x! 35,60! x! 50,30! x! 20,45! 60,80! y! 20,45! 60,80! y! 20,45! 60,80! y! 20,45! 60,80! y! 10,35! 80,40! x! 10,35! 80,40! x! 10,35! 80,40! x! 10,35! 80,40! x! 20,20! 60,20! 90,60! y! 20,20! 60,20! 90,60! y! 20,20! 50,30! 90,60! y! 20,20! 60,20! 90,60! y! x! 60,20! x! 70,10! leaf node, can 60,20! x! x! 70,10! replacement! be deleted! y! 70,10! 70,10! 70,10! y! delete (60,20)! Multidimensional Range Search! k-d Tree Range Search! void! Example applications:! kdTree::rangesearch(Link root,int disc, Key searchrange[], Key subtreerange[], List results)! • cycle through the dimensions as we traverse down the tree, same as with kdTree::insert()! What would the kdTree::rangesearch()" • searchrange[] holds 2 values (min, max) per dimension! function signature be?! • subtree’s ranges are defined for all dimensions! • What are its formal arguments?! • for 2D, both searchrange[] and" • What would it return?! subtreerange[] define a rectangle! • for dimension j’s range, subtreerange[2*j] holds the lower bound, subtreerange[2*j+1] holds the upper bound" ⇒ these need to be updated as we go down the levels! Example: Binary Space Axis-Aligned BSP Tree: Idea! Partitioning (BSP) Trees! Splitting plane aligned to x, y, or z axis! Axis-aligned BSP tree: a k-d tree used for spatial Minimal! Split along! range search, with the following distinctions:! box! plane! • items are stored in leaf nodes only! • other internal nodes store only the coordinates used to partition space! • an item that spans multiple partitions are stored in all corresponding leaf nodes! Example usages:! Split along! Split along! • occlusion culling and collision detection in computer plane! plane! graphics, computer games, robot motion planning! Tomas Akenine-Mőller © 2002! Axis-Aligned BSP Tree: Build! Example Use: Collision Detection! Plane Blue wants to get to B D E f 2 0 object e! a b 1a 1b Plane 1b Blue draws a straight line Plane 1a Plane 0 from itself to e! g A B C 2 c A C Now it needs to know d e D E which objects in the room could potentially obstruct h it if it follows this straight m line! j l Each internal node holds a divider plane! What is the brute force k Leaves hold geometry! way of finding potentially i n obstructing objects?! Tomas Akenine-Mőller © 2002! Collision Detection with BSP! Collision Detection with BSP! x=30 x=52 Algorithm:! 1 3 x=30 x=52 1 3 In this case, we use:! A B f C • use a plane to divide A B f C a 4 x=30, y=30, x=52, y=45! b y=45x=45" a 4 space into 2 parts! b y=45x=45" with the resulting BSP:! • 1 1 1 1 1 objects whose j- D x=30 x=30 x=30 x=30 x=30 D g coordinate is smaller than g c that of the partition go to d 2 2 2 2 2 c A A A A e d y=30 A y=30 y=30 y=30 y=30 the left subtree! e a, b , g a, b , ga, b , g a, b , g a, b , g h h, i, j, k h, i, j, kh, i, j, k h, i, j, k h, i, j, k h 3 3E 3 E3 E 3 E E • objects with j-coordinate x=52 x=52 x=52l x=52l l x=52 l m 2 l n n n n n larger than the partition’s y=30 m 2 j E E y=30 j B go to the right subtree! B B4 B4 4 B 4 4 l d y=45d d y=45 y=45d y=45 y=45 l d k • repeat for the two k i n C C C D C D partitions, cycling through i n D D C D f c f f c fc fc c e e e e e the coordinates! m m m m m Collision Detection with BSP! Collision Detection with BSP! x=30 x=52 1 1 1 1 1 1 3 Blue searches the BSP and x=30 x=30 x=30 x=30 x=30 Blue found out that c " A B f C found that at x=30, e is to its is in its way to e! a 4 right, so it can ignore objects b y=45x=45" 2 2 A 2 2 2 So it needs to navigate , , , , , , A A A A y=30 y=30 a b g h i j k! y=30 y=30 y=30 D a, b , g a, b , ga, b , g a, b , g around a, b , g c! g At y=30, e is to its left,h, i, j,so k it ✗h, i, j, kh, i, j, k h, i, j, k h, i, j, k Assumes Blue can query c can ignore objects l, n! 3 3E 3 E3 E 3 E E d x=52 x=52 e x=52 x=52l x=52l l l l object c for its dimension, n It now knows that it would ✗n n n n it now knows that it " h potentially have to navigate B needs to go to (52, 45) " m 2 B B4 B4 4 B 4 4 around , , , and d y=45 y=45 E y=30 d f c m! d y=45d y=45d d y=45