Package io.openml.gearbox
Class Matrix<T>
java.lang.Object
io.openml.gearbox.Matrix<T>
- Type Parameters:
T
- The type of cell value in the tensor
A 2D tensor.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionget
(int row, int col) Returns the data stored at specified row and column in this matrix.int
Returns the number of columns in thisMatrix
.int
Returns the number of rows within thisMatrix
.void
Mutates the data stored at specified row and column in this matrix.void
traverseBfs
(int startRow, int startCol, BiConsumer<Integer, Integer> processCurrentCell, BiPredicate<Integer, Integer> shouldEnqueue) Traversing all cells of this matrix using the BFS (Breadth-First search) algorithm.
-
Constructor Details
-
Matrix
Constructor.- Parameters:
matrix
- The tensor data. A shallow defensive copy is made. Changes to the originalmatrix
structure will not affect this object, but changes to its mutable elements will be reflected.- Throws:
IllegalArgumentException
- ifmatrix
isnull
, empty, have no columns, or non-rectangular
-
-
Method Details
-
getNumRows
public int getNumRows()Returns the number of rows within thisMatrix
.- Returns:
- the height of the matrix
-
getNumCols
public int getNumCols()Returns the number of columns in thisMatrix
.- Returns:
- the width of the matrix
-
get
Returns the data stored at specified row and column in this matrix.- Parameters:
row
- The row indexcol
- The column index- Returns:
- the data
- Throws:
IllegalArgumentException
- ifrow
orcol
is out-of-bound
-
set
Mutates the data stored at specified row and column in this matrix.- Parameters:
row
- The row indexcol
- The column indexvalue
- The new value- Throws:
IllegalArgumentException
- ifrow
orcol
is out-of-bound
-
traverseBfs
public void traverseBfs(int startRow, int startCol, BiConsumer<Integer, Integer> processCurrentCell, BiPredicate<Integer, Integer> shouldEnqueue) Traversing all cells of this matrix using the BFS (Breadth-First search) algorithm.In this 2D version, the neighbors are defined as adjacent cells (up, down, left, right); diagonals are not considered as adjacent and therefore not neighbors.
Create a queue to store the cells to be visited. While the queue is not empty: Dequeue a cell (current row, current column). Process the current cell (e.g., print its value, check if it's the target). For each unvisited neighbor of the current cell: Check if the neighbor's coordinates are within the grid boundaries. Check if the neighbor is a valid cell to visit (e.g., not an obstacle). If valid and unvisited mark the neighbor as visited and enqueue its coordinates.
An example application on "number of island" problem could be something along these lines (written in Groovy)int LAND = 1 int WATER = 0 Integer[][] map = data Matrix<Integer> matrix = new Matrix<>(map) int numLands = 0 for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[0].length; j++) { if (matrix.get(i, j) == LAND) { numLands++ matrix.traverseBfs(i, j, (x, y) -> matrix.set(x, y, WATER), (x, y) -> matrix.get(x, y) == LAND) } } }
System.out.println(numLands)- Parameters:
startRow
- The starting row index of the traversalstartCol
- The starting column index of the traversalprocessCurrentCell
- The logic for processing the current cellshouldEnqueue
- An extra check after the "valid and unvisited" passed- Throws:
IllegalArgumentException
- ifstartRow
orstartCol
is out of bounds
-