3
$\begingroup$

Saying I have shapes like these (made of a finite number of elementary 1cm*1cm squares):

Shapes to fill

Is there any algorithm to apply so I can fill these shapes with the least amount fo rectangles (besides listing all the fillings I could do and take the one with less rectangles)?

And what if the shape as holes in it?

All I found so far are just "be smart", but I would like to be able to let a computer do that. Thanks for the tips!

Image from http://static.studyladder.com/cdn/course/b3/2b1e1e9d0ed0.jpg

  • 1
    I'm not completely sure at all, but I think solving these problems exactly is very difficult (high complexity). But decent approximation algorithms are probably possible.2017-01-10

2 Answers 2

2

Not really an answer (or maybe yes), but too long for a comment.

It is important the filling to be minimal? If not, you can do the following.

  1. Begin at any tile of the figure. Consider this a rectangle of size 1.
  2. For each direction (N, S, E, W) enlarge the rectangle in that direction.
  3. If the new rectangle is contained in the figure, keep the change. Otherwise, discard it and try other direction.
  4. If the rectangle can't be enlarged anymore in any direction, remove it from the figure and append the data of the rectangle in some array to store it. Otherwise return to step 2.
  5. Repeat from 1 until the figure is totally removed.

I have tried some examples (by hand) and the results seems good.

  • 0
    Yes, the minimum is a requirement of the fun in the quest for such algorithm ☺2017-01-10
1

This section of the "Polygon covering" page in Wikipedia implies that brute-force trial and error (generating all the partitions into rectangles and taking the one with the fewest rectangles) is the best you can do in general. The problem I think you're describing is $NP$-hard.

But the $NP$-hardness of a problem often describes only the worst-case times due to some pathological cases. The figures you gave as examples look relatively "nice." You could use some heuristics to make the algorithm try large rectangles in cases such as these, which would often lead it to find the minimal covering quickly; you then might use some "branch and bound" method to quickly rule out all other possible coverings.

For example, once you have found a covering with $n$ rectangles, when you are searching for another covering and have already put $n-1$ rectangles in the covering without completing it, you can rule out all the coverings that include those $n-1$ rectangles. This could very quickly eliminate large portions of your search space.