Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Structure for Storing Sparse Matrices

I need to do some mathematics operations on sparse matrices. I noticed that using arrays may not be the most efficient way to utilize my memory, especially since the matrices may have over 200 rows. I have considered using a linked list too, but I'm not sure if that'll be better. Is there any suitable data structure [approach] to this situation.

like image 291
micaleel Avatar asked Jun 12 '09 22:06

micaleel


People also ask

Which data structure is appropriate to store a sparse matrix?

Besides array, a linked list can also be a good choice for storing the sparse matrix in a compressed form where each node of the linked list has exactly four entries containing row number, column number, the value of the non-zero element along with the pointer of the next node i.e. (i, j, value, next-pointer) as shown ...

How do you store sparse matrices?

A sparse matrix can be stored in full-matrix storage mode or a packed storage mode. When a sparse matrix is stored in full-matrix storage mode, all its elements, including its zero elements, are stored in an array.

What are sparse matrices in data structures?

A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix.

Which data structure can provide an efficient represent of a sparse matrix in computers memory?

Sparse Matrices can be represented more efficiently by using the Triplet Representation or Linked Representation.


2 Answers

How many "over 200 rows"? How sparse? A 1000x1000 matrix of doubles is still less than 8MB, which is not something I'd worry about unless you need to work with a lot of them simultaneously.

The ideal data structure depends mainly on what kind of operations you need to perform.

Note that there are ready-to-use sparse matrix libraries for all common languages out there - you're much better off using one of those than rolling your own.

like image 140
Michael Borgwardt Avatar answered Sep 22 '22 22:09

Michael Borgwardt


Here are a few open source Java maths libraries that include sparse matrices. You could study the data structures used (or even just use one of them if programming in Java).

  • Colt
  • Jsci
  • Matrix Toolkits Java
like image 22
Mark Avatar answered Sep 22 '22 22:09

Mark