
Overview Types of data. Points, lines, circles, rectangles, planes, polygons, ... This lecture. Intersection among N objects. Geometric Search Example problems. • 1d range searching. • 2d range searching. • Finding intersections among h-v line segments. ‣ range search • Find intersections among axis-aligned rectangles. ‣ space partitioning trees ‣ intersection search References: Algorithms in C (2nd edition), Chapters 26-27 http://www.cs.princeton.edu/algs4/73range http://www.cs.princeton.edu/algs4/74intersection th Algorithms in Java, 4 Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · April 23, 2008 10:28:07 AM 2 1D range search Extension of ordered symbol table. • Insert key-value pair. • Search for key k. • Rank: how many keys less than k? • Range count: how many keys between k1 and k2? • Range search: find over all keys between k1 and k2. ‣ range search Application. Database queries. ‣ space partitioning trees insert B B ‣ intersection search Geometric interpretation. insert D B D Keys are point on a line. insert A A B D • insert I A B D I • How many points in a given interval? insert H A B D H I insert F A B D F H I insert P A B D F H I P count G to K 2 search G to K H I 3 4 1D range search: implementations BST: range search Ordered array. Slow insert, binary search for lo and hi to find range. Range search. Find all keys between lo and hi? Hash table. No reasonable algorithm (key order lost in hash). • Recursively find all keys in left subtree (if any could fall in range). • Check key in current node. • Recursively find all keys in right subtree (if any could fall in range). data structure insert rank range count range search searching in range [F..T] ordered array N log N log N R + log N red keys are used in compares hash table 1 N N N but are not in the range S BST log N log N log N R + log N E X A R N = # keys C H R = # keys that match M L P black keys are in the range Range search in a BST Goal. Modify standard BST to support efficient range queries. Worst-case running time. R + log N (assuming BST is balanced). 5 6 inserting L BST: maintaining node counts S BST: maintaining node counts E X A R BST. In each node x, maintain number of nodes in tree rooted at x. BST. In each node x, maintain number of nodes in tree rooted at x. C H M node count N 8 search for L ends S at this null6 link 1 inserting L E X 2 3 S S A R Updating node counts after rotation. 1 2 E X E X C H A 1 R A R M C H h C H u h M v M a+b+c+2 a+b+c+2 1 h = rotL(u) Updating node counts after insertion. create new node L search for L ends v 8 u at this null link a 9 b+c+1 c S 2 E 5 S a+b+1 A C C 7 R E X 1 2 E 2 4 X A b c a b A R A H 1 1 X R 3 B C A B C H C M H S2 M M reset links and 1 L create new node L increment counts on the way up 9 S Two BSTsInsertion that represent into a BST 7 the same key sequence 7 8 E 4 X A R 3 C H 2 M reset links and increment counts L on the way up Insertion into a BST BST: range count 2D orthogonal range search Rank. How many keys < k ? Extension of ordered symbol-table to 2D keys. • Insert a 2D key. public int rank(Key key) node count N 8 S Search for a 2D key. { return rank(key, root); } 6 1 • 2 E 3 X • Range count: how many keys lie in a 2D range? private int rank(Key key, Node x) A 1 2 R Range search: find all keys that lie in a 2D range? { C H 1 • if (x == null) return 0; M int cmp = key.compareTo(x.key); if (cmp < 0) return rank(key, x.left); Applications. Networking, circuit design, databases. else if (cmp > 0) return 1 + size(x.left) + rank(key, x.right); 8 else return size(x.left); 2 E 5 } C R 1 2 2 A H 1 1 X Geometric interpretation. M S • Keys are point in the plane. Range count. How many keys between lo and hi? • How many points in a given h-v rectangle. Two BSTs that represent the same key sequence public int rangeCount(Key lo, Key hi) { if (contains(hi)) return rank(hi) - rank(lo) - 1; else return rank(hi) - rank(lo); } 9 10 2D orthogonal range search: grid implementation 2D orthogonal range search: grid implementation costs Grid implementation. [Sedgewick 3.18] Space-time tradeoff. • Divide space into M-by-M grid of squares. • Space: M2 + N. • Create list of points contained in each square. • Time: 1 + N / M2 per square examined, on average. • Use 2D array to directly index relevant square. • Insert: insert (x, y) into corresponding square. Choose grid square size to tune performance. • Range search: examine only those squares that intersect 2D range query. • Too small: wastes space. • Too large: too many points per square. • Rule of thumb: √N-by-√N grid. Running time. [if points are evenly distributed] RT RT Initialize: O(N). • M ≈ √N • Insert: O(1). • Range: O(1) per point in range. LB LB 11 12 Clustering Clustering Grid implementation. Fast, simple solution for well-distributed points. Grid implementation. Fast, simple solution for well-distributed points. Problem. Clustering a well-known phenomenon in geometric data. Problem. Clustering a well-known phenomenon in geometric data. Ex. USA map data. 13,000 points, 1000 grid squares Lists are too long, even though average length is short. Need data structure that gracefully adapts to data. half the squares are empty half the points are in 10% of the squares 13 14 Space-partitioning trees Use a tree to represent a recursive subdivision of k-dimensional space. Quadtree. Recursively divide plane into four quadrants. kD tree. Recursively divide k-dimensional space into two half-spaces. BSP tree. Recursively divide space into two regions. ‣ range search ‣ space partitioning trees ‣ intersection search Grid Quadtree kD tree BSP tree 15 16 Space-partitioning trees: applications Quadtree Applications. Idea. Recursively divide plane into 4 quadrants. • Ray tracing. Implementation. 4-way tree (actually a trie). • Flight simulators. (0..., 1...) • N-body simulation. b c a h • Collision detection. a d Astronomical databases. • d e f g • Adaptive mesh generation. • Accelerate rendering in Doom. e b c • Hidden surface removal and shadow casting. f g h public class QuadTree (01.., 00..) { private Quad quad; private Value val; private QuadTree NW, NE, SW, SE; } Grid Quadtree kD tree BSP tree Benefit. Good performance in the presence of clustering. Drawback. Arbitrary depth! 17 18 Quadtree: 2D range search N-body simulation Range search. Find all keys in a given 2D range. Goal. Simulate the motion of N particles, mutually affected by gravity. • Recursively find all keys in NE quad (if any could fall in range). • Recursively find all keys in NW quad (if any could fall in range). • Recursively find all keys in SE quad (if any could fall in range). • Recursively find all keys in SW quad (if any could fall in range). b c a h a d d e f g e b c f g h Gm m Typical running time. R + log N. Brute force. For each pair of particles, compute force. F = 1 2 r2 19 20 Subquadratic N-body simulation Barnes-Hut algorithm Key idea. Suppose particle is far, far away from cluster of particles. Algorithm. • Treat cluster of particles as a single aggregate particle. • Build quadtree with N particles as external nodes. • Compute force between particle and center of mass of aggregate particle. • Store center-of-mass of subtree in each internal node. • To compute total force acting on a particle, traverse tree, but stop as soon as distance from particle to quad is sufficiently large. 21 22 Curse of dimensionality 2D trees Range search / nearest neighbor in k dimensions? Recursively partition plane into two halfplanes. Main application. Multi-dimensional databases. Implementation. BST, but alternate using x- and y-coordinates as key. 3D space. Octrees: recursively divide 3D space into 8 octants. • Search gives rectangle containing point. 100D space. Centrees: recursively divide into 2100 centrants??? • Insert further subdivides the plane. p p points points left of p right of p even levels q q points points Raytracing with octrees below q above q http://graphics.cs.ucdavis.edu/~gregorsk/graphics/275.html odd levels 23 24 2D tree: 2D range search kD Trees Range search. Find all keys in a given 2D range. kD tree. Recursively partition k-dimensional space into 2 halfspaces. • Check if point in node lies in given range. • Recursively find all keys in left/top subdivision (if any could fall in range). Implementation. BST, but cycle through dimensions ala 2D trees. • Recursively find all keys in left/top subdivision (if any could fall in range). p level ≡ i (mod k) points points th whose ith whose i coordinate coordinate is less than p’s is greater than p’s Efficient, simple data structure for processing k-dimensional data. • Widely used. Worst case (assuming tree is balanced).
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages12 Page
-
File Size-