Sparse Matrix
Sparse Matrix
CSE, POSTECH
Special Matrices
A square matrix has the same number of rows
and columns.
Some special forms of square matrices are
– Diagonal: M(i,j) = 0 for i ≠ j
– Tridiagonal: M(i,j) = 0 for |i-j| < 1
– Lower triangular: M(i,j) = 0 for i < j
– Upper triangular: M(i,j) = 0 for i > j
– Symmetric M(i,j) = M(j,i) for all i and j
2
Special Matrices
3
Special Matrices
Why are we interested in these “special”
matrices?
– We can provide more efficient implementations for
specific special matrices.
– Rather than having a space complexity of O(n2), we
can find an implementation that is O(n).
– We need to be clever about the “store” and “retrieve”
operations to reduce time.
4
Example 7.4 – Distance Matrix
5
Example 7.5 – Stack Folding Problem
6
Diagonal Matrix
Naive way to represent n x n diagonal matrix
– T d[n][n]
– d[i-1][j-1] for D(i,j)
– requires n2 x sizeof(T) bytes of memory
Better way
– T d[n]
– d[i-1] for D(i,j) where i = j
0 for D(i,j) where i ≠ j
– requires n x sizeof(T) bytes of memory
7
Tridiagonal Matrix
Nonzero elements lie on one of three diagonals:
– main diagonal: i = j
– diagonal below main diagonal: i = j+1
– diagonal above main diagonal: i = j-1
3n-2 elements on these three diagonals: T t[3n-2]
Mappings of Figure 7.2(b)
– by row [2,1,3,1,3,5,2,7,9,0]
– by column [2,3,1,1,5,3,2,9,7,0]
– by diagonal [3,5,9,2,1,2,0,1,3,7]
more on diagonal mapping on the next page
8
Tridiagonal Matrix
Mapping by diagonals beginning with the lowest
D(2,1) -> t[0]
D(3,2) -> t[1]
…
D(n,n-1) -> t[n-2]
D(1,1) -> t[n-1]
D(2,2) -> t[n]
...
D(n, n) -> t[(n-2)+n] = t[2n-2]
D(1,2) -> t[2n-1]
D(2,3) -> t[2n]
…
D(n-1,n) -> t[(2n-2)+(n-1)] = t[3n-3]
See Program 7.11
9
Triangular Matrix
Nonzero elements lie in the region marked
“nonzero” in the figure below
10
Triangular Matrix
Both triangular matrices may be represented using
1-D array T t[n(n+1)/2]
Mappings
– by row?
[2,5,1,0,3,1,4,2,7,0]
– by column?
[2,5,0,4,1,3,2,1,7,0]
11
Lower Triangular Matrix
Mapping by row
L(i,j) =0 if i < j
L(i,j) = t[1+2+…+(i-1)+(j-1)] if i ≥ j
= t[i(i-1)/2 + j-1]
12
Upper Triangular Matrix
Mapping by column
[2, 1, 1, 3, 3, 1, 0, 8, 6, 0]
L(i,j) =? if i > j
L(i,j) =? if i ≤ j
13
Symmetric Matrix
An n x n matrix can be represented using 1-D
array of size n(n+1)/2 by storing either the lower or
upper triangle of the matrix
Use one of the methods for a triangular matrix
The elements that are not explicitly stored may be
computed from those that are stored
– How do we compute this?
14
Sparse Matrix
A matrix is sparse if many of its elements are zero
A matrix that is not sparse is dense
The boundary is not precisely defined
– Diagonal and tridiagonal matrices are sparse
– We classify triangular matrices as dense
Two possible representations
– array
– linked list
15
Array Representation of Sparse Matrix
The nonzero entries may be mapped into a 1D
array in row-major order
To reconstruct the matrix structure, need to record
the row and column each nonzero comes from
16
Array Representation of Sparse Matrix
template<class T> template<class T>
class Term { class sparseMatrix {
private: private:
int row, col; int rows, cols,
T value; int terms;
}; Term<T> *a;
int MaxTerms;
public:
//…
};
18
Linked Representation of Sparse Matrix
See Program 7.18 for linked representation of
sparse matrix
Read Chapter 7
19