Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store sparse matrix?

I need to implement 2 types of storing sparse matrix in C++:

  • Linked List
  • Array (effective way)

Space complexity is very important here. What are the most effective ways to do it?

like image 468
sayhello Avatar asked Jun 16 '15 09:06

sayhello


People also ask

How do you store sparse vectors?

To store the sparse vector efficiently, a vector of pairs can be used. The First element of pair will be the index of sparse vector element(which is non-zero) and the second element will be the actual element.

How sparse matrix are stored efficiently in memory?

However, if a matrix has most of its elements equal to zero, then the matrix is known as a sparse matrix. In the case of a sparse matrix, we don't store the zeros in the memory to reduce memory usage and make it more efficient. We only store the non-zero values of the sparse matrix inside the memory.

How do you store a sparse matrix in python?

Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).

How do you store a matrix?

In the computer memory, all elements are stored linearly using contiguous addresses. Therefore, in order to store a two-dimensional matrix a, two dimensional address space must be mapped to one-dimensional address space. In the computer's memory matrices are stored in either Row-major order or Column-major order form.


1 Answers

nnz : non-zero number of sparse matrix
row_size : matrix row number
column_size : matrix column number
There are many ways, their space complexity :

  • Compressed Sparse Row (CSR) : 2*nnz + row_size number of memory
  • Compressed Sparse Column (CSC) : 2*nnz + column_size number of memory
  • Coordinate Format (COO) : 3*nnz number of memory

For space complexity :
If row_size > column_size, use CSC format, otherwise, use CSR format.

For Time complexity:
For CSR format, Row will be indexed by O(1) time, Column will be indexed by O(log(k)) time, by binary search the Column, k is the number of non-zero element of that row. So value will be indexed by O(log(k)) time.
For COO format, value will be indexed in O(1) time.

Format details
[1] https://en.wikipedia.org/wiki/Sparse_matrix
[2] https://software.intel.com/en-us/node/471374

like image 143
pgplus1628 Avatar answered Sep 30 '22 11:09

pgplus1628