Question: Problem 2: Sparse Matrix Using Linked List (5 pts) Implement the linked list sparse matrix class in LLSparseMat.java so that LLMainClass can be executed with
Problem 2: Sparse Matrix Using Linked List (5 pts) Implement the linked list sparse matrix class in LLSparseMat.java so that LLMainClass can be executed with MAT argument. The command line arguments are VEC/MAT File1 A/S/M File2
RowHead nodes correspond to nonzero rows. Each rowhead node stores a LLSparseVec, storing a nonzero row. It also has a pointer to the next rowhead.
When a row becomes empty (no nonzero elements), the rowhead should be removed.
Implement the constructor, accessor methods:
getElement, setElement, clearElement, numElements (returns number of non-zero elements).
getRowIndices returns an array of indices of rows with nonzero elements.
getOneRowColIndices returns an array of nonzero column indices of the row.
Use LLSparseVec.getAllIndices(). getOneRowValues returns an array of nonzero values.
Use LLSparseVec.getAllValues().
NOTE that these methods should all be linear to the number of nonzero rows or nonzero elements. idx = 3 val = 7 idx = 4 val = -2 idx = 6 val = 9 idx = 9 val = 3 idx = 4 val = 2 idx = 7 val = -7 idx = 9 val = 2 idx = 4 val = -4 idx = 9 val = 6 Page 3 of 5 When LLMainClass is called using MAT argument and with at least one input file, the program should be able to run correctly and give the same output as ArrayMainClass, else a proper error message should be generated.
Below is the code to be overridden
public class LLSparseM implements SparseM {
public LLSparseM(int nr, int nc){
return;
}
@Override
public int nrows() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int ncols() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int numElements() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getElement(int ridx, int cidx) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void clearElement(int ridx, int cidx) {
// TODO Auto-generated method stub
}
@Override
public void setElement(int ridx, int cidx, int val) {
// TODO Auto-generated method stub
}
@Override
public int[] getRowIndices() {
// TODO Auto-generated method stub
return null;
}
@Override
public int[] getOneRowColIndices(int ridx) {
// TODO Auto-generated method stub
return null;
}
@Override
public int[] getOneRowValues(int ridx) {
// TODO Auto-generated method stub
return null;
}
@Override
public SparseM addition(SparseM otherM) {
// TODO Auto-generated method stub
return null;
}
@Override
public SparseM subtraction(SparseM otherM) {
// TODO Auto-generated method stub
return null;
}
@Override
public SparseM multiplication(SparseM otherM) {
// TODO Auto-generated method stub
return null;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Below is interface
public interface SparseM {
// problem 2
int nrows(); //return number of rows
int ncols(); //return number of columns
int numElements(); //return total number of nonzero elements in the matrix
int getElement(int ridx, int cidx); //return the element at a given entry (ridx, cidx),
void clearElement(int ridx, int cidx); //set the element at (ridx,cidx) to zero
void setElement(int ridx, int cidx, int val); //set the element at (ridx,cidx) to val
// get indices of non-empty rows, sorted
int[] getRowIndices();
// get indices of non-empty cols, sorted
int[] getOneRowColIndices(int ridx);
// get values of a given row
int[] getOneRowValues(int ridx);
// methods for problem 3
SparseM addition(SparseM otherM); // this matrix + otherM
// return a new matrix storing the result
SparseM subtraction(SparseM otherM); // this matrix - otherM
// return a new matrix storing the result
SparseM multiplication(SparseM otherM); // this matrix .* with otherM
// return a new matrix storing the result
}
------------------------------------------------------------------------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
