Finding the heavier rectangle
Moderator: Board moderators
Finding the heavier rectangle
Hi, everybody.
Having a matrix of numbers (n*n), we want to find the (x1,y1) and (x2,y2) 1<=x1<=x2<=n , 1<=y1<=y2<=n such that the sum of the numbers in this rectangle is bigger than any other rectangle in this matrix.
I have a solution with O(n^3) but I think that we must have a better solution ( for example with O(n^2)). If you have one , plz tell me that , and if you think we haven't such a solution , tell me why?
Sincerely yours.
Shahab Tasharrofi
Having a matrix of numbers (n*n), we want to find the (x1,y1) and (x2,y2) 1<=x1<=x2<=n , 1<=y1<=y2<=n such that the sum of the numbers in this rectangle is bigger than any other rectangle in this matrix.
I have a solution with O(n^3) but I think that we must have a better solution ( for example with O(n^2)). If you have one , plz tell me that , and if you think we haven't such a solution , tell me why?
Sincerely yours.
Shahab Tasharrofi
Hi, everybody
Now let's talk about a restircted form of this algorithm. Having a matrix of 0 and 1, we must find the biggest rectangle that includes only the 0's (like the problem 10074). surely, we can find the solution with changing the 1's to -(n^2+1) and 0's to 1 where n represents the size of the matrix. but what's the best solution(also I have no idea better than O(n^3)) ? I will be glad to hear your opinions.
Sincerely yours,
Shahab Tasharrofi
Now let's talk about a restircted form of this algorithm. Having a matrix of 0 and 1, we must find the biggest rectangle that includes only the 0's (like the problem 10074). surely, we can find the solution with changing the 1's to -(n^2+1) and 0's to 1 where n represents the size of the matrix. but what's the best solution(also I have no idea better than O(n^3)) ? I will be glad to hear your opinions.

Sincerely yours,
Shahab Tasharrofi
Yarin: What do you mean by "complexity doesn't matter on input size" ?
Anyway, the reference:
Hisao Tamaki and Takeshi Tokuyama.
Algorithms for the maxium subarray problem based on matrix multiplication.
In Proceedings of the Ninth Annual ACM-SIAM Symposium on Discrete Algorithms,
pages 446-452, San Francisco, California, 25-27 January 1998
Shahab: No, I will not explain the algorithm to you. Don't have the time, and besides if you have more than just a casual interest in it, you ought to read the paper yourself.
cheers
Anyway, the reference:
Hisao Tamaki and Takeshi Tokuyama.
Algorithms for the maxium subarray problem based on matrix multiplication.
In Proceedings of the Ninth Annual ACM-SIAM Symposium on Discrete Algorithms,
pages 446-452, San Francisco, California, 25-27 January 1998
Shahab: No, I will not explain the algorithm to you. Don't have the time, and besides if you have more than just a casual interest in it, you ought to read the paper yourself.
cheers
Best algorithm to do this (108)
Well, this one Pedro Demasi teach me. Maybe I am forgetting something on the way, but that's the main idea..
Consider the linear case. You can find the greatest value on a submatrix in the following way:
1. The best you can get on the first position, is its own value
2. Go right. If what you had before can make the value greater, than the best on this position is its value + what you had before. Try to understand the sample below, where I have a matrix and following the best on each position
You can see that the greatest value of a submatrix is 9. Try with other values. This works.
Now we can have something similar to solve the 2-dimensional case. You have to do the followin:
0. i = 1. max = 0. v[1..n] = 0
1. Calculate the best value on each position for the line i
2. Sum these values to the values on the array v[] (v[0] += best[0]; v[1] += best[1]... v[n] += best[n])
3. If there's something grater than max on v[], make max = v[good_one]
4. i++
5. goto 1
We now got the best matrix starting from line 1 (the first line). We've got to try some other matrixes starting from lines 2, 3, ... n. So, repeat the proccess with i = 2, i = 3... i = n.
If someone can't understand something send an email to rafaelcotta@yahoo.com.br, that I'll post more details in here (I don't read the forums very frequently).
Consider the linear case. You can find the greatest value on a submatrix in the following way:
1. The best you can get on the first position, is its own value
2. Go right. If what you had before can make the value greater, than the best on this position is its value + what you had before. Try to understand the sample below, where I have a matrix and following the best on each position
Code: Select all
Matrix -> 9 -1 -23 4 3 0 -1 0 2
Best -> 9 8 -15 4 7 7 6 6 8
(the "Best" can be explained as:
9 = 9
8 = 9 - 1
-15 = 8 - 23
4 = 4 (if I included the -15 it was gonna be smaller)
7 = 4 + 3
7 = 0 + 7
6 = -1 + 7
6 = 0 + 6
8 = 6 + 2
)
Now we can have something similar to solve the 2-dimensional case. You have to do the followin:
0. i = 1. max = 0. v[1..n] = 0
1. Calculate the best value on each position for the line i
2. Sum these values to the values on the array v[] (v[0] += best[0]; v[1] += best[1]... v[n] += best[n])
3. If there's something grater than max on v[], make max = v[good_one]
4. i++
5. goto 1
We now got the best matrix starting from line 1 (the first line). We've got to try some other matrixes starting from lines 2, 3, ... n. So, repeat the proccess with i = 2, i = 3... i = n.
If someone can't understand something send an email to rafaelcotta@yahoo.com.br, that I'll post more details in here (I don't read the forums very frequently).
Hi rfcotta,
I think that your algorithm won't provide the rectangular shape of the answer, it just provides the connectivity of it ( but also it won't be the answer even for the connected numbers , I think ). for example , check your algorithm with the following matrix.
-11 -11 -11 1
-11 -11 1 1
-11 1 1 1
1 1 1 1
I will be happy to know if i'm right about it.
Shahab Tasharrofi
I think that your algorithm won't provide the rectangular shape of the answer, it just provides the connectivity of it ( but also it won't be the answer even for the connected numbers , I think ). for example , check your algorithm with the following matrix.
-11 -11 -11 1
-11 -11 1 1
-11 1 1 1
1 1 1 1
I will be happy to know if i'm right about it.
Shahab Tasharrofi
Actually complexity is a function that determines the efficiency of a program. For different input size, same program will run in different time, but they are comparable by a constant factor.Yarin: What do you mean by "complexity doesn't matter on input size" ?
For algorithms with different complexity, they can not be compared in the same manner.
If I understand it right, given a n by n matrix and the number m where m <= n, you want to find a m by m matrix within the n by n matrix such that the sum of the individual values within the m by m matrix is maximised.
There is no easy way to solve this question.. you have to use brute force searching. However, there is a way to reduce the time required by the brute force searching.
Using dynamic programming, you can easily reduce the number of operations required to a very minimum, then by simply tweaking your parameters for the dp table, you can dramatically increase the speed of the code.
There is no easy way to solve this question.. you have to use brute force searching. However, there is a way to reduce the time required by the brute force searching.
Using dynamic programming, you can easily reduce the number of operations required to a very minimum, then by simply tweaking your parameters for the dp table, you can dramatically increase the speed of the code.