Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

SPARSE MATRIX

(DATA STRUCTURES)
SPARSE MATRIX
• 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.
Why to use Sparse Matrix instead of simple matrix
?
• Storage: There are lesser non-zero elements than
zeros and thus lesser 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
Example:
00304
00570
00000
02600
• 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).
SPARSE MATRIX
• Any matrix is called as Sparse Matrix if it contains
more number of zeros. The mathematical
formula behind this is: T >= (m * n )/2 where T is
total number of zeros.
• For example, the following 4x4 matrix is a sparse
Matrix. Conventional method of representation
of such a matrix is not space efficient. It will be
prudent to store non-zero elements only. If this is
done, then the matrix may be thought of as an
ordered list of non-zero elements. Information
about non-zero elements have three parts: Row,
Column and its value.
SPARSE MATRIX
Program
• Design, Develop and Implement a menu driven
Program in C for the following operations on two
dimensional array of Integers:
a. Find addition of two matrix
b. Find transpose of a matrix
c. Find multiplication of two matrix
d. Determine given matrix is sparse or not.
e. Exit
Support the program with appropriate functions for
each of the above operations.
SPARSE MATRIX
#include<stdio.h> if(a[i][j] == 0)
int main() {
{ totalzeros++;
int i, j, rows, columns, a[10][10], totalzeros = 0; }
printf("\n Please Enter Number of rows and }
columns: "); }
scanf("%d %d", &rows, &columns);
printf("\n Please Enter the Matrix Elements \n");
for(i = 0; i < rows; i++) if(totalzeros >= (rows * columns)/2)
{ {
for(j = 0;j < columns; j++) printf("\n The Matrix that you
{ entered is a Sparse Matrix ");
scanf("%d", &a[i][j]); }
} else
} {
for(i= 0; i < rows; i++) printf("\n The Matrix that you
{ entered is Not a Sparse Matrix ");
for(j = 0; j < columns; j++) }
{
return 0;
}

You might also like