Docsity

Lecture notes on Advanced List Structures - Sparse Matrices | COP 3503, Study notes of Computer Science

Toggle sidebar
bg1
Introduction
An m x n matrix (table) is said to be sparse if “many” of its elements are zero
(empty). A matrix which is not sparse is dense. The boundary between a
dense and a sparse matrix is not precisely defined. Diagonal and tridiagonal n
x n matrices are sparse since they have O(n) nonzero terms and O(n2) zero
terms.
Is an n x n triangular (either upper or lower) matrix sparse? A triangular matrix
will have at least n(n-1)/2 zero terms and at most n(n+1)/2 nonzero terms. For
the representation schemes that we are about to examine to be competitive
over the standard two-dimensional array representation, it will turn out that the
number of nonzero terms will need to be less than n2/3 and in some cases less
than n2/5. Thus, in the context of the representation schemes we are about to
see, a triangular matrix is considered to be dense rather than sparse.
Sparse matrices which are either diagonal or tridiagonal have sufficient
structure in their nonzero regions to allow fairly simple representation schemes
Sparse Matrices - 1
Advanced List Structures – Sparse Matrices (3)
Definition: A matrix M is diagonal iff M(i, j) = 0 for i j.
Definition: A matrix M is tridiagonal iff M(i, j) = 0 for |i – j| > 1.
[Both of these special matrices are special cases of the more general
square band matrix in which the non-zero elements are on a band which
is centered about the main diagonal.]
Considering matrix M to be a 6 x 6 matrix, for purposes of an example, we have:
2 0 0 0 0 0 4 2 0 0 0 0
0 1 0 0 0 0 1 3 1 0 0 0
0 0 4 0 0 0 0 4 5 2 0 0
0 0 0 6 0 0 0 0 2 9 4 0
0 0 0 0 5 0 0 0 0 6 3 3
0 0 0 0 0 3 0 0 0 0 1 2
Diagonal Matrix Tridiagonal Matrix
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture notes on Advanced List Structures - Sparse Matrices | COP 3503 and more Study notes Computer Science in PDF only on Docsity!

Introduction

An m x n matrix (table) is said to be sparse if “many” of its elements are zero

(empty). A matrix which is not sparse is dense. The boundary between a

dense and a sparse matrix is not precisely defined. Diagonal and tridiagonal n

x n matrices are sparse since they have O(n) nonzero terms and O(n

2

) zero

terms.

Is an n x n triangular (either upper or lower) matrix sparse? A triangular matrix

will have at least n(n-1)/2 zero terms and at most n(n+1)/2 nonzero terms. For

the representation schemes that we are about to examine to be competitive

over the standard two-dimensional array representation, it will turn out that the

number of nonzero terms will need to be less than n

2

/3 and in some cases less

than n

2

/5. Thus, in the context of the representation schemes we are about to

see, a triangular matrix is considered to be dense rather than sparse.

Sparse matrices which are either diagonal or tridiagonal have sufficient

structure in their nonzero regions to allow fairly simple representation schemes

Advanced List Structures – Sparse Matrices (3)

Definition: A matrix M is diagonal iff M(i, j) = 0 for i  j. Definition: A matrix M is tridiagonal iff M(i, j) = 0 for |i – j| > 1. [Both of these special matrices are special cases of the more general square band matrix in which the non-zero elements are on a band which is centered about the main diagonal.] Considering matrix M to be a 6 x 6 matrix, for purposes of an example, we have: 2 0 0 0 0 0 4 2 0 0 0 0 0 1 0 0 0 0 1 3 1 0 0 0 0 0 4 0 0 0 0 4 5 2 0 0 0 0 0 6 0 0 0 0 2 9 4 0 0 0 0 0 5 0 0 0 0 6 3 3 0 0 0 0 0 3 0 0 0 0 1 2 Diagonal Matrix Tridiagonal Matrix

to be developed whose space requirements equal the size of the nonzero

region. We are not concerned with these simple cases, rather our focus will be

on sparse matrices with irregular or unstructured nonzero regions.

Irregular Sparse Matrices

An irregular sparse matrix has a nonzero region in which no discernable pattern

exists. Without regularity in the nonzero region it is highly unlikely that a

standard representation, such as a two-dimensional array, would provide an

efficient representation of the matrix. On the other hand, if there is a high

degree of regularity or structure in the nonzero region, then an efficient

representation structure of the nonzero region can typically be developed using

standard linked lists that will require space equal in size to the nonzero region.

We will not examine these highly regular sparse matrices, our concern is finding

a suitable representation scheme for an irregular sparse matrix. Consider the

following irregular 4 x 8 sparse matrix.

Table 1 – Irregular 4 x 8 Sparse Matrix

Notice in the irregular sparse matrix shown in Table 1 of the 32 total elements in

the matrix that fully 23 or 71.8% of the cells have zero value. Clearly this is a

sparse matrix. Further notice that there appears to be no obvious regularity to

where the nonzero elements occur.

The nonzero elements of the sparse matrix in Table 1can be mapped into a

linear list. If this list is organized in row-major order we would have the

following: 2, 1, 6, 7, 3, 9, 8, 4, 5. To be able to reconstruct the matrix structure,

the original row and column must be recorded for each nonzero element in the

matrix. The linear list would contain nodes that look like the one shown below:

Figure 1 – Node for representation of the sparse matrix data value column row 1 1 2 2 2

Figure 3 – Linear list representation of the transpose of the list represented in Figure 2

Using the linear list to represent the sparse matrix provided us a very fast way

to perform the transpose of the matrix, but at what cost to the retrieval process?

Notice that the representation of the transposed sparse matrix is no longer

ordered in row-major fashion, but is now ordered in column-major fashion!

Our concern with the representation in Figure 2 is that access operations into

the sparse matrix are not efficient for random access into the matrix. Any

application performing predominantly access (look-up) operations into the

sparse matrix will not perform with anywhere near optimal behavior using this

representation. What we need is a better representation when considering

random access into the sparse matrix as the dominant operation. To illustrate

the access problem this representation presents, consider the sparse matrix

addition illustrated below:

Matrix A

Matrix B

The result of A + B is matrix C shown below, recall that matrix addition is

defined only when the two operand matrices have the same dimensions and is

defined as C(i,j) = A(i,j) + B(i,j); 1  i  m, 1  j  n where A and B are m x n

matrices:

Result matrix of A + B

Consider the representation of the matrices A and B in linear list format shown

below:

Matrix A Matrix B Matrix C = A + B

Think about the operational aspects of performing the addition of matrices A

and B using the linear list representation. Since the two matrices are stored in

a row-major fashion a simple iteration through each list will suffice for the

addition operation. For example, beginning in matrix A, we find the first nonzero

element to be in position (2,1), this too happens to be the first nonzero element

in matrix B, so this sum is computed an stored in the first element of the list

which represents the sum. Advancing iterators in both the lists we would find

that the next nonzero element in A is in position (2,3), but there is no

corresponding element in B since the iterator in B is on a node which

corresponds to position (2,4). Thus, a single pass through each list will produce

the sum of the two sparse matrices. Once again, we can see that this is a fairly

efficient way in which to produce the sum of two sparse matrices. Multiplication

is only slightly more difficult, try it yourself to see.

2 1 4 2 3 2 3 2 1 4 3 2 5 4 4 2 1 4 2 4 1 3 3 3 4 3 2 5 4 1 2 1 8 2 3 2 3 3 3 4 3 4 5 4 5 2 4 1 3 2 1

Notice that the transpose of the matrix from Table 1 already exists in the

representation of Figure 4. Simply interchange the row and column lists and

you have the transpose matrix! This takes O(1) time!

Toeplitz Matrices

An nxn matrix X is a Toeplitz matrix iff X(i,j ) = X(i-1, j-1) for all i and j where i >

and j > 1. Figure 5 illustrates a 4x4 Toeplitz matrix.

Figure 5 – A 4x4 Toeplitz Matrix

A Toeplitz matrix can be represented by a sparse matrix. The reason for this is

that the number of distinct elements in a Toeplitz matrix is at most 2n –1. Recall

that the definition given at the beginning of these notes for a sparse matrix

indicated that a sparse matrix contained O(n) nonzero elements. In the case of

a Toeplitz matrix rather than nonzero elements we need only to represent the

distinct elements, that the Toeplitz matrix can be represented as a sparse

matrix. Figure 6 illustrates the linear list implementation (in row major order) of

the Toeplitz matrix shown in Figure 5.

Figure 6 – Linear list representation of the Toeplitz matrix of Figure 5

C-matrices

An nxn C-matrix is one in which all elements other than those in row 1, row n ,

and column 1 are zero. A C-matrix contains at most 3n-2 nonzero elements

and is thus considered to be a sparse matrix. Figure 7 illustrates a 6x6 C-

matrix.

1 1 6 1 2 2 2 1 1 3 1 3 4 1 8 1 3 4 1 4 5

Figure 7 – A 6x6 C-matrix

Notice that Figure 7 contains exactly 16 nonzero elements which is (3(6) – 2).

As before, this sparse matrix is easily represented using the linear list structure.

Antidiagonal Matrices

An nxn square matrix X is antidiagonal iff all elements X(i,j ) where i+j  n+1 are

zero. Figure 8 illustrates a 5x5 antidiagonal matrix.

Figure 8 – A 5x5 Antidiagonal Matrix

An antidiagonal matrix has all the characteristics of a diagonal matrix except

that the nonzero elements run opposite to the main diagonal of the matrix.

There are also variants of antidiagonal matrices as there were for the diagonal

matrices. Similar representations are typically employed for all these variations

as well.