Sparse matrix
Sparse matrix
Module-2:Sparse Matrix
Introduction
There are two ways to represent the sparse matrix that are listed as follows -
Array representation
Linked list representation
Example
Array representation of the sparse matrix
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is because zeroes in the
matrix are of no use, so storing zeroes with non-zero elements is wastage of memory. 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.
● Row - It is the index of a row where a non-zero element is located in the matrix.
● Column - It is the index of the column where a non-zero element is located in the matrix.
● Value - It is the value of the non-zero element that is located at the index (row, column).
Example -
Let's understand the array representation of sparse matrix with the help of the example given below -
In a Lower triangular sparse matrix, all elements above the main diagonal have a zero value.
A lower-triangular Matrix Array of size n*n has one non-zero element in the first row, two non-zero elements in
the second row, and similarly n non-zero elements in the nth row.
Example:-
UPPER TRIANGULAR MATRIX/SPARSE MATRIX
In the Upper triangular sparse matrix, all elements below the main diagonal
have a zero value.
An n*n upper-triangular matrix Arr has n non-zero elements in the first row,
n–1 non-zero element in the second row, and likewise one non-zero element
in the nth row.
Example:-
TRI-DIAGONAL MATRIX
Tri-diagonal matrix is also another type of a sparse matrix, where elements with a non-zero value appear only on
the diagonal or immediately below or above the diagonal.
In a tridiagonal matrix, Arr i,j=0, where |i – j| > 1.
Example:-
Sparse Matrices In Data Structures
● Sparse matrix is a two dimensional array in which most of the elements have null value or zero “0”.
● In large number of applications sparse matrices are used.It is wastage of memory and processing time it
store null values of a matrix in array.
● To avoid such circumstances different techniques are used such as linked list.
● In simple words sparse matrices are matrices that allow special techniques to take advantage of the large
number of null elements and the structure.
Applications of Sparse Matrices
● Machine Learning: Storing large datasets with many zeroes (e.g., document-term matrices).
● Graph Theory: Adjacency matrices are often sparse in large graphs.
● Optimization Problems: Sparse matrices are used to store and solve linear equations.
● Image Processing: Efficiently storing and processing images with mostly black pixels.
Advantages of Using Sparse Matrices