2

I would like to know a few things about the algorithm for the following problem: Given a matrix with 0s and 1s and the size of a square, what is the maximum number of adjacent squares for covering the 1s and what is their position? In case there are several possible square combinations, just output one.

EX: For a size of 2:

Input:

0 0 1 0 0 0
0 1 1 1 1 0
0 0 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 0 0 0

Possible output: Maximum 3 squares (each marked with a letter)

0 0 1 0 0 0
0 1 a a 1 0
0 0 a a 1 0
0 b b c c 0
0 b b c c 0
0 1 1 0 0 0

I would like to know if a polynomial (or pseudo-polynomial) algorithm for the optimum solution exists. If yes, what is the algorithm and its asymptotic complexity? If not, what approximation algorithm should I use?

Also, you can assume, that there are no 'islands' of 0s inside the areas of 1s if this makes the problem easier, so the following case should not be encountered:

1 1 1
1 0 1
1 1 1

I am an amateur programmer and this is my first question here. It would be very useful for me if your answer would also suggest an area of study for this algorithm. Thanks in advance.

2 Answers2

1

I would like to know if a polynomial (or pseudo-polynomial) algorithm for the optimum solution exists. If yes, what is the algorithm and its asymptotic complexity?

Polynomial algorithm does not exist. Here's why. We will reduce this problem to a more widely known one.

We can do the following: for every cell (x, y) on the board we can say whether or not we can put a square on board such that the square's left upper corner is situated at (x, y) and the square covers only 1s. We can answer this kind of question in constant time with with O(n^2) time needed for precomputing. For all (x, y) which can do this we can construct the following graph G(E,V). The set of the vertices E is exactly this set of (x, y) which satisfy the condition mentioned above. We also say the ((x0, y0), (x1, y1)) is an edge if the squares starting from respectively, (x0, y0), (x1, y1), cover only 1s and they have a square in common. But since my guess it that they would be hard to implement and won't give that much of a improvement I suggest you to write a simple backtracking algorithm.

Notice now that your problem is now reduced to finding the maximum independent sets. This problem is NP-hard. Notice, however, that there exist more efficient algorithms than the usual brute force O(n^2*2^n) (examines every vertex subset and checks whether it is an independent set) that are mentioned in the article. But since I think that they won't give you that much of a improvement I suggest you to write a simple backtracking algorithm. It would be the best thing to do in this kind of situation.

sve
  • 4,336
  • 1
  • 19
  • 30
0

NP Complete proof

I think you can show this is NP-complete by reducing planar 3-sat (known to be NP-complete) to your problem for a size of 3.

The idea is to make use of wires that look like:

111 111 111
111 111 111
1111111111111
  111 111 111
  111 111 111

This can be covered in 2 ways, either:

aaa bbb ccc
aaa bbb ccc
aaa1bbb1ccc11
  111 111 111
  111 111 111

or

111 111 111
111 111 111
11aaa1bbb1ccc
  aaa bbb ccc
  aaa bbb ccc

Both ways use the same number of squares (3 in this case, although the wires can be extended further).

Now the idea is to use one of these wires for each of the literals in your planar 3-sat problem.
(The two ways of covering correspond to whether the literal is selected as true or false)

Also construct an empty box:

11X
111
1111
Y111
 11Z

for each of your clauses.

This box has space for a single square if and only if X is free or Y is free or Z is free.

Use more wires to connect up the clause box to the literal wires (connecting at X or Y or Z of the clause box and choosing the connection point on the wire according to whether the literal is used positively or negatively in the clause).

The idea is that each wire will always be covered with a fixed number of squares, but the clause box will only be covered with a square if at least one of the literal wires end up not covering one of the special corners (X,Y,Z).

Therefore the maximal number of squares that can be placed will be a fixed number (for the wires) + 1 for each clause that is satisfied.

You can therefore work out whether it is possible to solve planar 3-sat by finding an assignment to the literals satisfying all the clauses at the same time.

Approximation

In practice, you can get an easy approximation algorithm by using a greedy covering (within a factor of 4 because placing a square can collide with at most 4 squares in the optimal solution).

Real world solution

I would expect standard SAT solvers do do a really good job of finding approximate/optimal answers to this.

Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75