How to store a sparse matrix?
nnz
: non-zero number of sparse matrix row_size
: row number of matrix column_size
: column number of matrix
There are many ways, their spatial complexity:
- Compressed Sparse Series (CSR):
2*nnz + row_size
amount of memory - Compressed Sparse Column (CSC):
2*nnz + column_size
amount of memory - Coordinate format (COO):
3*nnz
amount of memory
For space complexity:
If row_size > column_size
, use format CSC
, otherwise use format CSR
.
For time complexity:
For format, the CSR
row will be indexed by O(1)
time, the column will be indexed by time O(log(k))
, binary search for the column, k
is the number of non-zero element of this row. Thus, the value will be time-indexed O(log(k))
.
For format, the COO
value will be indexed in O(1)
time.
Format information
[1] https://en.wikipedia.org/wiki/Sparse_matrix
[2] https://software.intel.com/en-us/node/471374
source to share
An efficient way would be to use a hashmap (per row) of hashmaps (to store the items in each row by column index). Then there will be access to any element in O (1) time.
You can implement all numerical algorithms like addition and multiplication, iterating with only non-zero elements, which will give you better complexity than O (N * M), where N and M are the number of columns and rows in the matrix.
source to share