algorithmsearchdata-structurescomputational-geometrybest-fit

Find the rectangle with the smallest area that can hold another rectangle


Assume that I have a set of rectangles (with different or same dimensions).

  1. The task is to find (and remove) the rectangle from the set that is larger or equal to a given rectangle.
  2. It should also be the smallest rectangle in the set than can encompass the given rectangle.

This is easily solved in O(n) time by doing a linear search / update, but is it possible to achieve better results? O(log n) would be optimal I'd assume. Insert and removal must also be faster than O(n) for this to be of any use in my case.

Can any shortcuts be made by not finding the optimal rectangle, but rather relax the 2nd restriction to: "It should also be one of the smallest rectangles that can encompass the given rectangle"-

I was thinking along the lines of using a Z-order curve (of the width/height) and use as a one dimensional index and combine that with a tree. Would that work? Or would there be too much waste?

Another approach would be to use tree using one axis, and then test the other linearly.

Anyone done something similar and can share their experience?


Solution

  • Here's an idea which is not fully elaborated yet:

    Maybe you could use a fourfold-branched tree with 2-tuple values (height and width) each representing one rectangle.

    One node (w, h) has 4 child-nodes:

    When you descend at a (w, h) rect node to look for a container for your (w2, h2) rect there are 4 different cases now:

    You would have to descend to all possible branches, which is still better than O(n).

    Inserting is O(log n). Not sure about deleting and balancing yet. But I am almost certain there is a solution for that as well.