public class Smawk
This class implement the SMAWK algorithm to compute column maxima on a totally monotone matrix as described.
This implementation derives from the paper of A.Aggarwal, M.Klawe, S.Moran, P.Shor, and R.Wilber, Geometric Applications of a Matrix Searching Algorithm, Algorithmica, 2, 195-208 (1987).
The matrix must be an object that implements the interface Matrix interface. It is also expected to be totally monotone, and the number of rows should be greater than or equals to the number of columns. If these conditions are not met, the the result is unpredictable and can lead to an ArrayIndexOutOfBoundsException.
is the main public method of this class. It computes the column maxima of a given matrix, i.e. the rows that contain the maximum value of each column in O(n) (linear) time, where n is the number of rows. This method does not return the maximum values itself, but just the indexes of their rows.net.maizegenetics.analysis.gbs.neobio.Smawk$computeColumnMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix,Array((kotlin.Int)))
Note that it is necessary to create an instance of this class to execute the computeColumnMaxima because it stores temporary data is that instance. To prevent problems with concurrent access, the computeColumnMaxima method is declared synchronized.
// create an instance of Smawk
Smawk smawk = new Smawk();
// create an array to store the result
int col_maxima = new int [some_matrix.numColumns()];
// now compute column maxima
smawk.computeColumnMaxima (some_matrix, col_maxima)
Note that the array of column maxima indexes (the computation result) must be created beforehand and its size must be equal to the number of columns of the matrix.
This implementation creates arrays of row and column indexes from the original array and simulates all operations (reducing, deletion of odd columns, etc.) by manipulating these arrays. The benefit is two-fold. First the matrix is not required to implement any of these this operations but only a simple method to retrieve a value at a given position. Moreover, it tends to be faster since it uses a manipulation of these small vectors and no row or column is actually deleted. The downside is, of course, the use of extra memory (in practice, however, this is negligible).
Note that this class does not contain a computeRowMaxima method, however, the computeColumnMaxima can easily be used to compute row maxima by using a transposed matrix interface, i.e. one that inverts the indexes of the valueAt method (returning [col,row] when [row,col] is requested) and swaps the number of rows by the number of columns, and vice-versa.
Another simpler method, , does the same job without using the SMAWK algorithm. It takes advantage of the monotone property of the matrix only (SMAWK explores the stronger constraint of total monotonicity), and therefore has a worst case time complexity of O(n * m), where n is the number of rows and m is the number of columns. However, this method tends to be faster for small matrices because it avoids recursions and row and column manipulations. There is also a net.maizegenetics.analysis.gbs.neobio.Smawk$naiveComputeColumnMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix,Array((kotlin.Int)))net.maizegenetics.analysis.gbs.neobio.Smawk$naiveComputeRowMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix,Array((kotlin.Int))) method to compute row maxima with the naive approach.
interface Matrix,
net.maizegenetics.analysis.gbs.neobio.Smawk$computeColumnMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix,Array((kotlin.Int))),
net.maizegenetics.analysis.gbs.neobio.Smawk$naiveComputeColumnMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix,Array((kotlin.Int))),
net.maizegenetics.analysis.gbs.neobio.Smawk$naiveComputeRowMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix,Array((kotlin.Int))),
interface Matrixprotected Matrix matrix
A pointer to the matrix that is being manipulated.
protected int numrows
The matrix's current number of rows. This reflects any deletion of rows already performed.
protected kotlin.Array[] row
An array of row indexes reflecting the current state of the matrix. When rows are deleted, the corresponding indexes are simply moved to the end of the vector.
protected kotlin.Array[] row_position
This array is used to store for each row of the original matrix, its index in the current state of the matrix, i.e. its index in the row array.
protected int numcols
The matrix's current number of columns. This reflects any deletion of columns already performed.
protected kotlin.Array[] col
An array of column indexes reflecting the current state of the matrix. When columns are deleted, the corresponding indexes are simply moved to the end of the vector.
public void computeColumnMaxima(Matrix matrix, kotlin.Array[] col_maxima)
Computes the column maxima of a given matrix. It first sets up arrays of row and column indexes to simulate a copy of the matrix (where all operations will be performed). It then calls the recursive protected computeColumnMaxima method.
The matrix is required to be an object that implements the Matrix interface. It is also expected to be totally monotone, and the number of rows should be greater than or equals to the number of columns. If it is not, the the result is unpredictable and can lead to an ArrayIndexOutOfBoundsException.
This method does not return the maximum values itself, but just the indexes of their rows. Note that the array of column maxima (the computation result) must be created beforehand and its size must be equal to the number of columns of the matrix.
To prevent problems with concurrent access, this method is declared synchronized.
matrix - the matrix that will have its column maxima computedcol_maxima - the array of column maxima (indexes of the rows containing maximum values of each column); this is the computation result#computeColumnMaxima(int[])protected void computeColumnMaxima(kotlin.Array[] col_maxima)
This method implements the SMAWK algorithm to compute the column maxima of a given matrix. It uses the arrays of row and column indexes to performs all operations on this 'fake' copy of the original matrix.
The first step is to reduce the matrix to a quadratic size (if necessary). It then delete all odd columns and recursively computes column maxima for this matrix. Finally, using the information computed for the odd columns, it searches for column maxima of the even columns. The column maxima are progressively stored in the col_maxima array (each recursive call will compute a set of column maxima).
col_maxima - the array of column maxima (the computation result)protected int valueAt(int r,
int c)
This is a helper method to simplify the call to the valueAt method of the matrix. It returns the value at row r, column c.
r - the row number of the value being retrievedc - the column number of the value being retrievedr, column cMatrix#valueAtprotected void deleteOddColumns()
This method simulates a deletion of odd rows by manipulating the col array of indexes. In fact, nothing is deleted, but the indexes are moved to the end of the array in a way that they can be easily restored by the restoreOddColumns method using a reverse approach.
#restoreOddColumnsprotected void restoreOddColumns(int original_numcols)
Restores the col array of indexes to the state it was before the deleteOddColumns method was called. It only needs to know how many columns there was originally. The indexes that were moved to the end of the array are restored to their original position.
original_numcols - the number of columns before the odd ones were deleted#deleteOddColumnsprotected void reduce()
This method is the key component of the SMAWK algorithm. It reduces an n x m matrix (n rows and m columns), where n >= m, to an n x n matrix by deleting m - n rows that are guaranteed to have no maximum value for any column. The result is an squared submatrix matrix that contains, for each column c, the row that has the maximum value of c in the original matrix. The rows are deleted with the deleteRowmethod.
It uses the total monotonicity property of the matrix to identify which rows can safely be deleted.
#deleteRowprotected void deleteRow(int reduced_rows,
int k)
This method simulates a deletion of a row in the matrix during the reduce operation. It just moves the index to the end of the array in a way that it can be restored afterwards by the restoreRows method (nothing is actually deleted). Deleted indexes are kept in ascending order.
reduced_rows - the current number of rows in the reducing matrixk - the index of the row to be deleted#restoreRowsprotected void restoreRows(int original_numrows)
Restores the row array of indexes to the state it was before the reduce method was called. It only needs to know how many rows there was originally. The indexes that were moved to the end of the array are restored to their original position.
original_numrows - the number of rows before the reduction was performed#deleteRow,
#reducepublic static void naiveComputeColumnMaxima(Matrix matrix, kotlin.Array[] col_maxima)
This is a simpler method for calculating column maxima. It does the same job as computeColumnMaxima, but without complexity of the SMAWK algorithm.
The matrix is required to be an object that implements the Matrix interface. It is also expected to be monotone. If it is not, the result is unpredictable but, unlike computeColumnMaxima, it cannot lead to an ArrayIndexOutOfBoundsException.
This method does not return the maximum values itself, but just the indexes of their rows. Note that the array of column maxima (the computation result) must be created beforehand and its size must be equal to the number of columns of the matrix.
It takes advantage of the monotone property of the matrix only (SMAWK explores the stronger constraint of total monotonicity), and therefore has a worst case time complexity of O(n * m), where n is the number of rows and m is the number of columns. However, this method tends to be faster for small matrices because it avoids recursions and row and column manipulations.
matrix - the matrix that will have its column maxima computedcol_maxima - the array of column maxima (indexes of the rows containing maximum values of each column); this is the computation result#naiveComputeRowMaximapublic static void naiveComputeRowMaxima(Matrix matrix, kotlin.Array[] row_maxima)
This is a simpler method for calculating row maxima. It does not use the SMAWK algorithm.
The matrix is required to be an object that implements the Matrix interface. It is also expected to be monotone. If it is not, the result is unpredictable but, unlike computeColumnMaxima, it cannot lead to an ArrayIndexOutOfBoundsException.
This method does not return the maximum values itself, but just the indexes of their columns. Note that the array of row maxima (the computation result) must be created beforehand and its size must be equal to the number of columns of the matrix.
It takes advantage of the monotone property of the matrix only (SMAWK explores the stronger constraint of total monotonicity), and therefore has a worst case time complexity of O(n * m), where n is the number of rows and m is the number of columns. However, this method tends to be faster for small matrices because it avoids recursions and row and column manipulations.
matrix - the matrix that will have its row maxima computedrow_maxima - the array of row maxima (indexes of the columns containing maximum values of each row); this is the computation result#naiveComputeColumnMaximaprotected void printMatrix()
Prints the current state of the matrix (reflecting deleted rows and columns) in the standard output. It can be used internally for debugging.
public static void printMatrix(Matrix matrix)
Prints the contents of an object implementing the matrix interface in the standard output. It can be used for debugging.
matrix - a matrix