Hierarchical Representations of Collections of Small Rectangles

HANAN SAMET Computer Science Department, Center for Automation Research, and Institute for Advanced Computer Studies, University of Maryland, College Park, Maryland 20742

A tutorial survey is presented of hierarchical data structures for representing collections of small rectangles. Rectangles are often used as an approximation of shapes for which they serve as the minimum rectilinear enclosing object. They arise in applications in cartography as well as very large-scale integration (VLSI) design rule checking. The different data structures are discussed in terms of how they support the execution of queries involving proximity relations. The focus is on intersection and subset queries. Several types of representations are described. Some are designed for use with the plane- sweep paradigm, which works well for static collections of rectangles. Others are oriented toward dynamic collections. In this case, one representation reduces each rectangle to a point in a higher multidimensional space and treats the problem as one involving point data. The other representation is area based-that is, it depends on the physical extent of each rectangle.

Categories and Subject Descriptors: B.7.2 [Integrated Circuits]: Design Aids--layout; placement and routing; E.l [Data]: Data Structures-trees; E.5 [Data]: Files- organization/structure; F.2.2 [Analysis of Algorithms and Problem Complexity]: Nonnumerical Algorithms and Problems-geometrical problems and computations; routing and layout; H.2.2 [Database Management]: Physical Design-access methods; H.3.2 [Information Storage and Retrieval]: Information Storage-file organization; 1.2.10 [Artificial Intelligence]: Vision and Scene Understanding-representations, data structures, and transforms; 1.3.5 [Computer Graphics]: Computational Geometry and Object Modeling-curve, surface, solid, and object representations; geometric algorithms, Languages, and systems; J.6 [Computer Applications]: Computer-Aided Engineering- computer-aided design (CAD) General Terms: Algorithms, Theory Additional Key Words and Phrases: Cartography, geographic information systems, interval trees, hierarchical data structures, multidimensional data structures, plane-sweep methods, priority search trees, , R-trees, rectangle intersection problem, rectangles, representative points, segment trees, VLSI design rule checking

INTRODUCTION minimum enclosing object. Of course, the exact boundaries of the object are also The problem of how to represent collec- stored; but usually they are only accessed tions of small rectangles arises in many if a need for greater precision exists. For applications. The most common example is example, bounding rectangles can be used when a rectangle is used to approximate in cartographic applications to approxi- other shapes for which it serves as the mate objects such as lakes, forests, and

Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 0 1988 ACM 0360-0300/88/1200-0271$01.50

ACM Computing Surveys, Vol. 20, No. 4, December 1988 272 ’ Hanan Samet

CONTENTS and the sizes of the rectangles are several orders of magnitude smaller than the space from which they are drawn. In this tutorial we focus primarily on how INTRODUCTION to represent a large collection of rectangles 1. CHOOSING A REPRESENTATION as is common in VLSI applications. Our 2. PLANE-SWEEP METHODS techniques, however, are equally applicable 2.1 Segment Trees 2.2 Interval Trees to other domains. We assume that all rec- 2.3 Priority Search Trees tangles are positioned so that their sides 2.4 Applications are parallel to the x and y coordinate axes. 3. POINT-BASED METHODS We first give a general introduction to the 4. AREA-BASED METHODS 4.1 MX-CIF Quadtrees problem domain and to the tasks whose 4.2 Multiple Block Representations solutions such representations are intended 4.3 R-Trees to facilitate. The representations and issues 5. CONCLUDING REMARKS that we discuss are also common to multi- ACKNOWLEDGMENTS attribute data. In order to compare the REFERENCES different representations, we will use the collection of rectangles given in Figure 1 and the collection of points correspond- ing to the locations of the cities given in hills [Matsuyama et al. 19841. In such Figure 2. a case, the approximation gives a rough Initially, we present representations that indication of the existence of an object. are designed for use with the plane-sweep This is useful in processing spatial queries solution paradigm [Preparata and Shamos in a geographic information system. Such 1985; Shamos and Hoey 19761. This solu- queries can involve the detection of over- tion consists of two passes. The first pass lapping areas, a determination of proxim- sorts the data along one dimension, and the ity, and so on. Another application is the second pass processes the result of the first detection of cartographic anomalies that pass. In this case, the rectangles are repre- require further resolution when a map is sented by the intervals that form their printed. boundaries. Use of this paradigm requires Rectangles are also used in the process maintaining a changing of intervals of very large-scale integration (VLSI) de- whose endpoints are known in advance (a sign rule checking as a model of chip com- by-product of the initial sorting pass). One ponents for the analysis of their proper representation that we discuss is the seg- placement. Again, the rectangles serve as ment . The represents the minimum enclosing objects. This process intervals as unions of atomic segments includes tasks such as determining whether whose endpoints are members of the collec- components intersect and ensuring the sat- tion of rectangles. It turns out that the isfaction of constraints involving factors as segment tree is suboptimal with respect to minimum separation and widths. These its space requirements, and this leads to tasks have a practical significance in that the development of the interval tree. The they can be used to avoid design flaws, and interval tree does not decompose each in- so i n. terval into segments and hence reduces the The size of the collection depends on the space requirements without increasing the application; it can vary tremendously. For execution time. In essence, the interval tree example, in cartographic applications the is a balanced of suitably chosen number of elements in the collection is points from the space spanned by the in- usually small, and frequently the sizes of tervals so that interval I is associated with the rectangles are of the same order of the point P that is closest to the root such magnitude as the space from which they that P is in I. are drawn. On the other hand, in VLSI Our discussion of the plane-sweep solu- applications, the size of the collection is tion paradigm uses the rectangle intersec- quite large (e.g., millions of components), tion problem as a motivating example. For

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 273

Y

0 0 IO 20 30 40 (a) (b)

Figure 1. (a) A collection of rectangles. Members of the collection are designated by solid lines and labeled alphabetically (A-G). Query rectangles are designated by broken lines and labeled numerically (l-3). P and Q are query points. (b) The locations of the endpoints of the rectangles and the points in (a). For a rectangle, x,, and .xRcorrespond to its left and right boundaries, respectively, and yH and yr correspond to its bottom and top boundaries, respectively. For a point, xL and yB are its x and y coordinate values,

The remaining representations are for a dynamic environment. They are differen- tiated by the way in which each rectangle is represented. The first type of represen- tation reduces each rectangle to a point in a higher (usually) dimensional space and then treats the problem as if it involves a collection of points. The second type is region based in the sense that the subdivi- sion of the space from which the rectangles are drawn depends on the physical extent of the rectangle-it does not just treat a Figure 2. A collection of points. rectangle as one point. A number of these region-based representations make use of variants of a data structure commonly re- this task, as well as many other tasks, the ferred to as a quadtree. Interestingly, these plane-sweep paradigm leads to worst-case quadtree representations are very similar optimal solutions in time and space. Rep- to the segment and interval trees that are resentations such as the segment and in- used with the plane-sweep paradigm. More- terval tree, however, are designed primarily over, we observe that the quadtree serves for formulations of the tasks in a static as a multidimensional sort and the process environment. This means that the identity of traversing it is analogous to a plane of all of the rectangles must be known a sweep in multiple dimensions. We conclude priori if the worst-case time and space with a discussion of some representations bounds are to hold. Furthermore, for some that are more commonly used in a problem tasks, the addition of a single object to the domain that involves a relatively small database may force the reexecution of the number of rectangles (e.g., as found in a algorithm on the entire database. cartographic application).

ACM Computing Surveys, Vol. 20, No. 4, December 1988 274 . Hanan Samet

(a) (b) Cc)

Level 3

Level 2

Level I

Level 0 7 8 9 IO 15 16 17 IS (4

Figure 3. An example (a) region, (b) its binary array, (c) its maximal blocks (blocks in the region are shaded), and (d) the corresponding quadtree.

Since the focus of this tutorial is on sentations are the region quadtree [Hunter hierarchical data structures, we will find 1978; Klinger 1971; Samet 19841 (really a ourselves making frequent comparisons ) and the point quadtree [Finkel and with quadtree like data structures. Hence Bentley 19741. in the following we briefly review them. The region quadtree is used to represent The term quadtree is used to describe a region data and is based on the successive class of hierarchical data structures whose subdivision of an image into four equal-size common property is that they are based on blocks until each block is of a uniform color the principle of recursive decomposition of or type. Figure 3 is an example of a binary space (similar to divide and conquer meth- image consisting of a region and its region ods [Aho et al. 19741). They can be differ- quadtree representation. The region quad- entiated on (1) the type of data they are tree is based on a regular decomposition. used to represent and (2) the principle guid- On the other hand, the point quadtree is a ing the decomposition process. The decom- representation of multiattribute data where position may be into equal parts on each the subdivision lines are determined by the level, termed a regular decomposition, or it input data. In essence, it is a multidimen- may be governed by the input. The second sional binary . For example, Fig- distinction is very similar to that between ure 4 is the point quadtree for the data in a trie [Fredkin 19601 and a tree, respec- Figure 2 when the cities are inserted in the tively. The most common quadtree repre- order in which they appear in Figure 2. See

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 275

(100.100)

(60,751 ( p,TORONTO

I (80,65) BUFFALO

(5.451 DENVER (35,4D) CHICAGO

(25,351 OMAHA (85, 15) (50, IO) ATLANTA MOBILE (90,511(90,5) 1 I I I MIAMI 1 (0,Ol (lOO,O) x-

CHICAGO

DENVER TORONTO OMAHA MOBILE

BUFFALO ATLANTA MIAMI

Figure 4. A point quadtree corresponding to the points in Figure 2.

Samet [ 1984,1989a, 1989b] for more details for the objects. Second, we must decide on quadtrees and related hierarchical data whether (and if so, how) to organize the structures. objects that make up the collection. During our decision process we will be confronted 1. CHOOSING A REPRESENTATION with many of the same issues that arise in the representation of multiattribute data. In choosing a representation for a collec- For example, we must decide between a tion of objects’ we are faced with two issues. static and a dynamic representation, be- First, we must choose a representation tween making use of comparative search ’ In this section we will speak of arbitrary objects and address computation, and between re- although the subsequent discussion will be restricted trieval on one key that is a combination of to rectangles. all the keys and just using a subset of the

ACM Computing Surveys, Vol. 20, No. 4, December 1988 276 l Hanan Samet keys. Of course, there are many other op- There are several difficulties with such tions and factors, and thus a choice can approaches. One problem is that updating only be made after a careful consideration (e.g., insertion or deletion) will generally of the type of operations (including queries) require processing several units. Another we wish to support. Not surprisingly, the more serious drawback is that at times a operations are similar to those commonly query is posed in such a manner that none considered for points. It should be men- of the characteristic parts of an object, say tioned that there are situations where the 0, that satisfies the query will match the representation issue is not as crucial. For query’s description, yet 0 does satisfy the example, implementing an operation using query. The problem is that not all proper- plane-sweep methods (see Section 2) im- ties are inherited by the parts. For example, plies a sequential process in which only a suppose we are dealing with a collection of subset of the objects is generally of interest. polygons stored so that each polygon is Thus, there is no need to be concerned represented by the line segments that con- about how to represent the entire collection stitute its edges. We wish to determine of objects. whether a given polygon contains a given Hinrichs and Nievergelt [ 19831 and Hin- rectangle. Clearly, no edge of the polygon richs [1985a] suggest that the representa- will contain the rectangle. Hence a solution tions of the individual objects can be to this problem requires that with each edge grouped into three principal categories, of a polygon (or rectangle) we store an which are briefly described below. First, we identifier that indicates the polygons or can use a representative point (e.g., the objects associated with each of its sides. centroid). Such an approach is not good for Third, we can represent an object by proximity queries if the object’s extent (e.g., partitioning the space from which it is lengths of the sides for a rectangle) is not drawn into cells that are adapted to the stored together with the coordinate values objects. Each cell is like a bucket that con- of the representative point. tains references to all objects that intersect Second, we can represent an object by it. The cells may be disjoint or may be its characteristic parts. There are many permitted to overlap. In the latter case, if choices, some of which are outlined below: the partition is such that there always ex- ists at least one cell that contains the object (1) The representation can be based on in its entirety, we can avoid the redundancy the interior of the object. For example, that is a natural consequence of the multi- we could decompose the object into ple references to the object. Of course, the smaller units (e.g., a decomposition of fact that cells may overlap will increase a rectangle into squares as would be the costs of certain query operations since done by a region quadtree). Each unit several cells may cover a specific point. contains a pointer to the complete de- Once a representation has been selected scription of the object. for the individual objects, we must choose (2) The representation can be based on the how to represent the collection of the ob- boundary of the object. For example, jects. There are numerous ways of doing SO. polygons are often represented as an In this tutorial we are primarily interested ordered collection of vertices or, equiv- in hierarchical representations, so the bulk alently, by the line segments compris- of our discussion concentrates on hierar- ing their boundaries. chical methods and on operations for which (3) The representation can be proce- they are useful. As we will see, the method dural-that is, a combination of (1) and that is used depends to a large degree on (2) according to a well-defined set of the manner in which the individual ob- rules. The combination could also be jects are represented. When objects are based on a decomposition into units represented using representative points, of a smaller dimension. For example, data structures for multiattribute data a rectangle is often represented as are applicable (e.g., grid file [Nievergelt the Cartesian product of two one- et al. 19841, k-d trees [Bentley 19751, point dimensional spheres (i.e., intervals). quadtrees [Finkel and Bentley 19741, PR

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 277 quadtrees [Orenstein 1982; Samet 19841, dimensions) across the plane (space in variations on B-trees [Comer 19791). The three dimensions) and halting at points choice depends on whether we wish to or- where the line (plane) makes its first or ganize the data to be stored (i.e., methods last intersection with any of the objects based on comparative search) or the being processed. At these points, the solu- embedding space from which the data are tion is partially computed so that at the drawn (i.e., methods based on address com- end of the sweep a final solution is avail- putation). Similar considerations apply able. In this discussion we are dealing with when individual objects are represented by two-dimensional data. Assume, without their characteristic parts, as is the case loss of generality, that the line is swept in when using variants of the region quadtree. the horizontal direction and from left to The principal tasks that are to be per- right. In order to use this solution tech- formed are similar to those for multiat- nique, we need to organize two sets of data. tribute data. They range from the basic The first set consists of the halting points operations such as insertion and deletion of the line (i.e., the points of initial or final to more complex queries that include exact intersection). It is usually organized as a match, partial match, range, partial range, list of x coordinate values sorted in ascend- finding all objects (e.g., rectangles) in a ing order. The second set consists of a given region, finding nearest neighbors description of the status of the objects that with respect to a given metric for the data are intersected by the current position of domain, and even join queries [Ullman the sweep line. This status reflects the in- 19821. The most common of these queries formation relevant to the problem that is involves proximity relations and are clas- being solved, and it must be updated at sified into two classes by Hinrichs [1985a]. each halting point. Thus, the data structure The first is an intersection query that seeks used to store the status must be dynamic. to determine if two sets intersect. This The characteristics of this data structure could be in the form of a window operation will determine, to a large extent, the effi- or query that finds all the rectangles that ciency of the solution. intersect (i.e., partially overlap) a given re- The application of plane-sweep methods gion. An alternative query is to determine to rectangle problems is much studied. The all rectangles that intersect (i.e., partially solutions to many of these problems require overlap) other rectangles. The second is a the data to be ordered in a manner that subset relation and can be formulated in makes use of some variant of multidimen- terms of enclosure (i.e., is A a subset of B) sional sorting. In such cases, the execution or of containment (i.e., does A contain B). times of optimal algorithms are often con- In describing queries involving these re- strained by how fast we can sort, which for lations we must be careful to distinguish N objects usually means a lower bound of between a point and an object. A point is O(N. log,N). At times, an increase in an element in the d-dimensional space from speed can be obtained by making use of which the objects are drawn. It is not an more storage. The text of Preparata and element of the space into which the objects Shamos [1985] contains an excellent dis- may be mapped by a particular represen- cussion of a number of problems to which tation. For example, in the case of a collec- such techniques are applicable. tion of rectangles in two dimensions, a We assume that each rectangle is speci- point is an element of the Euclidean plane fied by four values; the x coordinates of its and not a rectangle even though we may two vertical sides and the y coordinates of choose to represent each rectangle by a its two horizontal sides (equivalently, these point in some multidimensional space. are the x and y coordinates of its lower-left and upper-right corners). We also assume that each rectangle is closed on its left and 2. PLANE-SWEEP METHODS bottom sides and open on its top and right The term plane-sweep is used to character- sides. Applying the same open-closed con- ize a paradigm employed to solve geometric vention to the boundaries of a rectangle problems by sweeping a line (plane in three finds that its horizontal (vertical) bounda-

ACM Computing Surveys, Vol. 20, No. 4, December 1988 278 l Hanan Samet

ries are closed on their left (bottom) ends sent line segments. If we only care about and open on their right (top) ends. Alter- reporting the intersections of boundaries natively, the boundaries can be described (i.e., vertical boundaries with horizontal as being semiclosed. boundaries), then a balanced binary tree is In this section we focus on the efficient adequate to represent the bottom and top solution of the problem of reporting all boundaries (i.e., y coordinate values) of the intersections between rectangles and, to a active line segments [Bentley and Ottmann lesser extent, on some related problems. We 19791. Unfortunately, such a representa- assumed that a static environment, that is, tion fails to account for intersections that the identity of all rectangles is known a result when one rectangle is totally con- priori. Note that a naive way to report all tained within another rectangle. intersections is to check each rectangle In the rest of this section we focus on against every other rectangle, which re- solutions that use the segment, interval, quires O(N2) time for N rectangles. The and priority search trees to represent the plane-sweep solution of the problem con- active line segments. We first explain the sists of two passes. The first pass sorts the segment tree and then show how the order left and right boundaries (i.e., x coordinate of the space requirements of the solution values) of the rectangles in ascending order can be reduced by using either the interval and forms a list. For example, consider the or priority search trees while still retaining collection of rectangles given in Figure 1. the same order of execution time. We con- Letting RI and R, denote the left and right clude by briefly explaining how some re- boundaries of rectangle R, the result of the lated problems can be solved using the same first pass is a list consisting of 3, 6, 8, 21, techniques. 23, 25, 26, 31, 33, 34, 35, 37, 38, 38 corre- sponding to Al, EL, A,, IA, CL I%, E,, S, G, 2.1 Segment Trees B,, F,, C,, D,, G,, respectively. The second pass sweeps a vertical scan The segment tree is a representation for a line through the sorted list from left to right collection of line segments devised by Bent- halting at each one of these points. This ley [1977]. It is useful for detecting all the pass requires solving a quasi-dynamic ver- intervals that contain a given point. It is sion of the one-dimensional intersection best understood by first examining a sim- problem. At any instant, all rectangles that plified version that we call a unit-segment intersect the scan line are considered active tree, which is used to represent a single line (e.g., rectangles D, E, and G for a vertical segment. For the moment, assume that the scan line through x = 24 in Figure 1). We endpoints of the line segments of our col- must report all intersections between a lection are drawn from the set of integers newly activated rectangle and currently ac- f i ] 0 5 i 5 2&). Let S be a line segment with tive rectangles that lie on the active scan endpoints 1 and r (I < r). S consists of the line. The sweep process halts every time a set of consecutive unit intervals [j: j + 1) rectangle becomes active (causing it to be (1 5 j < r). The unit-segment tree is a inserted in the set of active rectangles) or complete binary tree of depth h such that ceases to be active (causing it to be deleted the root is at level h, and nodes at level 0 from the set of active rectangles). The key (i.e., the bottom) represent the sequence of to a good solution is to organize the active consecutive intervals [j: j + 1) (0 5 j < 2h). rectangles so that intersection detection, A node at level i in the unit-segment tree insertion, and deletion are executed effi- represents the interval [p: p + 2’) (i.e., the ciently. sequence of 2’ consecutive unit intervals The first pass involves sorting, and thus starting at p where p mod 2’ is 0). it requires O(N . log, N) time. Insofar as Representing line segment S in a unit- the second pass is concerned, each rectan- segment tree is easy. We start at the root gle is nothing more than a one-dimensional of the tree and check if its corresponding vertical line segment. There are several interval is totally contained in S. If yes, data structures that can be used to repre- then we mark the node with S. In such a

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles . 279

A A

Figure 5. The unit-segment tree for the segment [3 : 11) labeled A in the range [0: 16). case, we say that S covers the node’s inter- line segment, and (2) it is only defined for val. Otherwise, we repeat the process for line segments with integer endpoints. The the left and right sons of S. This process segment tree is an adaptation of the unit- visits at most four nodes at each level while segment tree that enables the use of one marking at most two of them. Thus, it is data structure to represent a collection of easy to see that inserting a line segment line segments with arbitrary endpoints by into a unit-segment tree in a top-down removing the restriction that the intervals manner can be achieved in O(h) time. An be of uniform length, and by replacing the equivalent bottom-up description of this mark at each node by a linked list of the process is that a node is marked with S if names of the line segments that contain all (i.e., both) the intervals corresponding that node. This is achieved in the following to its sons are totally contained in S, in manner. Given a set of N line segments, we which case the sons are no longer marked first sort their endpoints and remove du- with S. plicates to obtain yo, yl, . . . , ym (m < 2N). As an example of the unit-segment tree, Next, we form the segment tree in the same consider a collection of line segments with way as the unit-segment tree with the ex- integer endpoints that are in the range ception that interval [j: j + 1) is replaced [0: 16). In this case, there are 16 possible by the interval [yj: yj+l) (0 5 j c 2h and intervals, each of unit length. The unit- 2h-’ 5 m < 2h). Each line segment S with segment tree for a line segment, named A, endpoints y1 and yr consists of the sequence of length 8 whose endpoints are at 3 and 11 of consecutive intervals [yj: yj+,) (1 5 j is given in Figure 5. Note that the interval < r). A node at level i in the segment tree [i: i + 1) is represented by the node labeled represents the interval [y,: y,,,~) (i.e., the i. From the figure it is easy to observe the sequence of 2’ consecutive intervals start- close analogy between the unit-segment ing at yP, where p mod 2’ is 0). Each node tree and a one-dimensional region quadtree is marked with the names of all the line [Rosenfeld and Kak 19821, where the unit segments that cover the node’s correspond- intervals are the one-dimensional analog of ing interval and that do not cover the cor- pixels. The analogy is completed by letting responding interval of the parent node. As BLACK (WHITE) correspond to the la- in the case of the unit-segment tree, a node beled (unlabeled) nodes and merging and its brother cannot be both marked with brother nodes of the same color. the same line segment. The set of line seg- The unit-segment tree is inadequate for ments associated with each node is repre- two reasons: (1) it can only represent one sented as a doubly linked list.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 280 l Hanan Samet

i : yi: 3 6 8 II 14 15 19 21 23 27 3436 29 IAl I4 IFI ICI w IA.Cl

Figure 6. The segment tree for the set of line segments corresponding to the vertical boundaries of the rectangles in Figure 1. Terminal node i corresponds to the interval [yC : y,,,).

For example, Figure 6 is the segment tree that contains it. This could be expensive, for the set of line segments that correspond since in the worst case it requires the trav- to the vertical boundaries of the rectangles ersal of O(log, N) linked lists, each contain- in Figure 1. Although there are seven line ing O(N) entries. This difficulty is avoided segments, the segment tree contains 12 by maintaining an auxiliary table with one intervals since there are only 13 different entry for each of the N line segments. Each endpoints. Since the segment tree is a com- table entry points to a list of pointers. Each plete binary tree, in this case it has four pointer points to a list entry for the line unused intervals. Each terminal node is segment in a node, say P, of the segment labeled with its corresponding interval tree such that P’s interval is covered by the number and the leftmost endpoint of the line segment. This table is built as the line interval-that is, node i corresponds to the segments are entered into the segment tree. interval [yi: YL+~). Nodes are also labeled It contains at most N entries and can be with the sets of names of the line segments accessed or updated in 0 (log, N) time when that cover their corresponding intervals. implemented as a balanced binary tree (or For example, the interval [23 : 34) is labeled even in constant time if implemented as an with {A, C ) since it is covered by these line array, in which case each line segment must segments. be represented by a unique integer in the Inserting a line segment in the segment range 1 . . . N). We can use an array instead tree is analogous to inserting it in the unit- of a dictionary because we know the iden- segment tree. The only difference is that tities of the rectangles in advance (i.e., a the line segment must also be placed in the static environment). list of the line segments that is associated A segment tree for N line segments has with the node. It can be placed anywhere a maximum of 2 . N leaf nodes. Each line in the list and thus we usually attach it to segment covers the intervals of at most the front of the list. In a domain of N line 2 . [log, Nl nodes of the segment tree. At segments, insertion (into the tree and list) each of these nodes, deletion of the line takes 0 (log, N) time per line segment. segment can be done in constant time, Deleting a line segment from a segment since the segment lists that are associated tree is somewhat more complex. We must with these nodes are implemented as dou- remove the line segment from the doubly bly linked lists. Thus, the total cost of linked list that is associated with each node deleting a line segment is O(log, N). The

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 281 segment tree has a total of O(N) nodes, r. Thus, there is really only a need to be and since each line segment can appear concerned with an ordering that is based in (i.e., cover) O(log,N) nodes, the total on the starting points. space required (including the auxiliary The first problem is solved by performing table) in the worst case is O(N . logn N). a point query for point 1 on the segment Interestingly, Bucher and Edelsbrunner tree representation of the line segments. In [1983] have shown that the average space order to determine all the line segments requirement for a segment tree is also that contain 1, we simply locate the smallest O(N - log,N). interval that contains it. Since a segment Given F rectangle intersections, using a tree for N line segments has a maximum of segment tree to determine the set of rectan- 2 . N leaf nodes, this search visits at most gles that intersect each other is somewhat flog,Nl + 1 nodes. For each node that is complex [Bentley and Wood 19801 if we visited, we traverse its associated list of line want to do it in O(N . log, N + F) time. In segments and report them as containing 1. particular, it involves considerably more This process requires O(log,N + FI) time, work than just inserting a line segment and where F, is the number of line segments reporting the rectangles associated with the that contain 1. Since a segment tree is used, line segments that were encountered during it needs 0 (N . log, N) space. the insertion process. Conceptually, the The second problem is solved by per- problem is quite straightforward-for each forming a range query for range [l: r) on line segment S, with starting and ending the set of starting points of the line seg- points 1 and r, respectively, we want the set ments. This query is one for which a range of line segments A such that Ai fl S is tree [Bentley 1979, Bentley and Maurer nonempty for each Ai E A. Recalling that 19801 that stores the starting points of the the segment tree is good for detecting all line segments is ideal. A is a intervals that contain a given point, we balanced binary tree where the data points formulate the problem as an infinite set of are stored in sorted order in the leaf nodes, point queries-that is, for each point pi in which are linked in this order by use of a line segment S find all line segments that doubly linked list. Therefore, insertion and contain it. This process requires O(log,N) deletion are both O(log,N) processes. A time for each point that is queried. In order range query consists of locating the node to avoid looking at every point in S (an corresponding to the start of the range, say infinite number!), we can restrict our L, and the closest node to the end of the search to the endpoints of the line segments range, say R, and then reporting the line that are overlapped by S. An obvious, but segments corresponding to the nodes that unacceptable solution is to explicitly store lie between them by traversing the linked with each line segment the set of segments list of nodes. This process requires O(log, N that intersect it, at a total storage cost of + FL,) time, where F[, is the number of line O(N”). segments with starting points in [l: r). A more reasonable solution, which makes Since a balanced binary tree is used, it use of the above restriction on the search, needs O(N) space. The combination of the is given by Six and Wood [ 1980,1982], who point and range query solution requires decompose the search into two disjoint O(N . log, N) space and O(N . log, N + F) problems. They make use of the obvious time, where F is the number of rectangle fact that any line segment whose starting intersections. point is greater than r or whose ending point is less than 1 does not intersect the 2.2 Interval Trees line segment S. The first problem consists of determining all line segments with start- Suppose we try to determine the set of ing points less than 1 whose intersection rectangles that intersect each other by just with S is nonempty. The second problem using a segment tree. The problem with consists of determining all line segments this approach is that upon insertion of a with starting points that lie between 1 and line segment, say S, in a segment tree, we

ACM Computing Surveys, Vol. 20, No. 4, December 1988 282 ’ Hanan Samet cannot find all of the existing line segments The above modifications serve as the in the tree that are totally contained in S, foundation for the development of the or partially overlap S, without examining interval tree of Edelsbrunner [1980a, every node in each subtree that contains S. 1983a, 1983b] and the tile tree of McCreight For example, consider the segment tree of [ 19801. The difference between them is that Figure 6 without line segment A (i.e., for the tile tree is based on a regular decom- segments B, C, D, E, F, and G). Upon position, while the interval tree is not. In inserting line segment A in the node cor- the rest of this section, we only discuss the responding to interval [14 : 23), the only interval tree. way to determine the existence of line seg- The interval tree is designed specifically ment F (corresponding to the interval to detect all intervals that intersect a given [15 : 19)) that is totally contained in A is to interval. It is motivated by the dual goals descend to the bottom of the subtree rooted of reducing the space requirement to O(N) at [ 14 : 23). A similar example can be con- while maintaining an execution time of structed to show that this problem also O(N. log, N + F). The interval tree solu- arises in the case of partial overlap. Unfor- tion also makes use of the decomposition tunately, checking for total containment or of the search into the two disjoint tasks of partial overlap in this way takes O(N) time for each line segment, or O(N’) for all the (1) determining all line segments that line segments. overlap the starting point of the query The above problem can be overcome in line segment, and part by making the following modifications (2) determining all line segments whose to the segment tree. Link each marked node starting point lies within the query line (i.e., a node whose corresponding interval segment. overlaps at least one line segment), say P, to some of the nodes in P’s subtrees that Once again, assume that we are given a are marked. This could be implemented by set of N line segments such that line seg- an auxiliary binary tree whose elements are ment Si corresponds to the interval [1,: ri)- the marked nodes. Since each line segment that is, li and ri are its left and right end- can be associated with more than one node points, respectively. The endpoints of the in the segment tree, the number of inter- N line segments are sorted (with duplicates sections that can be detected is bounded by removed) to obtain the sequence yo, yl, . . . , 2 . N2 . logn N, while the number of differ- y,,, (m < 2N and 2h-’ % m < 2h). The ent intersections is bounded by N2. Remov- interval tree is a three-level structure, ing duplicates will require sorting, and even where the first (and principal) level is use of the bin method [Weid 19781, which termed the primary structure, the second is linear, still leaves us with an O(N2 . level is termed the secondary structure, and log2 N) process. The duplicate entries, how- the third level is termed the tertiary struc- ever, can be avoided by redefining the seg- ture. We shall illustrate our discussion with ment tree so that a line segment is only Figure 7, the interval tree for the set of associated with one node-the nearest line segments corresponding to the vertical common ancestor2 of all the intervals con- boundaries of the rectangles in Figure 1. tained in the line segment (e.g., the node The primary structure is a complete corresponding to the interval [3 : 38) for line binary tree with m + 1 external (i.e., ter- segments A and C in Figure 6). The absence minal) nodes such that when the tree is of duplicate entries also means that the flattened and the internal nodes are re- space requirements can be reduced to moved, external node i corresponds to yi. O(N). In Figure 7, the primary structure is de- noted by solid lines. In our example, *The principle of associating key information with although there are 7 line segments, the the nearest common ancestor is similar to Chazelle primary structure contains only 13 exter- and Guibas’ [1986a, 1986131fractional cascading. It is also used as the basis of an efficient solution of the nal nodes as there are only 13 different point location problem by Edelsbrunner et al. [1986]. endpoints. Each terminal node is labeled

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles 283

Figure 7. The interval tree for the set of line segments corresponding to the vertical boundaries of the rectangles in Figure 1. The primary structure is shown by solid lines. The secondary structure is shown by dotted lines. The tertiary structure is shown by broken lines, with the active nodes circled with thick lines. The interrelationships between the endpoints of the line segments are also shown.

with its corresponding endpoint [i.e., yi for the secondary structure is denoted by dot- terminal node i (0 % i < 2N)]. Each internal ted lines emanating from each internal node is assigned an arbitrary value, stored node that has a nonempty secondary struc- in the field VAL, that lies between the ture. The sets LS and RS are distinguished maximum value in its left subtree and the by dotted lines emanating from the internal minimum value in its right subtree (usually node to its left and right sides, respectively. their average). For example, the root node When LS and RS contain more than one in Figure 7 is labeled with 22. entry, we show them linked in increasing Given a line segment corresponding to and decreasing order, respectively (e.g., LS the interval [l :r), we say that its nearest of the root node shows 6 pointing to 21 common ancestor in the interval tree is the since the corresponding intervals are internal node that contains 1 and r in its [6 : 36) and [21: 36)). Each starting (ending) left and right subtrees, respectively. For point appears in LS (RS) as many times as example, in Figure 7, node 22 is the nearest there are line segments that have it as a common ancestor of line segment A, which starting (ending) point. For example, 36 corresponds to the interval [6, 36). appears twice in RS of the root node in Each internal node in the primary struc- Figure 7 as it is the ending point of line ture, say u, serves as the key to a pair of segments A and C. In order to support rapid secondary structures LS and RS that rep- insertion and deletion, the sets LS and RS resent the sets of left and right endpoints, are implemented as balanced binary trees respectively, of the line segments for which (as well as doubly linked lists). u is the nearest common ancestor (i.e., they Each internal node in the primary struc- contain U’S value). Elements of the sets LS ture has eight pointers-two to its left and and RS are linked in ascending and de- right subtrees in the primary structure (LP scending order, respectively. In Figure 7, and RP), two to the roots of LS and RS in

ACM Computing Surveys, Vol. 20, No. 4, December 1988 284 . Hanan Samet the secondary structure, one to the mini- inserting a vertical line segment, say S, mum value in LS, one to the maximum corresponding to the interval [1: r). During value in RS, and two (LT and RT) to its this process we search for and report the left and right subtrees in the tertiary struc- line segments that overlap S. Assume that ture, which we discuss below. the interval tree is rooted at TI . The search An internal node in the primary struc- has the following three stages: ture is marked active if its corresponding secondary structure is nonempty or both of (1) Start at T1 and find the first node v its sons have active descendants; otherwise, such that 1< VAL(v) < r. it is marked inactive. The active nodes of (2) Start at v and locate 1in the left subtree the primary structure form the tertiary of v. structure, which is a binary tree. It is rooted (3) Start at v and locate r in the right at the root of the primary structure and is subtree of v. linked via the LT and RT fields of the internal nodes of the primary structure. If This search involves the secondary struc- node v of the primary structure is inactive, tures of the nodes in the primary structure. then LT(v) and RT(v) are Q [i.e., pointers The tertiary structure is used to limit the to Nil]. If v is active, then LT(v) points to number of nodes in the primary structure, the closest active node in the left subtree with empty secondary structures, that must of v [i.e., in LP(v)], and RT(v) points to be examined. Note that all of the overlap- the closest active node in the right subtree ping line segments will be reported, and of v [i.e., in RP(v)]. If there are no closest each will be reported only once since it is active nodes in the left and right subtrees associated with the secondary structure of of v, then LT(v) and RT(v), respectively, just one node in the primary structure. are Q (i.e., pointers to NLL). In Figure 7, In the following we present the main the tertiary structure is denoted by broken ideas of the three stages. Figure 8 aids lines linking all of the active nodes (e.g., the visualization of the symbols used in nodes 22, 12.5, 7, 17, 35, and 25), which are the explanation for [I: r). All secondary also marked with thicker ellipses. The ter- and tertiary structures that are visited tiary structure is useful in collecting the are marked with dotted and broken lines, line segments that intersect a given line respectively. segment and enables us to avoid examining First, we explain stage (1). (Tij denotes primary nodes whose corresponding line the set of nodes encountered during the segments do not. It can be shown that more search for v. We use the insertion of line than half of the elements of the tertiary segment [6: 20) into the interval tree of structure (i.e., active nodes) have nonempty Figure 7 as our example. The secondary secondary structures. structures associated with each Ti must be Inserting the line segment [I: r) in the checked for a possible overlap with S. This interval tree is very simple. We start at the is quite simple. Either 2 < r < VAL(Ti) or root and locate the first node v such that VAL(T,) < 1< r. 1 < VAL(v) < r. In this case, we insert 1 If r < VAL(Ti), then we need only report into LS(v) and r into RS(v). Both of these the line segments in the secondary struc- processes can be achieved in O(logz N) ture of Ti whose starting points are less time. Updating the tertiary structure re- than r (e.g., line segment A upon examining quires us to traverse it in parallel with the the internal node with value 22). To achieve primary structure and takes O(log, N) this we visit LS( Ti) in ascending order until time. Deletion of a line segment is per- we encounter a line segment whose starting formed in a similar manner and with the point exceeds r. We then search the left same complexity. subtree of T; [i.e., LP(Ti)]. Reporting the rectangle intersections in Similarly, if VAL(Ti) < I, we need only an interval tree is straightforward, al- report the elements of the secondary struc- though there are a number of cases to con- ture of Ti whose ending points are greater sider. Again, this task is performed while than 1. To do this, we visit RS(Ti) in de-

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 285 If 1 < VAL(L,), then 5’ intersects every line segment in the secondary structure of Li as well as all the line segments in the secondary structures in the right subtree of Lie The first set consists of just the line segments in RS(L;) (e.g., line segment G upon examining the internal node with value 12.5). The second set is obtained by visiting all the active nodes in the right subtree of Li, RP(Li) (e.g., line segment F during the processing of the internal node with value 12.5 since F is associated with the active internal node with value 17). In order to avoid visiting irrelevant nodes we make use of the tertiary structure using the pointers LT(Li) and RT(Li). It can be shown that more than half of the elements of the tertiary structure have nonempty secondary structures, and thus the time necessary to execute this process is propor- tional to the number of intersections that are reported. The search is continued in the left subtree of Lip LP(Li). If VAL(L;) < 1, then we report the line segments in the secondary structure of Li whose ending points are greater than 1. To do this, we visit RS(Li) in descending order Figure 8. Example of an interval tree search for the interval [1 : r). All secondary structures that are visited until encountering a line segment whose are marked with dotted lines. All tertiary structures ending pont is less than 1. This process is that are visited are marked with broken lines. executed in time proportional to the num- ber of intersections that are reported. The search is continued in the right subtree of scending order until encountering a line LL, RP(Li). segment whose ending point is less than 1. Solving the rectangle intersection prob- The search is then continued in the right lem using an interval tree requires O(N) subtree of Z’i [i.e., RP(Ti)]. space and O(N . log, N + F) time, where F Both of these cases are executed in time is the number of rectangle intersections.3 proportional to the number of intersections The space requirements are obtained by that are reported. Once node IJ has been observing that for N line segments we located we report all elements of its second- need at most 2 . N terminal nodes in the ary structure as intersecting S. In our ex- primary structure and likewise for the ample, we would report line segment G, secondary structures. The tertiary struc- since v is the internal node with value 12.5. ture is constructed from nodes in the pri- Now, we explain stages (2) and (3). They mary structure, and thus it requires no are very similar and thus we just discuss additional space except for the pointer stage (2). We use the insertion of line seg- fields. Making use of the fact that the in- ment [6 : 34) into the interval tree of Figure 7 as our example. In this case, v is the root of the tree (the internal node with value ’ For the tile tree, the execution time is O(N . 22). Let (Lil denote the set of nodes en- log,(max(N, K)) + F), where the horizontal bounda- ries of the rectangles are integers between 0 and countered during the search for 1 in this K - 1. The execution time becomes O(N . log, N + stage. Recall that 1 < VAL(v). Either 1 < F) if the 2. N y coordinate values are first sorted and VAL(L;) or VAL(Li) < 1. then mapped into the integers from 0 to 2 . N - 1.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 286 l Hanan Samet

terval tree is a complete binary tree, the intervals, which is the first pass of the plane number of internal nodes in the primary sweep, still requires O(N . log, N) time. and secondary structures is bounded by 2 . A priority search tree keeps track of N- 1. points in a two-dimensional space. It is The execution time requirements are ob- built in the following manner. Assume that tained by noting that searching the primary no two data points have the same x coor- structure for the starting and ending points dinate value. Sort all the points along the of a line segment takes O(log, N) time. The x coordinate and store them in the leaf number of nodes in the secondary structure nodes of a balanced , say that are visited is of the same order as the T. We proceed from the root node toward number of rectangle intersections that are the leaf nodes. With each internal node of found. Since at least one-half of the active T, say I, associate the point in the subtree nodes have nonempty secondary structures, rooted at I with the maximum value for its the number of nodes in the tertiary struc- y coordinate that has not already been ture that are visited is no more than twice stored at a shallower depth in the tree. If the number of nodes visited in the second- such a point does not exist, then leave the ary structure. Constructing the interval node empty. For example, treating the ver- tree takes O(N . log, N) time since the end- tical boundaries [ ya, yT) of the rectangles points of the line segments that form the in Figure 1 as points (x, y), Figure 9 is their sides of the rectangles must be sorted. corresponding priority search tree. For N points, the priority search tree requires O(N) storage. 2.3 Priority Search Trees The priority search tree is designed for Using an interval tree, as described in Sec- solving queries involving semi-infinite tion 2.2, yields an optimal worst-case space ranges in two-dimensional space. Perform- and time solution to the rectangle intersec- ing a semi-infinite range query ([L,: R,], tion problem. The interval tree solution [L,: a~]) using a priority search tree is done requires that we know in advance the end- as follows. Descend the tree looking for the points of all of the vertical intervals since nearest common ancestor of L, and R,, say they must be sorted and stored in a com- Q. Now, recursively, apply the following plete binary tree. Thus, given N rectangles, search procedure to the subtree rooted at the storage requirement is always O(N). Q. Let T denote the root of the subtree that The solution can be slightly improved is currently being processed, and let P be by adapting the priority search tree of the point associated with it. If no such P McCreight [1985] to keep track of the exists, then we are finished with the entire active vertical intervals. In this case, the subtree rooted at T since all points in storage requirements for the sweep pass its subtrees have already been examined only depend on the maximum number of and/or reported. Examine the y coordinate vertical intervals that can be active at any value of P, say Py . If P, < L,, then we are one time, say M. Moreover, there is no need finished with the entire subtree rooted at T to know their endpoints in advance, and since P is the point with the maximum y thus there is no need to sort them. This coordinate value in the subtree. Otherwise, also has an effect on the execution time of perform the following steps: the algorithm since the data structure used to keep track of the endpoints of the verti- (1) Check if the x coordinate value of P is cal intervals is the determinative factor in in the range [L, : R,]; if yes, then output the amount of time necessary to do a P as satisfying the query. search. Thus, when using the priority (2) Determine where the search is to be search tree, the sweep pass of the solution continued. If both T and its right son to the rectangle intersection problem can are on the path from Q to L,, then be performed in O(N . log, M + F) time, continue in the right son of T; else if rather than O(N . log, N + F) time. How- both T and its left son are on the path ever, sorting the endpoints of the horizontal from Q to R,, then continue in the left

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles . 287

E A G F C D (3,8) (6,36) (11.14) (15,19) (21,36) (23,27)

Figure9. The priority search tree for the set of line segments corresponding to the vertical boundaries [ye, yT) of the rectangles in Figure 1. Each vertical boundary [yB, yT) is treated as a point (x, y) in a two-dimensional space. The leaf nodes contain the ye values and the internal nodes contain the maximum yT values.

son of T; else continue in the two sons requires O(log, M + F) time, where F is the of T. number of intersecting intervals found. In order for the space and time bounds For Npoints, this process requires O(log, N to be comparable with the interval tree, we + F) time, where F is the number of points must also show that a one-dimensional in- found. terval can be inserted and deleted from a There are two keys to understanding the priority search tree in O(log, M) time. This use of the priority search tree in solving is achieved by implementing the priority the rectangle intersection problem. As- search tree as a “red-black” balanced bi- sume, again, that all intervals are semi- nary tree [Guibas and Sedgewick 19781. closed (i.e., they are closed on their left The red-black balanced binary tree has the ends and open on their right ends). First, property that, for M items, insertions and each one-dimensional interval, say [a : b), is deletions take O(log,M) time with O(1) represented by the point (a, b) in two- rotations [Tarjan 19831. McCreight [ 19851 dimensional space. This two-dimensional shows that the priority search tree adapta- space is represented by a priority search tion of the red-black balanced binary tree tree. Second, we observe that the one- can be maintained at a cost of O(log, M) dimensional interval [a : b) intersects the per rotation. The use of a red-black bal- interval [c: d) if and only if a < d and anced binary tree does not affect the O(M) c < b. An equivalent observation is that the storage requirements of the priority search point (c, d) lies in the range ([-m: b), tree. Hence the desired time and space (a: ml). This equivalence means that in or- bounds are achieved. der to find all one-dimensional intervals When the priority search tree is imple- that intersect the interval [a : b), we need mented as a red-black balanced binary tree, only perform the semi-infinite range query its node structure differs from the way it ([--03 : b), (a : m]) in the priority search tree. was defined earlier in this section. In par- If the priority search tree contains M one- ticular, the internal nodes now also contain dimensional intervals, then this operation intervals. The interval associated with an

ACM Computing Surveys, Vol. 20, No. 4, December 1988 288 ’ Hanun Samet

internal node, say I, is the one whose left marked4 components of its corresponding endpoint, say L, is the median value of the interval with the vertical scan line. left endpoints of the intervals in I’s sub- As an example of the computation of the trees. All intervals in I’s left subtree have area, consider the collection of rectangles left endpoints that are less than L, whereas in Figure 1. When the scan line passes over the intervals in I’s right subtree have left x = 7, the lengths of its overlaps with the endpoints that are greater than L. nodes corresponding to intervals [6 : 8), Comparing the interval and priority [3 : 8), [8 : 14), [3 : 14), [14 : 23), [3 : 23), search tree solutions to the rectangle inter- [23 : 34), [34: 36), [34: 38), [23 : 38), [23 : w), section problem, we find that the priority and [3: 03) are 2, 5, 6, 11, 9, 20, 11, 2, 2, 13, search tree is considerably simpler from a 13, and 33, respectively. In addition, for conceptual standpoint than the interval each marked node of the segment tree, we tree. The execution time requirements of record the number of times, if any, it is the priority search tree are lower when the marked. In our example, when x = 7, the sort pass is ignored. Also, the priority nodes corresponding to the intervals [6 : 8), search tree enables a more dynamic solu- [3:8), [8:14), [14:23), [23:34), and tion than the interval tree because for the [34:36) are marked once; however, at no priority search tree only the endpoints of time in this example will any node be the horizontal intervals need to be known marked more than once. These values are in advance. On the other hand, for the adjusted whenever a vertical boundary of a interval tree the endpoints of both the hor- rectangle is inserted into, or deleted from, izontal and vertical intervals must be the segment tree-that is, at each halting known in advance. point. For N rectangles, this adjustment process requires O(log, N) steps per halting point or O(N . log, N) for the area of the 2.4 Applications entire collection. The total space require- ments is O(N). Data structures such as the segment tree As stated earlier, the unit-segment tree can be used within the plane-sweep para- is analogous to a region quadtree in one digm to solve a number of problems other dimension. This analogy is exploited by van than rectangle intersection. In fact, the seg- Leeuwen and Wood [1981] in solving mea- ment tree was originally developed by Bent- sure problems in higher dimensional spaces ley [ 19771 as part of a plane-sweep solution (i.e., cl> 2). Lee [1983] also uses the same to compute the area (also termed a measure technique to develop an algorithm that problem [Klee 19771) of a collection of rec- finds the maximum number of rectangles tangles where overlapping regions are only whose intersection is not empty (also counted once. It can also be used to com- termed a maximum clique [Harary 19691). pute the perimeter. As an example of this method consider the The central idea behind the use of the problem of computing the volume that is segment tree to compute the area is to keep occupied by the union of a collection of track of the total length of the parts of the three-dimensional rectangular parallel- vertical scan line, say Li at halting point epipeds. van Leeuwen and Wood [ 19811 use Xi, that overlap the vertical boundaries of a plane-sweep solution that sorts the rectangles that are active just to the left of boundaries of the parallelepipeds along one Xi. This quantity is adjusted at each halting direction (say x) and then sweeps a plane point. The total area is obtained by accu- (instead of a line) parallel to the y-z plane mulating the product of this quantity with across it. At any instant of time, the plane the difference between the current halting consists of a collection of cross sections point and the next halting point-that is, Li * (Xi - Xi-l). In order to facilitate this 4 By marked we mean that the node’s interval is com- computation, each node of the segment tree pletely contained in a vertical boundary of one of the contains the length of the overlap of the rectangles.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 289

(i.e., two-dimensional rectangles). This col- time of O(N* . log2 N). Generalizing these lection is represented as a region quadtree results to d dimensions reduces the time in a manner analogous to the segment tree. requirement of Bentley’s solution from The region quadtree is built as follows. O(Nd-’ . log2N) to O(Nd-‘); however, this Assume that there is a maximum of N increases the space requirement from 0 (N) boundaries in all directions. First, sort the to O(N*). This is achieved by recursively y and z boundaries of the parallelepipeds reducing the d-dimensional problem to N (removing duplicates) obtaining yo, yl, . . . , problems in (d - 1) dimensions until we yp (P < 2W, 20, 21, . . . , 2, (q < 2M, and obtain the three-dimensional case and then 2h-’ I max(p, q) < 2h. Assume without solving each three-dimensional problem as loss of generality that the boundaries are discussed above. distinct. If not, then there are fewer subdi- Another related problem is finding the visions. Also, add enough subdivision lines containment set (also known as the inclu- so that there are 2h subdivisions in each of sion or enclosure set) of a collection of y and z. Next, form a grid with an origin at rectangles. The containment set is the set the lower left corner such that the two- of all pairs of rectangles A and B such that dimensional interval with a lower left cor- A contains B. Variants of the segment tree ner at (i, j) corresponds to the rectangular are used by Vaishnavi and Wood [1980] to parallelepiped with (yi, z;) as its lower left solve this problem in O(N . log; N + F) corner. Each two-dimensional interval is time and O(N. log: N) space. The space marked with the name of the parallelepiped requirement is reduced to O(N) by Lee and of which it is a part. At this point, only the Preparata [1982], who map each rectangle terminal nodes of the quadtree are marked. into a point in four-dimensional space and The nonterminal nodes are marked as fol- solve a point dominance problem. lows. Visit the intervals in an order such as As described in Section 1, the rectangle that used in building a region quadtree from intersection problem is closely related to a binary array [Samet 19801. Whenever the following problems: four brother two-dimensional intervals, say 1i, are in the same parallelepiped, say P, (1) Determining all rectangles that are then their father node, say F, gets the label contained in a given rectangle, of the parallelepiped and P is no longer (2) Determining all rectangles that enclose associated with any of Ii. Whenever no a given rectangle, parallelepipeds are associated with four (3) Determining all rectangles that par- brother intervals, then they are merged and tially overlap or are contained in a their corresponding nodes are removed given rectangle (a window query). from the quadtree. Building the region quadtree is an O(N’) process. The plane-sweep approach is not appropri- As the scan line is swept, each halting ate for these problems since, regardless of point causes the insertion and/or deletion the data structures employed (e.g., seg- of two-dimensional intervals from the ment, interval, or priority search trees), quadtree. Insertion and deletion of a two- sorting is a prerequisite and thus any al- dimensional interval is an O(N) process gorithm requires at least 0 (N . log2 N) time since a rectangle can appear in at most for N rectangles. In contrast, the naive O(N) nodes. Thus, we see that a plane- solution of intersecting each rectangle with sweep algorithm employing the region the query rectangle is an O(N) process. quadtree will execute in time O(N . The problems described above can be log,N + N*) or O(N*) since there are O(N) solved by segment trees and interval trees halting points. This is an improvement over without making use of a plane sweep [Ov- a solution of Bentley [1977], which recur- ermars 19881. The key is to adapt these sively performs a plane sweep across each representations to store two-dimensional of the planes (i.e., it reduces it to N one- intervals in a manner similar to that in dimensional subproblems) for an execution which a two-dimensional range tree is de-

ACM Computing Surveys, Vol. 20, No. 4, December 1988 290 l Hanan Samet

veloped from a one-dimensional range tree changing set of points in sorted order. A [Edelsbrunner 19821. For example, a seg- more serious problem is that in a dynamic ment tree can be adapted to represent rec- environment, the sweep pass of a plane- tangles as follows. Project the rectangles sweep algorithm will usually have to be on the x axis and store these intervals in a reexecuted, since there is no data structure segment tree, say T. Let I be an internal corresponding to it.5 In the following sec- node in T and let RI denote the rectangles tions we discuss methods for a dynamic whose horizontal sides are associated with environment whose worst-case behavior is I. For each I build a segment tree for the not as good as that of plane sweep. Inter- projections of Rr on the y axis. We can also estingly, the data structures that were used build an interval tree for the projections of in the plane-sweep approach are also appli- RI on the y axis. cable in the dynamic environment. Using such adaptations of the segment and interval trees, for N rectangles the 3. POINT-BASED METHODS execution time of the solution to the win- dow query is 0 (log? N + F), where F is the In this section we first discuss the repre- number of rectangles that satisfy the query. sentation of rectangles as points and then The difference is in their storage require- examine the representation of the collec- ments-the segment tree solution requires tion of points. A common solution to the O(N . log; N) space, whereas the interval problem of representing a collection of ob- tree solution requires O(N . log, N) space. jects is to approximate elements of the col- For both of these structures, judicious use lection by simpler objects. One technique of doubly linked lists ensures that rectan- is to represent each object by using one of gles can be inserted and deleted in a number of primitive shapes that contain O(loggN) time. Of course, these structures it. Up to now we have used rectangles, but must still be built, which requires O(N. other shapes such as triangles, circles, IogZN) time in both cases. In the case of cubes, parallelepipeds, cylinders, and the segment tree solution, the query time spheres are also possible. This approach is can be reduced further to O(log,N + F) motivated, in part, by the fact that it is with the same space and preprocessing easier to test the containing objects for costs by adding some pointers (termed a intersection than it is to perform the test layered tree) [Vaishnavi and Wood 19821. using the actual objects. For example, it is It is not clear how to adapt the prior- easier to compute the intersection of two ity search tree to store two-dimensional rectangles than of two polygons for which intervals. the rectangles serve as approximations. The plane-sweep paradigm for solving More complex approximations can be cre- the geometric problems discussed earlier in ated by composing Boolean operations and this section (e.g., the rectangle intersection geometric transformations on instances problem) assumes that the set of rectangles of the primitive types. In fact, this is the is only processed once. It can be shown that basis of the constructive solid geometry for many of these problems, plane-sweep [Requicha 1980; Voelcker and Requicha methods yield a theoretically optimal solu- 19771 (CSG) technique of representing tion. A disadvantage is that such solutions three-dimensional objects. assume a static environment. The advantage of using such approxi- In contrast, in a dynamic environment mations is that each primitive can be where update operations occur frequently described by a small set of parameters (i.e., rectangles are added and deleted), the and can in turn represent a large class of plane-sweep approach is less attractive. objects. In particular, if primitive P is Plane-sweep methods require that the end- points of all rectangles are known a priori ’ Application of newly introduced methods employing and that the endpoints be sorted before the persistent search trees due to Sarnak and Tajan sweep pass. This is not a major problem, [1986] to the rectangle intersection problem may be since it is easy to maintain a dynamically useful in avoiding the reexecution of the sweep pass.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles 291 described by k parameters, then each set ‘c’ of parameter values defines a point in a ’ B’ t n k-dimensional space assigned to the class I of objects whose members are all the pos- E “F+ ,G sible instances of primitive P. Such a point , ‘A’, I + I -X- is termed a representative point. Note that 0 IO +2_2_9,,,.._30_-,. 40 a representative point, and the class to 31 2P which it belongs, completely define all of (a) the topological and geometric properties of the corresponding object. Most primitives can be described by more than one set of parameters. For example, using Cartesian coordinates, a circle is de- scribed by a representative point in three- dimensional space consisting of the x and y coordinates of its center and the value of its radius. On the other hand, using polar coordinates, a circle can also be de- Figure 10. (a) Horizontal (i.e., x) and (b) vertical (i.e., y) intervals corresponding to the sides of the scribed by a representative point in three- rectangles in Figure 1. Solid lines correspond to rec- dimensional space consisting of the p and 0 tangles in the collection, and broken lines correspond coordinates of its center and the value of to the query rectangles. its radius. For other primitives, the choices are even more varied. For example, the class of objects formed by a rectangle in parameters specify size, such as the radius two dimensions whose sides are parallel to of a circle. This distinction is always pos- the x and y coordinate axes is described by sible for objects that can be described as a representative point in four-dimensional Cartesian products of spheres of varying space. Some choices for the parameters are dimension. Many common objects can be as follows: described in this way. For example, a rec- tangle is the Cartesian product of two one- (1) The x and y coordinates of two diago- dimensional spheres, whereas a cylinder is nally opposite corners of the rectangle the Cartesian product of a one-dimensional (e.g., the lower left and upper right). sphere and a two-dimensional sphere. (2) The x and y coordinates of a corner of Whenever such a distinction between lo- the rectangle, together with its horizon- cation and extension parameters can be tal and vertical extents. drawn, the proximity queries that are de- (3) The x and y coordinates of the centroid scribed in Section 1 have cone-shaped of the rectangle, together with its hor- search regions where the tip of the cone is izontal and vertical extents (i.e., the usually in the subspace of the location pa- horizontal and vertical distances from rameters and has the shape of the query the centroid to the relevant sides). point or query object. The importance of making the right The actual choice depends on the type of choice can be seen by examining the class operations we intend to perform on the objects formed by them. of one-dimensional intervals on a straight line. As an example, consider the collection Different parameters have different ef- of rectangles given in Figure 1. Each rec- fects on the queries, and thus making the tangle can be represented as the Cartesian right choice is important. Hinrichs and product of two one-dimensional spheres Nievergelt [ 19831 and Hinrichs [1985a] corresponding to the sides that are given as lump the parameters into two classes- horizontal and vertical intervals in Fig- location and extension. Location parame- ure 10. These intervals can be represented ters specify the coordinates of a point such using any of the three representations as a corner or a centroid, whereas extension enumerated above. Representation (1)

ACM Computing Surveys, Vol. 20, No. 4, December 1988 292 l Hanan Samet R D:

cx 0 IO 20 30 I40 Figure 12. Representation of the horizontal inter- vals of Fiaure 1 as ordered pairs (CX. DX). where CX and DX are the centers ani half-ien&hs, kspectively, of the interval.

tangle is represented by the 4-tuple (c,, d,, Figure 11. Representation of the horizontal inter- c,, d,), which is interpreted as the Carte- vals of Figure 1 as ordered pairs (L, R), where L and sian product of a horizontal and a vertical R are the left and right endpoints, respectively, of the one-dimensional interval-that is, (c,, d,) interval. and (c,, $), respectively.6 This represen- tation is used by Hinrichs and Nievergelt yields an ordered pair (L, R) where L and [1983] and Hinrichs [1985a], and the fol- R correspond to the left and right endpoints lowing examples of its utility are due to of the interval, respectively. Figure 11 them. shows how the horizontal intervals would Proximity queries involving point and be represented using this method. rectangular query objects are easy to imple- In most applications the intervals are ment. Their answers are conic-shaped re- small. Therefore, for representation (l), L gions in the four-dimensional space formed and R are very close in value. L < R means by the Cartesian product of the two interval that the representative points are clustered query regions. This is equivalent to com- near and above the diagonal. Thus, the puting the intersection of the two query representative points are not well distrib- regions, but is much more efficient. It also uted and hence any data structure that is enables us to visualize our examples since based on organizing the embedding space of the horizontal and vertical intervals cor- the data (e.g., address computation), in respond to the projections of the query contrast to one based on the actual repre- responses on the c,-cl, and cY-dY planes, sentative points that are stored (e.g., com- respectively. parative search), will have to pay a price We illustrate our discussion with the col- for the empty half of the embedding space. lection of rectangles given in Figure 1 along On the other hand, Hinrichs and Nievergelt with query point P and query rectangles 1, [1983] point out that separating the loca- 2, and 3. Note that when the query objects tion parameters from the extension param- are not individual points or rectangles, the eters results in a smaller embedding space, representation of a rectangle as the Carte- which is filled more uniformly. For exam- sian product of two orthogonal intervals is ple, representation (3) is used in Figure 12, not that useful (e.g., query regions in the where the horizontal intervals are repre- form of an arbitrary line or circle). sented as an ordered pair (CX, DX) such For a point query, we wish to determine that CX is the centroid of the interval and all intervals that contain a given point, say DX is the distance from the centroid to the end of the interval. ’ The notation (c,, d,) corresponds to a point in a two- Bearing the above considerations in dimensional space. It is not the open one-dimensional mind, representation (3) seems to be the interval whose left and right endpoints are at c, and most appropriate. In such a case, a rec- d,, respectively.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles

DX DX

0 0 IO 20 30 P 40 (a)

DY DY 4

IO

0 0 IO 20 P 30 40 (b) (b)

Figure 13. Search region for a point query on P for Figure 14. Search region for a window query on (a) the horizontal intervals and (b) the vertical inter- query rectangle 1 of Figure 1 for (a) the horizontal vals of Figure 1. All intervals containing P are in the intervals and (b) the vertical intervals of that figure. shaded regions. Intervals appearing in the shaded All intervals that contain points of rectangle 1 are in regions of both (a) and (b) correspond to rectangles the shaded regions. Intervals appearing in the shaded that contain P. regions of both (a) and (b) correspond to rectangles that intersect rectangle 1. p. These intervals form a cone-shaped re- shaped region whose tip is the interval I. gion whose tip is an interval of length zero For example, the horizontal and vertical centered at P.~ For example, the horizontal intervals that overlap the horizontal and and vertical intervals containing P are vertical sides of query rectangle 1 are shown shown shaded in Figures 13a and 13b, re- shaded in Figures 14a and 14b, respectively. spectively. To find all the rectangles that To find all the rectangles that overlap the contain a given point, we access a specific query window, we access a specific region region in the four-dimension1 space defined in the four-dimensional space defined by by the Cartesian product of the horizontal the Cartesian product of the horizontal and and vertical point-in-interval query re- vertical interval-in-interval query regions. gions. For example, P is in the set of rec- For example, query rectangle 1 overlaps tangles with representative points in the the intersection of the shaded portions intersection of the shaded portions of Fig- of Figures 14a and 14b-that is, (B, D) ures 13a and 13b-that is, (C, D) is the is the intersection of (B, D, E, G) and intersection of (C, D, G) and (A, C, D). (4 B, C, JX A window query is a bit more complex. For a containment query, the one-dimen- In this case, the one-dimensional analog of sional analog is to find all the intervals that this query is to find all intervals that over- are totally contained within a given inter- lap a given interval, say I. Again, the set of val, say I. The set of contained intervals overlapping intervals consists of a cone- consists of a cone-shaped region whose tip is at I and that opens in the direction of ’ McCreight [1980] uses the same technique in con- smaller extent values. This makes sense junction with representation (1) to solve the problem. since all intervals within the cone are to-

ACM Computing Surveys, Vol. 20, No. 4, December 1988 294 . Hanan Samet

DX DX

E IO - . D IO l * G A . 0 I I + cx 0 cx 0 IO 20 30 40 0 IO 20 30 40 (a) (a)

DY DY A

A .

IO - C IO . E 2 D B . . 0 0 IO ?h. 20 30 4occy lb) (b)

Figure 15. Search regions for a containment query Figure 16. Search regions for an enclosure query on on query rectangle 2 of Figure 1 for (a) the horizontal query rectangle 3 of Figure 1 for (a) the horizontal intervals and (b) the vertical intervals of that figure. intervals and (b) the vertical intervals of that figure. All intervals that are contained in one of the intervals All figures that enclose one of the intervals forming forming rectangle 2 are in the shaded regions. Inter- rectangle 3 are in the shaded regions. Intervals ap- vals appearing in the shaded regions of both (a) and pearing in the shaded regions of both (a) and (b) (b) correspond to rectangles that are contained in correspond to the rectangles that enclose rectangle 3. rectangle 2.

enclosed) by all intervals within the cone. tally contained in the interval represented For example, the horizontal and vertical by the tip. For example, the horizontal and intervals that enclose the horizontal and vertical intervals that are contained in the vertical sides of query rectangle 3 are shown horizontal and vertical sides of query rec- shaded in Figures 16a and 16b, respectively. tangle 2 are shown shaded in Figures 15a To find all the rectangles that enclose the and 15b, respectively. To find all the rec- query window, we access a specific region tangles that are contained in the query in the four-dimensional space defined by window, we access a specific region in the the Cartesian product of the horizontal and four-dimensional space defined by the vertical enclose-interval query regions. For Cartesian product of the horizontal and example, query rectangle 3 contains the vertical contained-in-interval query re- intersection of the shaded portions of Fig- gions. For example, query rectangle 2 ures 16a and 16b-that is, (E). contains the intersection of the shaded por- In spite of the relative ease with which tions of Figures 15a and 15b-that is, (F). the above queries are implemented using For an enclosure query, the one-dimen- the representative point method with rep- sional analog is to find all the intervals that resentation (3), there are queries for which enclose the given interval, say I. The set of it is ill suited. For example, suppose we enclosing intervals consists of a cone- wish to solve the rectangle intersection shaped region whose tip is at I and that problem. The fact is, no matter which of opens in the direction of larger extent val- the three representations we use, in order ues. This is logical since the interval rep- to solve this problem we must intersect resented by the tip is contained (i.e., each rectangle with every other rectangle.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 295

The problem is that none of these represen- use of a balanced k-d tree to organize the tations is area oriented-that is, they rectangles whose representative point uses reduce a spatial object to a single repre- representation (1). The k-d tree [Bentley sentative point. Although the extent of the 19751 is a binary search tree where at each object is reflected in the representative level of the tree a different coordinate is point, the final mapping of the representa- tested when determining the direction in tive point in the four-dimensional space which a branch is to be made. Therefore, does not result in the preservation of near- in the two-dimensional case (i.e., a 2-d ness in the two-dimensional space from tree!), we compare x coordinates at the root which the rectangles are drawn. In other and at even levels (assuming the root is at words, two rectangles may be very close level 0) and y coordinates at odd levels. For (and possibly overlap), yet the Euclidean example, Figure 17 is the k-d tree corre- distance between their representative sponding to the data of Figure 2. points in four-dimensional space may be The balanced k-d tree is a k-d tree quite large, thereby masking the overlap- where at each level the number of nodes ping relationship between them. For ex- in the two subtrees is either equal or differ ample, even though rectangles B and D by 1. Lauther [1978] discusses the solution intersect query rectangle 1, we cannot eas- of the rectangle intersection problem using ily tell if they intersect each other except the balanced k-d tree. The solution is an by checking their sizes. adaptation of the O(N2) algorithm. It first Our discussion has emphasized represen- builds the tree (equivalent to a sorting pro- tation (3). Nevertheless, as we will see cess) and then traverses the tree in inorder below, the other representations are also and intersects each rectangle, say P, with commonly used. Interestingly, although a the remaining unprocessed rectangles (i.e., rectangle whose sides are parallel to the x the inorder successors of Pa). Two rectan- and y axes requires four values to be gles with sides parallel to the axes intersect uniquely specified, it is also frequently if their projections on the x axis intersect modeled by a representative point in a two- and their projections on the y axis inter- dimensional space. The representative sect. The one-dimensional analog of this point corresponds to the centroid of the condition has been used in the segment and rectangle or to one of its corners (e.g., lower interval tree solutions to the rectangle in- left). If rectangles are not permitted to tersection problem (see Sections 2.1 and overlap, then such a representation is suf- 2.2). More formally, we say that in order ficient to ensure that no two rectangles for rectangle Q to intersect rectangle P, all have the same representative point. Of four of the following conditions must be course, since two values do not uniquely satisfied: specify the rectangle, the remaining values are retained in the record corresponding to (1) xmin(Q)5 pax the rectangle that is associated with the (2) ymin(Q) 5 yrnax(P) representative point. If rectangles are per- (3) xmin(P) 5 pax mitted to overlap, then such a representa- (4) ymin(P) 5 yrnax(Q) tion means that there may be more than one record associated with a specific rep- Armed with this formulation of the prob- resentative point. lem, we see that there is no need to visit all Once a specific representative point of the inorder successors of P since when- method is chosen for the rectangle, we can ever one of these conditions fails to hold at use any one of a number of techniques for a node Q, the appropriate subtree of Q need representing multiattribute data to orga- not be searched. These conditions can be nize the collection of representative points. restated in the following manner, which is Again, the choice of representation depends to a large extent on the type of operations ’ In this discussion we use P to refer to both a rectangle that we will be performing. As an example, and its corresponding node in the tree. The correct Lauther [ 19781 and Rosenberg [1985] make meaning should be clear from the context.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 296 l Hanan Samet

(0,100 I- (100. loo)

(60,751 TORONTO (80.65) BUFFALO

t Y I(5,451 IDENVER (35,40) ” CHICAGO (25.35) t OMAHA

(B5.15) (50,10) ATLANTA MOBILE 1,5) MIAMI to,0 )

CHICAGO

DENVER MOBILE

Figure 17. A k-d tree corresponding to the points in Figure 2.

more compatible with the way in which the Now, we build a balanced k-d tree with balanced k-d tree is traversed: discriminators Ko, K,, K2, K3 correspond- ing to x,i,, ymin, -x,,,, and -ymax, respec- (5) xmin(Q)5 pax tively. Whenever we encounter node Q (6) ymin(Q)5 Ymax(P) discriminating on Kd such that Kd(Q) > (7) -&nax (8) 5 -xmin(P) K Cd+2jmod4(P),then all the nodes in the right (8) -~max(Q) 5 -Ymin(f’) subtree of Q need not be examined further.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 297

(4 (b)

Figure 18. Blocks examined when searching for points within a conic region for a collection of intervals represented by (a) a grid file and(b) a k-d tree.

Solving the rectangle intersection prob- file to organize the rectangles whose rep- lem as described above has an upper bound resentative point uses representation (3). A of O(N’) and a lower bound of O(N . grid file yields a partition of space into log,N). The lower bound is achieved when variable-sized blocks having a finite capac- pruning is assumed to occur at every right ity that are accessed with the aid of a subtree. When the rectangle intersection directory. For example, Figure 18a is an problem is posed in terms of conditions (5)- example space partition induced by a grid (8), the relation 5 between Q and P is said file. The result is that proximity queries to be a dominance relation [Preparata and are answered by examining all grid blocks Shamos 19851. In such a case, the intersec- that intersect the cone-shaped search re- tion problem is called the dominance merge gions. They prefer this method to one based problem by Preparata and Shamos [ 19851. on a tree (e.g., the k-d tree) because the Given F rectangle intersections, the algo- relevant grid blocks are in contiguous re- rithm of Preparata and Shamos solves the gions, whereas in a tree, contiguous blocks rectangle intersection problem in O(N . may appear in different subtrees. Hinrichs logs N + F) time instead of the optimal and Nievergelt are quite concerned with O(N . log, N + F) time. reducing the number of disk access opera- Building a balanced k-d tree takes more tions necessary to process such queries. For time than an ordinary k-d tree since medi- example, Figure 18 shows a conic-shaped ans must be computed in order to assure search region when the rectangles are or- balance. The balanced k-d tree makes point ganized with a grid file (Figure 18a) and a searches and region searches quite efficient. k-d tree (Figure 18b). In the case of a grid Rosenberg [1985] compares the perform- file, blocks A, B, C, D, E, and F would be ance of the k-d tree, a point method in his examined, whereas for a k-d tree blocks G, formulation, with linked lists’ and the area- H, I, J, K, L, and M would be examined. based quadtree approaches discussed in Note that blocks I and K are in a different Section 4.1 below and concludes that the subtree of the k-d tree than blocks G, H, J, point methods are superior. However, he L, and M. In the worst case, solving the only takes into account point and window rectangle intersection problem when using queries. Comparisons using queries such as a grid file takes O(N’) time. This is finding all intersecting rectangles may lead achieved by using the naive method of Sec- to a different conclusion. tion 2. The expected cost, however, will be Hinrichs and Nievergelt [ 19831 and Hin- lower since it is assumed that the points richs [1985a, 1985b] make use of the grid corresponding to the rectangles are well distributed among the grid blocks. For an analysis of grid file methods on randomly ’ The rectangles are stored in a doubly linked list. distributed point data, see Regnier [ 19851.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 298 l Hanan Samet

The techniques discussed above for or- a representative point. Instead, there is a ganizing the collection of representative predefined set of representative points with points assume that the representative point which rectangles can be stored. In some lies in four-dimensional space. Another sense this is analogous to hashing [Knuth possibility is to use a representative point 19731, where the representative points cor- in two-dimensional space. For example, respond to buckets. These techniques, suppose that in our application we must which we term CIF quadtrees, have been perform a point query (i.e., determine the developed independently by Kedem [ 19821 rectangles that contain a given point). In (called a quad-CIF tree) and by Abel and this case, when the representative point is Smith [ 19831. the centroid, some of the tree representa- In this section we expand further on the tions require that we search the entire data area-based approaches. We first present a structure. For example, this is the case for detailed implementation of one variant of the point quadtree (see Figure 4) because the CIF quadtree, which is related to the the rectangle that is centered at a given MX quadtree representation of point data point can lie in all of the quadrants. Of [Samet 19841. Next, we describe some course, pruning can occur at deeper levels quadtree-based alternatives that permit a in the tree. In contrast, using the lower left rectangle to be associated with more than corner as the representative point may per- one quadtree block. We conclude with a mit the pruning of up to three quadrants in discussion of the R-tree and some of its the search. For instance, when the query variants. It is a hierarchy of rectangular point lies in the SW quadrant, then no regions that contain the data rectangles. rectangle whose representative point lies in The hierarchy is constructed by rules sim- the NW, NE, or SE quadrants can contain ilar to those used to define a B-tree. The the query point. regions need not be disjoint. Analyzing the space requirements of these representa- 4. AREA-BASED METHODS tions as well as the execution time of algorithms that use them is quite difficult, The problem with using trees in conjunc- since it depends heavily on the distribution tion with representative point methods of the data. In most cases, a limited part of such as those discussed in Section 3 is that the tree must be traversed and thus the the placement of the node in the tree (i.e., execution time depends, in part, on the its depth) does not reflect the size (i.e., the depth and the shape of the tree. spatial extent) of the rectangle. It primarily depends on the location of the represent- 4.1 MX-CIF Quadtrees ative point. In this section we focus on alternative representations provided by The MX-CIF quadtree associates each rec- area-based methods that associate each rec- tangle, say R, with the quadtree node cor- tangle with blocks that contain it or blocks responding to the smallest block that con- that it contains. The sizes and positions of tains R in its entirety. Rectangles can be these blocks may be predetermined as is associated with both terminal and nonter- the case in an approach based on the region minal nodes. Subdivision ceases whenever quadtree. This need not be the case, how- a node’s block contains no rectangles. Al- ever, nor must the blocks be disjoint. ternatively, subdivision can also cease once As an example of a representation based a quadtree block is smaller than a predeter- on the region quadtree, suppose that we mined threshold size. This threshold is represent each rectangle by its minimum often chosen to be equal to the expected enclosing quadtree block (i.e., a square). size of the rectangle [Kedem 19821. Fig- The rectangle is associated with the center ure 19 is the MX-CIF quadtree for the set of the quadtree block. Of course, more than of rectangles in Figure 1. Once a rectangle one rectangle can be associated with a given is associated with a quadtree node, say P, enclosing square and a technique must be it is not considered to be a member of any used to differentiate among them. Observe that in this case, we do not explicitly store lo CIF denotes Caltech Intermediate Form.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 299

E D 0 (4 (b)

Figure 20. Binary trees for the y axes passing through (a) the root of the MX-CIF quadtree in Figure 19 and (b) the NE son of the root of the MX-CIF quadtree in Figure 19.

(a) one set, and all members of S that intersect the line y = CY form the other set. Equiv- alently, these sets correspond to the rec- {Ad tangles intersecting the y and x axes, respectively, passing through (CX, CY). If a rectangle intersects both axes (i.e., it con- tains the centroid of P’s block), then we adopt the convention that it is stored with the set associated with the y axis. These subsets are implemented as binary trees (really ), which in actuality are one- dimensional analogs of the MX-CIF quad- (b) tree. For example, Figure 20 illustrates the Figure 19. The MX-CIF quadtree (b) and the block binary tree associated with the y axes pass- decomposition induced by it (a) for the rectangles in ing through the root and the NE son of the Figure 1. root of the MX-CIF quadtree of Figure 19. The subdivision points of the axis lines are shown by tick works in Figure 19. of the sons of P. For example, in Figure 19, At this point, the following observations rectangle G overlaps the space spanned by can be made. The MX-CIF quadtree is both the SE son of the root and the NE related to the region quadtree in the same son of the SE son of the root; yet G is only way as the interval tree is related to the associated with the SE son of the root. segment tree. The MX-CIF quadtree is the It should be clear that more than one two-dimensional analog of the tile tree” rectangle can be associated with a given (without the tertiary structure). The tile enclosing block (i.e., node). There are sev- tree and the one-dimensional MX-CIF eral ways of organizing these rectangles. quadtree are identical. In particular, when The simplest solution is to maintain a rectangles are not permitted to overlap, the linked list of these rectangles. Another ap- secondary structures of the tile tree consist proach, due to Kedem [ 19821, is described of at most one rectangle. When the tile tree below. is used in this context, it is not a complete Let P be a quadtree node and let S be binary tree. Alternatively, it is not neces- the set of rectangles that are associated sarily balanced since the subdivision points with P. Members of S are organized into are fixed by virtue of regular decomposition two sets according to their intersection (or rather than being determined by the end- colinearity of their sides) with the lines points of the domain of rectangles as in the passing through the centroid of P’s block. definition of the interval tree. We shall use the term axes or axis lines to refer to these lines. For example, consider I1 The analogy is with the tile tree instead of the node P centered at (CX, CY). All members interval tree because the MX-CIF quadtree is based of S that intersect the line x = CX form on a regular decomposition.

ACM Computing Surveys, Vol. 20, No. 4, December 1988 300 l Hanan Samet

A rectangle is inserted into an MX-CIF to perform a union of the two MX-CIF quadtree by a top-down search for the po- quadtrees. Boolean queries can also be sition that it is to occupy. This position is easily handled. determined by a two-step process. First, the When the rectangles are allowed to in- first subdivision point must be located such tersect, reporting the pairs of rectangles that at least one of its axis lines (i.e., the that intersect each other is achieved by quadrant lines emanating from the subdi- traversing the MX-CIF quadtree and for vision point) intersects the input rectangle. each node examining all neighboring nodes Second, having found such a point and an that can contain rectangles that intersect axis, say point P and axis V, the subdivision it. In the worst case, for some rectangles we process is repeated for the V axis until the may have to examine the entire MX-CIF first subdivision point that is contained quadtree. If this is the case, however, the within the rectangle is located. During the remaining rectangles will not require this process of locating the destination position much work. Nevertheless, the worst-case for the input rectangle, the space spanned execution time of this task is O(n . W) for by the MX-CIF quadtree may have to be a tree of maximum depth n with N rec- repeatedly subdivided (termed splitting) tangles. The expected behavior should be creating new nodes in the process. In the better. worst case, each rectangle is at the maxi- Abel and Smith [1983] also represent a mum depth of the tree, say n.12 Thus, the collection of rectangles by an MX-CIF worst-case cost of building an MX-CIF quadtree. The difference between their ap- quadtree for N rectangles is O(n . N) in proach and that of Kedem [1982] is that space and time. Of course, the expected they do not use binary trees, or any other behavior should be better. It should be clear data structure, to organize the rectangles that the shape of the resulting MX-CIF that are associated with each quadtree node quadtree is independent of the order in separately. They represent each rectangle which the rectangles are inserted into it. by its locational code. The locational code Deletion of nodes is more complex and may represents a sequence of 2-bit directional require collapsing of nodes-that is, the codes that locate the quadtree node along a direct counterpart of the node splitting path from the root of the quadtree. The process outlined above. code also reflects the level at which the The most common operations are deter- node is found. These codes are subse- mining whether a given rectangle overlaps quently organized in a B+-tree [Comer (i.e., intersects) any of the existing rectan- 19791. Note that many rectangles have gles or performing a window query. Another identical locational codes, and thus the rec- popular query seeks to determine whether tangle dimensions must also be stored along one collection of rectangles can be overlaid with the locational codes in the B+-tree. on another collection without any of the For example, for the set of rectangles in component rectangles intersecting one an- Figure 19, rectangles B, C, and D have the other. These two operations can be imple- same locational code, and so do rectangles mented by using variants of algorithms A and E. developed for handling set operations (i.e., union and intersection) in region-based quadtrees [Hunter and Steiglitz 1979; 4.2 Multiple Quadtree Block Representations Shneier 19811. The window query is an- swered by intersecting the query window One of the problems with the MX-CIF with the MX-CIF quadtree. The overlay quadtree and other representations that as- query is answered by a two-step process. sociate each rectangle with the smallest The two MX-CIF quadtrees are first inter- enclosing quadtree block is that determin- sected. If the result is empty, then they can ing how many rectangles intersect a win- be safely overlaid, and all that is needed is dow (e.g., in the form of a rectangle) may be quite costly. The problem is that the I2 n is the sum of the maximum depths of the MX- quadtree nodes that intersect the query rec- CIF quadtree and of the binary tree. tangle may contain many rectangles that

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles 301 do not intersect the query rectangle, yet each one of them must be individually com- pared with the query rectangle to determine the existence of a possible intersection. For example, consider the MX-CIF quadtree in Figure 19, which corresponds to the collection of rectangles given in Fig- ure 1. Although query rectangle 1 (see Fig- ure 1) is in the NE quadrant of the root of the MX-CIF quadtree, we still have to check some of the rectangles that are stored at the root of the entire quadtree since these rectangles could conceivably overlap the query rectangle. This work could be avoided (a) by using a more compact (in an area sense) representation of each rectangle. Such a representation would use more, and smaller, quadtree blocks to represent each rectangle, but the total area of the blocks would be considerably less than that of the smallest enclosing quadtree block. The result is that more rectangles would be eliminated from consideration due to the pruning that occurs during the search of the quadtree. A number of alternatives are available to achieve this effect. They are examined briefly below. One possibility is to use a region quadtree IF) representation for each rectangle. Such a (b) representation would lead to many nodes Figure 21. The expanded MX-CIF quadtree (b) and since its underlying decomposition rule re- the block decomposition induced by it (a) for the quires that the block corresponding to each rectangles in Figure 1. node be homogeneous (i.e., that it be totally contained within one of the rectangles or not be in any of the rectangles). Permitting rectangles to overlap forces a modification Bi (i E (NW, NE, SW, SE]) such that Ri is of the decomposition rule. In particular, it the portion of R, if any, that is contained implies that decomposition ceases when a in B;. Next, for each Bi we find the mini- block is totally contained within one or mum enclosing quadtree block, say Di, that more rectangles. If a block is contained in contains Ri. Now, each rectangle is repre- more than one rectangle, however, it must sented by the set of blocks consisting of Die be totally contained in all of them. We term such a representation an expanded Abel and Smith [1985] present a less MX-CIF quadtree. radical alternative. They propose that in- As an example of the expanded MX-CIF stead of using the minimum enclosing quadtree, consider Figure 21, which corre- quadtree block, each rectangle is repre- sponds to the collection of rectangles of sented by a collection of enclosing quadtree Figure 1. Several items are worthy of note. blocks. They suggest that the collection First, each node appears at least one level contain a maximum of four blocks, al- lower in the expanded MX-CIF quadtree though other amounts are also possible. than it did in the MX-CIF quadtree. Sec- The four blocks are obtained by determin- ond, some of the Di may be empty (e.g., ing the minimum enclosing quadtree block, rectangle A in Figure 21 is covered by say B, for each rectangle, say R, and then blocks 2 and 4; rectangle F in Figure 21 is splitting B once to obtain quadtree blocks covered by block 14). Third, the covering

ACM Computing Surveys, Vol. 20, No. 4, December 1988 302 l Hanan Samet blocks are not necessarily of equal size (e.g., rectangle E in Figure 21 is covered by blocks 4 and 12). It should be clear that the area covered by the collection of blocks Di is not greater than that of B. The worst-case execution time for build- ing the expanded MX-CIF quadtree and the space requirements are the same as for the MX-CIF quadtree-that is, O(n. N) for a tree of maximum depth n with N rectangles. The worst-case execution time of the rectangle intersection problem is also the same as that for the MX-CIF quad- tree-that is, O(n . N’). Abel and Smith (a) suggest that the search process can be made more efficient by applying the splitting process again to the blocks Die Of course, the more times that we split, the closer we get to the region quadtree representation of the rectangles. Also, this increases the space requirement and the insertion and deletion costs. Shaffer [1986] presents a pair of data structures termed an RR quadtree that is somewhat related to the expanded MX- CIF quadtree. Two variants are given. The first, called an RR1 quudtree makes use of a decomposition rule that splits until each node contains either just one rectangle or all of the rectangles in the node intersect Figure 22. The RR, quadtree (b) and the block de- each other. Thus, all rectangles are associ- composition induced by it (a) for the rectangles in ated with terminal nodes. When rectangles Figure 1. are not permitted to overlap, this decom- position rule means that no block can con- tain a part of more than one rectangle. For a part of a chain of connected rectangles so example, consider Figure 22, which is the that each rectangle in the chain is also in RR1 quadtree corresponding to the collec- the node. The resulting structure is called tion of rectangles of Figure 1. Note that an RR, quudtree. For example, consider Fig- node 3 had to be decomposed further since ure 23, which is the RR2 quadtree corre- rectangles B, C, and D do not mutually sponding to the collection of rectangles of intersect each other. Figure 1. Note that now, unlike the RR1 The storage requirements of the RR1 quadtree, node 3 need not be further de- quadtree are much higher than those of the composed to deal with rectangles B, C, and MX-CIF and expanded MX-CIF quad- D. These three rectangles form a chain of trees. This is due to the need to decompose intersecting rectangles but they do not mu- the collection when many rectangles are tually intersect each other. near each other without mutually inter- The RR2 quadtree still requires consid- secting each other-for example, a chain erably more storage than the MX-CIF and formed by intersecting rectangles. This expanded MX-CIF quadtrees. The advan- problem is partially resolved by loosening tage of the RR quadtree family, however, is the decomposition criterion of the RR1 that if two rectangles intersect, then they quadtree to permit a node, say N, to contain must be stored in the same node. This a pair of rectangles if they intersect or are makes window queries quite efficient since

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles 303 such nodes and each can contain N rectan- gles. Solving the rectangle intersection problem is quite easy since it is done by traversing the tree and reporting all nodes that contain more than one rectangle. The time required is the same as the space re- quirement. It can be shown that each inter- section is only reported once.

4.3 R-Trees

I I I The R-tree of Guttman [1984] is a hierar- chical data structure that is derived from (a) the B-tree. Each node in the tree corre- sponds to the smallest d-dimensional rec- tangle that encloses its son nodes. The leaf nodes contain pointers to the actual geo- metric objects in the database, instead of sons. The objects are represented by the smallest aligned rectangle in which they are contained. Often the nodes correspond to disk pages, and thus the parameters de- fining the tree are chosen so that a small number of nodes is visited during a spatial query. Note that rectangles corresponding to different nodes may overlap. Also, a rec- IF} {G}{G} tangle may be spatially contained in several nodes, yet it can only be associated with (b) one node. This means that a spatial query Figure 23. The RR, quadtree (b) and the block de- may often require several nodes to be vis- composition induced by it (a) for the rectangles in ited before ascertaining the presence or ab- Figure 1. sence of a particular rectangle. Our discus- sion is limited to the representation of rectangles in two dimensions. fewer rectangles must be examined for in- The basic rules for the formation of an tersection. In particular, the number of rec- R-tree are very similar to those for a B- tangle comparisons required by a window tree. All leaf nodes appear at the same level. query in an RR2 quadtree is equal to the Each entry in a leaf node is a 2-tuple of the number of comparisons that would be made form (R, 0) such that R is the smallest were the query rectangle being inserted into rectangle that spatially contains data object the tree. 0. Each entry in a nonleaf node is a 2-tuple The space requirements of the region of the form (R, P) such that R is the small- quadtree and the RR quadtree family are est rectangle that spatially contains the dependent on the amount of space that is rectangles in the child node pointed at by required to store an individual rectangle. In P. An R-tree of order (m, M) means that all three cases, for a tree of maximum depth node in the tree, with the exception of the n, a rectangle requires O(2”) space. For N root, contains between m 5 TM/21 and M rectangles, the time required to build the entries. The root node has at least two region quadtree is O(N. 2’7, whereas for entries unless it is a leaf node. the RR quadtree family it may be as high For example, consider the collection of as O(N2 . 2”) since each rectangle must be rectangles given in Figure 1, and treat the checked against the rectangles in each node query rectangles (i.e., 1, 2, and 3) as ele- in which it is contained-there are O(27 ments of the collection so that there is a

ACM ComputingSurveys, Vol. 20, No. 4, December 1988 304 ’ Hanan Samet

r------I RI IR21 r-i I I u u I 0 1 I ;y+ff+g Id ’ R31 R41 I I .: :- 1 I I II :-:[I L------1E Al II 11E131 BICID1121FIG (a) Figure 25. (a) Four rectangles and the splits that would be induced (b) by minimizing the total area of the covering rectangles and (c) by minimizing the area common to the covering rectangles of both nodes.

distributed in the two nodes. Splits are propagated up the tree. There are many possible ways to split a node. One possible goal is to distribute the records among the nodes so that the like- lihood that the nodes will be visited in subsequent searches will be reduced. This is accomplished by minimizing the total .------A areas of the covering rectangles for the nodes (i.e., coverage). An alternative goal (b) is to reduce the likelihood that both nodes are examined in subsequent searches. This Figure 24. (a) R-tree for the collection of rectangles is accomplished by minimizing the area in Figure 1 and (b) the spatial extents of the enclosing rectangles. common to both nodes (i.e., overlap). Of course, at times these goals may be con- tradictory. For example, consider the four total of 10 rectangles. Let A4 = 3 and m = rectangles in Figure 25a. The first goal is 2. One possible R-tree for this collection is satisfied by the split in Figure 25b, whereas given in Figure 24a. Figure 24b shows the the second goal is satisfied by the split in spatial extent of the rectangles of the nodes Figure 25~. in Figure 24a, with broken lines denoting Deletion of a rectangle, say R, from an the rectangles corresponding to the sub- R-tree proceeds by locating the leaf node, trees rooted at the nonterminal nodes. Note say L, containing R and removing R from that the R-tree is not unique. Its structure L. Next, adjust the covering rectangles on depends heavily on the order in which the the path from L to the root of the tree while individual rectangles were inserted into removing all nodes in which underflow oc- (and possibly deleted from) the tree. curs and adding them to the set U. Once The algorithm for inserting an object the root node is reached, if it has just one (i.e., a record corresponding to its enclosing son, then the son becomes the new root. rectangle) in an R-tree is analogous to that The nodes in which underflow occurred used for B-trees. New rectangles are added (i.e., members of U) are reinserted at the to leaf nodes. The appropriate leaf node is root. Elements of U that correspond to leaf determined by traversing the R-tree start- nodes result in the placement of their con- ing at its root and at each step choosing stituent rectangles in the leaf nodes, the subtree whose corresponding rectangle whereas other nodes are placed at a level would have to be enlarged the least. Once so that their leaf nodes are at the same level the leaf node has been determined, then as those of the whole tree. check if insertion of the rectangle will cause The deletion algorithm for an R-tree dif- the node to overflow. If yes, then it must fers from that for a B-tree in that in the be split and the M + 1 records must be case of underflow, nodes are reinserted in-

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 305 stead of being merged with adjacent nodes. node. This is a static method that results The difficulty is that there is no concept of in each node being filled to capacity. adjacency in an R-tree, although we could Although the packed R-tree does not nec- merge with the sibling whose area will be essarily result in a minimum coverage and/ increased the least or even just distribute or overlap, empirical tests [Roussopoulos the elements of the underlowing node and Leifker 19851 of its performance on among its siblings. Nevertheless, reinser- point searches in a database of two-dimen- tion is used by Guttman [1984] because of sional points show its use to lead to signif- a feeling that it enables the tree to reflect icant improvements vis a vis an R-tree that the changing spatial structure of the data was built using the conventional R-tree dynamically rather than the gradual deg- insertion routine. In these tests each node radation that could arise when a rectangle was constructed by selecting an object from maintains the same parent throughout its a spatially sorted list and then adding its lifetime! M - 1 nearest neighbors. Once all the nodes Searching for points or regions in an R- at a given level have been constructed, the tree is straightforward. The only problem process is applied recursively, forming is that a large number of nodes may have nodes at successively higher levels in the to be examined since a rectangle may be tree until just one node remains. A better contained in the covering rectangles of approach, although far costlier from a many nodes while its corresponding record combinatorial standpoint, is to choose is only contained in one of the leaf nodes the M objects simultaneously so that the (e.g., in Figure 24, rectangle 1 is contained area of the resulting covering rectangle is in its entirety in Rl, R2, R3, and R5). For minimized. example, suppose we wish to determine the Another alternative to the R-tree is the identity of the rectangle element in the R+-tree [Sellis et al. 1987; Stonebraker et collection of rectangles given in Figure 1, al. 19861, which is an extension of the k-d- which contains point Q at coordinates B-tree [Robinson 19811 to deal with rectan- (21,24). Since Q can be in either of Rl and gles. The motivation for the R+-tree is to R2, we must search both of their subtrees. avoid overlap among the bounding rectan- Searching Rl first, we find that Q could gles. In particular, all bounding rectangles only be contained in R3. Searching R3 does (i.e., at levels other than the leaf) are non- not lead to the rectangle that contains Q overlapping. Thus, each rectangle is asso- even though Q is in a portion of rectangle ciated with all the bounding rectangles that D that is in R3. Thus, we must search R2, it intersects. The result is that there may and we find that Q can only be contained be several paths starting at the root to the in R5. Searching R5 results in locating D, same rectangle. This will lead to an increase which is the desired rectangle. in the height of the tree. Retrieval time, The insertion algorithm, and the ac- however, is sped up. The cell tree of companying node splitting techniques, Gunther [1987] is similar to the R+-tree, described above are based on a dynamic with the principal difference being that the database. If the database can be expected nonleaf nodes of the cell tree are convex to be static and all of the objects are known polyhedra instead of bounding rectangles. a priori, then a different technique can be Figure 26 is an example of one possible used to build an R-tree. Roussopoulos and R+-tree for the collection of rectangles in Leifker [1985] propose the use of a packed Figure 1. Once again, the query rectangles R-tree, which is an R-tree that is built by (i.e., 1, 2, and 3) are treated as elements of successively applying a nearest-neighbor the collection so that there is a total of 10 relation to group objects in a node after the rectangles. This particular tree is of order set of objects has been sorted according to (2, 3), although in general it is not possible a spatial criterion. Once an entire level of to always guarantee that all nodes will have the tree is built, the algorithm is reapplied a minimum of two entries. Notice that rec- to add nodes at the next higher level, ter- tangles D and E appear in three different minating when a level contains just one nodes, whereas rectangle B appears in two

ACM Computing Surveys, Vol. 20, No. 4, December 1988 306 . Hanan Samet

(4

IlIT- I, -lli4 , IL==--====_==_=-::= I

tb)

Figure 26. (a) R’-tree for the collection of rectangles in Figure 1 and (b) the spatial extents of the enclosing rectangles.

different nodes. Of course, other variants grouping of the rectangles at the leaf level. are possible since the R+-tree is not unique. The latter can be achieved by applying It is interesting to note that the decom- heuristics similar to those used to build a position into blocks induced by the R+-tree packed R-tree. is similar to the way a region quadtree Assume that the R-tree and the R+-tree would be used to represent a collection of are constructed in a batch manner-that rectangles (see Section 4.2). Since the R+- is, all the rectangles are known before the tree is an extension of the k-d-B-tree, it has construction and hence are arbitrarily a drawback that B-tree performance guar- grouped together. For N rectangles, the antees are no longer valid. For example, construction time and space requirements pages are not guaranteed to be 50% full of these two data structures are both O(N) without very complicated record insertion and O(N2), respectively. The reason for the and deletion procedures. Nevertheless, em- higher costs for the R+-tree is that a rec- pirical tests by Faloutsos et al. [ 19871 reveal tangle may appear in N nodes because of reasonable behavior in comparison to the its intersection with N other rectangles. conventional R-tree. These tests were cou- This analysis assumes that no optimization pled with a limited analysis of the behavior is performed when a node overflows. In of the two data structures when used to both cases, the worst-case execution time represent one-dimensional intervals of of the rectangle intersection problem is equal length by transforming them to O(N2). points in two dimensions using represen- tation (2) of Section 3. Sellis et al. [1987] suggest that performance of the R+-tree 5. CONCLUDING REMARKS can be improved by a judicious choice of In this tutorial we gave an overview of a partition lines, as well as by a careful initial number of different hierarchical represen-

ACM Computing Surveys, Vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles 9 307 tations for collections of rectangles. The gle retrieval problem. Comput. Vision Graph. Zm- choice of a representation is not clear-cut. age Process. 24, 1 (Oct.), 1-13. It depends on the problem domain and the AHO, A. V., HOPCROFT, J. E., AND ULLMAN, J. D. 1974. The Design and Analysis of Computer tasks to be performed. Although our cov- Algorithms. Addison-Wesley, Reading, Mass. erage has been limited, we have seen that BENTLEY, J. L. 1975. Multidimensional binary the various methods are quite similar. In search trees used for associative searching. particular, we have shown a close relation- Commun. ACM 18,9 (Sept.), 509-517. ship between quadtree like area-based rep- BENTLEY, J. L. 1977. Algorithms for Klee’s rec- resentations and the representations used tangle problems. Computer Science Department, to support solutions based on the plane- Carnegie-Mellon University, Pittsburgh. sweep paradigm. BENTLEY, J. L. 1979. Decomposable searching prob- In retrospect, this relationship is not sur- lems. Znf. Process. L&t. 8, 5 (June), 244-251. prising. It is based on the observation that BENTLEY, J. L., AND MAURER, H. A. 1980. Efficient worst-case data structures for range searching. algorithms that make use of traversals of a Acta Znf. 13, 2, 155-168. quadtree are in actuality performing a two- BENTLEY, J. L., AND OTTMANN, T. A. 1979. dimensional plane sweep. Furthermore, a Algorithms for reporting and counting geometric quadtree decomposition is really a multi- intersections. IEEE Trans. Comput. 28,9 (Sept.), dimensional sort. In contrast, solutions 643-647. based on representations such as the inter- BENTLEY, J. L., AND WOOD, D. 1980. An optimal val tree perform a sort in one dimension worst-case algorithm for reporting intersections of rectanrrles. IEEE Trans. Comnut. 29. 7 (Julv).“_, and then a sweep in the other direction. Of 571-577.- course, a similar solution could also be de- BUCHER, W., AND EDELSBRUNNER, H. 1983. On ex- vised in a multidimensional environment. pected and worst-case segment trees. In Advances In such a case, the concept of an active in Computing Research, vol. 1, Computational border [Samet and Tamminen 1985, 19881 Geometry, F. P. Preparata, Ed. JAI Press, Green- wich, Conn., pp. 109-125. used in the multidimensional plane sweep CHAZELLE, B., AND GUIBAS, L. J. 1986a. Fractional embodied by a quadtree traversal is the cascading: I. A data structuring technique. Algo- analog of the scan line in the one-dimen- rithmica 1, 2, 133-162. sional plane-sweep. At any instant in a CHAZELLE, B., AND GUIBAS, L. J. 1986b. Fractional traversal of the quadtree, the active border cascading: II. Applications. Algorithmica 1, 2, represents the boundary between the nodes 163-191. that have been processed and those that COMER, D. 1979. The ubiquitous B-tree. ACM Com- have not. The similarity is completed by put. Surv. 11, 2 (June), 121-137. devising data structures to represent the EDELSBRUNNER, H. 1980a. Dynamic rectangle intersection searching. Institute for Information active border that have properties analo- Processing Rept. 47, Technical University of gous to those of the segment and interval Graz, Graz, Austria. trees. It would be interesting to explore this EDELSBRUNNER, H. 1980b. Dynamic data structures relationship further. for orthogonal intersection queries. Institute for Information Processing. Rept. 59, Technical Uni- ACKNOWLEDGMENTS versity of Graz, Graz, Austria. EDELSBRUNNER, H. 1982. Intersection problems in I have benefitted greatly from many discussions with computational geometry. Institute for Informa- Mike Dillencourt as this paper was written. I would tion Processing Rept. 93, Technical Univ. of also like to think Timos Sellis for his comments on Graz, Graz, Austria. R-trees, and Azriel Rosenfeld and an anonymous ref- EDELSBRUNNER, H. 1983a. A new approach to rec- eree for a thorough review of the paper. The support tangle intersections: Part I. Znt. J. Comp. Math. of the National Science Foundation under Grant IRI- 13,3-4.209-219. 88-02457 is gratefully acknowledged. EDELSBRUNNER, H. 1983b. A new approach to rec- tangle intersections: Part II. Znt. J. Comput. REFERENCES Math. 13,3-4, 221-229. EDELSBRUNNER, H., GUIBAS, L. J., AND STOLFI, J. ABEL, D. J. 1985. Some elemental operations on 1986. Optimal point location in a monotone linear quadtrees for geographic information sys- subdivision. SIAM J. Comput. 15, 2 (May), tems. Comput. J. 28, 1 (February), 73-77. 317-340. ABEL, D. J., AND SMITH, J. L. 1983. A data structure FALOUTSOS, C., SELLIS, T., AND Rousso~ou~os, N. and algorithm based on a linear key for a rectan- 1987. Analysis of object oriented spatial access

ACM Computing Surveys, Vol. 20, No. 4, December 1988 308 l Hanan Samet

methods. In Proceedings of the SZGMOD Confer- in design rule verification of integrated circuits. ence (San Francisco, May). ACM, New York, pp. J. De&m Automat. Fault-Tolerant Comput. 2, 3 426-439. (July), 241-247. FINKEL, R. A., AND BENTLEY, J. L. 1974. Quad trees: LEE, D. T. 1983. Maximum clique problem of rectan- A data structure for retrieval on composite keys. gle graphs. In Advances in Computing Research. Acta Znf. 4, 1, l-9. Vol. 1, Computational Geometry. F. P. Preparata, FREDKIN, E. 1960. Trie memory. Commun. ACM 3, Ed. JAI Press, Greenwich, Conn., pp. 91-107. 9 (Sept.) 490-499. LEE, D. T., AND PREPARATA, F. P. 1982. An im- GUIBAS, L. J., AND SEDGEWICK, R. 1978. A dichro- proved algorithm for the rectangle enclosure matic framework for balanced trees. In Proceed- problem. J. Algorithms 3, 3 (Sept.), 218-224. ings of the 19th Annual IEEE Symposium on the MATSUYAMA, T., HAO, L. V., AND NAGAO, M. Foundations of Computer Science (Ann Arbor, 1984. A file organization for geographic infor- Mich., Oct.). IEEE, New York, pp. 8-21. mation systems based on spatial proximity. Com- GUNTHER, 0. 1987. Efficient structures for geomet- put. Vision Graph. Image Process. 26, 3 (June), ric data manaaement. Ph.D. dissertation, UCB/ 303-318. ERL M87/77,-Electronics Research Laboratory, MCCREIGHT, E. M. 1980. Efficient algorithms for College of Engineering, Univ. of California at enumerating intersecting intervals and rectan- Berkeley, Berkeley, Calif. gles. Report CSL-80-9, Xerox Palo Alto Research GUTTMAN, A. 1984. R-trees: A dynamic index struc- Center, Palo Alto, Calif. (June). ture for spatial searching. In Proceedings of the MCCREIGHT, E. M. 1985. Priority search trees. SZGMOD Conference (Boston. June). ACM. New SIAM J. Comput. 14,2 (May), 257-276. York, pp. 47-57. ’ ’ NIEVERGELT, J., HINTERBERGER, H., AND SEVCIK, HARARY, F. 1969. Graph Theory. Addison-Wesley, K. C. 1984. The grid file: An adaptable, sym- Reading, Mass., 1969. metric multikey tile structure. ACM Trans. Da- HINRICHS, K. 1985a. The grid file system: Imple- tabase Syst. 9, 1 (Mar.), 38-71. mentation and case studies of applications. Ph.D. ORENSTEIN, J. A. 1982. Multidimensional tries used dissertation, Institut fur Informatik, ETH, for associative searching. Znf. Process. Lett. 14, 4 Zurich, Switzerland. (June), 150-157. HINRICHS, K. 1985b. Implementation of the grid tile: OVERMARS, M. H. 1988. Geometric data structures Design concepts and experience. BIT 25, 4, for computer graphics: An overview. In Theoret- 569-592. ical Foundations of Computer Graphics and CAD, HINRICHS, K., AND NIEVERCELT, J. 1983. The grid R. A. Earnshaw, Ed. Springer-Verlag, Berlin, file: A data structure designed to support prox- pp. 21-49. imity queries on spatial objects. In Proceedings of PREPARATA, F. P., AND SHAMOS, M. I. 1985. the WG’83 (International Workshop on Graph- Computational Geometry: An Introduction. Sprin- theoretic Concepts in Computer Science), M. Nag1 ger-Verlag, New York. and J. Perl, Eds. (Trauner Verlag, Linz, Austria), REGNIER, M. 1985. Analysis of grid file algorithms. pp. 100-113. BIT 25,2,335-357. HUNTER, G. M. 1978. Efficient computation and REQUICHA, A. A. G. 1980. Representations of rigid data structures for graphics. Ph.D. dissertation, solids: Theory, methods, and systems. ACM Com- Dept. of Electrical Engineering and Computer put. Suru. 12, 4 (Dec.), 437-464. Science, Princeton Univ., Princeton, N.J. ROBINSON, J. T. 1981. The k-d-B-tree: A search HUNTER, G. M., AND STEIGLITZ, K. 1979. structure for large multidimensional dynamic in- Operations on images using quad trees. IEEE dexes. In Proceedings of the SZGMOD Conference Trans. Pattern Anal. Mach. Zntell. 1, 2 (Apr.), (Ann Arbor, Mich., Apr.). ACM, New York, pp. 145-153. 10-18. KEDEM, G. 1982. The quad-CIF tree: A data struc- ROSENBERG, J. B. 1985. Geographical data struc- ture for hierarchical on-line algorithms. In Pro- tures compared: A study of data structures sup- ceedings of the 19th Design Automation Confer- porting region queries. IEEE Trans. Comput. ence (Las Vegas, June), pp. 352-357. Aided Design 4, 1 (Jan.), 53-67. KLEE, V. 1977. Can the measure of U [a,, bi] be ROSENFELD, A., AND KAK, A. C. 1982. Digital Pic- computed in less than O(n log n) steps? Am. ture Processing, 2nd ed. Academic Press, New Math. Monthly 84,4 (Apr.), 284-285. York. KLINGER, A. 1971. Patterns and search statistics. In Rousso~ou~os, N., AND LEIFKER, D. 1985. Direct Optimizing Methods in Statistics, J. S. Rustagi, spatial search on pictorial databases using packed Ed. Academic Press, Orlando, Fla., pp. 303-337. R-trees. In Proceedings of the SZGMOD Confer- KNUTH, D. E. 1973. The Art of Computer Program- ence (Austin, TX., May). ACM, New York, pp. ming, Vol. 3, Sorting and Searching. Addison- 17-31. Wesley, Reading, Mass. SAMET, H. 1980. Region representation: Quadtrees LAUTHER, U. 1978. Four-dimensional binary search from binary arrays. Comput. Graph. Image Pro- trees as a means to speed up associative searches cess. 13, 1 (May), 88-93.

ACM Computing Surveys, vol. 20, No. 4, December 1988 Hierarchical Representations of Collections of Small Rectangles l 309

SAMET, H. 1984. The quadtree and related hierar- SIX, H. W., AND WOOD, D. 1980. The rectangle chical data structures. ACM Comput. Surv. 16, 2 intersection problem revisited. BIT 20, 4, 426- (June), 187-260. 433. SAMET, H. 1989a. Fundamentals of Spatial Data SIX, H. W., AND WOOD, D. 1982. Counting and Structures. Addison-Wesley, Reading, Mass. reporting intersections of d-ranges. IEEE Trans. SAMET, H. 1989b. Applications of Spatial Data Struc- Comput. 31, 3 (March), 181-187. tures. Addison-Wesley, Reading, Mass. STONEBRAKER,M., SELLIS, T., AND HANSON, E. SAMET, H., AND TAMMINEN, M. 1985. Computing 1986. An analysis of rule indexing implemen- geometric properties of images represented by tations in data base systems. In Proceedings of linear quadtrees. IEEE Trans. Pattern Anal. the 1st International Conference on Expert Data- Mach. Zntell. 7, 2 (Mar.), 229-240. base Systems (Charleston, S.C., Apr.), pp. 353- SAMET, H., AND TAMMINEN, M. 1988. Efficient 364. component labeling of images of arbitrary dimen- TARJAN, R. E. 1983. Updating a balanced search tree sion represented by linear bintrees. IEEE Trans. in O(1) rotations. Znf. Process. L.&t. 16, 5 (June), Pattern Anal. Mach. Intell. 10, 4 (July), pp. 579- 253-257. 586. ULLMAN, J. D. 1982. Principles of Database Systems, SARNAK, N., AND TARJAN, R. E. 1986. Planar point 2nd ed. Computer Science Press, Rockville, Md. location using persistent search trees. Commun. VAISHNAVI, V., AND WOOD, D. 1980. Data structures ACM 29, 7 (July), 669-679. for the rectangle containment and enclosure SELLIS, T., Rousso~ou~os, N., AND FALOUTSOS,C. problems. Comp. Graph. Image Process. 13, 4 1987. The R+-tree: A dynamic index for multi- (Aug.), pp. 372-384. dimensional objects. Computer Science TR-1795, VAISHNAVI, V., AND WOOD,D. 1982. Rectilinear line Univ. of Maryland, College Park, Md. segment intersection, layered segment trees and SHAFFER, C. A. 1986. Application of alternative dynamization. J. Algorithms 3, 2 (June), pp. 160- quadtree representations. Ph.D. dissertation, TR- 176. 1672, Computer Science Dept., Univ. of Mary- VAN LEEUWEN,J. AND WOOD,D. 1981. The measure land, College Park, Md. problem for rectangular ranges in d-space. J. SHAMOS, M. I., AND HOEY, D. 1976. Geometric in- Algorithms 2, 3 (Sept.), 282-300. tersection problems. In Proceedings of the 17th VOELCKER, H. B., AND REQUICHA, A. A. G. 1977. Annual IEEE Symposium on the Foundations of Geometric modeling of mechanical parts and Computer Science (Houston, October). IEEE, processes. IEEE Comp. 10, 12 (Dec.), 48-57. New York, pp. 208-215. WEIDE, B. W. 1978. Statistical methods in algorithm SHNEIER, M. 1981. Calculations of geometric prop- design and analysis. Res. Rep. CMU-CS-78-142, erties using quadtrees, Comp. Graph. Image Pro- Computer Science Department, Carnegie-Mellon cess. 16, 3 (July), 296-302. University, Pittsburgh, Pa.

Received January 1988; final revision accepted June 1988.

ACM ComputingSurveys, Vol. 20,No. 4, December1988