






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







At first sight it seems that database has little to do with geometry. The queries about data in database can be interpreted geometrically.In this case the records in the database are transformed into points in multidimensional space and the queries about records are transformed into the queries over this set of points. Every point in the space will have some information of person associated with it.
Consider the example of the database of personal administration where the general infor- mation of each employee is stored. Consider an example of query where we want to report all employees born between 1950 and 1955, who earns between Rs.3000 and Rs.4000 per month. The query will report all the points that whose frost co-ordinate lies between 1950 and 1955, and second co-ordinate lies between 3000 and 4000.
In general if we are interested in answering queries on d - fields of the records in our database, we transforms the records to points in d-dimensional space. Such a query is called rectangular range query, or an orthogonal range query.
In Range Search problems, the collection of points in space and the query is some standard geometric shape translatable in space. Range search consist either of retrieving ( report problems ) or of counting ( count problems ) all points contained within the query domain.
Figure 1:
2 1 - Dimensional Range Searching.
Let us consider the set of points P=p 1 ,p 2 ......pn. We have to search the range [x,x’] and we have to report which points lies in that range. To solve the problem of range searching we use the data structure known as balanced binary search tree T. The leaves of the T store the points of P and the internal nodes of T will store the splitting values to guide the search. Let xv denote the value stored at each split node v. The left subtree of the node v contains all points smaller than or equal to xv , and the right subtree contains all the points strictly greater than xv.
Let we search with x and x’ in T. μ and μ′^ be the two leaves where the searches end, respectively. Then the points in the interval [x:x’] are stored in the leaves in between μ and μ′^ including μ and μ′. To find the leaves between μ and μ′, we select the tree rooted at nodes v in between the two search paths whose parent are on the search path. To find these nodes we first find the node vsplit where the paths to x and x’ splits.
1: v←root(T). 2: while v is not a leaf and ( x’≤xv or x>xv ). 3: do if x’ ≤ xv 4: then v← lc(v) 5: else v← rc(v) 6: return v
Starting from vsplit we then follow the search path of x. At each node where the path goes left, we report all the leaves in the right subtree, because this subtree is in between the the two search paths. Similarly, we follow the path of x’ and we report the leaves in the left subtree of the node where the path goes right. Finally we check the points stored at the leaves whether they lies in the range [ x , x’ ] or not.
Now we see the query algorithm which uses the subroutine REPORTSUBTREE, which traverses the subtree rooted at the node and reports all the stored at its leaves.This subroutine takes takes the amount of time linear in the number of reported points, this is because the number of internal nodes of any binary tree is less than its internal node.
Procedure name 1DRANGEQUERY(T,[X:X’]) Input: A binary search tree T and a range [x,x’]. Output: All points stored in T that lie in the range.
1: vsplit←FINDSPLITNODE(T,x,x’). 2: if vsplit is a leaf. 3: then check if the point stored at vsplit must be reported. 4: else (* Follow the path to x and report the points in subtree right of the path*). 5: v← lc(vsplit) 6: while v is not a leaf. 7: do if x’ ≤ xv 8: then REPORTSUBTREE(rc(v))
9: v← lc(v) 10: else v← rc(v) 11: check if the point stored at the leaf v must be reported. 12: Similarly, follow the path to x’, reports the points subtree left of the path, and check if the point stored at the leaf where the path ends must be reported.
2.2.1 Complexity analysis
The data structure binary search tree uses O(n) storage and it can be built in O(n log n) time. In worst case all the points could be in query range, so the query time will be Θ(n). The query time of Θ(n) cannot be avoided when we have to report all the points. Therefore we shall give more refined analysis of query time. The refined analysis takes not only n, the number of points in the set P, into account but also k, the number of reported points. As we know that the REPORTSUBTREE is linear in the number of reported points, then the total time spent in all such calls is O(k). The remaining nodes that are visited are the nodes of the search path of x and x’. Because T is balanced, these paths have a length O(log n). The time we spent at each node is O(1), so the total time spent in these nodes is O(log n), which gives a query time of O(logn + k).
2.2.2 Theorem
Theorem 1 Let P be the set of n points in one 1-dimensional space. The set P can be stored in balanced binary search tree, which uses O(n) storage and has O(n logn) construction time, such that the points in the query range can be reported in time O(k + logn), where k is the number of reported points.
3 Kd - Trees.
Consider the 2-dimensional rectangular range searching problem. Let P be the set of n points in the plane. The basic assumption is no two point have same x-coordinate, and no two points have same y-coordinate.
and Pright, the subset of points to right is stored in the right subtree. At the left child of the root we split the Plef t into two subsets with a horizontal line. This is done by finding the median y-coordinate if the points in Plef t. The points below or on it are stored in the left subtree, and the points above are stored in right subtree. The left child itself store the splitting line. Similarly Pright is split with a horizontal line, which are stored in the left and right subtree of the right child. At the grandchildren of the root, we split again with a vertical line. In general, we split with a vertical line at nodes whose depth is even, and we split with horizontal line whose depth is odd.
Let us consider the procedure for constructing the kd-tree. It has two parameters, a set if points and an integer. The first parameter is set for which we want to build kd-tree, initially this the set P. The second parameter is the depth of the root of the subtree that the recursive call constructs. Initially the depth parameter is zero. The procedure returns the root of the kd-tree.
Procedure name BUILDKDTREE(P,depth) Input: A set of points P and the current depth depth. Output: The root of the kd-tree storing P.
1: if P contains only one point 2: then return a leaf storing this point 3: else if depth is even 4: then Split P into two subsets with a vertical line l through the median x-coordinate of the points in P.Let P 1 be the set of points to the left of l or on l, and let P 2 be the set of points to the right of l. 5: else Split P into two subsets with a horizontal line l through the median y-coordinate of the points in P.Let P 1 be the set of points to the below of l or on l, and let P 2 be the set of points above l. 6: vlef t←BUILDKDTREE(P 1 , depth +1). 7: vright←BUILDKDTREE(P 2 , depth +1). 8:Create a node v storing l, make vlef t the left child of v, and make vright the right child of v.
9:return v.
3.1.1 Construction time of 2-dimensional kd-tree
The most expensive step is to find the median. The median can be find in linear time, but linear time median finding algorithms are rather complicated. So first presort the set of points on x-coordinate and then on y-coordinate. The parameter set P is now passed to the procedure in the form of two sorted list. Given the two sorted list it is easy to find the median in linear time. Hence the building time satisfies the recurrence, T(n)=O(1), if n=1, O(n)+2T(dn/2e),if n> 1 which solves to O( n log n). Because the kd-tree is the binary tree, and every leaf and internal node uses O(1) storage, therefore the total storage is O(n).
Lemma 1 A kd-tree for a set of n-points uses O(n) storage and and can be constructed in O(n logn).
The splitting line stored at the root partition the plane in two half-planes. The left child of the root corresponds to the left half-plane, and right child corresponds to right half-plane. The left child of the left child of the root, corresponds to the region bounded to the right by the splitting line stored at the root and bounded from above by the line stored at the left child of the root. In general the region corresponding to the node v is the rectangle, which can be unbounded on one or more sides. It is bounded by splitting lines stored at the ancestors of v.
We denote the region corresponding to the node v by region(v). The region of the root of the kd-tree is whole plane.Observe that a point is stored in subtree rooted at node v if and only if it lies in region(v). We have to search the subtree rooted at v only if the query rectangle intersects the region(v).
Observation 1 We traverse the kd-tree, but visit only nodes whose region is intersected by query rectangle.
Output: All points at leaves below v that lies in range.
1: if v is a leaf 2: then Report the stored at v if it lies in R 3: else if region(lv(c)) is fully contained in R 4: then REPORTSUBTREE(lc(v)) 5: else if region(lc(v)) intersects R 6: then SEARCHKDTREE(lc(v),R) 7: if region(rv(c)) is fully contained in R 8: then REPORTSUBTREE(rc(v)) 9: else if region(lc(v)) intersects R 10: then SEARCHKDTREE(lc(v),R)
The region corresponding to the left child of a node v at even depth can be computed from region(v) as follows : region(lc(v)) = region(v)⋂l(v)lef t where l(v) is the splitting line stored at v, and l(v)lef t^ is the half plane to the left of and including l(v).
Lemma 2 A query with an axis-parallel rectangle in a kd-tree storing n points can be performed in O(√n+k) time, where k is the number of reported points.
First of all note that the time to traverse a subtree and report the points stored in its leaves is linear in number of reported points. Therefore the total time is O(k). It remains to bound the number of nodes visited by the query algorithm that are not in one of the traversed subtrees.For each such node v, the region(v) is intersected by, but not fully contained in the range.To analyze the number of such nodes, we shall bound the number of regions intersected by any vertical line. This will give us an upper bound on the number of regions intersected by the left and right edge of query rectangle. Similarly, we can find the number regions intersected by the top and bottom lines of query rectangle.
3.2.1 Complexity analysis
Let l be the vertical line, and T be a kd-tree. Let l(root(T)) be the splitting line stored at the root of the kd-tree. The line l intersects either the region to the left of l(root(T)) or the region to the right of l(root(T)), but not both.This observation seems to imply that Q(n), the number of intersected regions in the kd-tree storing a set of n points, satisfies the recurrence Q(n)=1+Q(n/2). But this is not true because the splitting lines are horizontal at the children of the root. This means that if the line l intersects the region(lc(root(T))), then it will always intersect the regions corresponding to both children of lc(root(T)). Hence the recurrence we get is incorrect. To write the correct recurrence for Q(n) we go down two steps in tree. Each of the four nodes at depth two in the tree corresponds to a region containing n/4 points. Two of the four nodes correspond to intersected regions, so we have to count the number of intersected regions in these subtrees recursively. More- over, l intersects the regions of the root and of the root and of one of its children. Hence, Q(n) satisfies the recurrence Q(n)=O(1),if n=1, 2+2Q(n/4),if n>1.
This recurrence solves to Q(n)=O(
n). In other words, any vertical line intersects O(
n) regions in kd-tree.Similarly, horizontal line intersects O(
n) regions. The total number of regions intersected by the boundary of a rectangular range query is bounded by O(
n).
Theorem 2 A kd-tree for a set P of n points can be build in O(n logn) time. A rectan- gular range query on the kd-tree takes O(√n + k) time, where k is the number of reported points.
Kd-trees can be also be used for higher-dimensional spaces. The construction algorithm is very similar to the planar case. At the root, we split the set of points into two subsets of roughly the same size by a hyperplane perpendicular to the x1-axis. In other words, at the root the point set is partitioned based on the first coordinate of the points. At the children of the root the partition is based on second coordinate, at nodes at depth two on the third coordinate, and so on, until depth of d-1 we partition on last coordinate. Ai depth d we start all over again, partitioning on first coordinate. The recursion stops only