I'm not sure how far you could go with this approach, but here we go:
I would try swapping rows and columns to move zeros in the upper left corner.
Thus, the resulting m: m matrix will be found in the upper left corner.
We need to evaluate whether it is interesting to change rows / columns.
To do this, we construct a cost function based on this weight matrix:
7 6 5 4 3 2 1 6 6 5 4 3 2 1 5 5 5 4 3 2 1 4 4 4 4 3 2 1 3 3 3 3 3 2 1 2 2 2 2 2 2 1 1 1 1 1 1 1 1
In other words, the weight of row i, column j (based on 0) is min (ni, nj).
Each 0 found is worth the corresponding weight, and we want to minimize the total cost.
An exchange is interesting if it reduces the overall cost.
To reduce the cost of evaluation, we could use some structure for a sparse matrix:
- the location of the positions of zeros in the row, that is, the set (columnIndex) for each rowIndex
- the location of the positions of zeros in the column, i.e. a set (rowIndex) for each columnIndex
Now we have a problem sorting rows and columns.
A suboptimal approach will be to solve the sub-sum separately:
- swap rows,
- swap
- iteration until costs evolve.
There is an advantage to replacing the two lines i and k if:
weigth.atRow(i).sumOfIndices(zerosPerRow.at(i)) + weigth.atRow(k).sumOfIndices(zerosPerRow.at(k)) > weigth.atRow(i).sumOfIndices(zerosPerRow.at(k)) + weigth.atRow(k).sumOfIndices(zerosPerRow.at(i))
Please note that this is not a complete relationship order, so not all sorting algorithms will be successful.
Perhaps there is interest in reducing an even greater combinatorial function with additional heuristics: swap rows with the most zeros at the bottom, a column with most zeros to the right.
Obviously, full-rank rows / columns do not need to be sorted after moving up / left.
So, it is possible that sorting a subset of rows / columns of the same rank is a reasonable (sub) optimal algorithm.