Find the cell in n*m matrix(containing 0s and 1s) with maximum neighbours(in all 8 directions) 1 - algorithm

Recently I was asked this question in an interview:
Given a n*m matrix with 0 and 1 only, find the cell which satisfies
the following condition:
The cell has one and max of its neighbors in all possible 8 directions
have same number as it has i.e. 1.
All I could think of is brute force which would be O(n*m).Any better ideas?
Thanks.
Eg:
0 1 1
0 1 0
1 1 1
Here, the required cell is (1,1).

Related

Is there an algorithm that divide a square cell grid map into 3 or 4 equal spaces?

So for example I have divide my map into something like this:
click on link
the matrix representative would be
0 1 0 1 0
1 1 1 1 0
0 1 1 1 1
0 1 0 0 0
one of the way I could divide it into even-ish would be:
click to see
where total square is 11 and since 11/3 gives us a decimal, I need to have 2 space with 4 square and one space with 3 squares.
but I don't know an algorithm that will be able to divide a small map like that.
there is probably a code that will be able to solve that particular map, but what if it is like :
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 1
Each value is a square in the map and 1 is the square that should be considered. 0 is an empty/null space that is not part of the map and should not be taken into consideration when dividing the map.
So far I try a for loop adding all value and divide by 3 to determine how many squares is needed for each space. Also, if I get a decimal, then one space can have one more square than the other. So in this problem there is 36 squares so if I try to divide it into 3 space, then each space would have 12 squares.
So I am looking to see if there is an algorithm that will be able to solve all types of map.
This is actually NP-hard for k>=2, where you want k=3 or k=4:
theorem 2.2 in On the complexity of partitioning graphs into connected subgraphs - M.E. DYER, A.M. FRIEZE
You can get a decent answer by greedily removing nodes from your graph, and backtracking if you can't merge the remaining nodes.
It would help if you gave a more rigorous definition of 'even-ish' - for example, consider a map with 13 nodes - Would you rather have divisions of size (4,4,5), (3,3,3,4), (4,4,4,1), (5,5,3), or (4,4,3,2)?

Using Greedy approach, given an m × n boolean matrix B, find its largest square submatrix whose elements are all zeros

Anybody know using greedy approach please help me out this problem.I have already done this by dynamic approach. But my main concern is by using Greedy method. And also we have to find max square sub-matrix of 0's.
https://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/
Here is the example using dynamic approach and finding elements having 1's.
NOTE:using greedy
Greedy approach is where you make a locally optimal choice, believing that it will lead to the optimal solution. So basically we start by searching for a 0 in one of the four margins of the mXn matrix i.e. in the 1st row, last row, first column or last column. Once a 0 is found, we start by searching for the largest square possible.
Example-
0 0 1 1 0 0
1 0 0 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
1 0 0 0 0 0
In the above example, the first element is 0, so check for the presence of 5X5 matrix, it fails as the third element is 1. There are a few cases now-
1] If you leave the first row from further evaluation, you can still aim for a matrix of size 4X4.
2] If you leave the third column and consider only the first two columns, aim for 2X2 matrix.
3] If you leave the third column and consider only the last three columns, aim for 3X3 matrix.
4] We wouldn't consider the option of eliminating both the row and the column, as it wouldn't be any better than the previous three options.
Considering greedy, we would choose the 1st case and repeat the process.

How to optimize search of rows x columns combination in a matrix?

Given a matrix of 1's and 0's, I want to find a combination of rows and columns with least or none 0's, maximizing the n_of_rows * n_of_columns picked.
For example, rows (0,1,2) and columns (0,1,3) have only one zero in col #0 row #1, and the rest 8 values are 1's.
1 1 0 1 0
0 1 1 1 0
1 1 0 1 1
0 0 1 0 0
Pracical task is to search over 1000's to 1000000's of rows and columns, finding the maximal biclique in a bipartite graph – rows and cols can be viewed as verticles, and values as connections.
The problem in NP-complete, as far as I learned.
Please advice an approach / algorithm that would speed up the task and reduce requirements to CPU and memory.
Not sure you could minimise thism
However, easy way to work this out would be...
Multiple your matrix by a 1 column and n rows full of 1's. This will give you number of ones in each row. Next do a 1 row by n columns multiplcation (at frot of) your matrix full of 1's. This will give you totals of 1's for each column, From there it's a pretty easy compairson........
ie original matrix...
1 0 1
0 1 1
0 0 0
do
1 0 1 x 1 = 2 (row totals)
o 1 1 1 2
0 0 0 1 0
do
1 1 1 x 1 0 1 = 1 (Column totals)
0 1 1 2
0 0 0 0
nb max sum is 2 (which you would keep track of as you work it out.
Actually given the following assumptions:
1. You don't care how many 0's are in each row or column
2. You don't need to keep track of their order....
Then you only really need to store values to count the total in each row/column as you read the values in and don't actually store the matrix itself.
If you are given the number of rows and columns prior to reading in the matrix you can do the following heuristics to reduce computational time...
Keep track of the current max. If the current row cannot reach this potential max stop counting for the row (but continue in the columns). Vice versa is true for the columns
But you still have a worst case scenario in which all rows and columns have sme number of 1's and 0's.... :)

Find minimum number of rectangles of length 2 in a grid of binary values

Given a grid, I need to "cover" the true values with rectangles. The rectangles can only be horizontal or vertical and they can cover two cells at max.
For example, in this case:
1 1 1 0 0
1 0 0 0 1
0 0 0 1 0
the minimum number of ractangles is 4: 1 from [0][0] to [1][0], 2 from [0][1] to [0][2], 3 with only [1][4] (since there aren't any adjacent 1s in the up, down, left and right directions) and the last one with only [2][3] in it.
I suppose, the problem comes when the grid has many consecutive 1s that split in many directions. For example
1 1 1 1 1
1 0 0 0 0
1 0 0 1 0
1 1 1 1 0
or
1 1 1
1 0 1
1 1 1
I cannot think of a pretty efficient algorithm to solve this problem, a greedy approach seems to be ineffective.
Any help would be really much appreciated, thanks.
EDIT:
The greedy algorithm I've tried: scan the matrix 1 row at a time, if there's a 1 in the current position, then check if there's another 1 in the next row position [rowIndex][columnIndex + 1] and "cover" both with one rectangle. If not, check the cell below the current position [rowIndex + 1][columnIndex] and do as before. If there aren't any 1s in these positions, cover only the current cell. As you can see, this algorithm doesn't work in the first case. Perhaps the algorithm must know all the consecutive 1s to compute the minimum number of rectangles. Because of this, I'm wondering if some graph stuff comes in, but I can't still think of an optimal solution.
I would simply go through the matrix and as soon I see a 1 I try to create a new rectangle with an adjacent 1 which can be on the same line or row and has no other adjacent 1 otherwise the rectangle will cover only the current cell and then you scan the matrix until you find a new uncovered 1 or you have scanned the enteir matrix, in this way you can cover all the 1 with the minumum number of rectangle (if I'm not wrong).
for i=0 to N
for j=0 to M
if (1==matrix[i][j] AND notCovered(i,j)){
k,l=adjacent(i,j)//return an adjacent 1 that cannot be covered, if all the adjacent 1 can be covered return the one on the same row
if (k AND l == VOID )
cover (i,j)
else
cover (i,j)(k,l)
}
Of course you can do some improvement on this algorithm, I hope this will help you

0-1 Knapsack revisited

Well, this is an old 0-1 Knapsack problem but after finding the total maximum price I can get I need to find the the items I can carry. But for the following test case ( total 3 items)
10 (max weight that I can carry)
5 3 (weight and value for each item)
5 2
6 5
Here maximum price is 5. But for weight it can be 6 or 10(5+5) . Both will give the same price but obviously the feasible would be to take 6 kg item than 10 kg. I want hint how can I calculate this from the dp matrix. I got the following matrix for this test case.
0 0 0 0 3 3 3 3 3 3
0 0 0 0 3 3 3 3 3 5
0 0 0 0 3 5 5 5 5 5
Using this algo it finds weight as 10 but the optimal is 6 kg.
i=n, k=W(max weight)// n= total items
while i,k > 0
if dp[i,k] ≠ dp[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-w(weight of ith item)
else
i = i−1
Simple solution is to iteratively run the knapsack algorithm on varying bag sizes, and chose the smallest bag that still gives you the same value the original bag gave you.
This can be done using binary search on the weigths [0,W] - so you will run the knapsack algorithm total of O(logW) times, giving you total of O(nW*log(W)) solution that finds maximal value and minimal possible bag size.
Idea of how to imply the binary search:
Let the original bag be of size W, run knapsack(W,items), and get the value. Now check if knapsack(W/2,items) still returns value. If it does - search in range (0,W/2]. If it doesn't, search in range (W/2,W], until you find the smallest bag size that returns value.

Resources