2. Sparse Matrices
•
Definition
–
head node
•
•
•
•
using
the down field to link into a column list
using the right field to link into a row list
the next field links the head nodes
the total number of head nods
= max{number of rows, number of columns}
entry
–
entry node
•
•
the down field links to the next nonzero term in the same column
the right field links to the next nonzero term in the same row
3. Sparse Matrices(Cont’d)
•
Declarations
class SparseMatrices{
int MAX_SIZE = 50;
MatrixNode hdnode[MAX_SIZE];
enum TagField{
head, entry
}
class EntryNode{
int row;
int col;
int value;
}
class MatrixNode{
MatrixNode down;
MatrixNode right;
TagField tag;
MatrixNode next;
EntryNode entry;
}
}
20. Sparse Matrices(Cont’d)
•
Analysis of mread
–
–
–
–
–
O(max{numRows, numCols}) = new keyword works in a constant amount of time.
O(numTerms) = in a constant amount of time to set up each none zero
O(max{numRows, numCols}, numTerms) = O(numRows + numCols + numTerms)
Sparse Matrix using two-dimensional array
• O(numRows∙numCols)
linked list is better, but it is slightly worse than sequential method
21. Sparse Matrices(Cont’d)
•
mwrite()
–
–
–
–
•
using two for loop
outer for loop = the number of rows
inner for loop = the number of terms
O(numRows + numTerms)
merase()
–
–
–
–
–
–
resembles the structure found in mwrite()
erasing the entry nodes resembles the structure found in mwrite()
O(numRows + numTerms)
requiring extra time to erase the head nodes
O(numRows + numCols)
finally, O(numRows + numCols + numTerms)
22. Sparse Matrices(Cont’d)
•
Union in c vs Inheritance in java
AbstractNode
down : AbstractNode
right : AbstractNode
tag : TagField
getEntry()
getNext()
setNext(AbstractNode)
VS
HeadNode
next : AbstractNode
getEntry()
getNext()
setNext(AbstractNode)
EntryNode
entry : Entry
entry
EntryNode()
getEntry()