Upload
robinpt
View
257
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Sparse Class
Citation preview
DATA STRUCTURES AND ALGORITHMSModule 2SPARSE MATRIX
SPARSE ARRAYA special array that contains more number of zero values than the non-zero values for their elements.
Eg: No of zero elements =6No. of non zero elements = 3Therefore, its a sparse matrix
106000002
SPARSE MATRIXA sparse matrix =2D sparse arrayA matrix is said to be a sparse matrix if most of its elements are zero.dense matrix =A matrix that is not sparseThe 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 tripletNo of rows in the new array = No. of non zero elements + 1No. of columns in the new array = 3Triplet contains row number of the non-zero element column number of the non-zero element Value of non-zero elementTriplet can be represented by
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 matrixArray Representation (Tuple matrix)
100000500000
342001125
QuestionConvert sparse matrix to tuple matrix0 0 0 0 7 0 9 00 0 0 5
Sparse Matrix additionConvert sparse matrix to tuple matrix
040000706000
000310002000
343033101202
343014127206
+Sparse Matrix addition
343014127206
343033101202
345014033101127208
Sparse Matrix addition case 1If ((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 2If ((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 3If((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 5If ((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
Algorithm SPARSE MATRIX ADDITION
Input: two sparse Matrices MAT1 and MAT2Output: Resultant Matrix SUM is the sum of two matricesData Structure: Sparse Matrix implemented by using array.
Steps:I=1,j=1,SUM[100][3],ptr=1,elem=0TUPLE1=SPARSE_TO_TUPLE(MAT1)TUPLE2=SPARSE_TO_TUPLE(MAT2)r1=row size,c1=column size of MAT1r2=row size, c2=column size of MAT2n1=ROWSIZE(TUPLE1)n2=ROWSIZE(TUPLE2)If(COMPARE(r1,r2) =FALSE) OR(COMPARE(c1,c2)=FALSE)Print Addition is not possibleExitElse SUM[0][0]=r1SUM[0][1]=c1SUM[0][2]=elem Stop
Limitations of array representation
We do not know the number of non-zero elements in advanceInsertion and deletion is not an easy task in an array
Solution:Use dynamic representation i.e Use linked lists
Special types of matricesSquare matrixA matrix in which no. of rows = no. of columns
Diagonal matrixA matrix in which only diagonal elements are non-zero
100060008
147562938
Upper triangular matrixA matrix in which all the non-zero elements occur only on or above the diagonal
Lower Triangular matrixA matrix in which all the non-zero elements occur only on or below the diagonalSpecial types of matrices
147062008
100260358
Scalar matrixA diagonal matrix in which all diagonal elements are same
Identity or Unit matrixA diagonal matrix in which all diagonal elements are 1Special types of matrices
300030003
100010001