From an interview: Removing rows and columns in an n×n matrix to maximize the sum of remaining values

Viewed 7319

Given an n×n matrix of real numbers. You are allowed to erase any number (from 0 to n) of rows and any number (from 0 to n) of columns, and after that the sum of the remaining entries is computed. Come up with an algorithm which finds out which rows and columns to erase in order to maximize that sum.

17 Answers

For slightly less than optimal solution, I think this is a PTIME, PSPACE complexity issue.

The GREEDY algorithm could run as follows:


Load the matrix into memory and compute row totals. After that run the main loop,

1) Delete the smallest row,

2) Subtract the newly omitted values from the old row totals

--> Break when there are no more negative rows.


Point two is a subtle detail: subtracted two rows/columns has time complexity n.

While re-summing all but two columns has n^2 time complexity!

Related