10

This is an interview question.
We are given dimensions of various rectangles, we have to find out the area(minimum) of rectangle that can enclose all of them? rectangles can be rotated also .

test case:-
input:
3   //number of rectangles
8 8
4 3
3 4

output:
88

11x8:
+ - - - - - - + + - +
|             | |   |
|             | |   |
|             | + - +
|             | + - +
|             | |   |
|             | |   |
+ - - - - - - + + - +

i looked at a similar question asked before fitting rectangles in the smallest possible area
the above approach looks at all possibilities ,rotations, and determine the minimum over all such possibilities in all layout cases.
can't we base an algorithm in which we find the sum of area of rectangles first and then look for max length ,width?

Community
  • 1
  • 1
k53sc
  • 6,910
  • 3
  • 17
  • 21

3 Answers3

7

There is no absolute solution to this problem, but there are several approximate solutions, you can read about some of them here.

pseudoDust
  • 1,336
  • 1
  • 10
  • 18
  • p.s. if you were asked this at an interview, most likely you were not expected to solve this, they just wanted to see how you approach a problem. – pseudoDust Sep 17 '12 at 09:23
  • 3
    "there is no absolute solution to this problem" - ??? I think what you meant is that for a big input (lot of rectangles) you need heuristics, because exhaustive search takes too much time. – Karoly Horvath Sep 17 '12 at 10:20
  • @KarolyHorvath well, yes, obviously there is always the brute force. what i meant was that there is no algorithm with a reasonable asymptotic running time that would give a definite absolute minimum – pseudoDust Sep 17 '12 at 11:52
7

Optimal Rectangle Packing on Non-Square Benchmarks:

Given a set of rectangles, our problem is to find all enclosing rectangles of minimum area that will contain them without overlap. We refer to an enclosing rectangle as a bounding box. The optimization problem is NP-hard, while the problem of deciding whether a set of rectangles can be packed in a given bounding box is NP-complete, via a reduction from bin-packing (Korf 2003).

New Improvements in Optimal Rectangle Packing:

Korf [2003] divided the rectangle packing problem into two subproblems: the minimal bounding box problem and the containment problem. The former finds a bounding box of least area that can contain a given set of rectangles, while the latter tries to pack the given rectangles in a given bounding box. The algorithm that solves the minimal bounding box problem calls the algorithm that solves the containment problem as a subroutine.

Minimal Bounding Box Problem

A simple way to solve the minimal bounding box problem is to find the minimum and maximum areas that describe the set of feasible and potentially optimal bounding boxes. Bounding boxes of all dimensions can be generated with areas within this range, and then tested in non-decreasing order of area until all feasible solutions of smallest area are found. The minimum area is the sum of the areas of the given rectangles. The maximum area is determined by the bounding box of a greedy solution found by setting the bounding box height to that of the tallest rectangle, and then placing the rectangles in the first available position when scanning from left to right, and for each column scanning from bottom to top.

See also Optimal Rectangle Packing: New Results.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

First of all you should check, could be enclosing rectangle be rotated or no? Anyway, you could ignore "rectangles" condition and resolve task in points. You have array of points (which are vertexes of rectangles). Your task is to find encosing rectangle with minimum area. If enclosing rectangle could not be rotated then solution is silly and has complexity O(n).

Generated array of rectangles and make array of points, which are vertexes of rectangles. Next is simple:

long n; // Number of vertexes
point arr[SIZE]; //Array of vertexes
long minX = MAXNUMBER, minY = MAXNUMBER, maxX = -MAXNUMER, maxY = -MAXNUMBER;
for (long i = 0; i < 4 * n; i++)
{
    minX = MIN(minX, arr[i].x);
    minY = MIN(minY, arr[i].y);
    maxX = MIN(maxX, arr[i].x);
    maxY = MIN(maxY, arr[i].y);
}
long width = maxX - minX, height = maxY - minY;
printf("%ddX%ld", width, height);

Another task if rectangle could be rotated. Then you should first:

  1. Build minimum convex polygon of all the points in rectangle. You can use any of existing algorythms. Complexity O(n log n). As exmaple "Graham's Scan" : http://en.wikipedia.org/wiki/Graham%27s_scan
  2. Use simple algorithm for convex polygon. Link: http://cgm.cs.mcgill.ca/~orm/maer.html

Link for your task in wiki: http://en.wikipedia.org/wiki/Minimum_bounding_rectangle

Gloomcore
  • 940
  • 7
  • 11
  • :thanks for the solution. Can you explain little more how to express input points(rectangles) to make a convex polygon? i understood how to find minimum area enclosing rectangle from the convex polygon. – k53sc Sep 17 '12 at 09:42
  • There are multiple algo's. As example Graham's scan: http://en.wikipedia.org/wiki/Graham%27s_scan. Or QuickHull: http://algolist.ru/maths/geom/convhull/qhull3d.php. – Gloomcore Sep 17 '12 at 09:49
  • 4
    Looking at the input data it seems you have missunderstood the problem. The problem is not about finding the minimun enclosing rectangle for a given set of rectangles positioned in 2D space but how to pack a set of rectangles of known sizes inside a minimal rectangle. – salva Sep 17 '12 at 09:58