Finding biggest negative submatrix in python












2














I want find the biggest submatrix which contains only negative numbers in a matrix, for example:



In



[[1,  -9, -2,   8,  6,  1],    
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]


the biggest submatrix containing only negative numbers is



[[-11, -7],
[-1, -9],
[-3,-5]]


(left upper corner coordinates: 1,2, right lower corner coordinates: 3,3).



What's the most effective way to do it?










share|improve this question
























  • Can the solution matrix contain zeros? Since 0 can be seen as both a positive and a negative number ...
    – quant
    Nov 11 at 20:14










  • No, in this case 0 is seen as a positive number.
    – IHav
    Nov 11 at 20:20










  • What does "biggest" mean here?
    – slider
    Nov 11 at 20:23










  • @slider Probably the biggest subarea.
    – quant
    Nov 11 at 20:25










  • A brute-force algorithm would be quite easy to write - however I guess that there are smarter algorithms for this problem. Does the solution have to be fast?
    – quant
    Nov 11 at 20:25
















2














I want find the biggest submatrix which contains only negative numbers in a matrix, for example:



In



[[1,  -9, -2,   8,  6,  1],    
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]


the biggest submatrix containing only negative numbers is



[[-11, -7],
[-1, -9],
[-3,-5]]


(left upper corner coordinates: 1,2, right lower corner coordinates: 3,3).



What's the most effective way to do it?










share|improve this question
























  • Can the solution matrix contain zeros? Since 0 can be seen as both a positive and a negative number ...
    – quant
    Nov 11 at 20:14










  • No, in this case 0 is seen as a positive number.
    – IHav
    Nov 11 at 20:20










  • What does "biggest" mean here?
    – slider
    Nov 11 at 20:23










  • @slider Probably the biggest subarea.
    – quant
    Nov 11 at 20:25










  • A brute-force algorithm would be quite easy to write - however I guess that there are smarter algorithms for this problem. Does the solution have to be fast?
    – quant
    Nov 11 at 20:25














2












2








2


6





I want find the biggest submatrix which contains only negative numbers in a matrix, for example:



In



[[1,  -9, -2,   8,  6,  1],    
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]


the biggest submatrix containing only negative numbers is



[[-11, -7],
[-1, -9],
[-3,-5]]


(left upper corner coordinates: 1,2, right lower corner coordinates: 3,3).



What's the most effective way to do it?










share|improve this question















I want find the biggest submatrix which contains only negative numbers in a matrix, for example:



In



[[1,  -9, -2,   8,  6,  1],    
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]


the biggest submatrix containing only negative numbers is



[[-11, -7],
[-1, -9],
[-3,-5]]


(left upper corner coordinates: 1,2, right lower corner coordinates: 3,3).



What's the most effective way to do it?







python python-3.x matrix submatrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:55

























asked Nov 11 at 19:55









IHav

585




585












  • Can the solution matrix contain zeros? Since 0 can be seen as both a positive and a negative number ...
    – quant
    Nov 11 at 20:14










  • No, in this case 0 is seen as a positive number.
    – IHav
    Nov 11 at 20:20










  • What does "biggest" mean here?
    – slider
    Nov 11 at 20:23










  • @slider Probably the biggest subarea.
    – quant
    Nov 11 at 20:25










  • A brute-force algorithm would be quite easy to write - however I guess that there are smarter algorithms for this problem. Does the solution have to be fast?
    – quant
    Nov 11 at 20:25


















  • Can the solution matrix contain zeros? Since 0 can be seen as both a positive and a negative number ...
    – quant
    Nov 11 at 20:14










  • No, in this case 0 is seen as a positive number.
    – IHav
    Nov 11 at 20:20










  • What does "biggest" mean here?
    – slider
    Nov 11 at 20:23










  • @slider Probably the biggest subarea.
    – quant
    Nov 11 at 20:25










  • A brute-force algorithm would be quite easy to write - however I guess that there are smarter algorithms for this problem. Does the solution have to be fast?
    – quant
    Nov 11 at 20:25
















Can the solution matrix contain zeros? Since 0 can be seen as both a positive and a negative number ...
– quant
Nov 11 at 20:14




Can the solution matrix contain zeros? Since 0 can be seen as both a positive and a negative number ...
– quant
Nov 11 at 20:14












No, in this case 0 is seen as a positive number.
– IHav
Nov 11 at 20:20




No, in this case 0 is seen as a positive number.
– IHav
Nov 11 at 20:20












What does "biggest" mean here?
– slider
Nov 11 at 20:23




What does "biggest" mean here?
– slider
Nov 11 at 20:23












@slider Probably the biggest subarea.
– quant
Nov 11 at 20:25




@slider Probably the biggest subarea.
– quant
Nov 11 at 20:25












A brute-force algorithm would be quite easy to write - however I guess that there are smarter algorithms for this problem. Does the solution have to be fast?
– quant
Nov 11 at 20:25




A brute-force algorithm would be quite easy to write - however I guess that there are smarter algorithms for this problem. Does the solution have to be fast?
– quant
Nov 11 at 20:25












1 Answer
1






active

oldest

votes


















3














A brute force solution. Will work, but may be considered too slow for a bigger matrix:



mOrig = [[1,  -9, -2,   8,  6,  1],
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]

# reduce the problem
# now we have a matrix that contains only 0 and 1
# at the place where there was a negative number
# there is now a 1 and at the places where a positive
# number had been there is now a 0. 0s are considered
# to be negative numbers, if you want to change this,
# change the x < 0 to x <= 0.
m = [[1 if x < 0 else 0 for x in z] for z in mOrig]

# now we have the problem to find the biggest submatrix
# consisting only 1s.

# first a function that checks if a submatrix only contains 1s
def containsOnly1s(m, x1, y1, x2, y2):
for i in range(x1, x2):
for j in range(y1, y2):
if m[i][j] == 0:
return False
return True

def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)

best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
for x2 in range(x1, len(m)):
for y2 in range(y1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)

for x in range(best[0], best[2]):
print("t".join([str(mOrig[x][y]) for y in range(best[1], best[3])]))


Will output



-11 -7
-1 -9
-3 -5


In case something else is meant with "biggest submatrix", the only function that needs to get changed is the following:



def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)


which is calculating the size of a submatrix.



Edit 1 ... first speedup



best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
if m[x1][y1] == 1: # The starting point must contain a 1
for x2 in range(x1 + 1, len(m)): # actually can start with x1 + 1 here
for y2 in range(y1 + 1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)
else:
# There is at least one 0 in the matrix, so every greater
# matrix will also contain this 0
break


Edit 2



Ok, after converting the matrix into a matrix of 0 and 1 (as I do via the line m = [[1 if x < 0 else 0 for x in z] for z in mOrig] the problem is the same as what is called the maximal rectangle problem in literature. So I googled a bit about known algorithms for this kind of problem and came across this site here http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529 which is describing a very fast algorithm to solve this kind of problem. To summarize the points of this website, the algorithm is exploiting the structure. This can be done by using a stack in order to remember the structure profile which allows us to recalculate the width in case a narrow rectangle gets reused when an wider one gets closed.






share|improve this answer























  • This works perfectly, but too slow for bigger matrix.
    – IHav
    Nov 11 at 20:53






  • 1




    I will check whether I can speed it up ...
    – quant
    Nov 11 at 20:55






  • 1




    We're talking about matrix of dimensions 5000x5000... and optimal time of solution would be less than 60 seconds. Though, I know.
    – IHav
    Nov 11 at 21:05










  • @IvanHlivan From my understanding of the problem this should be possible. I know of a very fast algorithm that is doing something similar in 1d not in 2d. So I only need to twist my head how I can do the same in 2d ...
    – quant
    Nov 11 at 21:08










  • @IvanHlivan Can you provide one 5000x5000 matrix, so that I can check the time on my computer?
    – quant
    Nov 11 at 21:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252626%2ffinding-biggest-negative-submatrix-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














A brute force solution. Will work, but may be considered too slow for a bigger matrix:



mOrig = [[1,  -9, -2,   8,  6,  1],
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]

# reduce the problem
# now we have a matrix that contains only 0 and 1
# at the place where there was a negative number
# there is now a 1 and at the places where a positive
# number had been there is now a 0. 0s are considered
# to be negative numbers, if you want to change this,
# change the x < 0 to x <= 0.
m = [[1 if x < 0 else 0 for x in z] for z in mOrig]

# now we have the problem to find the biggest submatrix
# consisting only 1s.

# first a function that checks if a submatrix only contains 1s
def containsOnly1s(m, x1, y1, x2, y2):
for i in range(x1, x2):
for j in range(y1, y2):
if m[i][j] == 0:
return False
return True

def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)

best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
for x2 in range(x1, len(m)):
for y2 in range(y1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)

for x in range(best[0], best[2]):
print("t".join([str(mOrig[x][y]) for y in range(best[1], best[3])]))


Will output



-11 -7
-1 -9
-3 -5


In case something else is meant with "biggest submatrix", the only function that needs to get changed is the following:



def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)


which is calculating the size of a submatrix.



Edit 1 ... first speedup



best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
if m[x1][y1] == 1: # The starting point must contain a 1
for x2 in range(x1 + 1, len(m)): # actually can start with x1 + 1 here
for y2 in range(y1 + 1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)
else:
# There is at least one 0 in the matrix, so every greater
# matrix will also contain this 0
break


Edit 2



Ok, after converting the matrix into a matrix of 0 and 1 (as I do via the line m = [[1 if x < 0 else 0 for x in z] for z in mOrig] the problem is the same as what is called the maximal rectangle problem in literature. So I googled a bit about known algorithms for this kind of problem and came across this site here http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529 which is describing a very fast algorithm to solve this kind of problem. To summarize the points of this website, the algorithm is exploiting the structure. This can be done by using a stack in order to remember the structure profile which allows us to recalculate the width in case a narrow rectangle gets reused when an wider one gets closed.






share|improve this answer























  • This works perfectly, but too slow for bigger matrix.
    – IHav
    Nov 11 at 20:53






  • 1




    I will check whether I can speed it up ...
    – quant
    Nov 11 at 20:55






  • 1




    We're talking about matrix of dimensions 5000x5000... and optimal time of solution would be less than 60 seconds. Though, I know.
    – IHav
    Nov 11 at 21:05










  • @IvanHlivan From my understanding of the problem this should be possible. I know of a very fast algorithm that is doing something similar in 1d not in 2d. So I only need to twist my head how I can do the same in 2d ...
    – quant
    Nov 11 at 21:08










  • @IvanHlivan Can you provide one 5000x5000 matrix, so that I can check the time on my computer?
    – quant
    Nov 11 at 21:11
















3














A brute force solution. Will work, but may be considered too slow for a bigger matrix:



mOrig = [[1,  -9, -2,   8,  6,  1],
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]

# reduce the problem
# now we have a matrix that contains only 0 and 1
# at the place where there was a negative number
# there is now a 1 and at the places where a positive
# number had been there is now a 0. 0s are considered
# to be negative numbers, if you want to change this,
# change the x < 0 to x <= 0.
m = [[1 if x < 0 else 0 for x in z] for z in mOrig]

# now we have the problem to find the biggest submatrix
# consisting only 1s.

# first a function that checks if a submatrix only contains 1s
def containsOnly1s(m, x1, y1, x2, y2):
for i in range(x1, x2):
for j in range(y1, y2):
if m[i][j] == 0:
return False
return True

def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)

best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
for x2 in range(x1, len(m)):
for y2 in range(y1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)

for x in range(best[0], best[2]):
print("t".join([str(mOrig[x][y]) for y in range(best[1], best[3])]))


Will output



-11 -7
-1 -9
-3 -5


In case something else is meant with "biggest submatrix", the only function that needs to get changed is the following:



def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)


which is calculating the size of a submatrix.



Edit 1 ... first speedup



best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
if m[x1][y1] == 1: # The starting point must contain a 1
for x2 in range(x1 + 1, len(m)): # actually can start with x1 + 1 here
for y2 in range(y1 + 1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)
else:
# There is at least one 0 in the matrix, so every greater
# matrix will also contain this 0
break


Edit 2



Ok, after converting the matrix into a matrix of 0 and 1 (as I do via the line m = [[1 if x < 0 else 0 for x in z] for z in mOrig] the problem is the same as what is called the maximal rectangle problem in literature. So I googled a bit about known algorithms for this kind of problem and came across this site here http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529 which is describing a very fast algorithm to solve this kind of problem. To summarize the points of this website, the algorithm is exploiting the structure. This can be done by using a stack in order to remember the structure profile which allows us to recalculate the width in case a narrow rectangle gets reused when an wider one gets closed.






share|improve this answer























  • This works perfectly, but too slow for bigger matrix.
    – IHav
    Nov 11 at 20:53






  • 1




    I will check whether I can speed it up ...
    – quant
    Nov 11 at 20:55






  • 1




    We're talking about matrix of dimensions 5000x5000... and optimal time of solution would be less than 60 seconds. Though, I know.
    – IHav
    Nov 11 at 21:05










  • @IvanHlivan From my understanding of the problem this should be possible. I know of a very fast algorithm that is doing something similar in 1d not in 2d. So I only need to twist my head how I can do the same in 2d ...
    – quant
    Nov 11 at 21:08










  • @IvanHlivan Can you provide one 5000x5000 matrix, so that I can check the time on my computer?
    – quant
    Nov 11 at 21:11














3












3








3






A brute force solution. Will work, but may be considered too slow for a bigger matrix:



mOrig = [[1,  -9, -2,   8,  6,  1],
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]

# reduce the problem
# now we have a matrix that contains only 0 and 1
# at the place where there was a negative number
# there is now a 1 and at the places where a positive
# number had been there is now a 0. 0s are considered
# to be negative numbers, if you want to change this,
# change the x < 0 to x <= 0.
m = [[1 if x < 0 else 0 for x in z] for z in mOrig]

# now we have the problem to find the biggest submatrix
# consisting only 1s.

# first a function that checks if a submatrix only contains 1s
def containsOnly1s(m, x1, y1, x2, y2):
for i in range(x1, x2):
for j in range(y1, y2):
if m[i][j] == 0:
return False
return True

def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)

best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
for x2 in range(x1, len(m)):
for y2 in range(y1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)

for x in range(best[0], best[2]):
print("t".join([str(mOrig[x][y]) for y in range(best[1], best[3])]))


Will output



-11 -7
-1 -9
-3 -5


In case something else is meant with "biggest submatrix", the only function that needs to get changed is the following:



def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)


which is calculating the size of a submatrix.



Edit 1 ... first speedup



best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
if m[x1][y1] == 1: # The starting point must contain a 1
for x2 in range(x1 + 1, len(m)): # actually can start with x1 + 1 here
for y2 in range(y1 + 1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)
else:
# There is at least one 0 in the matrix, so every greater
# matrix will also contain this 0
break


Edit 2



Ok, after converting the matrix into a matrix of 0 and 1 (as I do via the line m = [[1 if x < 0 else 0 for x in z] for z in mOrig] the problem is the same as what is called the maximal rectangle problem in literature. So I googled a bit about known algorithms for this kind of problem and came across this site here http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529 which is describing a very fast algorithm to solve this kind of problem. To summarize the points of this website, the algorithm is exploiting the structure. This can be done by using a stack in order to remember the structure profile which allows us to recalculate the width in case a narrow rectangle gets reused when an wider one gets closed.






share|improve this answer














A brute force solution. Will work, but may be considered too slow for a bigger matrix:



mOrig = [[1,  -9, -2,   8,  6,  1],
[8, -1,-11, -7, 6, 4],
[10, 12, -1, -9, -12, 14],
[8, 10, -3, -5, 17, 8],
[6, 4, 10, -13, -16, 19]]

# reduce the problem
# now we have a matrix that contains only 0 and 1
# at the place where there was a negative number
# there is now a 1 and at the places where a positive
# number had been there is now a 0. 0s are considered
# to be negative numbers, if you want to change this,
# change the x < 0 to x <= 0.
m = [[1 if x < 0 else 0 for x in z] for z in mOrig]

# now we have the problem to find the biggest submatrix
# consisting only 1s.

# first a function that checks if a submatrix only contains 1s
def containsOnly1s(m, x1, y1, x2, y2):
for i in range(x1, x2):
for j in range(y1, y2):
if m[i][j] == 0:
return False
return True

def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)

best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
for x2 in range(x1, len(m)):
for y2 in range(y1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)

for x in range(best[0], best[2]):
print("t".join([str(mOrig[x][y]) for y in range(best[1], best[3])]))


Will output



-11 -7
-1 -9
-3 -5


In case something else is meant with "biggest submatrix", the only function that needs to get changed is the following:



def calculateSize(x1, y1, x2, y2):
return (x2 - x1) * (y2 - y1)


which is calculating the size of a submatrix.



Edit 1 ... first speedup



best = (-1, -1, -1, -1, -1)
for x1 in range(len(m)):
for y1 in range(len(m[0])):
if m[x1][y1] == 1: # The starting point must contain a 1
for x2 in range(x1 + 1, len(m)): # actually can start with x1 + 1 here
for y2 in range(y1 + 1, len(m[0])):
if containsOnly1s(m, x1, y1, x2, y2):
sizeOfSolution = calculateSize(x1, y1, x2, y2)
if best[4] < sizeOfSolution:
best = (x1, y1, x2, y2, sizeOfSolution)
else:
# There is at least one 0 in the matrix, so every greater
# matrix will also contain this 0
break


Edit 2



Ok, after converting the matrix into a matrix of 0 and 1 (as I do via the line m = [[1 if x < 0 else 0 for x in z] for z in mOrig] the problem is the same as what is called the maximal rectangle problem in literature. So I googled a bit about known algorithms for this kind of problem and came across this site here http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529 which is describing a very fast algorithm to solve this kind of problem. To summarize the points of this website, the algorithm is exploiting the structure. This can be done by using a stack in order to remember the structure profile which allows us to recalculate the width in case a narrow rectangle gets reused when an wider one gets closed.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 21:35

























answered Nov 11 at 20:35









quant

1,58411526




1,58411526












  • This works perfectly, but too slow for bigger matrix.
    – IHav
    Nov 11 at 20:53






  • 1




    I will check whether I can speed it up ...
    – quant
    Nov 11 at 20:55






  • 1




    We're talking about matrix of dimensions 5000x5000... and optimal time of solution would be less than 60 seconds. Though, I know.
    – IHav
    Nov 11 at 21:05










  • @IvanHlivan From my understanding of the problem this should be possible. I know of a very fast algorithm that is doing something similar in 1d not in 2d. So I only need to twist my head how I can do the same in 2d ...
    – quant
    Nov 11 at 21:08










  • @IvanHlivan Can you provide one 5000x5000 matrix, so that I can check the time on my computer?
    – quant
    Nov 11 at 21:11


















  • This works perfectly, but too slow for bigger matrix.
    – IHav
    Nov 11 at 20:53






  • 1




    I will check whether I can speed it up ...
    – quant
    Nov 11 at 20:55






  • 1




    We're talking about matrix of dimensions 5000x5000... and optimal time of solution would be less than 60 seconds. Though, I know.
    – IHav
    Nov 11 at 21:05










  • @IvanHlivan From my understanding of the problem this should be possible. I know of a very fast algorithm that is doing something similar in 1d not in 2d. So I only need to twist my head how I can do the same in 2d ...
    – quant
    Nov 11 at 21:08










  • @IvanHlivan Can you provide one 5000x5000 matrix, so that I can check the time on my computer?
    – quant
    Nov 11 at 21:11
















This works perfectly, but too slow for bigger matrix.
– IHav
Nov 11 at 20:53




This works perfectly, but too slow for bigger matrix.
– IHav
Nov 11 at 20:53




1




1




I will check whether I can speed it up ...
– quant
Nov 11 at 20:55




I will check whether I can speed it up ...
– quant
Nov 11 at 20:55




1




1




We're talking about matrix of dimensions 5000x5000... and optimal time of solution would be less than 60 seconds. Though, I know.
– IHav
Nov 11 at 21:05




We're talking about matrix of dimensions 5000x5000... and optimal time of solution would be less than 60 seconds. Though, I know.
– IHav
Nov 11 at 21:05












@IvanHlivan From my understanding of the problem this should be possible. I know of a very fast algorithm that is doing something similar in 1d not in 2d. So I only need to twist my head how I can do the same in 2d ...
– quant
Nov 11 at 21:08




@IvanHlivan From my understanding of the problem this should be possible. I know of a very fast algorithm that is doing something similar in 1d not in 2d. So I only need to twist my head how I can do the same in 2d ...
– quant
Nov 11 at 21:08












@IvanHlivan Can you provide one 5000x5000 matrix, so that I can check the time on my computer?
– quant
Nov 11 at 21:11




@IvanHlivan Can you provide one 5000x5000 matrix, so that I can check the time on my computer?
– quant
Nov 11 at 21:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252626%2ffinding-biggest-negative-submatrix-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini