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

Setting Up GCP Instance for Deep Learning

This post is going to be very different from what I write here. The content is a memo how I create a GCP (Google Cloud Platform) instance for Deep Learning. While I study algorithm stuff, I also have been studying Deep Learning. In my early days, I tried to train my Deep Learning model only on a laptop. My laptop is 2012 model MacBook Pro, so I would say it is reasonably fast. However, when it comes to Deep Learning, the training was quite painful on the such machine. Often, I ran the training over night to get a disappointed result. Still, I didn’t use any of pricey cloud environment since it was my personal study unrelated to my day job. I wanted to save money.

Complete Binary Tree

Problems which ask a binary tree traverse, add/delete nodes, etc. are popular in algorithm questions. The binary trees are often just a binary tree or binary search tree. Sometimes, the problem pinpoints a particular type of a binary tree, for example, a balanced binary tree or complete binary tree.

Catalan number

What is Catalan number ? The Catalan number belongs to the domain of combinatorial mathematics. It is a sequence of natural numbers such that: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, ... The sequence appears in counting problems. Wikipedia has the details about the sequence: Catalan Number.