[Algorithm design] maximum submatrix Problem

Source: Internet
Author: User
I. Maximum submatrix problems:
Given a matrix of N * n (0 <n <= 100), locate a sub-matrix of this matrix, and output the maximum value of each element of this Sub-matrix.
Example:
0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2
The child matrix in the upper left corner:
9 2
-4 1
-1 8
The sub-matrix value is 9 + 2 + (-4) + 1 + (-1) + 8 = 15.

Ii. Analysis

Child MatrixIs a new matrix composed of some rows and columns in the matrix.

For example

It can also be represented by a (3; 2), indicating that the remaining matrices of 3rd rows and 2nd columns are removed. These two methods are commonly used, but there is still no standard way to represent the child matrix.

The above is the definition provided by Wikipedia. I feel that it is not the same as the definition of this question?


The first method we come up with is to enumerate all the child matrices of a matrix, however, when n is large, the number of submatrices of a n * n matrix is a big number o (N ^ 2 * n ^ 2). Obviously, this method is not feasible. How can we reduce the complexity of the problem? By the way, I believe everyone should know and use dynamic planning. How to use dynamic planning for this question?

Please refer to --> the maximum sub-segment and question first
What is the connection between this question and the largest subsection?

Assume that the maximum sub-matrix is a sub-matrix from row R to row K, from column I to column J, as shown below (Ari represents a [r] [I], suppose the array subscript starts from 1 ):

| A11 ...... A1i ...... A1j ...... A1N |
| A21 ...... A2i ...... A2j ...... A2n |
| ...... |
| ...... |
| AR1 ...... Ari ...... ARJ ...... Arn |
| ...... |
| ...... |
| Ak1 ...... Aki ...... Akj ...... Akn |
| ...... |
| An1 ...... Ani ...... Anj ...... Ann |

Then, we add the same columns in each row from row R to row K. We can get a one-dimensional array as follows:
(AR1 + ...... + Ak1, AR2 + ...... + Ak2 ,......, Arn + ...... + Akn)
From this we can see that the last thing we want is the maximum sub-segment and problem of this one-dimensional array. Now we have converted the problem into the problem that has been solved above.

3. Source Code

C ++:

# Include <iostream> <br/> using namespace STD; </P> <p> int maxsubarray (int A [], int N) <br/> {<br/> int B = 0, sum = A [0]; <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> If (B> 0) <br/> B + = A [I]; <br/> else <br/> B = A [I]; <br/> If (B> sum) <br/> sum = B; <br/>}< br/> return sum; <br/>}< br/> int maxsubmatrix (INT array [] [3], int N) <br/>{< br/> int I, J, K, max = 0, sum =-100000000; <br/> int B [3]; <br/> for (I = 0; I <n; I ++) <br/> {<br/> for (k = 0; k <N; k ++) // initialize B [] <br/>{< br/> B [k] = 0; <br/>}< br/> for (j = I; j <n; j ++) // adds row I to row J, calculate the maximum value for each addition <br/>{< br/> for (k = 0; k <n; k ++) <br/>{< br/> B [k] + = array [J] [k]; <br/>}< br/> max = maxsubarray (B, k); <br/> If (max> sum) <br/>{< br/> sum = max; <br/>}< br/> return sum; <br/>}< br/> int main () <br/> {<br/> int n = 3; <br/> int array [3] [3] = {1, 2, 3}, {-1, -2,-3 },{ 4, 5, 6 };</P> <p> cout <"maxsum:" <maxsubmatrix (array, n) <Endl; </P> <p>}

Java:

Import Java. util. topology; <br/> public class pku_1050 <br/>{< br/> private int maxsubarray (int n, int A []) <br/> {<br/> int B = 0, sum =-10000000; <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> If (B> 0) <br/> B + = A [I]; <br/> else <br/> B = A [I]; <br/> If (B> sum) <br/> sum = B; <br/>}< br/> return sum; <br/>}< br/> private int maxsubmatrix (int n, int [] [] array) <br/> {<br/> int I, J, K, max = 0, sum =-100000000; <br/> int B [] = new int [101]; <br/> for (I = 0; I <n; I ++) <br/> {<br/> for (k = 0; k <N; k ++) // initialize B [] <br/>{< br/> B [k] = 0; <br/>}< br/> for (j = I; j <n; j ++) // adds row I to row J, calculate the maximum value for each addition <br/>{< br/> for (k = 0; k <n; k ++) <br/>{< br/> B [k] + = array [J] [k]; <br/>}< br/> max = maxsubarray (K, b); <br/> If (max> sum) <br/>{< br/> sum = max; <br/>}< br/> return sum; <br/>}< br/> Public static void main (string ARGs []) <br/>{< br/> pku_1050 P = new pku_1050 (); <br/> CIN = new partition (system. in); <br/> int n = 0; <br/> int [] [] array = new int [101] [101]; <br/> while (CIN. hasnext () <br/>{< br/> N = cin. nextint (); <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> for (Int J = 0; j <n; j ++) <br/>{< br/> array [I] [J] = cin. nextint (); <br/>}< br/> system. out. println (P. maxsubmatrix (n, array); <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.