Sparse Matrices

An m by n matrix A contains nm elements in it. For tex2html_wrap_inline386 , tex2html_wrap_inline388 , A[i,j] is the (i,j)th element of matrix A.

Let b be a 1 by m matrix (a row vector). The product of b and A is a 1 by n row vector c such that tex2html_wrap_inline412 . It is the dot product between b and the columns of A.

The transpose of a m by n matrix A is a n by m matrix tex2html_wrap_inline428 such that tex2html_wrap_inline430 . We've exchanged rows and columns.

The product of two matrices A (m by n) and B (n by p) is a matrix C (m by p) where tex2html_wrap_inline450 . Note that this is most often computed using a simple triply nested loop.

In many applications, we need to manipulate sparse matrices. These are matrices that consist of many zeros and very few non-zero elements. Instead of taking m n space to represent a matrix A with l non-zero elements, we can represent it using tex2html_wrap_inline458 space using three vectors: for tex2html_wrap_inline460 , tex2html_wrap_inline462 is the column of non-zero element i, tex2html_wrap_inline466 is its row, and tex2html_wrap_inline468 is its value. For ease of computation, we require that tex2html_wrap_inline470 if tex2html_wrap_inline472 . Further, if tex2html_wrap_inline474 , then tex2html_wrap_inline476 . This means that the non-zero elements are sorted by column, then by row within column.

Given an m by n matrix A, we can convert it to the sparse representation in time tex2html_wrap_inline484 . (Simply read across the rows one at a time, creating an entry in the sparse matrix format for each non-zero encountered.) Given a sparse matrix A with l non-zero elements, we can convert it to a standard (dense) matrix starting from a matrix of all zeros in tex2html_wrap_inline458 time. (Simply assign tex2html_wrap_inline492 for each tex2html_wrap_inline460 .)

Describe an algorithm for each of these problems. Provide an analysis for the number of primitive operations they take. Make your analyses as tight as possible.

1.
Let A be a sparse m by n matrix with l non-zero elements. For tex2html_wrap_inline386 , tex2html_wrap_inline388 , compute A[i,j].

2.
Let b be a dense 1 by m row vector. Let A be a sparse m by n matrix with l non-zero elements. Compute b times A.

3.
Same question again, but let b be sparse with k non-zero elements.

4.
Let A be a sparse m by n matrix with l non-zero elements. Compute the transpose of A.

5.
Let A be a sparse m by n matrix with l non-zero elements. Let B be a sparse n by p matrix with k non-zero elements. Compute A times B.


next up previous
Next: Homework 3 Up: HOMEWORK Previous: HOMEWORK