Maximal rectangle set cover - algorithm

I've got a binary matrix and I'm trying to find all the largest rectangles that can be formed by adjoining elements in the matrix. By largest rectangles I mean, all the rectangles which are unique, non subsets of any other rectangles. For example, the following matrix contains six such rectangles.
This is related to the set cover problem, though here I'm interested in the maximum number of rectangles, not the minimum. An approach I've tried is to find all the rectangles regardless of size, then compare rectangles and remove them if they are a subset of another rectangle. This is not an optimal approach. It seems like this case of the set cover problem shouldn't be too hard.
I've had a look and not found anything similar to this problem. There is this paper, which has some good ideas, but still wide of the mark. Is there another name for this particular problem? Are there any existing algorithms for finding all possible rectangles in a set cover problem?

After a bit more work, I've realised this is not really related to the set cover problem. It's actually the 'finding the unique rectangles that are not contained within in any other rectangle in a binary matrix problem'.
I have come up with something that works well, but I have no idea about it's complexity.
Basically, line sweep across the matrix horizontally and vertically. In each case, look for contiguous groups of 1's that can form rectangles with the next line. This results in a number of rectangles, some of which are duplicates or sub rectangles of others. These rectangles are reduced to a unique set where no rectangle is a sub rectangle of another. Then you have all the rectangles.
Here is a diagram that relates to the image in the original post:

i dont speek english very wheel but, maximal rectangles overlapping is a problem that resolve many pb, if you somme maximal rectangles, geometrique sommation you find optimal volume that you derive to find queleton etc

Related

Maximizing the area of small rectangles in a bigger one (Rectangle packing)

Let's say I have a set of rectangles of varying widths and heights, which cannot be rotated.
I now want to fit a subset of those into a larger rectangle with a set height and width, so that the sum of the areas of the smaller rectangles is maximized.
Is there an algorithm that can help me solve this problem?
I tried looking into rectangle packing in general, but all I could find were questions about minimizing the area taken up by the rectangles.
Here is a graphical example of what I want to do:
The rectangles on the right are the aforementioned set of smaller rectangles, which I want to fit into the left one.
As I cannot possibly put all the small rectangles into the bigger one, I now have to choose an optimal subset of them, like this, for example:
Is this the best arrangement and subset of rectangles? Probably not, since there is still some space left in the bigger rectangle. In an ideal case, the would be exactly 0 space left, and the sum of the areas of the small rectangles would be equal to the area of the big rectangle. However, as this is rarely possible, I need an algorithm, that can find the best possible arrangement and subset.

Efficient rectangles fitting algorithm

As an input, I have a "map", which is a polygon. All its sides are parallel to x- or y-axis (so all these polygons are described by rectangles which they consist of, size of all polygons are integers). Here you can see an example of a correct and a bad input.
The second input is a set of rectangles I want to fit in. All rectangles are described by theirs sizes width*height (every rectangle could have different integer size).
For a given input, I want to find out if it is possible to put all rectangles to the map. And if so, I want to get positions of all rectangles. Moreover, I could have some more conditions on location of rectangles. For example, I know that a two rectangles A,B in a map must be connected by one side.
Is there any efficient algorithm to this problem? I would say it can be transformed to some graph problem, but I have no idea how to represent it. Thanks for any help!
There's almost certainly no always-efficient algorithm, because this problem is NP-hard. To see this, notice that you can reduce any instance of the NP-hard Partition Problem to an instance of your problem:
For each number x_i in the partition problem, create a 1-by-x_i rectangle.
Set the "first input rectangle" size 2-by-(0.5*s), where s is the sum of all the x_i.
The instance of your problem described above has a solution if and only if the original Partition Problem instance has a solution, so if there was an efficient way to solve your problem, then you could use that same algorithm to efficiently solve the Partition Problem (and, via similar reductions, every other NP-hard problem).

How to compute the union polygon of two (or more) rectangles

For example we have two rectangles and they overlap. I want to get the exact range of the union of them. What is a good way to compute this?
These are the two overlapping rectangles. Suppose the cords of vertices are all known:
How can I compute the cords of the vertices of their union polygon? And what if I have more than two rectangles?
There exists a Line Sweep Algorithm to calculate area of union of n rectangles. Refer the link for details of the algorithm.
As said in article, there exist a boolean array implementation in O(N^2) time. Using the right data structure (balanced binary search tree), it can be reduced to O(NlogN) time.
Above algorithm can be extended to determine vertices as well.
Details:
Modify the event handling as follows:
When you add/remove the edge to the active set, note the starting point and ending point of the edge. If any point lies inside the already existing active set, then it doesn't constitute a vertex, otherwise it does.
This way you are able to find all the vertices of resultant polygon.
Note that above method can be extended to general polygon but it is more involved.
For a relatively simple and reliable way, you can work as follows:
sort all abscissas (of the vertical sides) and ordinates (of the horizontal sides) independently, and discard any duplicate.
this establishes mappings between the coordinates and integer indexes.
create a binary image of size NxN, filled with black.
for every rectangle, fill the image in white between the corresponding indexes.
then scan the image to find the corners, by contour tracing, and revert to the original coordinates.
This process isn't efficient as it takes time proportional to N² plus the sum of the (logical) areas of the rectangles, but it can be useful for a moderate amount of rectangles. It easily deals with coincidences.
In the case of two rectangles, there aren't so many different configurations possible and you can precompute all vertex sequences for the possible configuration (a small subset of the 2^9 possible images).
There is no need to explicitly create the image, just associate vertex sequences to the possible permutations of the input X and Y.
Look into binary space partitioning (BSP).
https://en.wikipedia.org/wiki/Binary_space_partitioning
If you had just two rectangles then a bit of hacking could yield some result, but for finding intersections and unions of multiple polygons you'll want to implement BSP.
Chapter 13 of Geometric Tools for Computer Graphics by Schneider and Eberly covers BSP. Be sure to download the errata for the book!
Eberly, one of the co-authors, has a wonderful website with PDFs and code samples for individual topics:
https://www.geometrictools.com/
http://www.geometrictools.com/Books/Books.html
Personally I believe this problem should be solved just as all other geometry problems are solved in engineering programs/languages, meshing.
So first convert your vertices into rectangular grids of fixed size, using for example:
MatLab meshgrid
Then go through all of your grid elements and remove any with duplicate edge elements. Now sum the number of remaining meshes and times it by the area of the mesh you have chosen.

Minimum area calculation algorithm (Place tiles on edge only)

I have different dimension of small rectangles (1cm x 2xm, 2cmx3cm, 4cm*6cm etc). The number of different type rectangles may vary depending on case. Each type of different rectangles may have different number of counts.
I need to create a big rectangle with all these small rectangles which these small rectangles can only be placed on the edges. no rotations. The final outer rectangle should ideally be smiliar to a square shape. X ~Y. Not all edges need to be filled up. There can be gaps in between smaller rectangles. Picture Example:
http://i.stack.imgur.com/GqI5z.png
I am trying to write a code that finds out the minimum possible area that can be formed.
I have an algorithm that loop through all possible placement to find out the minimum area possible. But that takes a long run time as number of different type rectangles and number of rectangles increase. i.e. 2 type of rectangles, each has 100 + rectangles. 8 for loops. That will be ~100^8 iterations
Any ideas on better and faster algorithm to calculate the minimum possible area? code is in python, but any algorithm concept is fine.
for rectange_1_top_count in (range(0,all_rectangles[1]["count"]+1)):
for rectange_1_bottom_count in range(0,all_rectangles[1]["count"]-rectange_1_top_count+1):
for rectange_1_left_count in (range(0,all_rectangles[1]["count"]-rectange_1_top_count-rectange_1_bottom_count+1)):
for rectange_1_right_count in ([all_rectangles[1]["count"]-rectange_1_top_count-rectange_1_bottom_count-rectange_1_left_count]):
for rectange_2_top_count in (range(0,all_rectangles[2]["count"]+1)):
for rectange_2_bottom_count in (range(0,all_rectangles[2]["count"]-rectange_2_top_count+1)):
for rectange_2_left_count in (range(0,all_rectangles[2]["count"]-rectange_2_bottom_count-rectange_2_top_count+1)):
for rectange_2_right_count in [(all_rectangles[2]["count"]-rectange_2_bottom_count-rectange_2_left_count-rectange_2_top_count)]:
area=calculate_minimum_area()
if area< minimum_area:
minimum_area=area
This looks like an NP-hard problem, so there exists no simple and efficient algorithm. It doesn't mean that there is no good heuristic that you can use, but if you have many small rectangles, you won't find the optimal solution fast.
Why is it NP-hard? Let's assume all your rectangles have height 1 and you have on rectangle of height 2, then it would make sense to look for a solution with total height 2 (basically, you try to form two horizontal lines of height-1 rectangles with the same length). To figure out if such a solution exists, you would have to form two subsets of your small rectangles, both adding up to the same total width. This is called the partition problem and it is NP-complete. Even if there may be gaps and the total widths are not required to be the same, this is still an NP-hard problem. You can reduce the partition problem to your rectangle problem by converting the elements to partition into rectangles of height 1 as outlined above.
I'll wait for the answer to the questions I posted in the comments to your question and then think about it again.

Cover a polygon with K rectangles minimizing the rectangles area

Given a general polygon P (might contain holes) and a number of rectangles K, I want to find K rectangles {r1,r2,...,rk} such that the polygon is contained in the union of these rectangles. Also, I want to minimize the area of the union of the rectangles.
Note that the rectangles may overlap each other. Also, they're axis-aligned.
As an example, for K=1, the algorithm should report the minimum bounding box.
I've read some papers on the subject, namely "A linear-time approximation algorithm for minimum rectangular covering" and "Covering a polygonal region by rectangles".
The first article approaches the problem by dividing the polygon into simpler polygons, but it considers only rectangles that are within the polygon.
The second article assumes that the rectangles dimension are fixed and simply translates them around and tries to cover the polygon with the minimum number of rectangles.
I know that there is an article named "covering a polygon with few rectangles" that has what I believe to be exactly what I'm looking for, but I would have to pay in order to have access to it and I'd like to dig depeer before spending any money.
Since this is an np-complete or np-hard (not sure) problem, i'm not expecting fast exact algorithms, therefore approximations are also relevant.
Does anyone have knowledge of previous work on this particular problem? Any work that might relate is also welcome. Also, if you have ideas on how to address the problem it would be great!
Thanks in advance.

Resources