Maximal Square and Rectangle

Published: Jun 16, 2017

A bunch of algorithm questions take a style of “maximum is a good thing.” Maximal sum, maximal length or maximal size are examples. This memo is about maximal size, precisely, square and rectangle.

These two have quite similar descriptions. So, I call those siblings. The problems are “given 2D matrix filled with 0’s and 1’s, find maximal square/rectangle.” Approaches how to solve are also similar. However, a difficulty level is not the same. The maximal square question is much easier. The maximal rectangle question needs an additional step to find the maximum.

I’m going to start off with the maximal square.

Problem Description - Maximal Square

Given a 2D binary matrix filled with 0’s and 1’s, find the maximal square with all 1’s.

For example, following 2D matrix is given:


1   0   1   0   0

1   0   1   1   1

1   1   1   1   1

1   0   0   1   0

The answer will be 4.


1   0   1   0   0
      +-------+
1   0 | 1   1 | 1
      |       |
1   1 | 1   1 | 1
      +-------+
1   0   0   1   0

The idea to find maximal square

This is a dynamic programming question, so optimal substructure exists:

  1. include the current cell to form a square
  2. exclude the current call

The state to maintain in the auxiliary table is the size of the square so far. Following the DP manner, the table will be created by bottom up.

The first column and row remain as those are. When the value of matrix at i’th row and j’th column is 1, look above, above-left, and left. Among three, find minimum then plus one. This is the value in the auxiliary table.

Java code for finding maximal square

The performance is: time O(nm), space O(nm), where n: rows, m: columns

The result is:


4

Problem Description - Maximal Rectangle

Given a 2D binary matrix filled with 0’s and 1’s, find the maximal rectangle with all 1’s.

For example, following 2D matrix is given:


1   0   1   0   0

1   0   1   1   1

1   1   1   1   1

1   0   0   1   0

The answer will be 6.


1   0   1   0   0
      +-----------+
1   0 | 1   1   1 |
      |           |
1   1 | 1   1   1 |
      +-----------+
1   0   0   1   0

The idea to find maximal rectangle

Also, this is a dynamic programming problem, but has an extra process. The first step of DP sees vertically. The second step sees horizontally.

For the DP step, the optimal substructure exists, like the square problem.

  1. include the current cell to form a rectangle
  2. exclude the current cell

The state to maintain in the auxiliary table is the size of 1’s of vertical stretch. Following the DP manner, the table will be created by bottom up. When the value of matrix at i’th row and j’th column is 1, look above then plus one. This is the value in the auxiliary table.

After creating the auxiliary table, I need to find the maximal area looking at horizontally. How to find it? This is exactly the same as Largest Rectangle in Histogram problem.

The second step looks each row to find its max. By comparison of each max, I can get the maximal area.

Java code for finding maximal rectangle

The performance is: time O(nm), space O(nm), where n: rows, m: columns

The result is:


6

Resources

Latest Posts

Application Development by Rails Action Cable

The previous two blog posts introduced WebSocket and how to implement a WebSocket application on Ruby on Rails. This blog post digs deeper. It is a memo on creating a more realistic application by Action Cable.

Real-time App on Rails by Action Cable

The previous blog post, WebSocket on Rails by Action Cable, focused on WebSocket as a protocol. As in the previous post, by default, Rails app responds to WebSocket connection requests without any hassle. However, other than connecting and sending ping frames, it doesn’t do anything. This blog post focuses on an application side and explains how we can create a full-duplex, bidirectional app.

WebSocket on Rails by Action Cable

In the web application domain, we hear some protocol names. Absolutely, HTTP or HTTPS is the most famous protocol that all web developers know. Although there’s a mechanism of Keep-Alive, a single request/response sequence with a single client/server is all done by HTTP. The client initiates the HTTP request to the server. Once the client receives the HTTP response from the server, communication finishes. As far as HTTP is used, the server just waits and waits. Only when the request comes in, the server can send back some data to the client. This communication style is surprisingly capable of doing many things, so most web applications are satisfied with HTTP.