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

Sparse Matrices: Matrix Table of Values

Sparse matrices have mostly zero elements, allowing them to be stored more efficiently than dense matrices. Structured sparse matrices store nonzero elements in predefined patterns that can be mapped to a linear array, while unstructured matrices use lists to store the sparse nonzero elements by row. The transpose of a sparse matrix can be computed in linear time relative to the number of nonzero elements by rearranging the sparse elements in the row-major ordered list.

Uploaded by

test test
Copyright
© © All Rights Reserved
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)
45 views

Sparse Matrices: Matrix Table of Values

Sparse matrices have mostly zero elements, allowing them to be stored more efficiently than dense matrices. Structured sparse matrices store nonzero elements in predefined patterns that can be mapped to a linear array, while unstructured matrices use lists to store the sparse nonzero elements by row. The transpose of a sparse matrix can be computed in linear time relative to the number of nonzero elements by rearranging the sparse elements in the row-major ordered list.

Uploaded by

test test
Copyright
© © All Rights Reserved
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/ 21

Sparse Matrices

Matrix table of values


Sparse Matrices

Matrix table of values

00304
Row 2
4 x 5 matrix
00570
4 rows
00000 5 columns
0 2Column
6 0 04 20 elements
6 nonzero elements
Sparse Matrices
Sparse matrix  #nonzero elements/#elements
is small.
Examples:
• Diagonal
 Only elements along diagonal may be nonzero
 n x n matrix  ratio is n/n2 = 1/n
• Tridiagonal
• Only elements on 3 central diagonals may be nonzero
• Ratio is (3n-2)/n2 = 3/n – 2/n2
Sparse Matrices
• Lower triangular (?)
• Only elements on or below diagonal may be nonzero
• Ratio is n(n+1)(2n2) ~ 0.5

These are structured sparse matrices. Nonzero


elements are in a well-defined portion of the
matrix.
Sparse Matrices
An n x n matrix may be stored as an n x n array.
This takes O(n2) space.
The example structured sparse matrices may be
mapped into a 1D array so that a mapping
function can be used to locate an element
quickly; the space required by the 1D array is
less than that required by an n x n array (next
lecture).
Unstructured Sparse Matrices
Airline flight matrix.
 airports are numbered 1 through n
 flight(i,j) = list of nonstop flights from airport i
to airport j
 n = 1000 (say)
 n x n array of list pointers => 4 million bytes
 total number of nonempty flight lists = 20,000
(say)
 need at most 20,000 list pointers => at most
80,000 bytes
Unstructured Sparse Matrices
Web page matrix.
web pages are numbered 1 through n
web(i,j) = number of links from page i to page j

Web analysis.
authority page … page that has many links to it
hub page … links to many authority pages
Web Page Matrix
 n = 2 billion (and growing by 1 million a day)
 n x n array of ints => 16 * 1018 bytes (16 * 109
GB)
 each page links to 10 (say) other pages on
average
 on average there are 10 nonzero entries per
row
 space needed for nonzero elements is
approximately 20 billion x 4 bytes = 80 billion
bytes (80 GB)
Representation Of Unstructured
Sparse Matrices
Single linear list in row-major order.
scan the nonzero elements of the sparse matrix in row-
major order (i.e., scan the rows left to right
beginning with row 1 and picking up the nonzero
elements)
each nonzero element is represented by a triple
(row, column, value)
the list of triples is stored in a 1D array
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
One Linear List Per Row

row1 = [(3, 3), (5,4)]


row2 = [(3,5), (4,7)]
00304 row3 = []
row4 = [(2,2), (3,6)]

00570
00000
Single Linear List
• Class SparseMatrix
– Array smArray of triples of type MatrixTerm
• int row, col, value
– int rows, // number of rows
cols, // number of columns
terms, // number of nonzero elements
capacity; // size of smArray
• Size of smArray generally not predictable at time
of initialization.
– Start with some default capacity/size (say 10)
– Increase capacity as needed
Approximate Memory Requirements

500 x 500 matrix with 1994 nonzero elements, 4


bytes per element

2D array 500 x 500 x 4 = 1million bytes


Class SparseMatrix 3 x 1994 x 4 + 4 x 4
= 23,944 bytes
Array Resizing

if (newSize < terms) throw “Error”;


MatrixTerm *temp = new MatrixTerm[newSize];
copy(smArray, smArray+terms, temp);
delete [] smArray;
smArray = temp;
capacity = newSize;
Array Resizing

• To avoid spending too much overall time


resizing arrays, we generally set newSize =
c * oldSize, where c >0 is some constant.
• Quite often, we use c = 2 (array doubling)
or c = 1.5.
• Now, we can show that the total time spent
in resizing is O(s), where s is the maximum
number of elements added to smArray.
Matrix Transpose

0000
00304
0002
3506
00570
0700
00000
4000
02600
Matrix Transpose
0000
00304
0002
3506
00570
0700
00000
4000
02600
row 1 1 2 2 4 4 2 3 3 3 4 5
column 3 5 3 4 2 3 4 1 2 4 2 1
value 3 4 5 7 2 6 2 3 5 6 7 4
Matrix Transpose
0000 Step 1: #nonzero in each row of transpose.
00304
0002 = #nonzero in each column of
original matrix
3506
00570 = [0, 1, 3, 1, 1]
0700
00000 Step2: Start of each row of transpose
4000 = sum of size of preceding rows of
02600
transpose
row 1 1 2 2 4 4 = [0, 0, 1, 4, 5]

column 3 5 3 4 2 3 Step 3: Move elements, left to right, from


original list to transpose list.
value 3 4 5 7 2 6
Matrix Transpose
Step 1: #nonzero in each row of transpose.
= #nonzero in each column of Complexity
original matrix m x n original matrix
= [0, 1, 3, 1, 1] t nonzero elements
Step2: Start of each row of transpose Step 1: O(n+t)
= sum of size of preceding rows of Step 2: O(n)
transpose Step 3: O(t)
= [0, 0, 1, 4, 5] Overall O(n+t)
Step 3: Move elements, left to right, from
original list to transpose list.
Runtime Performance
Matrix Transpose
500 x 500 matrix with 1994 nonzero elements
Run time measured on a 300MHz Pentium II PC

2D array 210 ms
SparseMatrix 6 ms
Performance
Matrix Addition.
500 x 500 matrices with 1994 and 999 nonzero
elements

2D array 880 ms
SparseMatrix 18 ms

You might also like