问题
I have an a*b matrix of numbers (from 0 to 9), for example
[[1,2,3],
[4,5,6],
[7,6,5]]
And I want to find the largest rectagle, where is count of each number even (in this example, the result will be)
[[5,6],
[6,5]] -- numbers 5 an 6 are here 2 times
Is here any way, how to find it in n*log(n) or better time where n is count of items in matrix (n = a*b)? (If that rectagle doesn't exist, the result will be an empty set)
thanks
回答1:
You can create a second matrix in O(N) time (where N is the number of cells), which can then be used to check whether a certain rectangle has an even number of every digit in O(1) time. The number of candidate rectangles is however related to N2, so the worst case will be O(N2). You could use dynamic programming and build up from smaller rectangles to larger ones, but because we're looking for the largest rectangle, that may be counter-productive for the average case. (Update: I suspect that the worst case, a matrix with no valid rectangles, is impossible for square input larger than 63x63; see below.)
We'll create a second matrix holding 10-bit integers, where each bit indicates whether there is an even or odd number of a certain digit in the rectangle from that position to the bottom-right corner. We'll start with the bottom-right corner, and work our way up to the top-left corner, using the adjacent integers we've already added to calculate the new ones:
2 8 6 5 ---------- ---------- ---------- ----------
3 2 1 4 ---------- ---------- ---------- ----------
1 3 2 5 ---------- ---------- ---------- ----------
7 2 0 9 ---------- ---------- ---------- 1000000000
The integer for the bottom-right corner has its 9th bit set, because the value at that position is 9. Then we calculate the two adjacent edge positions:
2 8 6 5 ---------- ---------- ---------- ----------
3 2 1 4 ---------- ---------- ---------- ----------
1 3 2 5 ---------- ---------- ---------- 1000100000
7 2 0 9 ---------- ---------- 1000000001 1000000000
In general, we can calculate the bit pattern by setting the bit that corresponds to the position's value, and xor-ing this pattern with the three adjacent patterns to the right, bottom, and bottom-right of the current position:
position (2,2): 0000000100 (value = 2)
position (3,2): 1000100000
position (2,3): 1000000001
position (3,3): 1000000000
XOR: 1000100101
2 8 6 5 ---------- ---------- ---------- ----------
3 2 1 4 ---------- ---------- ---------- ----------
1 3 2 5 ---------- ---------- 1000100101 1000100000
7 2 0 9 ---------- ---------- 1000000001 1000000000
For positions along the right and bottom edges, we only xor with one adjacent pattern, under or to the right of the current position. Eventually we get this matrix:
2 8 6 5 1111010001 1101011111 1001010111 1000010000
3 2 1 4 1010110101 1000111111 1000110111 1000110000
1 3 2 5 1010101011 1000101001 1000100101 1000100000
7 2 0 9 1010000101 1000000101 1000000001 1000000000
With this matrix, we can now check whether any rectangle has an even number of every digit, by xor-ing its top-left pattern with the pattern under it, to the right of it, and to the bottom-right of it:
- - - - ---------- ---------- ---------- ----------
3 2 1 - 1010110101 ---------- ---------- 1000110000
1 3 2 - ---------- ---------- ---------- ----------
- - - - 1010000101 ---------- ---------- 1000000000
position (0,1): 1010110101
position (3,1): 1000110000
position (0,3): 1010000101
position (3,3): 1000000000
XOR: 0000000000 <- 0 means even number of all digits
For rectangles up to the right or bottom edge, only two patterns are xor-ed. For rectangles up to the bottom-right corner, the pattern can simply be read from the matrix.
- - - - ---------- ---------- ---------- ----------
3 2 1 4 1010110101 ---------- ---------- ----------
1 3 2 5 ---------- ---------- ---------- ----------
- - - - 1010000101 ---------- ---------- ----------
position (0,1): 1010110101
position (0,3): 1010000101
XOR: 0000110000 <- non-zero means odd number of some digits
We'd then proceed by checking the whole rectangle, then the rectangles one row or column smaller, and so on, from large to small. Obviously, rectangles with an odd number of rows and columns can be skipped.
The worst case complexity of this is N2 (if the last rectangle we check is the only valid solution). The best case is N (if the whole rectangle is a valid solution) and the average depends on the specifics of the input.
If you want to try dynamic programming, build a table with the bit patterns for every 2x1 and 1x2 rectangle, then xor two of these to build up 4x1, 1x4 and 2x2 rectangles, and so on... This requires xor-ing 2 patterns per rectangle, whereas the non-dp solution requires xor-ing 4 patterns (or 2 or 0 for rectangles up to the right or bottom edge), so I don't expect major improvement. Also, if one of the largest rectangles is a valid solution, it would be found quickly in the non-dp solution, but only after calculating most of the rectangles in the dp solution.
I've done some tests with a simple Javascript implementation, and found that while the theoretical worst case (checking every rectangle with an even number of elements) indeed has a complexity of O(N2), the average case (when checking only rectangles larger than the current best solution) with random input levels off at around 1314 for rectangles sized 48x48 and larger. So the average case complexity is O(N) because building the second matrix becomes the dominant factor; large cases like 2048x2048 can be run in under a second.
size N worst average max
2x2 4 5 4 5
3x3 9 20 14 20
4x4 16 64 40 64
5x5 25 144 81 144
6x6 36 297 155 297
7x7 49 528 250 528
8x8 64 896 375 896
9x9 81 1,400 496 1,355
10x10 100 2,125 614 1,981
11x11 121 3,060 698 2,876
12x12 144 4,320 770 3,883
13x13 169 5,880 815 5,098
14x14 196 7,889 866 6,603
15x15 225 10,304 897 7,729
16x16 256 13,312 937 8,123
...
20x20 400 32,000 1,057 9,019
24x24 576 65,664 1,144 9,640
32x32 1,024 204,800 1,248 10,521
40x40 1,600 496,000 1,293 10,369
48x48 2,304 1,022,976 1,306 11,006
64x64 4,096 3,211,264 1,314 11,334
80x80 6,400 7,808,000 1,314 10,962
96x96 9,216 16,146,432 1,312 10,970
128x128 16,384 50,855,936 1,318 10,875
The column "worst" shows the number of rectangles with an even-sized area; in the worst case, all these rectangles would have to be checked. The column "average" shows the average number of rectangles that actually needed to be checked in a test with random data, when going from large to small and skipping rectangles that are not larger than the largest valid rectangle found so far. The column "max" shows the actual worst case that was encountered in the test; this also seems to level off above 48x48. (Algorithm tested 1,000,000 times for each size, using random values.)
The average number of rectangles that have to be checked is of course related to the fact that a large rectangle with random digits has a probability of around 1/2 of having an even number of a certain digit (but only an even number of digits can be present an even number of times) and thus 1/(C(10,0) + C(10,2) + C(10,4) + C(10,6) + C(10,8) + C(10,10)) or 1/512 of having an even number of all ten digits. The fact that I get an average of 1314 instead of 512 is probably because my algorithm doesn't strictly go from large to small rectangles, but tries 10x10 first, and then 9x10, 10x9, 8x10, 10x8 ... 1x10, 10x1, before switching to 8x9, 9x8, 6x9, 9x6 ... 2x9, 9x2, and then 8x8, 7x8, 8x7 ... to keep the code simple. If it were completely optimized, I'd expect the average to approach 512 for large rectangles.
When trying to find out whether the worst case, a matrix with no valid rectangles, was possible at large matrix sizes, I automatically arrived at squares based on this sequence:
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5...
which you get by repeating the sequence so far, and adding 1 to the last number (OEIS A007814). This results in these matrices which contain no rectangles with even numbers of each digit:
0,1
1,2
0,1,0,2
1,2,1,3
0,1,0,2
2,3,2,4
0,1,0,2,0,1,0,3
1,2,1,3,1,2,1,4
0,1,0,2,0,1,0,3
2,3,2,4,2,3,2,5
0,1,0,2,0,1,0,3
1,2,1,3,1,2,1,4
0,1,0,2,0,1,0,3
3,4,3,5,3,4,3,6
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
3,4,3,5,3,4,3,6,3,4,3,5,3,4,3,7
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4
4,5,4,6,4,5,4,7,4,5,4,6,4,5,4,8
At his point, if you repeat the 16x16 matrix to create a 32x32 matrix and add 1 to the right-most column and bottom row, the bottom-right cell would have the value 10; we can replace this with a 1 (but that's the only value that works):
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,6
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
2,3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,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,7
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,6
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
3,4,3,5,3,4,3,6,3,4,3,5,3,4,3,7,3,4,3,5,3,4,3,6,3,4,3,5,3,4,3,8
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,6
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,7
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,6
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5
5,6,5,7,5,6,5,8,5,6,5,7,5,6,5,9,5,6,5,7,5,6,5,8,5,6,5,7,5,6,5,1
Then, when going from 32x32 to 64x64, there are several cells in the right-most column and bottom row for which every value creates a rectangle with an even number of each digit. So the maximum-sized square which creates the worst case input may be this 63x63 matrix:
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,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2,1,3,1,2,1,4,1,2,1,3,1,2,1
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,7,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,6,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,6,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,2,1,3,1,2,1,4,1,2,1,3,1,2,1
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
If this is indeed the largest input without rectangles with even numbers of each digit (but I have no proof, and I've only considered square matrices), then that would put a maximum of 3,015,680 on the number of rectangles to be checked, and the worst case complexity would also become O(N) for large input (greater than 1736x1736). (See also this question on Mathematics.)
来源:https://stackoverflow.com/questions/46568388/searching-for-largest-rectangle-with-even-count-of-numbers-in-matrix