Sparse Array
If most of the elements of the matrix have 0 value, then it is called a sparse matrix.
If most of the elements are nonzero, then the matrix is dense.
Why to use Sparse Matrix instead of simple matrix ?
• Storage: There are fewer non-zero elements than zeros, and thus less memory can be used
to store only those elements.
• Computing time: Computing time can be saved by logically designing a data structure
traversing only non-zero elements.
Sparse Matrix Representation
A sparse matrix can be represented by using TWO representations; those are as follows...
Triplet Representation (Array Representation)
Linked Representation
Array Representation
2D array is used to represent a sparse matrix in which there are three rows named as
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index - (row,column)
This will be represented as 2-D array as,
Rows will be from 0 to number of non-zero elements in matrix
Columns will be from 1 to 3 (hence triplet)
0 0 3
1 1 5
2 2 7
3 0 8
Linked List Representation of Sparse Matrix
The benefit of using a linked list instead of an array to represent the sparse matrix is that it is
simpler to add or remove nodes from a linked list than from an array.
• Row - Row where the non-zero element is located.
• Column - Column where the non-zero element is located.
• Value - It is the value of the non-zero element that is located at the index (row, column).
• Next node - Address of the next node