While working on the image processing problem, I encountered the following problem: in a unit square there are n points with coordinates $ x_i $ and $ y_i $, each of which has a positive or negative weight $ w_i $. Find the rectangle so that the sum of all the weights of these points lying inside the rectangle is positive and maximum.
By defining the correct grid, the problem can be rephrased as finding the submatrix in the n-on-n matrix A, the sum of the elements of which is maximal. This is also known as the “maximum sub-problem” and has been discussed earlier on SO. While the brute force approach has an O (n ^ 5) runtime, there is a peculiar complex solution with O (n ^ 3) runtime. It uses a solution for the corresponding one-dimensional problem, called the “maximum subaram problem” , with an O (n) runtime.
I implemented both algorithms in R and can solve 100 seconds of points in a few seconds. But with thousands of points, it will be too slow, perhaps even when outsourcing loops to some fortran or C code.
Now let's look at the matrix A. If we assume (without loss of generality) that all points have different x- or y-coordinates, then A has a special form: each row and column A has exactly one non-element -zero. For matrices with this special property, I assume that there must be an algorithm that performs the task in O (n ^ 2), or even better.
Here is an example with the addition of an optimal rectangle:
set.seed(723) N <- 50; w <- rnorm(N) x <- runif(N); y <- runif(N) clr <- ifelse (w >= 0, "blue", "red") plot(x, y, pch = 20, col = clr, xlim = c(0, 1), ylim = c(0, 1)) rect(0.075, 0.45, 0.31, 0.95, border="gray")
You see what may be red, i.e. negative indicates an optimal rectangle. It also shows that it is not enough to solve one-dimensional cases for x- and y-coordinates.
I will translate the standard solution into Fortran, but I would really like to have a more efficient algorithm.