Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

An Optimal Algorithm for Rectangle Placement

Patrick Healy
Marcus Creavin
Department of Computer Science and Information Systems
University of Limerick
Limerick
Ireland

Technical Report UL-CSIS-97-1

Abstract
In this paper we consider the problem of placing eciently a rectangle in a two-
dimensional layout that may not have the bottom-left placement property. This
problem arises when we apply any one of a number of iterative improvement algo-
rithms to the cutting stock problem or its variants. Chazelle has given an O(n)-time
placement algorithm when the layout has the bottom-left property; we extend this
result to the more general situation by presenting a (n log n)-time algorithm.

1 Introduction
Rectangle layout is an important problem with applications in the glass cutting and paper
cutting industries. There are many variants of the problem but the essential task is to
place a given set of rectangles, or pieces, in a given larger rectangular area or, stock sheet,
so that the wasted space in the resulting layout is minimized subject to the layout being
feasible. We call a placement feasible if there are no overlapping pieces in the layout.
The solution may have to meet additional constraints, such as a requirement on the
minimum or maximum number of a particular piece that should appear or, that the pieces
be recoverable from the stock sheet by making full length, or guillotine, cuts across the
piece. This latter requirement is important for industries such as glass cutting where
partial cuts across a stock sheet are either forbidden or are prohibitively expensive.
The bottom-left placement heuristic is a popular strategy for placing rectangles in a
stock sheet. The heuristic places a rectangle at a feasible location that is as low as possible
and as far left as possible at this level. Although this strategy does not usually generate
layouts that meet the guillotine cut requirement its speed makes it a useful heuristic
nonetheless. It is complete in the sense that if a rectangle can be placed in a layout then
there is a corresponding bottom-left location which will be found by the heuristic.
Chazelle [2] presents an ecient algorithm for the bottom-left placement of rectangles.
His algorithm, which can nd a location for a rectangle if one exists and place the rectangle
at that location, operates in time that is linear in the number of rectangles already placed.

1
C D

E
p
A H
G
F

Figure 1: Placing a rectangle in a non-bottom-left layout.

Therefore the running time cost of placing n rectangles bottom-leftwise in a stock-piece is


O(n2). He remarks that this is optimal for algorithms that report all feasible bottom-left
placement locations.
The ecacy of the bottom-left placement heuristic can be enhanced by optimizing
the layout that results through any one of a large number of iterative improvement al-
gorithms. Healy and Moll [6] report on a system that optimizes a bottom-left generated
layout through a generalization of local search. The process works by swapping already
placed rectangles for unplaced ones provided that the swap improves the area covered
by rectangles or improves the chances of nding an improvement later. Related to this
idea of swapping rectangles in and out of the layout, Dowsland [4] reports on a simu-
lated annealing-based system for packing rectangles into a given stock piece, although the
bottom-left heuristic is not used here.
Figure 1 illustrates a general layout containing 7 rectangles. Note that since rectangles
D and E are not placed as far south and west as possible the layout does not have the
bottom-left property. In the algorithms we describe the rectangle placements will be
bottom-left. However we will not be able to assume that the layout into which we place
the rectangle will have the property. This derives from the fact that in attempting to
swap an unplaced rectangle B for an already placed rectangle A, the removal of A may
result in a non-bottom-left layout into which we must place B. The point p in the same
gure illustrates the bottom-left location for the placement of a new rectangle, H, shown
alongside.
The remainder of the paper is organised as follows. Below we describe work on prob-
lems related to this. In section 2 we describe our O(n log n)-time placement1 algorithm.
1Technically we should distinguish between the process of nding a feasible location for the rectangle
and the process of updating any data structures relevant to the insertion or placement of the rectangle
at that location. However, since the latter can be performed in linear time it will not be of concern to
us. Therefore when we speak of rectangle placement we are referring to the nding of a feasible location
at which to place a rectangle. For details of the process of insertion and the updating of the associated
data structures we refer the reader to [5].

2
We close by discussing the optimality of the algorithm and open problems that are related
to the rectangle placement problem.

1.1 Related Approaches


In two dimensions the problem is quite closely related to the largest empty rectangle
(LER) problem. Given n points, the largest empty rectangle problem is to nd the
largest rectangle, areawise or perimeterwise, that does not properly contain any of the n
points. The best algorithm runs in O(n log2 n) time for the area problem and O(n log n)
time for the permieter problem [1]. On the other hand, other algorithms enumerate all
empty rectangles and report the maximum area (perimeter) rectangles [7]. The running
time of these algorithms is O(n log n + K ), where K is the number of empty rectangles;
K can be seen to be O(n2 ) in the worst case.
We might model our rectangle placement (RP) problem by noting the O(n) corners
of the already placed rectangles and using these as the input to the LER problem. The
LER problem, however, di ers from our RP problem in two respects. Firstly, for RP we
need to be able to specify for each point from which directions the LER may touch the
point. That is, we want to place the new rectangle in an empty region of the layout so for
each corner of the already placed rectangles at least 2 of the four possible directions are
eliminated. Secondly, the LER problem does not place any restriction on the dimensions
of the reported rectangle; on the other hand, to place a rectangle we need to ensure that
there is sucient room widthwise and heightwise. That is, we would like to know if there
is an LER larger in area (perimeter) than our candidate rectangle subject to say, its width,
being at least the width of our rectangle. For at least some of the algorithms above we
have been unable to modify them while maintaining the stated running times.
The rst limitation that we have described above has been considered and solved
previously [3]. The largest rectangle in an orthogonal polygon problem nds the largest
rectangle subject to it tting within the bounds of the given polygon. In both cases the
running times of the algorithms are O(n log n + K ), where K is the number of candidate
rectangles. We can represent any empty space in our layout as an orthogonal polygon
whose sides are de ned by the adjoining rectangles and nd the largest such rectangle;
however, limitation two above still holds since we have no way of restricting the search to
rectangles whose widths are not less than ours.
We now go on to present our O(n log n) rectangle placement algorithm.

2 Two-Dimensional Rectangle Placement


We reduce the problem of nding a feasible bottom-left location for a piece in the 2-D
case to a series of 1-D tests of overlapping line segments by sweeping over the layout from
bottom to top. By representing these line segments eciently and keeping track carefully
of changes as we sweep upwards we can achieve the stated bound.

2.1 Notation and Terminology


In the following, we denote the width of the stock sheet by W and its height by H . We
will refer to a rectangle's width and height, respectively, as w and h. If the rectangle, r, is
already placed then (rx; ry ) refers to its bottom-left location; to distinguish between the

3
dimensions of di erent placed rectangles we may also refer to a rectangle r's width and
height as rw and rh, respectively. We assume integer dimensions throughout.
Given a placed rectangle, r, we call the line segment, [rx; rx + rw ], of the interval [0; W ]
the rectangle's interval. We call a layout's pro le, Py , at height y, the one-dimensional
cross-section of a layout at that height. More speci cally, each pro le, Py , is a set of line
segments each corresponding to a rectangle of width rw whose bottom-left location is rx
that spans height, y. That is,
Py = f[rx; rx + rw ]jry  y  ry + rhg
In what follows we will use the notation, yi, to refer to the ith value of the ordered set
Y = f0g [ fyj8r; y = ry _ y = ry + rh g
There can be at most 2n + 1 unique pro les corresponding to those taken at yi 2 Y .
Therefore any layout can be completely speci ed by its (at most) 2n + 1 pro les with
duplicates removed,
P = fPyi g; yi 2 Y
We will assume that P is an ordered set with the following ordering: Pyi  Pyj if and
only if yi < yj . For each pro le, Py , we associate a count, cy (x), with each integer value
in the range [0; W ]. This value is a count of the number of line segments that span the
value, x. In a feasible layout it is clear that 0  cy (x)  2.
We call the lower horizontal side of an already-placed rectangle its base. Similarly,
we call the upper horizontal side of an already-placed rectangle a support, since this is a
potential support for the bottom-left placement of a subsequent rectangle; we call the right
hand side of a rectangle an adjacent, since a rectangle may be placed adjacent to it. We
may restrict our search for an (x; y) location for the piece to those y-values corresponding
to the upper side of some already placed rectangle.
We note that in any bottom-left layout where a rectangle r2 is supported by another
rectangle r1 the pro les that correspond to the support of r1 and the base of r2 will be
identical since they occur at the same height. To distinguish between the two pro les
above we can re ne further the notion of pro le by distinguishing between rectangles that
truly span height y and those rectangles whose base or support occurs at y. We de ne Py+ ,
the upwards pro le at height y, as Py+ = Py \ Py+1 (those intervals of Py that would also
appear in Py+1 ) and, analgously, Py?, the downwards pro le at height y, as Py? = Py?1 \ Py
(those intervals of Py that would also appear in Py?1 ). More formally
Py+ = f[rx; rx + rw ]jry  y < ry + rh g
and
Py? = f[rx; rx + rw ]jry < y  ry + rh g
Note that given an arbitrary height y, Py is identical to Py?0 , where a support occurs at y0
and the support is the lowest such that y0  y.
We de ne upwards and downwards counts, c+y and c?y , respectively, as c+y = cy [ cy+1
and c?y = cy [ cy?1 .
Given a set of m pro les
S = fPyl ; : : : ; Pyl+m?1 g
we can take their composition by superimposing the pro les. We will restrict our attention
to composing subsequences of (the ordered set) P where the rst pro le will be an upwards

4
pro le, the last pro le in the subsequence will be a downwards pro le and the di erence in
height between rst and last will be no less than h. That is, we will compose subsequences
of pro les of the form
Py+i ; Pyi+1 ; : : : ; Py?j
where there is some rectangle, s, placed at height yi ? sh and another, s0, placed at height
yj subject to yj ? yi  h and s0 being the lowest such rectangle. We will use Ci to refer
to the composition that starts at y = yi that satis es the previous restrictions.2
Associated with a composition Ci we can de ne c(x), the count of intervals spanning
each integer value, x, in the composition by
c(x) = c+l (x) +
X ci(x) + c?l+m?1(x)
l+m?2

i=l+1
We can now state a necessary and sucient condition for the placeability of a rectangle.
A rectangle of width w and height h is placeable in a layout if and only if there is a
composition, Ci, of P such that there exists
xs; xe 2 Ci with xe ? xs  w and c(x) = 0; 8x 2 [xs; xe]
That is, there is an uninterrupted sequence of pro les that are separated by a vertical
distance of at least h and each has the \same" interval of width at least w blank.
We now describe how we can eciently maintain a subsequence of these pro les and
detect a space in the layout that will accommodate the insertion.

2.2 The Placement Algorithm


Our algorithm sweeps over the placed rectangles from bottom to a height no less than
h to build an initial composition, C0. By continuing the plane sweep and updating C0
through insertions and deletions, we can generate the sequence of compositions
C = C0; : : : ; Cl; 0  l  2n ? 1
Finding the rst feasible bottom-left location for a rectangle, r = (w  h), is reduced to
nding the rst gap of width w, or more, in the sequence C .
Any composition may contain O(n) intervals so we cannot simply search each composi-
tion for an appropriately sized gap. The key to our algorithm is that for a given rectangle
there are only O(n) possible bottom-left locations in any layout and therefore we may
restrict our attention to searching for gaps at those bottom-left locations. In section 2.3
we will justify this claim and describe how we can nd { and order according to their
bottom-leftness { the set of bottom-left locations of a given layout in O(n log n)-time.
Having generated L, the (ordered) list of bottom-left locations, our algorithm traverses
the list and, for a given location, li 2 L, with y-value, y0 = y(li), we generate the
corresponding composition through a series of updates from the previously generated
composition by sweeping northwards until we reach the support at y0. We then check if
this location is feasible, returning y0 as the rst feasible bottom-left location in the layout
if true; otherwise we proceed to the next location on the list, returning the empty set if
we reach the end of the list unsuccessfully.
All that remains, now, is to describe how we may search a composition eciently for
a gap of width w and update the composition as we sweep over the layout.
2 For consistency we may imagine that the layout is surrounded by four rectangles placed, respectively,
at locations (0 0) (0 ) (0 0)and( 0), where the former two have width and height 0 and the latter
; ; ;H ; ; W; W

two have width 0 and height . H

5
2.2.1 Representing and Searching a Composition
Two separate data-structures are required to facilitate our test for a suciently wide gap
in a composition to accommodate the rectangle in question.
For l = (x0; y0) to be feasible the following condition must hold
c(x0 + 1) = : : : = c(x0 + w ? 1) = 0
That is, 1) no interval of the composition may start or may end in the (open) interval
I = (x0; x0 + w) and 2) the interval I may not be spanned by (properly contained in)
any interval of the composition. The former condition can be tested by testing that no
interval properly contains either the point x0 or the point x0 + w and that no interval of the
composition is spanned entirely by I . Both of these tests may be achieved in O(log n)-time
by storing the end points of the intervals of the current composition in a height-balanced
binary search tree, T , ordered by increasing abscissa (with interval end-points preceding
interval start-points in the case of ties). If start- and end-points of intervals are labelled
accordingly then a simple descent of the tree will determine whether x0 or x0 +w is properly
contained in any interval of the composition. If the rightmost interval to the left of x0
is Il and the leftmost interval to the right of x0 + w is Ir then to verify that there is no
interval in the composition spanned by I , we simply test that the end-point of Il and the
start-point of Ir are adjacant in the binary search tree, T .
If condition 1) above is satis ed then either the interval ts cleanly in the composition
without overlaps or, some other interval spans entirely the interval I . This may be resolved
by checking that an arbitrary point in the interval I is not spanned by any interval of
the composition. To achieve this we use Bentley's segment tree data structure [8, page
13]. A segment tree facilitates O(log n)-time queries of the form \how many intervals
cover point x" with insertions and deletions also possible in O(log n)-time. The segment
tree operates with ordinal values in the range [1::n] as opposed to absolute values so it is
necessary to preprocess the layout prior to each search (or, more appropriately, following
each rectangle insertion) so that the end-points of each interval are mapped to some value
in the range [1; : : : ; M ], 1 < M  2n.
2.2.2 Updating a Composition
If the bottom-left location, li 2 L, is an infeasible location for the placement of r = (w  h)
then we proceed to the next, li+1. This may involve inserting or removing intervals
from the composition: any rectangle whose support occurs at or below y(li+1) becomes
inactive and the corresponding interval can be removed from the composition; similarly,
any rectangle, p, placed at (px ; py ) where y(li+1) < py + ph ^ py < y(li+1) + h becomes
active now and the corresponding interval is inserted in the composition.
As part of the preprocessing step following the insertion of each rectangle we maintain
two sorted lists of the placed rectangles in order of ascending y-value: one maintains
the rectangles' bases and the second maintains the rectangles' supports. As we traverse
the list of locations, L, we traverse these lists concurrently with the aid of pointers to
remove from, and insert intervals in to, the composition: when we encounter li we advance
through the list of supports, removing their intervals from the composition, until a support
is found which no longer satis es ry + rh  y(li); similarly, we advance through the list
of bases, adding intervals to the composition until a base is encountered which no longer
satis es ry < y(li) + h.
Inserting or removing an interval from the height-balanced binary search tree, T ,
can be performed in O(log n)-time. A similar bound holds for inserting or deleting the

6
3 4

2
1

Figure 2: Four types of bottom-left locations for rectangles

corresponding interval from the segment tree [8]. Since each of the 2n intervals will be
traversed exactly once and inserted in and deleted from the binary search tree at most
once the cost of searching for a feasible bottom-left location is O(n log n).

2.3 Detection of Bottom-Left Locations


Below we present an O(n log n)-time algorithm for detecting bottom-left locations for the
placement of a given rectangle. We distinguish here between bottom-left locations and
feasible bottom-left locations. A feasible bottom-left location is one where the rectangle
does not overlap any other rectangles when placed at that location. In this section we
are only interested in nding the set of bottom-left locations for a given rectangle; their
feasibility is checked by the algorithm presented earlier.
Since our algorithm hinges upon the fact that there can be only O(n) bottom-left
locations we will prove this rst.
2.3.1 Number of Bottom-Left Locations
In general, there are O(n2) locations at which rectangles can be placed in a given layout,
as illustrated in gure 2. Two of the bottom-left locations supported by the top support
of the bottom \stairway" are shown. With approximately n=2 rectangles on the upper
stairway and the same on the lower stairway n=2 unique rectangles can be placed on the
top support of the lower stairway such that the bottom-left location of each is di erent.
The next highest support can support n=2 ? 1 such rectangles and so on, until the lowest
support which can accommodate a single rectangle.
In total, therefore, there are O(n2 ) locations which may accommodate di erently-
sized rectangles. However, we now go on to show that for a given rectangle there are
O(n) bottom-left locations for it in a layout.
There are four di erent types of bottom-left location at which the bottom-left corner
of a rectangle may be placed:
1. the corner coincides with both its support and its adjacent
2. the corner coincides with its support but not its adjacent
3. the corner coincides with its adjacent but not its support

7
4. the corner coincides with neither its support nor its adjacent
Examples of each type of location are shown in gure 2.
Bottom-left locations of type 1 coincide with the north-west corner of a rectangle.
Therefore, there are at most n such locations.
The second type of bottom-left location arises when the downward extension of an
adjacent intersects a support. We call this intersection a projection. If the distance from
the adjacent to its projection is greater than 0 and less than h then this will be a bottom-
left location. Since there are O(n) adjacents and an adjacent may generate at most one
unique projection there can be at most O(n) such bottom-left locations.
Bottom-left locations of type 3 and 4 can be considered as one for our purposes.
These corners can be detected by extending a line leftwards from a support by a distance
of w ? 1 units and extending a line downwards from an adjacent by h ? 1 units. If the
horizontal extension intersects an adjacent or its extension then their intersection is a
possible bottom-left location for a rectangle of width w and height h. This process is
analogous to placing the rectangle on a support and sliding it as far left as it will go,
provided it does not encounter an adjacent corresponding to type 1 and 2. There can be
only one such location for each support and therefore the total is O(n).
2.3.2 An Algorithm for Finding Bottom-Left Locations
It is convenient (although not necessary) to represent a layout in terms of its holes. A
hole is a maximal set of points in a layout such that all the points are uncovered and there
is a Jordan curve that connects any two points in a hole. Using the doubly-connected-
edge-list (DCEL) notation [8, page 15] we can represent a hole by a list of line segments
where each line segment borders a rectangle on one side. By convention we represent each
hole by a clockwise traversal of its border segments. We call such segments of a rectangle
exposed segments.
A given rectangle side may contribute more than one segment to a hole's representation
but, since segments are maximal, the two segments can not be consecutive i.e., some other
rectangle must intervene. Since there are n rectangles in a layout there are clearly O(n)
exposed segments.
Bottom-left corners of type 1 above can be detected in a simple linear traversal of the
hole outlines by noting the right turns of a hole's perimeter.
Bottom-left locations of type 2 above can be detected by means of a binary search. We
begin by gathering all the rectangle adjacents and extending downwards the lower point
of the interval by a distance h ? 1. These intervals are then inserted in a height-balanced
binary search tree ordered by increasing abscissa and, in the case of ties, by increasing
ordinate of the lower end of the interval. The intersection of an exposed portion of a
support and an extended exposed adjacent is a potential bottom-left location. Finding
such an intersection may be achieved in O(log n)-time by performing a binary search on
the tree for an interval that intersects the given exposed segment support.
However, as gure 3 illustrates, nding a location for an arbitrarily tall rectangle may
lead to O(n2) possible such intersections. We note that the only intersection of interest to
us is the northernmost one encountered. Once a vertical interval is found that intersects
the query support then it can be removed from future consideration. Therefore, we order
the search so that the higher up supports are tested rst. When a query is successful
we remove the associated adjacent and rebalance the tree. For each support we continue
to query until no more intersections are found since a support may be home to more
than one projection. Each of the O(n) supports will generate at least one query at cost

8
Figure 3: O(n2 ) possible \type 2" projection intersections of height h

O(log n) each, but since there are O(n) bottom-left locations of this type there can be at
most O(n) successful searches.
To detect bottom-left locations of types 3 or 4 we perform a binary search similar to
above. We build the binary tree as before but now, however, we change the queries we
make. Since we are searching for bottom-left locations that are to the left of the support
we need to extend the support interval query leftwards by w ? 1. The set of support
intervals that we present as our queries are limited in this case to those that correspond
to exposed northwestern corners of rectangles. In all other respects the procedure and
analysis is the same as the above case.
Having identi ed all the bottom-left locations by means of the previous three steps
they may sorted now according to their bottom-leftness to be presented to our feasibility
checking algorithm described in section 2.2.

3 Concluding Remarks
We claim that the algorithm we have presented is optimal and that an o(n log n)-time
algorithm can not exist. We base this claim on the fact that the -closeness problem is
reducible to the 1-D version of rectangle placement. The -closeness problem determines
whether any two of n given numbers are within  of each other and reports either true or
false; the 1-D version of rectangle placement nds the leftmost gap of width at least w
between a set of given intervals, returning the null interval if none exists.
We can decide -closeness by creating a zero-length interval for each given point and
use our placement algorithm to nd a suitable location for an interval of width . The
placement algorithm will nd a location if and only if there is no pair of points spaced
within  of each other. The -closeness problem is known to require (n log n)-time [8,
page 325] and, therefore, is a lower bound on the 1-D version of rectangle placement.
We have presented an algorithm to nd a feasible location at which to place a given
rectangle. The location returned will be on the lowest support that will accommodate it
and will be left-justi ed on that support. In principle the algorithm can be modi ed to nd

9
say, bottom-right or, top-right, locations. However, it is not obvious how the O(n log n)-
time bound could hold for other placement strategies such as nding the location that
minimizes the perimeter of the resulting hole.

References
[1] A. Aggarwal and S. Suri. Fast algorithms for computing the largest empty rectangle.
In Proc. 3rd Ann. ACM Symp. Comput. Geom., pages 278{290, 1987.
[2] Bernard Chazelle. The bottom-left bin-packing heuristic: An ecient implementation.
IEEE Transactions on Computers, C-32(8):697{707, August 1983.
[3] A. Datta. Ecient algorithms for the largest rectangle problem. Inform. Sci., 64:121{
141, 1992.
[4] Kathryn A. Dowsland. Some experiments with simulated annealing techniques for
packing problems. European Journal of Operational Research, 68:389{399, 1993.
[5] Patrick Healy. Rule-based local search in rectangle layout. Master's thesis, University
of Massachusetts, Amherst, May 1988.
[6] Patrick Healy and Robert N. Moll. A local optimization-based solution to the rectangle
layout problem. Journal of the Operational Research Society, 47:523 { 537, April 1996.
[7] M. Orlowski. A new algorithm for the largest empty rectangle problem. Algorithmica,
5:65{73, 1990.
[8] F. P. Preparata and M. I. Shamos. Computational Geometry: An Introduction.
Springer-Verlag, New York, Inc., 175 Fifth Avenue, New York, New York 10010, USA,
1985.

10

You might also like