I'm thinking about this problem: Given $$$n$$$ rectangles on a unit grid with coordinates around $$$10^9$$$. We want to decompose the union of them into non-overlapping rectangles. There are two interests: the number of output rectangles and the running time. Here is one of my approaches:
Process rectangles one by one. We keep a list of processed non-overlapping rectangles. For a newly added rectangle, we iterate over the list and find one that intersects our new rectangle. If there is, we divide the new rectangles into 4 small parts and process them recursively. This approach depends on the size of the result; so if there are $$$k$$$ output rectangles, the running time seems to be around $$$O(nk)$$$. In random test cases, $$$k \approx O(n)$$$. And it seems to be very hard to generate a counter test since we could shuffle the order of rectangles first.
I'm also thinking about scanline but haven't figured out how to get a good number of output rectangles. The best thing I came up with would result $$$O(n \log n)$$$ time complexity but may contain $$$O(n^2)$$$ rectangles in the worst cases, which is easy to generate counter tests.
I also tried googling but the closest thing I found is partitioning a polygon. Would you share some thoughts on this problem?
What do you want if there are several decompositions into non-overlapping rectangles? I mean usually there are plenty. I assume you want as few rectangles as possible. It doesn't look trivial to me why your first approach would be optimal. The decomposition is clearly not unique (not even the optimal one, take (0, 0), (1, 2) and (0, 0), (2, 1))
Any decomposition is fine. I'm only interested in the number of output rectangles and the running time.
Do you want the output rectangles to be non-overlapping? If so I think it might require n^2 rectangles in the answer, and the scanline would be asymtotically optimal.
For instance, consider the case of O(n) rectangles forming a 2d grid, like this:
I don't think that is even possible to represent in fewer than O(n^2) rectangles...
Nice, this is what I'm looking for. Thanks!
if i meet it as task i would just use a quadtree. we build a quadtree keeping rectangles inside vertexes and just recursively merge them without overlapping UPD: oh i think it doesn't work