Shmuel Wimer Bar Ilan Univ., School of Engineering Finding Objects Shmuel Wimer Bar Ilan Univ., School of Engineering March 2011 Finding objects
The Problem Here is a floor (can also be space) Objects are located in the room What objects located here? Need efficient way to store objects and apply queries March 2011 Finding objects
Outline 1D range search 2D Kd-Tree 2D Range Tree Accelerating 2D Query Time Layered Range Tree March 2011 Finding objects
1D Range Tree March 2011 Finding objects 59 62 70 80 89 100 105 3 10 19 23 30 37 49 March 2011 Finding objects
1D Range Searching March 2011 Finding objects
searching paths reported points March 2011 Finding objects 59 62 70 80 89 100 105 3 10 19 23 30 37 49 reported points March 2011 Finding objects
March 2011 Finding objects
March 2011 Finding objects
March 2011 Finding objects
Correctness of Query Algorithm March 2011 Finding objects
March 2011 Finding objects
March 2011 Finding objects
March 2011 Finding objects
2D Kd-Tree Key idea: splitting points in alternating directions x’’ y(p) x(p) Ptop Key idea: splitting points in alternating directions Pleft Pright Pbottom March 2011 Finding objects
Vertical and horizontal splits are alternating Vertical and horizontal splits are alternating. Points on split lines belong to lower left regions. Split ends when region contains one point. We assume without loss of generality that all coordinates are distinct. l5 l1 l7 l1 p3 p2 p4 p5 p9 p7 p8 p6 p1 p10 l2 l3 l2 l4 l3 l4 l5 l6 l7 l6 l8 l8 p3 p4 p5 l9 p8 p9 p10 l9 p1 p2 p6 p7 March 2011 Finding objects
March 2011 Finding objects
Construction Time and Storage March 2011 Finding objects
March 2011 Finding objects
Querying Every internal node of Kd-tree stores a region of xy plane defined by the path to root. Regions are defined in O(1) time per node at construction. l1 l2 l3 region (v) v l1 l2 l3 March 2011 Finding objects
l1 l2 l3 region (v) v l1 l2 l3 If node’s region is disjoint to a query rectangle, no point in node’s sub-tree satisfies the query. If query rectangle contains node’s region all points in node’s sub-tree satisfy the query. Otherwise search must proceed. March 2011 Finding objects
p13 p11 p12 p3 p5 p4 p1 p6 p2 p8 p7 p10 p9 p3 p2 p4 p5 p12 p8 p11 p7 March 2011 Finding objects
March 2011 Finding objects
March 2011 Finding objects
Reminder of 1D Range Tree Objects are stored in leaves of balanced binary tree. Internal nodes store search directives. Starts search at root until node’s key falls in range. Left path is issued from forking node down to left end of range. All right sub-trees are reported. Similar for right path. March 2011 Finding objects
2D Range Tree [x`,x``] × [y`,y``] is a range query. In 1D range tree P ( V ), the points stored at leaves of T(V), is called the canonical subset of V. P (root) is all points, P (leaf) is a single point. In 1D range tree points in [x`,x``] are obtained from O (log n) disjoint sub-trees (right sub-trees of left path and left sub-trees of right path). Hence [y`, y``] query further looks into P (V). Construct 2-level data structure: Binary search tree T built on x-coordinate of P. For any node V store P (V) in an associated binary tree Tassoc (V) built on y-coordinate of points. March 2011 Finding objects
March 2011 Finding objects
March 2011 Finding objects
Storage and Construction Time A point p is stored in log n associated trees. p The size of associated tree (binary) is linear in number of stored points Associated trees at a level of primary tree are disjoint, hence total storage consumed at a level is O(n) March 2011 Finding objects
2D range tree requires O(nlogn) storage Construction described in Build_2D_RangeTree is not efficient as it takes O(nlogn) time to build the associated trees, thus resulting in O(nlog2n) time. Total construction time can be reduced to O(nlogn) by pre-sorting of the points by y-coordinate and then building the 2D range tree bottom-up rather than top-down. March 2011 Finding objects
March 2011 Finding objects
Query Time March 2011 Finding objects
Accelerating 2D Query Time S1 – set of objects ordered in an array A1 S2 – subset of S1, ordered in an array A2 Reporting objects of S1 in range 20 to 75 Find 23 by binary search, then traverse and report until 70 in O(logn+k) time Reporting objects from S2 can save binary search ! Every object in A1 points to the smallest object in A2 larger or equal to it. Use NIL if such one does not exist. This is only O(k) time ! 3 10 19 23 30 37 59 62 70 80 100 105 A1 10 19 30 62 70 80 100 A2 March 2011 Finding objects
Observation: The canonical sub-set P(LSON[V]) and the canonical sub-set P(RSON[V]) are canonical sub-set of P(V) 17 8 52 5 15 33 58 21 49 33 30 41 95 52 23 58 59 67 89 93 70 2 19 5 80 7 10 8 37 12 3 15 99 17 62 2 7 12 21 41 67 March 2011 Finding objects
Implementation T is a range tree of P of n points. P (V) is canonical point set stored in leaves of T(V) Instead of storing P(V) in associated tree sorted by y-coordinate, it is stored in an array A(V) sorted by y-coordinate. Each entry in A(V) maintains two pointers: One to an entry in A(LSON[V]) (the smallest equal or larger) One to an entry in A(RSON [V]) (the smallest equal or larger) Reporting from node V is done directly from A(V) rather than by T(V) traversal. March 2011 Finding objects
Layered Range Tree 3 10 23 19 30 37 59 49 62 70 89 80 95 99 3 10 19 37 62 80 99 23 30 59 49 70 89 95 10 19 37 80 3 62 99 23 30 49 95 59 70 89 19 80 10 37 3 99 62 30 49 23 95 59 70 89 19 80 37 10 3 99 30 49 95 23 89 70 March 2011 Finding objects
Performance of Layered Range Tree [x`,x``]×[y`,y``] is a range query. Perform x-range search on primary tree T to determine left and right paths down to leaves x` and x`` in O(logn) time. Points of canonical subsets adhere [x`,x``]. This takes O(logn). We find in A[Vsplit] the smallest entry of [y`,y``] in O(logn) time. Reports take place from A[V ] in right nodes of left path and left nodes of right path. The smallest entry of [y`,y``] is found in constant time by the pointer to parents propagating up to Vsplit. March 2011 Finding objects
Storage is O(nlogn) same as 2D range tree. Report time per node is O(1+kV), yielding a total of O(logn+k), compared to O (log2n+k) in 2D range tree. Storage is O(nlogn) same as 2D range tree. Construction time is O(nlogn) as in 2D range tree. Sort initially all objects by y-coordinate stored in an array with pointers to objects in primary binary tree T. Every split to left and right sub-trees in T is followed by linear traversal of A (Vparent) and split into sorted A(Vleft) and A (Vright) according to the split made by x-coordinate in T. Total work per level of T to construct all A(V) is O(n). March 2011 Finding objects