0% found this document useful (0 votes)
256 views

Sparse Matrix

This document discusses different types of special matrices including diagonal, tridiagonal, triangular, symmetric, and sparse matrices. It provides examples of how each type can be represented more efficiently using arrays or linked lists by taking advantage of their special structure and storing only nonzero elements. Representations include storing values by diagonal, row, or column. The goal is to reduce space complexity from the standard O(n^2) to O(n) when possible.

Uploaded by

meniki2009
Copyright
© Attribution Non-Commercial (BY-NC)
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)
256 views

Sparse Matrix

This document discusses different types of special matrices including diagonal, tridiagonal, triangular, symmetric, and sparse matrices. It provides examples of how each type can be represented more efficiently using arrays or linked lists by taking advantage of their special structure and storing only nonzero elements. Representations include storing values by diagonal, row, or column. The goal is to reduce space complexity from the standard O(n^2) to O(n) when possible.

Uploaded by

meniki2009
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 19

Matrices

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

 See Figure 7.7

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.

 Read Examples 7.4 & 7.5

4
Example 7.4 – Distance Matrix

5
Example 7.5 – Stack Folding Problem

 What is H(1,2)? Is this correct?

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

 See Program 7.8 for the class diagonalMatrix

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

 1+2+…+n = Σ(i=1..n) = n(n+1)/2 elements in the


nonzero region

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]

• See Program 7.12 for the method


lowerTriangularMatrix<T>::set

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

 Exercise: Write the method for


upperTriangularMatrix<T>::set

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

 Read Example 7.6

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:
//…
};

 See Programs 7.13~7.17 for the class definition


and methods of sparseMatrix
17
Linked Representation of Sparse Matrix
• A shortcoming of the 1-D array of a sparse matrix is that we
need to know the number of nonzero terms in each of the
sparse matrices when the array is created
• A linked representation can overcome this shortcoming

18
Linked Representation of Sparse Matrix
 See Program 7.18 for linked representation of
sparse matrix

 Read Chapter 7

19

You might also like