Vous êtes sur la page 1sur 3

AN ANALYSIS OF THE MAXIMUM SUBRECTANGLE PROBLEM

Lecturer in Mathematics, Radu Boriga Ph.D. cand. Titu Maiorescu University of Bucharest Faculty of Science and Technology Information
Abstract: We present three algorithms which solve the well-known Maximum Subrectangle Problem. Using some optimization techniques we reduce the time complexity from O(n6) in the initial approach to O(n3) in the final approach, keeping the dimension of the necessary memory space at the minimum possible - O(n2) . INTRODUCTION Firstly, we review the terms of the Maximum Subrectangle Problem: Given a matrix A of n*n integer numbers, we want to find the subrectangle of it which has the largest sum of elements among all the others subrectangles. In the paragraphs below we will solve the problem, using some well-known techniques based on a precalculated data and/or the Dynamic Programming, as shown in [1], [2]. We will denote a subrectangle of the matrix by (r, c, h, w) where r and c are respectively the row and the column of the left-upper corner of the subrectangle, while h and w are respectively the height and the width of it. In all approaches we will find only the largest sum, leaving as an exercise for the reader to perform some minor adds to the presented algorithms, so that these may compute the subrectangle for which the sum is maximum, too. FIRST APPROACH Using a brute-force type algorithm we are able to find all the subrectangles (r, c, h, w) of the matrix (1r,cn; 0hn-r; 0wn-c; ) and, after calculating the sum of the current subrectangle and comparing it with the maximum sum founded until that moment, to get the maximum sum as is shown below: maximum_sum a1,1 for r 1,n do for c 1,n do for h 0,n-r do for w 0,n-c do current_sum=0 for i r,r+h do for j c,c+w do current_sum current_sum + ai,j if current_sum > maximum_sum then maximum_sum current_sum Its obviously that the complexity of this algorithm is O(n6), which is practically too big even for small values of n. SECOND APPROACH In this approach we use some pre-calculated data to avoid the calculation of the sum for the each subrectangle. For this purpose, we consider another matrix S, having n rows and n columns of integer numbers too, whose elements si,j retain the sum of the elements of the subrectangle (1, 1, i, j) for each i and j between 1 and n. Using this new matrix S we are able to compute the sum of the elements of the current subrectangle (r, c, h, w) in O(1), instead of O(n2), with the help of the next assignment: current_sum sr+h,c+w - sr-1,c+w - sr+h,c-1 + sr-1,c-1 (1)

Replacing in the algorithm above the code block, shown below, in which is computed the sum of the elements of the current subrectangle (r, c, h, w) with (1) current_sum=0

for i r,r+h do for j c,c+w do current_sum

current_sum + ai,j

we obtain the following algorithm: maximum_sum a1,1 for r 1,n do for c 1,n do for h 0,n-r do for w 0,n-c do current_sum sr+h,c+w - sr-1,c+w - sr+h,c-1 + sr-1,c-1 if current_sum > maximum_sum then maximum_sum current_sum As we can see very easy, the complexity of the new algorithm is O(n4), so the doubling of the amount of necessary memory, due to the use of the second matrix S, is justified. The elements si,j of the matrix S must be calculated before, using the following formulas: s0,i = si,0 = 0, 0in s1,1 = a1,1 si,1 = si-1,1 + ai,1, 2in s1,i = s1,i-1 + a1,i, 2in (4) (5) (6) (2) (3)

si,j = si,j-1 + si-1,j - si-1,j-1 + ai,j, 2i,jn

The code blocks that implement the formulas (2) (6) are shown below: s1,1 a1,1 for i 1,n do s0,i 0 si,0 0 for i 1,n do si,1 si-1,1 + ai,1 s1,i s1,i-1 + a1,i for i 1,n do for j 1,n do si,j si,j-1 + si-1,j - si-1,j-1 + ai,j As we can see clearly, the complexity of the computing of the elements of the matrix S is only O(n2), so it dont affect the complexity of the new algorithm, which remains equal to O(n4). THIRD APPROACH In this approach we keep using the pre-calculated matrix S and we elaborate a new algorithm, based on the Maximum Interval Problem which is the one-dimensional array version of the Maximum Subrectangle Problem. Using the Dynamic Programming we can solve the Maximum Interval Problem in O(n) ([2], [3]), so, if we will find a way to apply it for solving the Maximum Subrectangle Problem we will obtain an algorithm with the complexity O(n3).

The mainly idea which is used to solve the Maximum Interval Problem is to scan the given one-dimensional array from left to right and if the current sum is positive to try to extend it. Otherwise, there is no sense to try to extend it, so we must to start a new sequence. We can apply this idea to solve the the Maximum Subrectangle Problem in the next manner: for each two rows r1 and r2 (1r1r2n) we scan the possible subrectangles from a left column lc to a right one rc, starting at the first column, and, if the sum of the current subrectangle is positive we extend it to the next column. Otherwise, we start a new current subrectangle from the next column. The algorithm that implements this idea is shown below: maximum_sum a1,1 for r1 1,n do for r2 r1,n do lc 1 for rc 1,n do current_sum sr2,rc - sr1-1,rc - sr2,lc-1 + sr1-1,lc-1 if current_sum > maximum_sum then maximum_sum current_sum if current_sum <= 0 then lc rc + 1 This algorithm has a very good complexity, the minimum possible for this problem, respectively O(n3). CONCLUSIONS The Maximum Subrectangle Problem has a large applicability field not only in computer science, but also in the economics, chemistry, physics and logic games. The knowledge of a fast solving algorithm is very important because, in the most cases, this problem is only a little part of a bigger problem. In a future work we hope to extend this algorithm to a three-dimensional array, keeping the complexity as low as possible. REFERENCES [1] T.H. Cormen, C.E. Leiserson, R.R. Rives, Introducere n algoritmi, Editura Byblos, 2004 [2] H. Georgescu, Tehnici de programare, Editura Univiversitii din Bucureti, 2005 [3] L. Livovschi, H. Georgescu, Sinteza i analiza algoritmilor, Editura tiinific i Enciclopedic, 1986

Vous aimerez peut-être aussi