DS-sparse Matrix Comparision

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

SPARSE MATRIX

Comparison with Array representation and Linked List representation


Sparse Matrix

col1 col2 col3 col4 col5 col6


row0 15 0 0 22 0  15
row1
 0 11 3 0 0 0 
 
row2  0 0 0 6 0 0
 
row3  0 0 0 0 0 0 
row4
91 0 0 0 0 0
 
row5  0 0 28 0 0 0
5*3 6*6
(a) 15/15 (b) 8/36

Two matrices
sparse matrix
data structure?
2
SPARSE MATRIX ABSTRACT DATA TYPE
Structure Sparse_Matrix is
objects: a set of triples, <row, column, value>, where row
and column are integers and form a unique combination, and
value comes from the set item.
functions:
for all a, b  Sparse_Matrix, x  item, i, j, max_col,
max_row  index

Sparse_Marix Create(max_row, max_col) ::=


return a Sparse_matrix that can hold up to
max_items = max _row  max_col and
whose maximum row size is max_row and
whose maximum column size is max_col.

3
Sparse_Matrix Transpose(a) ::=
return the matrix produced by interchanging
the row and column value of every triple.
Sparse_Matrix Add(a, b) ::=
if the dimensions of a and b are the same
return the matrix produced by adding
corresponding items, namely those with
identical row and column values.
else return error
Sparse_Matrix Multiply(a, b) ::=
if number of columns in a equals number of
rows in b
return the matrix d produced by multiplying
a by b according to the formula: d [i] [j] =
(a[i][k]•b[k][j]) where d (i, j) is the (i,j)th
element
else return error.
CHAPTER 2 4
Abstract data type Sparse-Matrix
(1) Represented by a two-dimensional array.
Sparse matrix wastes space.
(2) Each element is characterized by <row, col, value>.
row col value row col value
# of rows (columns)
# of nonzero terms
a[0] 6
6 8 b[0] 6 6 8
[1] 0
0 15 [1] 0 0 15
[2] 0
3 22 [2] 0 4 91
[3] 0
5 -15 [3] 1 1 11
[4] 1
1 11 transpose [4] 2 1 3
[5] 1
2 3 [5] 2 5 28
[6] 2
3 -6 [6] 3 0 22
[7] 4
0 91 [7] 3 2 -6
[8] 5
2 28 [8] 5 0 -15
(a) (b)
row, column in ascending order
Sparse matrix and its transpose stored as triples
5
Sparse_matrix Create(max_row, max_col) ::=

#define MAX_TERMS 101 /* maximum number of terms +1*/


typedef struct {
int col; # of rows (columns)
int row; # of nonzero terms
int value;
} term;
term a[MAX_TERMS]

6
Transpose a Matrix

(1) for each row i


take element <i, j, value> and store it
in element <j, i, value> of the transpose.

difficulty: where to put <j, i, value>


(0, 0, 15) ====> (0, 0, 15)
(0, 3, 22) ====> (3, 0, 22)
(0, 5, -15) ====> (5, 0, -15)
(1, 1, 11) ====> (1, 1, 11)
Move elements down very often.

(2) For all elements in column j,


place element <i, j, value> in element <j, i, value>
7
void transpose (term a[], term b[])
/* b is set to the transpose of a */
{
int n, i, j, currentb;
n = a[0].value; /* total number of elements */
b[0].row = a[0].col; /* rows in b = columns in a */
b[0].col = a[0].row; /*columns in b = rows in a */
b[0].value = n;
if (n > 0) { /*non zero matrix */
currentb = 1;
for (i = 0; i < a[0].col; i++)
/* transpose by columns in a */
for( j = 1; j <= n; j++)
/* find elements from the current column */
if (a[j].col == i) {
/* element is in current column, add it to b */
8
columns

elements

b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++
}
}
}
Transpose of a sparse matrix

Scan the array “columns” times. ==> O(columns*elements)


The array has “elements” elements.
9
Discussion: compared with 2-D array representation

O(columns*elements) vs. O(columns*rows)

elements --> columns * rows when nonsparse


O(columns*columns*rows)

Problem: Scan the array “columns” times.

Solution:
Determine the number of elements in each column of
the original matrix.
==>
Determine the starting positions of each row in the
transpose matrix.
10
a[0] 6 6 8
a[1] 0 0 15
a[2] 0 3 22
a[3] 0 5 -15
a[4] 1 1 11
a[5] 1 2 3
a[6] 2 3 -6
a[7] 4 0 91
a[8] 5 2 28
[0] [1] [2] [3] [4] [5]
row_terms = 2 1 2 2 0 1
starting_pos = 1 3 4 6 8 8 11
Representation Of Unstructured
Sparse Matrices
Single linear list in row-major order.
scan the nonzero elements of the sparse matrix in row-
major order
each nonzero element is represented by a triple
(row, column, value)
the list of triples may be an array list or a linked list
(chain)
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
Array Linear List Representation

row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6

element 0 1 2 3 4 5
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 7 2 6
Chain Representation

Node structure.

row col
value next
Single Chain

row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6

1 3 1 5 2 3 2 4 4 2 4 3
3 4 5 7 2 6 null

firstNode
One Linear List Per Row

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


00570 row2 = [(3,5), (4,7)]
00000 row3 = []
02600 row4 = [(2,2), (3,6)]
Array Of Row Chains

Node structure.

next
col value
Array Of Row Chains

null
3 3 5 4

00304 null
00570 3 5 4 7

00000
null
02600
null
2 2 3 6

row[]
Orthogonal List Representation

Both row and column lists.

Node structure.
row col value
down next
Row Lists

1 3 3 1 5 4
n
00304
00570 2 3 5 2 4 7
n
00000
02600 null

4 2 2 4 3 6
n
Column Lists

1 3 3 1 5 4
n
00304
00570 2 3 5 2 4 7

00000
02600

4 2 2 4 3 6
n n
Orthogonal Lists

1 3 3 1 5 4
n n
00304
00570 2 3 5 2 4 7
n
00000
02600 null

4 2 2 4 3 6
n n n

row[]

You might also like