Finding the largest rectangular submatrix
$begingroup$
I have a sparse non-symmetric binary matrix with a block structure. The dimensions of the matrix are thousands rows and less than one hundred columns.
How do I identify the largest non-contiguous rectangular sub-matrix consisting only of 1-entries?
Edit
The pattern is either very sparse, or there are very few large submatrices. However, the blocks are likely to overlap.
In the simplified example below, it would be elements {2, 4} towards {6, 6}.
$qquad mat=left( begin{array}{ccc}
1&1&1&1&0&0\
1&1&1&1&1&1\
0&0&0&1&1&1\
1&1&0&1&1&1\
1&1&1&1&1&1\
1&1&1&1&1&1\
end{array} right)$
matrix graphs-and-networks regions
$endgroup$
|
show 8 more comments
$begingroup$
I have a sparse non-symmetric binary matrix with a block structure. The dimensions of the matrix are thousands rows and less than one hundred columns.
How do I identify the largest non-contiguous rectangular sub-matrix consisting only of 1-entries?
Edit
The pattern is either very sparse, or there are very few large submatrices. However, the blocks are likely to overlap.
In the simplified example below, it would be elements {2, 4} towards {6, 6}.
$qquad mat=left( begin{array}{ccc}
1&1&1&1&0&0\
1&1&1&1&1&1\
0&0&0&1&1&1\
1&1&0&1&1&1\
1&1&1&1&1&1\
1&1&1&1&1&1\
end{array} right)$
matrix graphs-and-networks regions
$endgroup$
$begingroup$
How is this related to graphs and networks? Might there be an additional structure behind the matrix? For example, ifmatis an adjacency matrix of a graph, you might be looking for a maximal complete subgraph or a clique. Szabolcs' package"IGraphM`"has tools for that...
$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
You still want to solve the problem for general binary matrices? (It is not unlikely that his discrete optimization problem is a very hard...)
$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
If you want non-contiguous too, then this is indeed the clique problem, which is NP-complete, and there's not going to be a simpler solution. UseFindClique, then filter for blocks that also have 1s on the diagonal.
$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
BeforeFindClique, remove every row/column that has a 0 on the diagonal.
$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
I added the size (thousands long, less than a hundred wide and that I am looking for non-contiguous solutions.
$endgroup$
– Sander
6 hours ago
|
show 8 more comments
$begingroup$
I have a sparse non-symmetric binary matrix with a block structure. The dimensions of the matrix are thousands rows and less than one hundred columns.
How do I identify the largest non-contiguous rectangular sub-matrix consisting only of 1-entries?
Edit
The pattern is either very sparse, or there are very few large submatrices. However, the blocks are likely to overlap.
In the simplified example below, it would be elements {2, 4} towards {6, 6}.
$qquad mat=left( begin{array}{ccc}
1&1&1&1&0&0\
1&1&1&1&1&1\
0&0&0&1&1&1\
1&1&0&1&1&1\
1&1&1&1&1&1\
1&1&1&1&1&1\
end{array} right)$
matrix graphs-and-networks regions
$endgroup$
I have a sparse non-symmetric binary matrix with a block structure. The dimensions of the matrix are thousands rows and less than one hundred columns.
How do I identify the largest non-contiguous rectangular sub-matrix consisting only of 1-entries?
Edit
The pattern is either very sparse, or there are very few large submatrices. However, the blocks are likely to overlap.
In the simplified example below, it would be elements {2, 4} towards {6, 6}.
$qquad mat=left( begin{array}{ccc}
1&1&1&1&0&0\
1&1&1&1&1&1\
0&0&0&1&1&1\
1&1&0&1&1&1\
1&1&1&1&1&1\
1&1&1&1&1&1\
end{array} right)$
matrix graphs-and-networks regions
matrix graphs-and-networks regions
edited 3 mins ago
m_goldberg
85k872196
85k872196
asked 6 hours ago
SanderSander
1,114512
1,114512
$begingroup$
How is this related to graphs and networks? Might there be an additional structure behind the matrix? For example, ifmatis an adjacency matrix of a graph, you might be looking for a maximal complete subgraph or a clique. Szabolcs' package"IGraphM`"has tools for that...
$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
You still want to solve the problem for general binary matrices? (It is not unlikely that his discrete optimization problem is a very hard...)
$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
If you want non-contiguous too, then this is indeed the clique problem, which is NP-complete, and there's not going to be a simpler solution. UseFindClique, then filter for blocks that also have 1s on the diagonal.
$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
BeforeFindClique, remove every row/column that has a 0 on the diagonal.
$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
I added the size (thousands long, less than a hundred wide and that I am looking for non-contiguous solutions.
$endgroup$
– Sander
6 hours ago
|
show 8 more comments
$begingroup$
How is this related to graphs and networks? Might there be an additional structure behind the matrix? For example, ifmatis an adjacency matrix of a graph, you might be looking for a maximal complete subgraph or a clique. Szabolcs' package"IGraphM`"has tools for that...
$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
You still want to solve the problem for general binary matrices? (It is not unlikely that his discrete optimization problem is a very hard...)
$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
If you want non-contiguous too, then this is indeed the clique problem, which is NP-complete, and there's not going to be a simpler solution. UseFindClique, then filter for blocks that also have 1s on the diagonal.
$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
BeforeFindClique, remove every row/column that has a 0 on the diagonal.
$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
I added the size (thousands long, less than a hundred wide and that I am looking for non-contiguous solutions.
$endgroup$
– Sander
6 hours ago
$begingroup$
How is this related to graphs and networks? Might there be an additional structure behind the matrix? For example, if
mat is an adjacency matrix of a graph, you might be looking for a maximal complete subgraph or a clique. Szabolcs' package "IGraphM`" has tools for that...$endgroup$
– Henrik Schumacher
6 hours ago
$begingroup$
How is this related to graphs and networks? Might there be an additional structure behind the matrix? For example, if
mat is an adjacency matrix of a graph, you might be looking for a maximal complete subgraph or a clique. Szabolcs' package "IGraphM`" has tools for that...$endgroup$
– Henrik Schumacher
6 hours ago
1
1
$begingroup$
You still want to solve the problem for general binary matrices? (It is not unlikely that his discrete optimization problem is a very hard...)
$endgroup$
– Henrik Schumacher
6 hours ago
$begingroup$
You still want to solve the problem for general binary matrices? (It is not unlikely that his discrete optimization problem is a very hard...)
$endgroup$
– Henrik Schumacher
6 hours ago
1
1
$begingroup$
If you want non-contiguous too, then this is indeed the clique problem, which is NP-complete, and there's not going to be a simpler solution. Use
FindClique, then filter for blocks that also have 1s on the diagonal.$endgroup$
– Szabolcs
6 hours ago
$begingroup$
If you want non-contiguous too, then this is indeed the clique problem, which is NP-complete, and there's not going to be a simpler solution. Use
FindClique, then filter for blocks that also have 1s on the diagonal.$endgroup$
– Szabolcs
6 hours ago
1
1
$begingroup$
Before
FindClique, remove every row/column that has a 0 on the diagonal.$endgroup$
– Szabolcs
6 hours ago
$begingroup$
Before
FindClique, remove every row/column that has a 0 on the diagonal.$endgroup$
– Szabolcs
6 hours ago
1
1
$begingroup$
I added the size (thousands long, less than a hundred wide and that I am looking for non-contiguous solutions.
$endgroup$
– Sander
6 hours ago
$begingroup$
I added the size (thousands long, less than a hundred wide and that I am looking for non-contiguous solutions.
$endgroup$
– Sander
6 hours ago
|
show 8 more comments
3 Answers
3
active
oldest
votes
$begingroup$
A brute force approach:
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
pairs = Transpose /@ MaximalBy[DeleteDuplicates[CoordinateBounds /@
Subsets[SparseArray[mat]["NonzeroPositions"], {2}]],
Min[#] Total[#, 2] &@mat[[## & @@ Span @@@ #]] &]
{{{2, 4}, {6, 6}}}
$endgroup$
$begingroup$
A brute brain force approach as well, thanks. Will try to understand this ... do you think theSubsetsexpansion will become a constraint?
$endgroup$
– Sander
3 hours ago
$begingroup$
@Sanders, although we are considering only 2-subsets of the non-zero positionsSubsetsis the brute part likely to cause pain for large and relatively dense input matrices.
$endgroup$
– kglr
3 hours ago
add a comment |
$begingroup$
Update: This answer is not correct but for referencing, because the Subsets does not give all the possible slices of the matrix.
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1,
1, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
mat//MatrixForm//TeXForm
$left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 0 & 0 \
1 & 1 & 1 & 1 & 1 & 1 \
0 & 0 & 0 & 1 & 1 & 1 \
1 & 1 & 0 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)$
lst = Subsets@mat;
result = DeleteDuplicates@(MatrixForm /@ Select[lst, DeleteDuplicates@Flatten@# == {1} &]) // Sort;
result // TeXForm
$
left{left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)right}
$
Is this ok? I'm not sure to apply Transpose to the last one of the result.
$endgroup$
$begingroup$
Thanks Jerry, there seem to be two issues: 1 the largest sub-matrix in the example is 5x3, your answer results in (after transpose) a 6x3 sub-matrix; Also, I am concerned the Subset will explode beyond memory capacity once we work with large matrices? 2. I would like to recover the coordinates of where the sub-matrix is residing.
$endgroup$
– Sander
5 hours ago
$begingroup$
@Sander Here is a similar question in other language, see geeksforgeeks.org/…
$endgroup$
– Jerry
4 hours ago
add a comment |
$begingroup$
This can be solved by dynamic programing. is called monochromatic square.
Let you Matrix be T of size n*m.
you have to make build a second matrix L that has the same size as the first one fill with 0.
i, j the row and column coordinate respectively.
Then you start by the lower right triangle (i=n, j=m).
and you iterate from there :
if T[i,j] == 0
then L[i,j] = 0
else:
if T[i,j] == T[i+1,j] == T[i, j+1] == T[i+1, j+1]
then L[i,j] = 1 + min(L[i+1, j], L[i, j+1], L[i+1, j+1])
else: L[i,j] = 1
at the end the maximal value of L will be the upper coordinate of the largest rectangle with only '1'
I am not a mathematician and it is my first answer so if I have forgot anything, feel free to ask for clarification or EDIT, also I can provides python code that does this.
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190162%2ffinding-the-largest-rectangular-submatrix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
A brute force approach:
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
pairs = Transpose /@ MaximalBy[DeleteDuplicates[CoordinateBounds /@
Subsets[SparseArray[mat]["NonzeroPositions"], {2}]],
Min[#] Total[#, 2] &@mat[[## & @@ Span @@@ #]] &]
{{{2, 4}, {6, 6}}}
$endgroup$
$begingroup$
A brute brain force approach as well, thanks. Will try to understand this ... do you think theSubsetsexpansion will become a constraint?
$endgroup$
– Sander
3 hours ago
$begingroup$
@Sanders, although we are considering only 2-subsets of the non-zero positionsSubsetsis the brute part likely to cause pain for large and relatively dense input matrices.
$endgroup$
– kglr
3 hours ago
add a comment |
$begingroup$
A brute force approach:
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
pairs = Transpose /@ MaximalBy[DeleteDuplicates[CoordinateBounds /@
Subsets[SparseArray[mat]["NonzeroPositions"], {2}]],
Min[#] Total[#, 2] &@mat[[## & @@ Span @@@ #]] &]
{{{2, 4}, {6, 6}}}
$endgroup$
$begingroup$
A brute brain force approach as well, thanks. Will try to understand this ... do you think theSubsetsexpansion will become a constraint?
$endgroup$
– Sander
3 hours ago
$begingroup$
@Sanders, although we are considering only 2-subsets of the non-zero positionsSubsetsis the brute part likely to cause pain for large and relatively dense input matrices.
$endgroup$
– kglr
3 hours ago
add a comment |
$begingroup$
A brute force approach:
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
pairs = Transpose /@ MaximalBy[DeleteDuplicates[CoordinateBounds /@
Subsets[SparseArray[mat]["NonzeroPositions"], {2}]],
Min[#] Total[#, 2] &@mat[[## & @@ Span @@@ #]] &]
{{{2, 4}, {6, 6}}}
$endgroup$
A brute force approach:
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
pairs = Transpose /@ MaximalBy[DeleteDuplicates[CoordinateBounds /@
Subsets[SparseArray[mat]["NonzeroPositions"], {2}]],
Min[#] Total[#, 2] &@mat[[## & @@ Span @@@ #]] &]
{{{2, 4}, {6, 6}}}
answered 5 hours ago
kglrkglr
180k9200413
180k9200413
$begingroup$
A brute brain force approach as well, thanks. Will try to understand this ... do you think theSubsetsexpansion will become a constraint?
$endgroup$
– Sander
3 hours ago
$begingroup$
@Sanders, although we are considering only 2-subsets of the non-zero positionsSubsetsis the brute part likely to cause pain for large and relatively dense input matrices.
$endgroup$
– kglr
3 hours ago
add a comment |
$begingroup$
A brute brain force approach as well, thanks. Will try to understand this ... do you think theSubsetsexpansion will become a constraint?
$endgroup$
– Sander
3 hours ago
$begingroup$
@Sanders, although we are considering only 2-subsets of the non-zero positionsSubsetsis the brute part likely to cause pain for large and relatively dense input matrices.
$endgroup$
– kglr
3 hours ago
$begingroup$
A brute brain force approach as well, thanks. Will try to understand this ... do you think the
Subsets expansion will become a constraint?$endgroup$
– Sander
3 hours ago
$begingroup$
A brute brain force approach as well, thanks. Will try to understand this ... do you think the
Subsets expansion will become a constraint?$endgroup$
– Sander
3 hours ago
$begingroup$
@Sanders, although we are considering only 2-subsets of the non-zero positions
Subsets is the brute part likely to cause pain for large and relatively dense input matrices.$endgroup$
– kglr
3 hours ago
$begingroup$
@Sanders, although we are considering only 2-subsets of the non-zero positions
Subsets is the brute part likely to cause pain for large and relatively dense input matrices.$endgroup$
– kglr
3 hours ago
add a comment |
$begingroup$
Update: This answer is not correct but for referencing, because the Subsets does not give all the possible slices of the matrix.
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1,
1, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
mat//MatrixForm//TeXForm
$left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 0 & 0 \
1 & 1 & 1 & 1 & 1 & 1 \
0 & 0 & 0 & 1 & 1 & 1 \
1 & 1 & 0 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)$
lst = Subsets@mat;
result = DeleteDuplicates@(MatrixForm /@ Select[lst, DeleteDuplicates@Flatten@# == {1} &]) // Sort;
result // TeXForm
$
left{left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)right}
$
Is this ok? I'm not sure to apply Transpose to the last one of the result.
$endgroup$
$begingroup$
Thanks Jerry, there seem to be two issues: 1 the largest sub-matrix in the example is 5x3, your answer results in (after transpose) a 6x3 sub-matrix; Also, I am concerned the Subset will explode beyond memory capacity once we work with large matrices? 2. I would like to recover the coordinates of where the sub-matrix is residing.
$endgroup$
– Sander
5 hours ago
$begingroup$
@Sander Here is a similar question in other language, see geeksforgeeks.org/…
$endgroup$
– Jerry
4 hours ago
add a comment |
$begingroup$
Update: This answer is not correct but for referencing, because the Subsets does not give all the possible slices of the matrix.
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1,
1, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
mat//MatrixForm//TeXForm
$left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 0 & 0 \
1 & 1 & 1 & 1 & 1 & 1 \
0 & 0 & 0 & 1 & 1 & 1 \
1 & 1 & 0 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)$
lst = Subsets@mat;
result = DeleteDuplicates@(MatrixForm /@ Select[lst, DeleteDuplicates@Flatten@# == {1} &]) // Sort;
result // TeXForm
$
left{left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)right}
$
Is this ok? I'm not sure to apply Transpose to the last one of the result.
$endgroup$
$begingroup$
Thanks Jerry, there seem to be two issues: 1 the largest sub-matrix in the example is 5x3, your answer results in (after transpose) a 6x3 sub-matrix; Also, I am concerned the Subset will explode beyond memory capacity once we work with large matrices? 2. I would like to recover the coordinates of where the sub-matrix is residing.
$endgroup$
– Sander
5 hours ago
$begingroup$
@Sander Here is a similar question in other language, see geeksforgeeks.org/…
$endgroup$
– Jerry
4 hours ago
add a comment |
$begingroup$
Update: This answer is not correct but for referencing, because the Subsets does not give all the possible slices of the matrix.
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1,
1, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
mat//MatrixForm//TeXForm
$left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 0 & 0 \
1 & 1 & 1 & 1 & 1 & 1 \
0 & 0 & 0 & 1 & 1 & 1 \
1 & 1 & 0 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)$
lst = Subsets@mat;
result = DeleteDuplicates@(MatrixForm /@ Select[lst, DeleteDuplicates@Flatten@# == {1} &]) // Sort;
result // TeXForm
$
left{left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)right}
$
Is this ok? I'm not sure to apply Transpose to the last one of the result.
$endgroup$
Update: This answer is not correct but for referencing, because the Subsets does not give all the possible slices of the matrix.
mat = {{1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {1,
1, 0, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}};
mat//MatrixForm//TeXForm
$left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 0 & 0 \
1 & 1 & 1 & 1 & 1 & 1 \
0 & 0 & 0 & 1 & 1 & 1 \
1 & 1 & 0 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)$
lst = Subsets@mat;
result = DeleteDuplicates@(MatrixForm /@ Select[lst, DeleteDuplicates@Flatten@# == {1} &]) // Sort;
result // TeXForm
$
left{left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right),left(
begin{array}{cccccc}
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
1 & 1 & 1 & 1 & 1 & 1 \
end{array}
right)right}
$
Is this ok? I'm not sure to apply Transpose to the last one of the result.
edited 5 hours ago
answered 5 hours ago
JerryJerry
1,252112
1,252112
$begingroup$
Thanks Jerry, there seem to be two issues: 1 the largest sub-matrix in the example is 5x3, your answer results in (after transpose) a 6x3 sub-matrix; Also, I am concerned the Subset will explode beyond memory capacity once we work with large matrices? 2. I would like to recover the coordinates of where the sub-matrix is residing.
$endgroup$
– Sander
5 hours ago
$begingroup$
@Sander Here is a similar question in other language, see geeksforgeeks.org/…
$endgroup$
– Jerry
4 hours ago
add a comment |
$begingroup$
Thanks Jerry, there seem to be two issues: 1 the largest sub-matrix in the example is 5x3, your answer results in (after transpose) a 6x3 sub-matrix; Also, I am concerned the Subset will explode beyond memory capacity once we work with large matrices? 2. I would like to recover the coordinates of where the sub-matrix is residing.
$endgroup$
– Sander
5 hours ago
$begingroup$
@Sander Here is a similar question in other language, see geeksforgeeks.org/…
$endgroup$
– Jerry
4 hours ago
$begingroup$
Thanks Jerry, there seem to be two issues: 1 the largest sub-matrix in the example is 5x3, your answer results in (after transpose) a 6x3 sub-matrix; Also, I am concerned the Subset will explode beyond memory capacity once we work with large matrices? 2. I would like to recover the coordinates of where the sub-matrix is residing.
$endgroup$
– Sander
5 hours ago
$begingroup$
Thanks Jerry, there seem to be two issues: 1 the largest sub-matrix in the example is 5x3, your answer results in (after transpose) a 6x3 sub-matrix; Also, I am concerned the Subset will explode beyond memory capacity once we work with large matrices? 2. I would like to recover the coordinates of where the sub-matrix is residing.
$endgroup$
– Sander
5 hours ago
$begingroup$
@Sander Here is a similar question in other language, see geeksforgeeks.org/…
$endgroup$
– Jerry
4 hours ago
$begingroup$
@Sander Here is a similar question in other language, see geeksforgeeks.org/…
$endgroup$
– Jerry
4 hours ago
add a comment |
$begingroup$
This can be solved by dynamic programing. is called monochromatic square.
Let you Matrix be T of size n*m.
you have to make build a second matrix L that has the same size as the first one fill with 0.
i, j the row and column coordinate respectively.
Then you start by the lower right triangle (i=n, j=m).
and you iterate from there :
if T[i,j] == 0
then L[i,j] = 0
else:
if T[i,j] == T[i+1,j] == T[i, j+1] == T[i+1, j+1]
then L[i,j] = 1 + min(L[i+1, j], L[i, j+1], L[i+1, j+1])
else: L[i,j] = 1
at the end the maximal value of L will be the upper coordinate of the largest rectangle with only '1'
I am not a mathematician and it is my first answer so if I have forgot anything, feel free to ask for clarification or EDIT, also I can provides python code that does this.
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
This can be solved by dynamic programing. is called monochromatic square.
Let you Matrix be T of size n*m.
you have to make build a second matrix L that has the same size as the first one fill with 0.
i, j the row and column coordinate respectively.
Then you start by the lower right triangle (i=n, j=m).
and you iterate from there :
if T[i,j] == 0
then L[i,j] = 0
else:
if T[i,j] == T[i+1,j] == T[i, j+1] == T[i+1, j+1]
then L[i,j] = 1 + min(L[i+1, j], L[i, j+1], L[i+1, j+1])
else: L[i,j] = 1
at the end the maximal value of L will be the upper coordinate of the largest rectangle with only '1'
I am not a mathematician and it is my first answer so if I have forgot anything, feel free to ask for clarification or EDIT, also I can provides python code that does this.
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
This can be solved by dynamic programing. is called monochromatic square.
Let you Matrix be T of size n*m.
you have to make build a second matrix L that has the same size as the first one fill with 0.
i, j the row and column coordinate respectively.
Then you start by the lower right triangle (i=n, j=m).
and you iterate from there :
if T[i,j] == 0
then L[i,j] = 0
else:
if T[i,j] == T[i+1,j] == T[i, j+1] == T[i+1, j+1]
then L[i,j] = 1 + min(L[i+1, j], L[i, j+1], L[i+1, j+1])
else: L[i,j] = 1
at the end the maximal value of L will be the upper coordinate of the largest rectangle with only '1'
I am not a mathematician and it is my first answer so if I have forgot anything, feel free to ask for clarification or EDIT, also I can provides python code that does this.
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
This can be solved by dynamic programing. is called monochromatic square.
Let you Matrix be T of size n*m.
you have to make build a second matrix L that has the same size as the first one fill with 0.
i, j the row and column coordinate respectively.
Then you start by the lower right triangle (i=n, j=m).
and you iterate from there :
if T[i,j] == 0
then L[i,j] = 0
else:
if T[i,j] == T[i+1,j] == T[i, j+1] == T[i+1, j+1]
then L[i,j] = 1 + min(L[i+1, j], L[i, j+1], L[i+1, j+1])
else: L[i,j] = 1
at the end the maximal value of L will be the upper coordinate of the largest rectangle with only '1'
I am not a mathematician and it is my first answer so if I have forgot anything, feel free to ask for clarification or EDIT, also I can provides python code that does this.
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 20 mins ago
RomainL.RomainL.
1011
1011
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
RomainL. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190162%2ffinding-the-largest-rectangular-submatrix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$begingroup$
How is this related to graphs and networks? Might there be an additional structure behind the matrix? For example, if
matis an adjacency matrix of a graph, you might be looking for a maximal complete subgraph or a clique. Szabolcs' package"IGraphM`"has tools for that...$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
You still want to solve the problem for general binary matrices? (It is not unlikely that his discrete optimization problem is a very hard...)
$endgroup$
– Henrik Schumacher
6 hours ago
1
$begingroup$
If you want non-contiguous too, then this is indeed the clique problem, which is NP-complete, and there's not going to be a simpler solution. Use
FindClique, then filter for blocks that also have 1s on the diagonal.$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
Before
FindClique, remove every row/column that has a 0 on the diagonal.$endgroup$
– Szabolcs
6 hours ago
1
$begingroup$
I added the size (thousands long, less than a hundred wide and that I am looking for non-contiguous solutions.
$endgroup$
– Sander
6 hours ago