Javatpoint Logo
Javatpoint Logo

Sparse Matrix

A matrix can be defined as a two-dimensional array having 'm' columns and 'n' rows representing m*n matrix. Sparse matrices are those matrices that have the majority of their elements equal to zero. In other words, the sparse matrix can be defined as the matrix that has a greater number of zero elements than the non-zero elements.

Why do we need to use a sparse matrix instead of a simple matrix?

We can also use the simple matrix to store the elements in the memory; then why do we need to use the sparse matrix. The following are the advantages of using a sparse matrix:

  • Storage: As we know, a sparse matrix that contains lesser non-zero elements than zero so less memory can be used to store elements. It evaluates only the non-zero elements.
  • Computing time: In the case of searching n sparse matrix, we need to traverse only the non-zero elements rather than traversing all the sparse matrix elements. It saves computing time by logically designing a data structure traversing non-zero elements.

Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. The zeroes in the matrix are of no use to store zeroes with non-zero elements. To avoid such wastage, we can store only non-zero elements. If we store only non-zero elements, it reduces the traversal time and the storage space.

Sparse Matrix Representation

The non-zero elements can be stored with triples, i.e., rows, columns, and value. The sparse matrix can be represented in the following ways:

  • Array representation
  • Linked list representation

Array Representation

The 2d array can be used to represent a sparse matrix in which there are three rows named as:

  1. Row: It is an index of a row where a non-zero element is located.
  2. Column: It is an index of the column where a non-zero element is located.
  3. Value: The value of the non-zero element is located at the index (row, column).

Let's understand the sparse matrix using array representation through an example.

Sparse Matrix

As we can observe above, that sparse matrix is represented using triplets, i.e., row, column, and value. In the above sparse matrix, there are 13 zero elements and 7 non-zero elements. This sparse matrix occupies 5*4 = 20 memory space. If the size of the sparse matrix is increased, then the wastage of memory space will also be increased. The above sparse matrix can be represented in the tabular form shown as below:

Sparse Matrix

In the above table structure, the first column is representing the row number, the second

column is representing the column number and third column represents the non-zero value at index(row, column). The size of the table depends upon the number of non-zero elements in the sparse matrix. The above table occupies (7 * 3) = 21 but it more than the sparse matrix. Consider the case if the matrix is 8*8 and there are only 8 non-zero elements in the matrix then the space occupied by the sparse matrix would be 8*8 = 64 whereas, the space occupied by the table represented using triplets would be 8*3 = 24.

In the 0th row and 1nd column, 4 value is available. In the 0th row and 3rd column, value 5 is stored. In the 1st row and 2nd column, value 3 is stored. In the 1st row and 3rd column, value 6 is stored. In 2nd row and 2nd column, value 2 is stored. In the 3rd row and 0th column, value 2 is stored. In the 3rd row and 1st column, value 3 is stored.

Array implementation of sparse matrix in C

Output

Sparse Matrix

Linked List Representation

In linked list representation, linked list data structure is used to represent a sparse matrix. In linked list representation, each node consists of four fields whereas, in array representation, there are three fields, i.e., row, column, and value. The following are the fields in the linked list:

  • Row: It is an index of row where a non-zero element is located.
  • Column: It is an index of column where a non-zero element is located.
  • Value: It is the value of the non-zero element which is located at the index (row, column).
  • Next node: It stores the address of the next node.

Let's understand the sparse matrix using linked list representation through an example.

Sparse Matrix
Sparse Matrix

In the above figure, sparse represented in the linked list form. In the node, first field represents the index of row, second field represents the index of column, third field represents the value and fourth field contains the address of the next node.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA