<p>Ok, so you have a polygon $P$ with integer-length axis-parallel sides and possibly with holes (the shape you want to cover) and you want to partition it into as few $1\times a$ or $b\times 1$ rectangles as possible. At first I thought you wanted the minimum partition into rectangles of arbitrary shapes, which has a known polynomial time solution involving a reduction to maximum independent sets in bipartite graphs. But I think this one is also polynomial, via a different reduction to the same problem.</p>

<p>Draw a graph $G$ that has a vertex for every unit-length line segment that separates two squares of $P$, and that has an edge connecting the vertices for every two perpendicular line segments that share an endpoint. Then the partitions of $P$ into unit-width rectangles correspond one-for-one with independent sets of $G$. If a vertex $v$ of $G$ corresponds to a line segment $s$, then $v$ belongs to a given independent set exactly when the two squares separated by $s$ belong to the same rectangle as each other in the corresponding partition.</p>

<p>Under this correspondence, we have the equation $R=S-I$ where $R$ denotes the number of rectangles in a given partition, $S$ denotes the number of squares in $P$, and $I$ denotes the cardinality of the independent set; this is easy to see by induction on $I$, by removing one independent set element at a time. Since $S$ is fixed, minimizing $R$ is the same as maximizing $I$, and the optimal partition of $P$ corresponds to the maximum independent set of $G$. But $G$ is a bipartite graph (horizontal segments are adjacent only to vertical segments and vice versa) so its maximum independent set may be found in polynomial time (see <a href="http://en.wikipedia.org/wiki/K%C3%B6nig%27s_theorem_%28graph_theory%29">König's theorem</a> on Wikipedia).</p>

CC BY-SA 3.0
2013-02-18T23:47:30.897Z
2013-02-19T06:42:52.833Z
2013-02-19T06:42:52.833Z
David Eppstein
<p>Consider a problem in which you are given a 2D grid (e.g. a chessboard) where certain squares are occupied
and you need to put the minimum number of non-overlapping rectangles of any size w x h where w = 1 or h = 1 (i.e. "square segments") such that all unoccupied squares are covered and each rectangle covers only unoccupied squares. </p>

<p>For example, for the grid</p>

<pre><code>..###
.....
..###
.#...
</code></pre>

<p>the solution would be 4, since you can cover all the unoccupied squares (denoted by '.') with 4 such rectangles as follows:</p>

<pre><code>12###
12333
12###
1#444
</code></pre>

<p>I tried to come up with a polynomial algorithm or prove that this problem is NP-hard, but without success. Can anyone help me to prove / disprove that this is problem is in P, or point me to some related work / problems?</p>

CC BY-SA 3.0
2013-02-18T23:35:02.763Z
Can rectangles also cover occupied squares?  Also, can rectangles overlap?  The way you presented the solution of the example suggests that neither is allowed, but your problem statement does not say any restriction.
Tsuyoshi Ito
CC BY-SA 3.0
2013-02-18T23:37:16.260Z
Correct, neither is allowed. I'm going to update the problem statement.
eold
CC BY-SA 3.0
2017-02-19T15:09:40.150Z
http://stackoverflow.com/questions/8639154/algorithm-to-find-the-minimum-number-of-rectangles-covering-certain-elements-in
Ciro Santilli OurBigBook.com
<reference-request><np-hardness><planar-graphs><packing><polynomial-time>
Fitting minimum number of rectangles of width/height 1 into a 2D grid
eold
# Fitting minimum number of rectangles of width/height 1 into a 2D grid

(Asked by: username_0 on 2013-02-18)

Consider a problem in which you are given a 2D grid (e.g. a chessboard) where certain squares are occupied
and you need to put the minimum number of non-overlapping rectangles of any size w x h where w = 1 or h = 1 (i.e. "square segments") such that all unoccupied squares are covered and each rectangle covers only unoccupied squares. 
For example, for the grid
..###
.....
..###
.#...

the solution would be 4, since you can cover all the unoccupied squares (denoted by '.') with 4 such rectangles as follows:
12###
12333
12###
1#444

I tried to come up with a polynomial algorithm or prove that this problem is NP-hard, but without success. Can anyone help me to prove / disprove that this is problem is in P, or point me to some related work / problems?

**Tags:** reference-request, np-hardness, planar-graphs, packing, polynomial-time
> **Comments:**
> **username_1**: Can rectangles also cover occupied squares?  Also, can rectangles overlap?  The way you presented the solution of the example suggests that neither is allowed, but your problem statement does not say any restriction.  
> **username_0**: Correct, neither is allowed. I'm going to update the problem statement.  
> **username_2**: http://stackoverflow.com/questions/8639154/algorithm-to-find-the-minimum-number-of-rectangles-covering-certain-elements-in  

---

## 1 answers

### Answer by username_3 on 2013-02-18. Score: 11 | Accepted answer

Ok, so you have a polygon $P$ with integer-length axis-parallel sides and possibly with holes (the shape you want to cover) and you want to partition it into as few $1\times a$ or $b\times 1$ rectangles as possible. At first I thought you wanted the minimum partition into rectangles of arbitrary shapes, which has a known polynomial time solution involving a reduction to maximum independent sets in bipartite graphs. But I think this one is also polynomial, via a different reduction to the same problem.

Draw a graph $G$ that has a vertex for every unit-length line segment that separates two squares of $P$, and that has an edge connecting the vertices for every two perpendicular line segments that share an endpoint. Then the partitions of $P$ into unit-width rectangles correspond one-for-one with independent sets of $G$. If a vertex $v$ of $G$ corresponds to a line segment $s$, then $v$ belongs to a given independent set exactly when the two squares separated by $s$ belong to the same rectangle as each other in the corresponding partition.

Under this correspondence, we have the equation $R=S-I$ where $R$ denotes the number of rectangles in a given partition, $S$ denotes the number of squares in $P$, and $I$ denotes the cardinality of the independent set; this is easy to see by induction on $I$, by removing one independent set element at a time. Since $S$ is fixed, minimizing $R$ is the same as maximizing $I$, and the optimal partition of $P$ corresponds to the maximum independent set of $G$. But $G$ is a bipartite graph (horizontal segments are adjacent only to vertical segments and vice versa) so its maximum independent set may be found in polynomial time (see [König's theorem](http://en.wikipedia.org/wiki/K%C3%B6nig%27s_theorem_%28graph_theory%29) on Wikipedia).


---

