0% found this document useful (0 votes)
74 views23 pages

Sparse Class

The document discusses sparse matrices and their representations. A sparse matrix has mostly zero elements. They can be represented in array format by storing the non-zero elements and their indices. This is called a tuple representation. Algorithms for adding two sparse matrices are presented. The steps involve converting the matrices to tuple format and then traversing the tuples to add corresponding elements based on their indices. Limitations of array representation and alternative dynamic representation using linked lists are also covered. Special types of matrices like diagonal and triangular matrices are defined.

Uploaded by

robinpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views23 pages

Sparse Class

The document discusses sparse matrices and their representations. A sparse matrix has mostly zero elements. They can be represented in array format by storing the non-zero elements and their indices. This is called a tuple representation. Algorithms for adding two sparse matrices are presented. The steps involve converting the matrices to tuple format and then traversing the tuples to add corresponding elements based on their indices. Limitations of array representation and alternative dynamic representation using linked lists are also covered. Special types of matrices like diagonal and triangular matrices are defined.

Uploaded by

robinpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

DATA STRUCTURES AND

ALGORITHMS
Module 2

SPARSE MATRIX

SPARSE ARRAY
A special array that contains more number of zero

values than the non-zero values for their elements.


1

No. of non zero elements = 30

Eg:
No of zero elements =6

Therefore, its a sparse matrix

SPARSE MATRIX
A sparse matrix =2D sparse array
A matrix is said to be a sparse matrix if most of its

elements are zero.


dense matrix =A matrix that is not sparse
The density of a matrix is the percentage of entries that

are non-zero

Alternative Representations

If most of the elements are zero then the occurrence of


zero elements in a large array is both a computational
and storage inconvenience
Alternative Representations
Array representation
Dynamic representation

Array Representation (Tuple matrix)

All non-zero elements are stored in another array of


triplet
No of rows in the new array = No. of non zero
elements + 1

No. of columns in the new array = 3

Triplet contains
row number of the non-zero element
column number of the non-zero element
Value of non-zero element

Triplet can be represented by


<Row, Col, Element>

Array Representation (Tuple matrix)


Tuple matrix
(0,0) No of rows in sparse matrix
(0,1) No of columns in sparse matrix
(0,2) No of non-zero elements in
sparse matrix

1 0 0 0
0 0 5 0
0 0 0 0
3

Question
Convert sparse matrix to tuple matrix
0000
7090
0005

Sparse matrix to tuple matrix


i=0, j=0, k=1,count=0
While(i<r)
While(j<c)
I/p: sparse matrix A[][]
If ( A[i][j]!=0 )
O/p: tuple matrix TUPLE[][3]
TUPLE[k][0]=i
TUPLE[k][1]=j
TUPLE[k][2]=A[i][j]
k=k+1, count = count +1
EndIf
j=j+1
EndWhile
i=i+1
EndWhile
TUPLE[0][0]=r
TUPLE[0][1]=c
TUPLE[0][2]=count

Sparse Matrix addition


0

Convert
sparse matrix
to
tuple matrix

Sparse Matrix addition

Sparse Matrix addition case 1


If ((TUPLE1 [i][0] < TUPLE2 [j][0] ))
SUM [ptr][0] =TUPLE1 [i][0]
SUM [ptr][1] =TUPLE1 [ i][1]
SUM [ptr][2] =TUPLE1 [ i][2]
i=i+1,
ptr=ptr+1,
elem=elem+1

Sparse Matrix addition case 2


If ((TUPLE1 [i][0] > TUPLE2 [j][0] ))
SUM [ptr][0] =TUPLE2 [j][0]
SUM [ptr][1] =TUPLE2 [ j][1]
SUM [ptr][2] =TUPLE2 [ j][2]
j=j+1,
ptr=ptr+1,
elem=elem+1

Sparse Matrix addition case 3


If((TUPLE1 [i][0] = TUPLE2 [j][0] ) AND (TUPLE1 [i][1]
=TUPLE2 [j][1] ))
SUM [ptr][0] =TUPLE1 [i][0]
SUM [ptr][1] =TUPLE1 [i][1]
SUM [ptr][2] =TUPLE1 [i][2] +TUPLE2 [j][2]
Ptr=ptr+1,
i=i+1,
j=j+1,
elem=elem+1

Sparse Matrix addition case 4


If ((TUPLE1 [i][0] = TUPLE2 [j][0] ) && (TUPLE1 [i][1]
<TUPLE2 [j][1] ) )
SUM [ptr][0] =TUPLE1 [i][0]
SUM [ptr][1] =TUPLE1 [ i][1]
SUM [ptr][2] =TUPLE1 [ i][2]
i=i+1,
ptr=ptr+1,
elem=elem+1

Sparse Matrix addition case 5


If ((TUPLE1 [i][0] = TUPLE2 [j][0] ) && (TUPLE1 [i][1]
>TUPLE2 [j][1] ) )
SUM [ptr][0] =TUPLE2 [j][0]
SUM [ptr][1] =TUPLE2[ j][1]
SUM [ptr][2] =TUPLE2[ j][2]
j=j+1,
ptr=ptr+1,
elem=elem+1

Sparse Matrix addition case 6a


While(i<n1)
SUM[ptr][0]=TUPLE1[i][0]
SUM[ptr][1]=TUPLE1[i][1]
SUM[ptr][2]=TUPLE1[i][2]
i=i+1,
ptr=ptr+1,
elem=elem+1
EndWhile

Sparse Matrix addition case 6b


While(j<n2)
SUM[ptr][0]=TUPLE2[j][0]
SUM[ptr][1]=TUPLE2[j][1]
SUM[ptr][2]=TUPLE2[j][2]
j=j+1,
ptr=ptr+1,
elem=elem+1
EndWhile

Algorithm SPARSE MATRIX ADDITION


Input: two sparse Matrices MAT1 and MAT2
Output: Resultant Matrix SUM is the sum of two matrices
Data Structure: Sparse Matrix implemented by using array.
Steps:
I=1,j=1,SUM[100][3],ptr=1,elem=0
TUPLE1=SPARSE_TO_TUPLE(MAT1)
TUPLE2=SPARSE_TO_TUPLE(MAT2)
r1=row size,c1=column size of MAT1
r2=row size, c2=column size of MAT2
n1=ROWSIZE(TUPLE1)
n2=ROWSIZE(TUPLE2)
If(COMPARE(r1,r2) =FALSE) OR(COMPARE(c1,c2)=FALSE)
Print Addition is not possible
Exit
Else

SUM[0][0]=r1
SUM[0][1]=c1
SUM[0][2]=elem
Stop

Limitations of array representation

We do not know the number of non-zero elements in


advance
Insertion and deletion is not an easy task in an array

Solution:

Use dynamic representation


i.e Use linked lists

Special types of matrices


Square matrix
A matrix in which no. of rows = no. of columns

Diagonal matrix
A matrix in which only diagonal elements are non-zero
1

Special types of matrices


Upper triangular matrix
A matrix in which all the non-zero elements occur only on or above
the diagonal

Lower Triangular matrix

A matrix in which all the non-zero elements occur only on or below


the diagonal
1

Special types of matrices


Scalar matrix
A diagonal matrix in which all diagonal elements are same

Identity or Unit matrix

A diagonal matrix in which all diagonal elements are 1


1

You might also like