public class Smawk extends Object
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 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.
computeColumnMaxima
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.
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, naiveComputeColumnMaxima
,
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
naiveComputeRowMaxima method to compute row maxima
with the naive approach.
Matrix
Constructor and Description |
---|
Smawk() |
Modifier and Type | Method and Description |
---|---|
void |
computeColumnMaxima(Matrix matrix,
int[] col_maxima)
Computes the column maxima of a given matrix.
|
static void |
naiveComputeColumnMaxima(Matrix matrix,
int[] col_maxima)
This is a simpler method for calculating column maxima.
|
static void |
naiveComputeRowMaxima(Matrix matrix,
int[] row_maxima)
This is a simpler method for calculating row maxima.
|
static void |
printMatrix(Matrix matrix)
Prints the contents of an object implementing the matrix interface in the standard
output.
|
public void computeColumnMaxima(Matrix matrix, int[] col_maxima)
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 resultcomputeColumnMaxima(int[])
public static void naiveComputeColumnMaxima(Matrix matrix, int[] col_maxima)
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 resultnaiveComputeRowMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix, int[])
public static void naiveComputeRowMaxima(Matrix matrix, int[] row_maxima)
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 resultnaiveComputeColumnMaxima(net.maizegenetics.analysis.gbs.neobio.Matrix, int[])
public static void printMatrix(Matrix matrix)
matrix
- a matrixCopyright © 2018. All rights reserved.