INFORMA TION SCIENCES 21,59-67 (1980)
59
V. K. VAISHNAVI De#. of ComputerScience, ConcordiaUniversi~, Mont&al, P.Q., H4B IRIS Can& H. P. KRIEGEL Lehrmhl Infomwtik VJ Unioer&t Dortmu& D-4600, Dortmund SO, Wut Gernwy and D. WOOD Unitfor Conputer Science,M&aster Uniowsi~, Ham&m, OntarioLBS 4K1, Canada Communicated by John M. Richardson
ABSTRACT The i&motion problem for a subclass of mtanglts called r-rwtanglea is inv&igated and reduced to the balanced batched r(wricted~range searching problem as well as to the balanced batched inverse r-range searching problem. Simple algorithmsfor these problems are given which are space and time optimal. The algorithm given for the balanced batched r-range searching problem leads to a new algorithm for the all-points ECDF problem in 2-apace whioh is simple and optimal. Again, the balanced bat&d r-range mrohing algorithm is combinedwitb a known algorithmfor batched range sear&@ problems,leadingto a new algorithm for the r&angle inters&ionproblemwhichis spaceand timeoptimalin the
worstczsc whenthegivenset of rectan&a containsa muchhigherproportionof r-rcctangl~.
1.
INTRODUCTION
Computational geometry is an area which deals with the computational complexity of finite geometric problems. Recently there has been considerable activity in a subclass of these problems, called the geometric intersection *Work supported by a Natural Scienxs and EngineeringRawrch Cmmeil of Canada Grant No. A-7700 and by the German Academic Exchange Service under NATO Grant 430/402/584/8. OElwvier North Holland, Inc., 1980 52 VanderbiltAve., New York, NY 10017
60
V. K. VAISHNAVI, H. P. KRIEGEL, AND D. WOOD
problems. These problems deal with the computation of intersection properties
among sets of planar objects such as line segments, circles, half spaces, and rectangles. Recent papers in this area are [7], [3], [5], and [8]. Bentley and Wood [5] and Vaishnavi [8] deal, in particular, with the rectangle intersection problem. In this paper, we investigate a special case of this problem. The rectangle intersection problem has an important application in the area of very large-scale integrated-circuit artwork analysis (see [I] and [6]). Specifically, it is important in identifying locations where circuit function is threatened by the loss of edge acuity, which occurs during fabrication. Bentley and Wood [5] and Vaishnavi [8] give O(n log n)-space and O(n logn + k)-time worst-case algorithms for it. In the present paper, we investigate the intersection problem for r-rectangles which form an important subclass of rectangles. We give two relatively simple algorithms for this problem which use only O(n) space and take O(n logn + k) time in the worst case. There is additional interest in one of these solutions in that it leads to a new optimal algorithm for the ail-points ECDFproblem, which has many applications in statistics (see [4]). Also, it leads to a new algorithm for the (general) rectangle intersection problem which is optimal in certain situations. One of the algorithms for the r-rectangle intersection problem actually involves the solution of the baZanced batched r-range searching problem. The other involves the solution of the balanced batched inverse r-range searching problem. Both the balanced batched r-range and inverse r-range searching problems are of importance in their own right and have applications in the data-base area. It is the solution of the balanced batched r-range searching problem which immediately leads to a new algorithm for the all-points ECDF problem in 2-space. In Sec. 2, we show how the r-rectangle intersection problem can be reduced to either the balanced batched r-range searching problem or the balanced batched inverse r-range searching problem. In Sec. 3, we develop algorithms for each of the two searching problems and also outline how the algorithm for the balanced batched r-range searching problem can be modified to give a simple algorithm for the all-points ECDF problem in 2-space. In Sec. 4, we show how one of our algorithms for the r-rectangle intersection problem can be combined with that available for the (general) rectangle intersection problem to advantage in certain situations. We also discuss the implications of our work for the batched range searching problem. Finally, in Sec. 5, we draw various conclusions. 2. R-RECTANGLE INTERSECTION, BATCHED R-RANGE SEARCHING, AND BATCHED INVERSE R-RANGE SEARCHING
A (rectilinearly oriented) r(estricted)-rectangle is a rectangle whose edges are +rallel to the coordinate axes and which either is open on one side or has one dge coinciding with one of the coordinate axes.
61
SPACE AND TIME OPTIMAL ALGORITHMS
The r-rectangle intersection problem can be stated as: Given n r-rectangles, report all pairwise r-rectangle intersections. Two r-rectangles A and B intersect if A and B edge-intersect (i.e. an edge of A intersects an edge of B) or A (completely) encloses B or B encloses A. EXAMPLE 2.1. In Fig.1, eight r-rectangles A,B,C,D,E,F,G,H are shown. Observe that A is open on the upper side, B and C are open on the right-hand sides, one edge of D and one edge of E coincide with the y-axis, and one edge of each of E, F, G, H coincides with the x-axis, which is in accordance with the property of r-rectangles. The pairs (D, E,), (D, F), (D, G), (E, G), (E,H), (F, G), (F,H) edge-intersect each other. E intersects F because it encloses it. Similarly (G,H), (C, B) are the other intersecting pairs. Thus, in all there are ten r-rectangle intersections. The general approach that we adopt for the solution of the r-rectangle intersection problem is the same as that adopted by Bentley and Wood [5]. This is to report the intersecting pairs of r-rectangles in two phases. In the first phase all those pairs of r-rectangles are reported which pairwise edge-intersect, using the algorithm of Bentley and Ottman [3], which costs O(n) in space and O(n logn + k) in time, where k is the number of such intersecting pairs. In the second phase, a number of r-rectangle pairs are reported, which include every painvise r-rectangle enclosure and which may include some r-rectangle pairwise edge intersections. The technique for this second phase we now describe. A point is chosen inside each r-rectangle as its representant point. Now there are two choices,
G
Fig. 1.
V. K. VAISHNAVI, H. P. KRIEGEL, AND D. WOOD which lead to two different algorithms: (1) For each r-rectangle, report all the (representant) points (interpreted as the corresponding r-rectangles), which lie within it. This we call the balanced batched r-range searchingproblem, that is, when the number of r-ranges is equal to the number of points. (2) For each (representant) point (interpreted as the corresponding r-rectangle), report all the r-rectangles in which it lies. This we call the balanced batched inwrse r-range searchingproblem, that is, when the number of points is equal to the number of r-ranges. The r-rectangle intersection problem can thus be solved in optimal space and time by giving optimal solutions to the corresponding balanced batched r-range searching problem or the corresponding balanced batched inverse r-range searching problem, which we do in the next section,
3. ALGORITHMS FOR THE BALANCED BATCHED R-RANGE AND INVERSE R-RANGE SEARCHING PROBLEMS Let us first introduce the balanced batched r-range searching and the balanced batched inverse r-range searching problems. Let R = [I, : u, ; 12:u2] be a range in two dimensions. R is a r(estricted)-range if I,-0 or ut=oo or I,=0 or u2= 00. A point (x,y) lies in range R if I,
SPACE AND TIME OPTIMAL ALGORITHMS
63
Given n r-rectangles and n points in the plane: (1) (2) (3) (4)
Report, Report, Report, Report,
for for for for
each x-based r-rectangle, all the points that lie within it. each y-based r-rectangle, all the points that lie within it. each top-open r-rectangle, all the points that lie within it. each right-open r-rectangle, all the points that lie within it.
Observe that in order to obtain the two optimal algorithms for the r-rectangle intersection problem, we must have optimal algorithms for the balanced batched r-rectangle searching problem and for the balanced batched inverse r-rectangle searching problem. In order to achieve this, it suffices to give O(n)-space and O(nlogn+k)-time algorithms for each of the four subproblems into which the balanced batched r-rectangle searching problem decomposes and for each of the four subproblems into which the balanced batched inverse r-rectangle searching problem decomposes. Observe that in each of these subproblems, the number of r-rectangles is less than or equal to n, the number of points. In the following, we will give such algorithms for the batched x-based r-rectangle searching problem and for the batched inverse x-based r-rectangle searching problem, and leave the rest of them to the reader. For both of these algorithms, we shall assume that the given x-based r-rectangles are presented as triples (x,,y,xr), where (x,,~) and (x,,~) are the top two comer points of the rectangles. These algorithms involve a balanced search tree as the basic data structure and a suitable “scanning procedure.” AN ALGORITHM PROBLEM
FOR
THE BATCHED
x-BASED
r-RECTANGLE
This algorithm involves basically a bottom-to-top r-rectangles.
SEARCHING
scan of the x-based
ALGORITHM.
1. The m rectangles in the given set R of rectangles are sorted primarily by their y-values and secondarily by their x,-values. Similarly the n points in the given set F of points are sorted primarily by their y-coordinates and secondarily by their x-coordinates. 2. A balanced search tree T is initialized to 0. 3. A bottom-to-top scan (the primary scan) of the figure representing the rectangles and the points is now carried out. Within this primary scan the points are scanned from left to right (the point scan), and an appropriate action (described below) is taken at each point. The point scan is followed by a left-to-right scan of the rectangles (the rectangle scan), and an appropriate
V. K. VAISHNAVI, H. P. KRIEGEL, AND D. WOOD
64
action is taken at each top left comer point of a rectangle: if (Z#) is a point in F then insert it into T by its x-value, if (.T,jj) is the top left comer point of a rectangle (xr,y,x3 then query T and report all points (x,y) in T which satisfy xl
BATCHED INVERSE
x-BASED
r-RECTANGLE
SEARCHING
PROBLEM
Here we give an algorithm for this problem with performance of O(n) space and O(n logn) time when the number of r-rectangles is less than or equal to the number of points. This algorithm basically involves a left-to-right scan. ALGORITHM.
1. The top left comer points (of the m rectangles in the given set R of rectangles), the top right comer points, and the n points in the given set Fare sorted primarily by their x-coordinates and secondarily by their y-coordinates. 2. A balanced search tree T is initialized to 0. 3. A left-to-right scan (the primary scan) of the figure representing the rectangles and the points is carried out. Within this primary scan, a bottom-totop scan is carried out of the top left comer points of the rectangles, followed by such a scan of the points (in F), and finally followed by such a scan of the top right wmer points of the rectangles. In each of these three scans, appropriate actions (described below) are taken: if (X,,v3 is a top left comer point of a rectangle (x,,y,x,) then insert the rectangle in T by its y-value, if (Z,jQ is a point in F then query T and report all rectangles (x[,y,x3 which satisfy y >jj, if (~,y? is a top right comer point of a rectangle (x,,y,x3 then delete it from T by its y-value.
SPACE AND TIME OPTIMAL ALGORITHMS
65
The reader can readily verify the correctness of the algorithm. The algo-. rithm uses O(m+n) space and O((m+n)logm+nlogn+k) time in the worst case. Its performance is thus O(n) space and O(nlogn+ k) time when m
ECDF PROBLEM
The algorithm given in this section for the batched x-based r-rectangle searching problem can be easily modified so as to count rather than report the points within each rectangle. This can be done by letting each node of the balanced tree contain the “rank” field, i.e. the number of nodes in the subtree rooted at the node. In this way, the time complexity of the modified algorithm will be reduced to O(n logn). The all-points empirical cumulative distribution function (ECDF) problem in 2-space is a special case of this counting problem. Thus we have obtained a new simple and optimal algorithm for this problem.
4. THE RECTANGLE INTERSECTION
PROBLEM
In the previous section we gave two worst-case optimal algorithms for the r-rectangle intersection problem. We now study the (general) rectangle intersection problem. We combine one of our algorithms for the r-rectangle intersection problem with that of Vaishnavi [8] for the rectangle intersection problem so as to give an algorithm for the rectangle intersection problem which is both space and time optimal in the worst case in certain situations. Assume that the n rectangles in a rectangle intersection problem comprise n, restricted rectangles and n2 unrestricted rectangles (which are not restricted) so that n,+n,= n. The following is an algorithm for the rectangle intersection problem which, in addition to using the algorithm of Bentley and Ottmann [3] for edge intersections, uses our algorithm for the balanced batched r-range searching problem and the algorithm of Vaishnavi [8] for the batched range searching problem. It is based on the crucial observation that an unrestricted rectangle cannot enclose a restricted rectangle. ALOORITHM.
1. Given a set R of n rectangles, report all pairs of rectangles which mutually edge-intersect, using the algorithm of Bentley and Ottmann [3]. 2. Choose a point within each rectangle as its representant point. 3. Partition the set R of rectangles into sets R, and R2 such that R, contains only r-rectangles and R2 contains no r-rectangle. Let RI contain n, rectangles and R2 contain n2 rectangles.
66
V. K. VAISHNAVI,
H. P. KRIEGEL,
AND D. WOOD
4. For each rectangle (i.e. r-rectangle) in R,, report the rectangles corresponding to all the representant points contained within it, using the algorithm given in the previous section. 5. Solve the batched range searching problem using the algorithm of Vaishnavi [S], where the rectangles in Rz represent the given ranges and the set of representant points of rectangles in R, is the given set of points. (Instead of reporting a point, report the corresponding rectangle.) Let us now compute the worst-case above steps of the algorithm: 1. Space: O(n); time: secting pairs of rectangles. 2. Time: O(n). 3. Space: O(n); time: 4. Space: O(n); time: reported. 5. Space: O(nzlognz); rectangles reported.
space and time bounds
in each of the
O(nlogn
+ k), where k is the number
of edge-inter-
O(n). O(nlogn
+ k,), where kl is the number
of rectangles
time:
O(n,lognz+
kJ, where k2 is the number
of
Thus, O(n+ +logn,) is the total space used and O(nlogn+ k) is the total time taken. The space used reduces to O(n) when n2 is significantly smaller than n, that is, when O(n,) < O(n/logn). For this case we have an algorithm for the rectangle intersection problem which is optimal in both space and time. It may be noted that similar combination of our algorithm for the batched inverse r-range searching problem with that of Bentley and Wood [5] for the batched inverse range searching problem does not lead to a similar improve ment. This is essentially because a rectangle in R2 may be enclosed in rectangles in R, as well as in R,. What has been said above also applies to the two-dimensional batched range searching problem for the case when the number of ranges is of the same order of magnitude as the number of points. The best algorithm available for this case is that of Vaishnavi [8] with O(n logn) space and O(n logn + k) time performance. By combining our algorithm with that of Vaishnavi [5], we can, however, obtain an O(n) space bound for the case when the number of ranges which are not r-ranges is much less than the total number of ranges. By a standard technique (see [2]), such a combined algorithm can be extended to an 0(nlo$-2n)-space and O(nlogd-‘n + k)-time worst-case algorithm for the following batched range searching problem in d-space: Let R be a set of n (of theorderof)rangesri=[h,,:u,,,;1z,2;...; Zi,d:Q], 1 < i
SPACE AND TIME OPTIMAL ALGORITHMS
67
5. CONCLUSIONS In this paper, we have investigated the intersection problem for an important subclass of rectangles which we call the r(estricted)-rectangles. An r-rectangle is one which either is open on one side or has edges that coincide with one of the coordinate axes, and an r-range is a data-base version of an r-rectangle. We have given two simple algorithms for the r-rectangle intersection problem which are worst-case optimal for both space and time. The first of these involves a new algorithm for the balanced batched r-range searching problem, and the second one involves a new algorithm for the balanced batched inverse r-range searching problem. We have shoti how a straightforward modification of our algorithm for the balanced batched r-range searching problem leads to a new simple optimal algorithm for the ECDF problem in 2-space. Finally, we have shown how our balanced batched r-range searching algorithm can be combined with ti available balanced batched range searching algorithm so as to give a new algorithm for the (general) rectangle intersection problem. This algorithm has optimal worst-case performance for both space and time when the given set of n rectangles has much less than n (that is, < n/logn) rectangles which are not r-rectangles. REFERENCES 1. H. S. Baird, Fast algorithms for LSI artwork analysis, in Design Automation and FaultTokramt Cowpding 2, 1978, pp. 179-209. 2. J. L. Bentley, Multidimensional divide--and-conquer, Camegie Mellon University Computer science Department Kescarcb Review, 1978. 3. J. L. Bentley and Tb. Ottmann, Algorithms for reporting and counting geometric intersee tim, IEEE Tram. Electronic Cony&em, G28,1979, pp. 643-647. 4. J. L. Bentley and M. I. Sbamos, A problem in multivariate statistics: Algorithms, data structures and applications, in Proceed& of he Fifrccnt All&on Conference on Communicah~~ ControL and Computing, 1977, pp. 193-201. 5. J. L. Bentley and D. Wood, An optimal worst+xsc algorithm for report& interacctions of rectangle+ IEEE Tram Compufers, 1980, to appear. 6. U. Lautber, 4dimcmdonal binary sear& trees aa a mians to speed up associative sear&s in design rule verification of integrated circuits, in Design &&ma&n and Fat&-Tolerant Computing 2 1978, pp. 241-247. 7. M. I. Shamos and D. J. Hoey, Geometric intersection problems, in Proceedings of the Seventeenth FOCS Conference, 1976, pp. 208-215. 8. V. K. Vaisbnavi, Optimal wont case algorithms for rectangle iutemection and bat&d range scar&in8 problems. Computer Science Tcclmical Report 79-12, M&aster univ., 1979. Receiwd iuoy 1979