Sparse Matrices
Sparse Matrices
Sparse matrices
CSE Department
National Institute of Technology Rourkela
Reference
Most of the slides in this presentation belongs to : Douglas
Wilhelm Harder, M.Math, LEL, University of Waterloo
https://ece.uwaterloo.ca/~dwharder/aads/,
Tridiagonal
Only elements on 3 central diagonals may be nonzero
Ratio is (3n-2)/n2 = 3/n – 2/n2
tri-diagonal matrix
80000/(1000x1000x4)=0.02 = 2%
list =
00304 row 1 1 2 2 4 4
00570
00000 column 3 5 3 4 2 3
02600
value 3 4 5 7 2 6
row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6
element 0 1 2 3 4 5
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 7 2 6
row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6
1 3 1 5 2 3 2 4 4 2 4 3
3 4 5 7 2 6 null
firstNode
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
One Linear List Per Row
next
Node structure.
col value
null
3 3 5 4
00304
null
00570
3 5 4 7
00000
02600
null
null
2 2 3 6
row[]
Node structure.
1 3 3 1 5 4
n
00304
00570 2 3 5 2 4 7
n
00000
02600
null
4 2 2 4 3 6
n
1 3 3 1 5 4
n
00304
00570 2 3 5 2 4 7
00000
02600
4 2 2 4 3 6
n n
1 3 3 1 5 4
n n
00304
00570 2 3 5 2 4 7
n
00000
02600
null
4 2 2 4 3 6
n n n
row[]
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Approximate Memory Requirements
0 0 3 0 4 0000
0 0 5 7 0 0002
0 0 0 0 0 3506
0 2 6 0 0 0700
4000
row 1 1 2 2 4 4 2 3 3 3 4 5
column 3 5 3 4 2 3 4 1 2 4 2 1
value 3 4 5 7 2 6 2 3 5 6 7 4
row 1 1 2 2 4 4 2 3 3 3 4 5
column 3 5 3 4 2 3 4 1 2 4 2 1
value 3 4 5 7 2 6 2 3 5 6 7 4
Starting index of array is 1 not 0
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Sparse matrix addition
Simply traverse through both matrices element by element
insert the smaller element (one with smaller row and col value) into
the resultant matrix.
If we come across an element with the same row and column value,
we simply add their values and insert the added data into the
resultant matrix.
Transpose
Any row value equal to x in the first matrix and row value equal to
y in the second matrix (transposed one) will contribute towards
result[x][y].