DS-sparse Matrix Comparision
DS-sparse Matrix Comparision
DS-sparse Matrix Comparision
Two matrices
sparse matrix
data structure?
2
SPARSE MATRIX ABSTRACT DATA TYPE
Structure Sparse_Matrix is
objects: a set of triples, <row, column, value>, where row
and column are integers and form a unique combination, and
value comes from the set item.
functions:
for all a, b Sparse_Matrix, x item, i, j, max_col,
max_row index
3
Sparse_Matrix Transpose(a) ::=
return the matrix produced by interchanging
the row and column value of every triple.
Sparse_Matrix Add(a, b) ::=
if the dimensions of a and b are the same
return the matrix produced by adding
corresponding items, namely those with
identical row and column values.
else return error
Sparse_Matrix Multiply(a, b) ::=
if number of columns in a equals number of
rows in b
return the matrix d produced by multiplying
a by b according to the formula: d [i] [j] =
(a[i][k]•b[k][j]) where d (i, j) is the (i,j)th
element
else return error.
CHAPTER 2 4
Abstract data type Sparse-Matrix
(1) Represented by a two-dimensional array.
Sparse matrix wastes space.
(2) Each element is characterized by <row, col, value>.
row col value row col value
# of rows (columns)
# of nonzero terms
a[0] 6
6 8 b[0] 6 6 8
[1] 0
0 15 [1] 0 0 15
[2] 0
3 22 [2] 0 4 91
[3] 0
5 -15 [3] 1 1 11
[4] 1
1 11 transpose [4] 2 1 3
[5] 1
2 3 [5] 2 5 28
[6] 2
3 -6 [6] 3 0 22
[7] 4
0 91 [7] 3 2 -6
[8] 5
2 28 [8] 5 0 -15
(a) (b)
row, column in ascending order
Sparse matrix and its transpose stored as triples
5
Sparse_matrix Create(max_row, max_col) ::=
6
Transpose a Matrix
elements
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++
}
}
}
Transpose of a sparse matrix
Solution:
Determine the number of elements in each column of
the original matrix.
==>
Determine the starting positions of each row in the
transpose matrix.
10
a[0] 6 6 8
a[1] 0 0 15
a[2] 0 3 22
a[3] 0 5 -15
a[4] 1 1 11
a[5] 1 2 3
a[6] 2 3 -6
a[7] 4 0 91
a[8] 5 2 28
[0] [1] [2] [3] [4] [5]
row_terms = 2 1 2 2 0 1
starting_pos = 1 3 4 6 8 8 11
Representation Of Unstructured
Sparse Matrices
Single linear list in row-major order.
scan the nonzero elements of the sparse matrix in row-
major order
each nonzero element is represented by a triple
(row, column, value)
the list of triples may be an array list or a linked list
(chain)
Single Linear List Example
00304 list =
00570 row 1 1 2 2 4 4
00000 column 3 5 3 4 2 3
02600 value 3 4 5 7 2 6
Array Linear List Representation
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
Chain Representation
Node structure.
row col
value next
Single Chain
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
One Linear List Per Row
Node structure.
next
col value
Array Of Row Chains
null
3 3 5 4
00304 null
00570 3 5 4 7
00000
null
02600
null
2 2 3 6
row[]
Orthogonal List Representation
Node structure.
row col value
down next
Row Lists
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
Column Lists
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
Orthogonal Lists
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[]