Search code examples
algorithmpseudocodemaxminimum

Multi-variate maximization over a table


I'm looking for a simple to implement algorithm that can find the row of a table having the maximum value on a certain column. Then, it should find all the rows that have a value close to the maximum on that particular column (can these 2 steps be combined?). Then, out of the selected rows, I need to find the one that has the minimum value on another column.

Bonus: in case there are several such entries, I need to find the row having the minimum value on another column.

Yes, I am aware that it's easy to do this with SQL(ite), but I don't want to waste time parsing the data from my text file into a database table...

I am interested in a simple idea on how to do this (pseudocode is fine) and, right now, I can only think of something rather complex along these lines:

  • iterate over all rows & find maximum
  • iterate over all rows and insert the ones that are "close" to the maximum in a list
  • find minimum value in the new list of rows

Solution

  • Actually you are doing the right thing. Unless your row values are already sorted, you can't avoid going through all values in step 1, so you will end up spending O(R) time in there, where R is the number of rows.

    For the second step, its cost is also O(R) so it doesn't degrade your algorithm's complexity.

    If we consider that the number of values "close to the maximum" is O(1) with respect to R, then the third step is O(C) where C is the number of columns. You can't do better than this if your values aren't sorted because you need to test all values in order to find the minimum.

    Your algorithm has the best complexity you will get.