Data Structures
Array Representation
Andres Mendez-Vazquez
May 6, 2015
1 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
2 / 81
Images/cinvestav-
Introduction
Observation
When a program manipulates many variables that contain “similar” forms
of data, organizational problems quickly arise.
Example
When a program manipulates many variables that contain “similar” forms
of data, organizational problems quickly arise.
We could use a name variable for each data
double score0; double score1; double score2;
double score3; double score4; double score5;
3 / 81
Images/cinvestav-
Introduction
Observation
When a program manipulates many variables that contain “similar” forms
of data, organizational problems quickly arise.
Example
When a program manipulates many variables that contain “similar” forms
of data, organizational problems quickly arise.
We could use a name variable for each data
double score0; double score1; double score2;
double score3; double score4; double score5;
3 / 81
Images/cinvestav-
Introduction
Observation
When a program manipulates many variables that contain “similar” forms
of data, organizational problems quickly arise.
Example
When a program manipulates many variables that contain “similar” forms
of data, organizational problems quickly arise.
We could use a name variable for each data
double score0; double score1; double score2;
double score3; double score4; double score5;
3 / 81
Images/cinvestav-
Making your life miserable
Because you can ask
Which is the highest score?
Look at the code
4 / 81
Images/cinvestav-
Making your life miserable
Because you can ask
Which is the highest score?
Look at the code
double high_score = score0 ;
i f ( score1 > high_score ) { high_score = score1 ; }
i f ( score2 > high_score ) { high_score = score2 ; }
i f ( score3 > high_score ) { high_score = score3 ; }
i f ( score4 > high_score ) { high_score = score4 ; }
i f ( score5 > high_score ) { high_score = score5 ; }
System . out . p r i n t l n ( high_score ) ;
4 / 81
Images/cinvestav-
How do we solve this?
Thus
Java, C and C++ use array variables to put together this collection of
equal elements.
Memory Layout
5 / 81
Images/cinvestav-
How do we solve this?
Thus
Java, C and C++ use array variables to put together this collection of
equal elements.
Memory Layout
a b c d
START
MEMORY LAYOUT
5 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
6 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
1D Array Representation In Java, C, and C++
Notes
For 1-dimensional array:
The elements are mapped into contiguous memory locations
They are accessed through the use of location (x [i]) = start + i
How much memory is used for representing an array?
1 Space Overhead
1 Storing the start address: 4 bytes
2 Storing x.length: 4 bytes
2 Space for the elements, for example 4 bytes for n elements
Total: 4 + 4 + 4n = 8 + 4n bytes
7 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
8 / 81
Images/cinvestav-
Now, 2D Arrays
We declare 2-dimensional arrays as follow (Java, C)
int[][] A = new int[3][4]
It can be shown as a table
A[0][0] A[0][1] A[0][2] A[0][3]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2][0] A[2][1] A[2][2] A[2][3]
9 / 81
Images/cinvestav-
Now, 2D Arrays
We declare 2-dimensional arrays as follow (Java, C)
int[][] A = new int[3][4]
It can be shown as a table
A[0][0] A[0][1] A[0][2] A[0][3]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2][0] A[2][1] A[2][2] A[2][3]
9 / 81
Images/cinvestav-
Rows and Columns in a 2-D Array
Rows
A[0][0] A[0][1] A[0][2] A[0][3]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2][0] A[2][1] A[2][2] A[2][3]
Columns
10 / 81
Images/cinvestav-
Rows and Columns in a 2-D Array
Rows
A[0][0] A[0][1] A[0][2] A[0][3]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2][0] A[2][1] A[2][2] A[2][3]
Columns
A[0][0] A[0][1] A[0][2] A[0][3]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2][0] A[2][1] A[2][2] A[2][3]
10 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
11 / 81
Images/cinvestav-
2D Array Representation In Java, C, and C++
Given the following 2-dimensional array x
x =







a b c d
e f g h
i j k l







This information is stored as 1D arrays with a reference to them
12 / 81
Images/cinvestav-
2D Array Representation In Java, C, and C++
Given the following 2-dimensional array x
x =







a b c d
e f g h
i j k l







This information is stored as 1D arrays with a reference to them
12 / 81
Images/cinvestav-
Some Properties
If we call the lengths of each of them
x.length ⇒ 3
x[0].length == x[1].length == x[2].length ⇒ 3
Space Overhead
Remember, it is 8 bytes per 1D array
1 Thus, we have 4 × 8 = 32 bytes
2 Or we can see this as (number of rows + 1) × 8 bytes
13 / 81
Images/cinvestav-
Some Properties
If we call the lengths of each of them
x.length ⇒ 3
x[0].length == x[1].length == x[2].length ⇒ 3
Space Overhead
Remember, it is 8 bytes per 1D array
1 Thus, we have 4 × 8 = 32 bytes
2 Or we can see this as (number of rows + 1) × 8 bytes
13 / 81
Images/cinvestav-
Some Properties
If we call the lengths of each of them
x.length ⇒ 3
x[0].length == x[1].length == x[2].length ⇒ 3
Space Overhead
Remember, it is 8 bytes per 1D array
1 Thus, we have 4 × 8 = 32 bytes
2 Or we can see this as (number of rows + 1) × 8 bytes
13 / 81
Images/cinvestav-
Some Properties
If we call the lengths of each of them
x.length ⇒ 3
x[0].length == x[1].length == x[2].length ⇒ 3
Space Overhead
Remember, it is 8 bytes per 1D array
1 Thus, we have 4 × 8 = 32 bytes
2 Or we can see this as (number of rows + 1) × 8 bytes
13 / 81
Images/cinvestav-
Some Properties
If we call the lengths of each of them
x.length ⇒ 3
x[0].length == x[1].length == x[2].length ⇒ 3
Space Overhead
Remember, it is 8 bytes per 1D array
1 Thus, we have 4 × 8 = 32 bytes
2 Or we can see this as (number of rows + 1) × 8 bytes
13 / 81
Images/cinvestav-
More Properties
First
This representation is called the array-of-arrays representation.
Second
It requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4
bytes for the 4 1D-arrays.
Third
One memory block of size number of rows and number of rows blocks
of size number of columns.
14 / 81
Images/cinvestav-
More Properties
First
This representation is called the array-of-arrays representation.
Second
It requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4
bytes for the 4 1D-arrays.
Third
One memory block of size number of rows and number of rows blocks
of size number of columns.
14 / 81
Images/cinvestav-
More Properties
First
This representation is called the array-of-arrays representation.
Second
It requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4
bytes for the 4 1D-arrays.
Third
One memory block of size number of rows and number of rows blocks
of size number of columns.
14 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
15 / 81
Images/cinvestav-
Row-Major Mapping
Again
x =







a b c d
e f g h
i j k l







For this
We will convert it into 1D-array y by collecting elements by rows.
Thus
Within a row elements are collected from left to right.
Rows are collected from top to bottom.
16 / 81
Images/cinvestav-
Row-Major Mapping
Again
x =







a b c d
e f g h
i j k l







For this
We will convert it into 1D-array y by collecting elements by rows.
Thus
Within a row elements are collected from left to right.
Rows are collected from top to bottom.
16 / 81
Images/cinvestav-
Row-Major Mapping
Again
x =







a b c d
e f g h
i j k l







For this
We will convert it into 1D-array y by collecting elements by rows.
Thus
Within a row elements are collected from left to right.
Rows are collected from top to bottom.
16 / 81
Images/cinvestav-
We get
An array
y[] = {a, b, c, d, e, f, g, h, i, j, k, l}
Memory Map
17 / 81
Images/cinvestav-
We get
An array
y[] = {a, b, c, d, e, f, g, h, i, j, k, l}
Memory Map
17 / 81
Images/cinvestav-
How do we locate an element?
Look At This
Then
Assume x has r rows and c columns.
Each row has c elements.
i rows to the left of row i.
Thus
We have ic elements to the left of x[i][0].
Then, x[i][j] is mapped to position ic + j of 1D array.
18 / 81
Images/cinvestav-
How do we locate an element?
Look At This
Then
Assume x has r rows and c columns.
Each row has c elements.
i rows to the left of row i.
Thus
We have ic elements to the left of x[i][0].
Then, x[i][j] is mapped to position ic + j of 1D array.
18 / 81
Images/cinvestav-
How do we locate an element?
Look At This
Then
Assume x has r rows and c columns.
Each row has c elements.
i rows to the left of row i.
Thus
We have ic elements to the left of x[i][0].
Then, x[i][j] is mapped to position ic + j of 1D array.
18 / 81
Images/cinvestav-
How do we locate an element?
Look At This
Then
Assume x has r rows and c columns.
Each row has c elements.
i rows to the left of row i.
Thus
We have ic elements to the left of x[i][0].
Then, x[i][j] is mapped to position ic + j of 1D array.
18 / 81
Images/cinvestav-
How do we locate an element?
Look At This
Then
Assume x has r rows and c columns.
Each row has c elements.
i rows to the left of row i.
Thus
We have ic elements to the left of x[i][0].
Then, x[i][j] is mapped to position ic + j of 1D array.
18 / 81
Images/cinvestav-
How do we locate an element?
Look At This
Then
Assume x has r rows and c columns.
Each row has c elements.
i rows to the left of row i.
Thus
We have ic elements to the left of x[i][0].
Then, x[i][j] is mapped to position ic + j of 1D array.
18 / 81
Images/cinvestav-
Space Overhead
We have
4 bytes for start of 1D array.
4 bytes for length of 1D array.
4 bytes for c (number of columns)
Total: 12 bytes
Note: The number of rows = length/c
Still
Disadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-
Space Overhead
We have
4 bytes for start of 1D array.
4 bytes for length of 1D array.
4 bytes for c (number of columns)
Total: 12 bytes
Note: The number of rows = length/c
Still
Disadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-
Space Overhead
We have
4 bytes for start of 1D array.
4 bytes for length of 1D array.
4 bytes for c (number of columns)
Total: 12 bytes
Note: The number of rows = length/c
Still
Disadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-
Space Overhead
We have
4 bytes for start of 1D array.
4 bytes for length of 1D array.
4 bytes for c (number of columns)
Total: 12 bytes
Note: The number of rows = length/c
Still
Disadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-
Space Overhead
We have
4 bytes for start of 1D array.
4 bytes for length of 1D array.
4 bytes for c (number of columns)
Total: 12 bytes
Note: The number of rows = length/c
Still
Disadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-
Space Overhead
We have
4 bytes for start of 1D array.
4 bytes for length of 1D array.
4 bytes for c (number of columns)
Total: 12 bytes
Note: The number of rows = length/c
Still
Disadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-
Column-Major Mapping
Similar Setup
Convert into 1D array y by collecting elements by columns.
Within a column elements are collected from top to bottom.
Columns are collected from left to right.
Thus
x =







a b c d
e f g h
i j k l







We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-
Column-Major Mapping
Similar Setup
Convert into 1D array y by collecting elements by columns.
Within a column elements are collected from top to bottom.
Columns are collected from left to right.
Thus
x =







a b c d
e f g h
i j k l







We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-
Column-Major Mapping
Similar Setup
Convert into 1D array y by collecting elements by columns.
Within a column elements are collected from top to bottom.
Columns are collected from left to right.
Thus
x =







a b c d
e f g h
i j k l







We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-
Column-Major Mapping
Similar Setup
Convert into 1D array y by collecting elements by columns.
Within a column elements are collected from top to bottom.
Columns are collected from left to right.
Thus
x =







a b c d
e f g h
i j k l







We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
21 / 81
Images/cinvestav-
Matrix
Something Notable
Table of values.
It has rows and columns, but numbering begins at 1 rather than 0.
We use the following notation
Use notation x(i,j) rather than x[i][j].
Note
It may use a 2D array to represent a matrix.
22 / 81
Images/cinvestav-
Matrix
Something Notable
Table of values.
It has rows and columns, but numbering begins at 1 rather than 0.
We use the following notation
Use notation x(i,j) rather than x[i][j].
Note
It may use a 2D array to represent a matrix.
22 / 81
Images/cinvestav-
Matrix
Something Notable
Table of values.
It has rows and columns, but numbering begins at 1 rather than 0.
We use the following notation
Use notation x(i,j) rather than x[i][j].
Note
It may use a 2D array to represent a matrix.
22 / 81
Images/cinvestav-
Drawbacks of using a 2D Array
First
Indexes are off by 1.
Second
Java arrays do not support matrix operations such as add, transpose,
multiply, and so on.
However
You can develop a class Matrix for object-oriented support of all matrix
operations.
23 / 81
Images/cinvestav-
Drawbacks of using a 2D Array
First
Indexes are off by 1.
Second
Java arrays do not support matrix operations such as add, transpose,
multiply, and so on.
However
You can develop a class Matrix for object-oriented support of all matrix
operations.
23 / 81
Images/cinvestav-
Drawbacks of using a 2D Array
First
Indexes are off by 1.
Second
Java arrays do not support matrix operations such as add, transpose,
multiply, and so on.
However
You can develop a class Matrix for object-oriented support of all matrix
operations.
23 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
24 / 81
Images/cinvestav-
Some Basic Matrices: Diagonal Matrix
Diagonal Matrix
An n × n matrix in which all nonzero terms are on the diagonal.
Properties
x(i,j) is on diagonal iff i = j
Example
x =










a 0 0 0
0 b 0 0
0 0 c 0
0 0 0 d










25 / 81
Images/cinvestav-
Some Basic Matrices: Diagonal Matrix
Diagonal Matrix
An n × n matrix in which all nonzero terms are on the diagonal.
Properties
x(i,j) is on diagonal iff i = j
Example
x =










a 0 0 0
0 b 0 0
0 0 c 0
0 0 0 d










25 / 81
Images/cinvestav-
Some Basic Matrices: Diagonal Matrix
Diagonal Matrix
An n × n matrix in which all nonzero terms are on the diagonal.
Properties
x(i,j) is on diagonal iff i = j
Example
x =










a 0 0 0
0 b 0 0
0 0 c 0
0 0 0 d










25 / 81
Images/cinvestav-
Some Basic Matrices: Diagonal Matrix
Properties
x(i,j) is on diagonal iff i = j
DRAWBACK
For a n × n you are required to have:
For example using integers: 4n2 bytes...
What to do?
Store diagonal only vs n2 whole
26 / 81
Images/cinvestav-
Some Basic Matrices: Diagonal Matrix
Properties
x(i,j) is on diagonal iff i = j
DRAWBACK
For a n × n you are required to have:
For example using integers: 4n2 bytes...
What to do?
Store diagonal only vs n2 whole
26 / 81
Images/cinvestav-
Some Basic Matrices: Diagonal Matrix
Properties
x(i,j) is on diagonal iff i = j
DRAWBACK
For a n × n you are required to have:
For example using integers: 4n2 bytes...
What to do?
Store diagonal only vs n2 whole
26 / 81
Images/cinvestav-
Some Basic Matrices: Lower Triangular Matrix
Definition
An n × n matrix in which all nonzero terms are either on or below the
diagonal.
Example
x =










1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10










27 / 81
Images/cinvestav-
Some Basic Matrices: Lower Triangular Matrix
Definition
An n × n matrix in which all nonzero terms are either on or below the
diagonal.
Example
x =










1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10










27 / 81
Images/cinvestav-
Some Basic Matrices: Lower Triangular Matrix
Properties
x(i,j) is part of lower triangle iff i >= j.
Number of elements in lower triangle is
1 + 2 + 3 + ... + n =
n (n + 1)
2
(1)
Thus
You can store only the lower triangle
28 / 81
Images/cinvestav-
Some Basic Matrices: Lower Triangular Matrix
Properties
x(i,j) is part of lower triangle iff i >= j.
Number of elements in lower triangle is
1 + 2 + 3 + ... + n =
n (n + 1)
2
(1)
Thus
You can store only the lower triangle
28 / 81
Images/cinvestav-
Some Basic Matrices: Lower Triangular Matrix
Properties
x(i,j) is part of lower triangle iff i >= j.
Number of elements in lower triangle is
1 + 2 + 3 + ... + n =
n (n + 1)
2
(1)
Thus
You can store only the lower triangle
28 / 81
Images/cinvestav-
Array Of Arrays Representation
We can use this
Something Notable
You can use an irregular 2-D array ... length of rows is not required to be
the same.
29 / 81
Images/cinvestav-
Array Of Arrays Representation
We can use this
Something Notable
You can use an irregular 2-D array ... length of rows is not required to be
the same.
29 / 81
Images/cinvestav-
Code
Code for irregular array
// d e c l a r e a two−dimensional a r r a y v a r i a b l e
// and a l l o c a t e the d e s i r e d number of rows
i n t [ ] [ ] i r r e g u l a r A r r a y = new i n t [ numberOfRows ] [ ] ;
// now a l l o c a t e space f o r the elements i n each row
f o r ( i n t i = 0; i < numberOfRows ; i++)
i r r e g u l a r A r r a y [ i ] = new i n t [ s i z e [ i ] ] ;
// use the a r r a y l i k e any r e g u l a r a r r a y
i r r e g u l a r A r r a y [ 2 ] [ 3 ] = 5;
i r r e g u l a r A r r a y [ 4 ] [ 6 ] = i r r e g u l a r A r r a y [ 2 ] [ 3 ] + 2;
i r r e g u l a r A r r a y [ 1 ] [ 1 ] += 3;
30 / 81
Images/cinvestav-
However, You can do better
Map Lower Triangular Array Into A 1D Array
You can use a row-major order, but omit terms that are not part of the
lower triangle.
For example
x =










1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10










We get
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
31 / 81
Images/cinvestav-
However, You can do better
Map Lower Triangular Array Into A 1D Array
You can use a row-major order, but omit terms that are not part of the
lower triangle.
For example
x =










1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10










We get
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
31 / 81
Images/cinvestav-
However, You can do better
Map Lower Triangular Array Into A 1D Array
You can use a row-major order, but omit terms that are not part of the
lower triangle.
For example
x =










1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10










We get
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
31 / 81
Images/cinvestav-
The final mapping
We want
We have that
1 Order is: row 1, row 2, row 3, ...
2 Row i is preceded by rows 1, 2, ..., i-1
3 Size of row i is i.
32 / 81
Images/cinvestav-
The final mapping
We want
We have that
1 Order is: row 1, row 2, row 3, ...
2 Row i is preceded by rows 1, 2, ..., i-1
3 Size of row i is i.
32 / 81
Images/cinvestav-
The final mapping
We want
We have that
1 Order is: row 1, row 2, row 3, ...
2 Row i is preceded by rows 1, 2, ..., i-1
3 Size of row i is i.
32 / 81
Images/cinvestav-
The final mapping
We want
We have that
1 Order is: row 1, row 2, row 3, ...
2 Row i is preceded by rows 1, 2, ..., i-1
3 Size of row i is i.
32 / 81
Images/cinvestav-
More
Find the total number of elements before row i
1 + 2 + 3 + ... + i − 1 =
i (i − 1)
2
(2)
Thus
So element (i,j) is at position i(i-1)/2 + j -1 of the 1D array.
33 / 81
Images/cinvestav-
More
Find the total number of elements before row i
1 + 2 + 3 + ... + i − 1 =
i (i − 1)
2
(2)
Thus
So element (i,j) is at position i(i-1)/2 + j -1 of the 1D array.
33 / 81
Images/cinvestav-
Now the Interface Matrix
You will need this for your homework
p u b l i c i n t e r f a c e Matrix<Item >{
p u b l i c Item get ( i n t row , i n t column ) ;
p u b l i c void add ( i n t row , i n t column , Item myobj ) ;
p u b l i c Item remove ( i n t row , i n t column ) ;
p u b l i c void output ( ) ;
}
34 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
35 / 81
Images/cinvestav-
Sparse Matrices
Definition
A sparse array is simply an array most of whose entries are zero (or null, or
some other default value).
For Example
Suppose you wanted a 2-dimensional array of course grades, whose rows
are students and whose columns are courses.
Properties
There are about 90,173 students
There are about 5000 courses
This array would have about 450,865,000 entries
36 / 81
Images/cinvestav-
Sparse Matrices
Definition
A sparse array is simply an array most of whose entries are zero (or null, or
some other default value).
For Example
Suppose you wanted a 2-dimensional array of course grades, whose rows
are students and whose columns are courses.
Properties
There are about 90,173 students
There are about 5000 courses
This array would have about 450,865,000 entries
36 / 81
Images/cinvestav-
Sparse Matrices
Definition
A sparse array is simply an array most of whose entries are zero (or null, or
some other default value).
For Example
Suppose you wanted a 2-dimensional array of course grades, whose rows
are students and whose columns are courses.
Properties
There are about 90,173 students
There are about 5000 courses
This array would have about 450,865,000 entries
36 / 81
Images/cinvestav-
Sparse Matrices
Definition
A sparse array is simply an array most of whose entries are zero (or null, or
some other default value).
For Example
Suppose you wanted a 2-dimensional array of course grades, whose rows
are students and whose columns are courses.
Properties
There are about 90,173 students
There are about 5000 courses
This array would have about 450,865,000 entries
36 / 81
Images/cinvestav-
Thus
Something Notable
Since most students take fewer than 5000 courses, there will be a lot of
empty spaces in this array.
Something Notable
This is a big array, even by modern standards.
37 / 81
Images/cinvestav-
Thus
Something Notable
Since most students take fewer than 5000 courses, there will be a lot of
empty spaces in this array.
Something Notable
This is a big array, even by modern standards.
37 / 81
Images/cinvestav-
Example: Airline Flights
Example
Airline flight matrix.
Properties
Airports are numbered 1 through n
flight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representation
n × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-
Example: Airline Flights
Example
Airline flight matrix.
Properties
Airports are numbered 1 through n
flight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representation
n × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-
Example: Airline Flights
Example
Airline flight matrix.
Properties
Airports are numbered 1 through n
flight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representation
n × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-
Example: Airline Flights
Example
Airline flight matrix.
Properties
Airports are numbered 1 through n
flight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representation
n × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-
Example: Airline Flights
However
The total number of flights is way lesser =⇒ 20,000
Needed Storage
We need at most 20,000 list references =⇒ Thus, we need at most 80,000
bytes
39 / 81
Images/cinvestav-
Example: Airline Flights
However
The total number of flights is way lesser =⇒ 20,000
Needed Storage
We need at most 20,000 list references =⇒ Thus, we need at most 80,000
bytes
39 / 81
Images/cinvestav-
Web Page Matrix
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.
40 / 81
Images/cinvestav-
Web Page Matrix
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.
40 / 81
Images/cinvestav-
Web Page Matrix
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.
40 / 81
Images/cinvestav-
Web Page Matrix
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.
40 / 81
Images/cinvestav-
Web Page Matrix
Something Notable
n= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representation
n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
Properties
Each page links to 10 (say) other pages on average.
On average there are 10 nonzero entries per row.
Space needed for non-zero elements is approximately 20,000,000,000
x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-
Web Page Matrix
Something Notable
n= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representation
n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
Properties
Each page links to 10 (say) other pages on average.
On average there are 10 nonzero entries per row.
Space needed for non-zero elements is approximately 20,000,000,000
x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-
Web Page Matrix
Something Notable
n= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representation
n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
Properties
Each page links to 10 (say) other pages on average.
On average there are 10 nonzero entries per row.
Space needed for non-zero elements is approximately 20,000,000,000
x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-
Web Page Matrix
Something Notable
n= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representation
n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
Properties
Each page links to 10 (say) other pages on average.
On average there are 10 nonzero entries per row.
Space needed for non-zero elements is approximately 20,000,000,000
x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-
Web Page Matrix
Something Notable
n= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representation
n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
Properties
Each page links to 10 (say) other pages on average.
On average there are 10 nonzero entries per row.
Space needed for non-zero elements is approximately 20,000,000,000
x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-
What we need...
We need...
There are ways to represent sparse arrays efficiently
42 / 81
Images/cinvestav-
Sparse Matrices
Definition
Sparse ... many elements are zero
Dense ... few elements are zero
Examples of structured sparse matrices
Diagonal
Tridiagonal
Lower triangular (Actually in the Middle of Sparse and Dense)
Actually
They may be mapped into a 1D array so that a mapping function can be
used to locate an element.
43 / 81
Images/cinvestav-
Sparse Matrices
Definition
Sparse ... many elements are zero
Dense ... few elements are zero
Examples of structured sparse matrices
Diagonal
Tridiagonal
Lower triangular (Actually in the Middle of Sparse and Dense)
Actually
They may be mapped into a 1D array so that a mapping function can be
used to locate an element.
43 / 81
Images/cinvestav-
Sparse Matrices
Definition
Sparse ... many elements are zero
Dense ... few elements are zero
Examples of structured sparse matrices
Diagonal
Tridiagonal
Lower triangular (Actually in the Middle of Sparse and Dense)
Actually
They may be mapped into a 1D array so that a mapping function can be
used to locate an element.
43 / 81
Images/cinvestav-
Sparse Matrices
Definition
Sparse ... many elements are zero
Dense ... few elements are zero
Examples of structured sparse matrices
Diagonal
Tridiagonal
Lower triangular (Actually in the Middle of Sparse and Dense)
Actually
They may be mapped into a 1D array so that a mapping function can be
used to locate an element.
43 / 81
Images/cinvestav-
Sparse Matrices
Definition
Sparse ... many elements are zero
Dense ... few elements are zero
Examples of structured sparse matrices
Diagonal
Tridiagonal
Lower triangular (Actually in the Middle of Sparse and Dense)
Actually
They may be mapped into a 1D array so that a mapping function can be
used to locate an element.
43 / 81
Images/cinvestav-
Sparse Matrices
Definition
Sparse ... many elements are zero
Dense ... few elements are zero
Examples of structured sparse matrices
Diagonal
Tridiagonal
Lower triangular (Actually in the Middle of Sparse and Dense)
Actually
They may be mapped into a 1D array so that a mapping function can be
used to locate an element.
43 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
44 / 81
Images/cinvestav-
Representation Of Unstructured Sparse Matrices
We can use a
Single linear list in row-major order.
Thus
We scan the non-zero 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).
45 / 81
Images/cinvestav-
Representation Of Unstructured Sparse Matrices
We can use a
Single linear list in row-major order.
Thus
We scan the non-zero 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).
45 / 81
Images/cinvestav-
Representation Of Unstructured Sparse Matrices
We can use a
Single linear list in row-major order.
Thus
We scan the non-zero 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).
45 / 81
Images/cinvestav-
Representation Of Unstructured Sparse Matrices
We can use a
Single linear list in row-major order.
Thus
We scan the non-zero 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).
45 / 81
Images/cinvestav-
Outline
1 Introduction
1D Arrays
2D Arrays
2 Representation
2D Array Representation
3 Improving the representation
Row-Major Mapping
4 Matrix
Definition
Types of Matrices
5 Sparse Matrices
Sparse Matrices
Representation Of Unstructured Sparse Matrices
Sparse Arrays
46 / 81
Images/cinvestav-
Example with an Sparse Array
First
We will start with sparse one-dimensional arrays, which are simpler
Example
47 / 81
Images/cinvestav-
Example with an Sparse Array
First
We will start with sparse one-dimensional arrays, which are simpler
Example
47 / 81
Images/cinvestav-
Representation
we can have the following representation
Where
1 The front element is the index.
2 The second element is the value at cell index.
3 A pointer to the next element
48 / 81
Images/cinvestav-
Representation
we can have the following representation
Where
1 The front element is the index.
2 The second element is the value at cell index.
3 A pointer to the next element
48 / 81
Images/cinvestav-
Representation
we can have the following representation
Where
1 The front element is the index.
2 The second element is the value at cell index.
3 A pointer to the next element
48 / 81
Images/cinvestav-
Representation
we can have the following representation
Where
1 The front element is the index.
2 The second element is the value at cell index.
3 A pointer to the next element
48 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Sparse Array ADT
For a one-dimensional array of Item
A constructor:
SparseArray(int length)
A way to get values from the array:
Object get(int index)
A way to store values in the array:
void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position
1 For number, it should return ZERO.
2 For Item, it should return NULL.
49 / 81
Images/cinvestav-
Other Operations
What about?
1 int length():
return the size of the original array
2 int elementCount() :
how many non-null values are in the array
Question
Do we forget something?
50 / 81
Images/cinvestav-
Other Operations
What about?
1 int length():
return the size of the original array
2 int elementCount() :
how many non-null values are in the array
Question
Do we forget something?
50 / 81
Images/cinvestav-
Other Operations
What about?
1 int length():
return the size of the original array
2 int elementCount() :
how many non-null values are in the array
Question
Do we forget something?
50 / 81
Images/cinvestav-
Other Operations
What about?
1 int length():
return the size of the original array
2 int elementCount() :
how many non-null values are in the array
Question
Do we forget something?
50 / 81
Images/cinvestav-
Other Operations
What about?
1 int length():
return the size of the original array
2 int elementCount() :
how many non-null values are in the array
Question
Do we forget something?
50 / 81
Images/cinvestav-
Example of Get
Code
p u b l i c Item get ( i n t index ) {
ChainNode c u r r e n t = t h i s . f i r s t N o d e ;
do {
i f ( index == c u r r e n t . index ) {
// found c o r r e c t l o c a t i o n
r e t u r n c u r r e n t . value ;
}
c u r r e n t = c u r r e n t . next ;
} w hi le ( index < c u r r e n t . index && next != n u l l ) ;
r e t u r n n u l l ;
}
51 / 81
Images/cinvestav-
Time Analysis
First
We must search a linked list for a given index
We can keep the elements in order by index
Expected Time for both get and add
If we have n elements, we have the following complexity O(n).
For the other methods
length, elementCount =⇒ O(1)
52 / 81
Images/cinvestav-
Time Analysis
First
We must search a linked list for a given index
We can keep the elements in order by index
Expected Time for both get and add
If we have n elements, we have the following complexity O(n).
For the other methods
length, elementCount =⇒ O(1)
52 / 81
Images/cinvestav-
Time Analysis
First
We must search a linked list for a given index
We can keep the elements in order by index
Expected Time for both get and add
If we have n elements, we have the following complexity O(n).
For the other methods
length, elementCount =⇒ O(1)
52 / 81
Images/cinvestav-
Problem with this analysis
True fact
In an ordinary array, indexing to find an element is the only operation we
really need!!!
True fact
In a sparse array, we can do indexing reasonably quickly
False Conclusion
In a sparse array, indexing to find an element is the only operation we
really need
The problem is that in designing the ADT, we did not think enough
about how it would be used !!!
53 / 81
Images/cinvestav-
Problem with this analysis
True fact
In an ordinary array, indexing to find an element is the only operation we
really need!!!
True fact
In a sparse array, we can do indexing reasonably quickly
False Conclusion
In a sparse array, indexing to find an element is the only operation we
really need
The problem is that in designing the ADT, we did not think enough
about how it would be used !!!
53 / 81
Images/cinvestav-
Problem with this analysis
True fact
In an ordinary array, indexing to find an element is the only operation we
really need!!!
True fact
In a sparse array, we can do indexing reasonably quickly
False Conclusion
In a sparse array, indexing to find an element is the only operation we
really need
The problem is that in designing the ADT, we did not think enough
about how it would be used !!!
53 / 81
Images/cinvestav-
Look at this code
To find the maximum element in a normal array:
double max = a r r a y [ 0 ] ;
f o r ( i n t i = 0; i < a r r a y . length ; i++)
{
i f ( a r r a y [ i ] > max) max = a r r a y [ i ] ;
}
54 / 81
Images/cinvestav-
Look at this code
To find the maximum element in a sparse array:
Double max = ( Double ) a r r a y . get ( 0 ) ;
f o r ( i n t i = 0; i < a r r a y . length ( ) ; i++)
{
Double temp = ( Double ) a r r a y . get ( i ) ;
i f ( temp . compareTo (max) > 0) {
max = temp ;
}
}
55 / 81
Images/cinvestav-
What is the problem?
First
A lot of wrapping and casting because using generics.
Second
More importantly, in a normal array, every element is relevant
Third
If a sparse array is 1% full, 99% of its elements will be zero.
This is 100 times as many elements as we should need to examine
56 / 81
Images/cinvestav-
What is the problem?
First
A lot of wrapping and casting because using generics.
Second
More importantly, in a normal array, every element is relevant
Third
If a sparse array is 1% full, 99% of its elements will be zero.
This is 100 times as many elements as we should need to examine
56 / 81
Images/cinvestav-
What is the problem?
First
A lot of wrapping and casting because using generics.
Second
More importantly, in a normal array, every element is relevant
Third
If a sparse array is 1% full, 99% of its elements will be zero.
This is 100 times as many elements as we should need to examine
56 / 81
Images/cinvestav-
What is the problem?
Fourth
Our search time is based on the size of the sparse array, not on the
number of elements that are actually in it
Question
What to do?
57 / 81
Images/cinvestav-
What is the problem?
Fourth
Our search time is based on the size of the sparse array, not on the
number of elements that are actually in it
Question
What to do?
57 / 81
Images/cinvestav-
Fixing the ADT
Something Notable
Although “stepping through an array” is not a fundamental operation on
an array, it is one we do frequently.
However
This is a very expensive thing to do with a sparse array
This should not be so expensive:
We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-
Fixing the ADT
Something Notable
Although “stepping through an array” is not a fundamental operation on
an array, it is one we do frequently.
However
This is a very expensive thing to do with a sparse array
This should not be so expensive:
We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-
Fixing the ADT
Something Notable
Although “stepping through an array” is not a fundamental operation on
an array, it is one we do frequently.
However
This is a very expensive thing to do with a sparse array
This should not be so expensive:
We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-
Fixing the ADT
Something Notable
Although “stepping through an array” is not a fundamental operation on
an array, it is one we do frequently.
However
This is a very expensive thing to do with a sparse array
This should not be so expensive:
We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-
Fixing the ADT
Poor Solution
Let the user step through the list.
The user should not need to know anything about implementation.
We cannot trust the user not to screw up the sparse array.
These arguments are valid even if the user is also the implementer!
Correct Solution
Use an Inner iterator!!!
59 / 81
Images/cinvestav-
Fixing the ADT
Poor Solution
Let the user step through the list.
The user should not need to know anything about implementation.
We cannot trust the user not to screw up the sparse array.
These arguments are valid even if the user is also the implementer!
Correct Solution
Use an Inner iterator!!!
59 / 81
Images/cinvestav-
Fixing the ADT
Poor Solution
Let the user step through the list.
The user should not need to know anything about implementation.
We cannot trust the user not to screw up the sparse array.
These arguments are valid even if the user is also the implementer!
Correct Solution
Use an Inner iterator!!!
59 / 81
Images/cinvestav-
Fixing the ADT
Poor Solution
Let the user step through the list.
The user should not need to know anything about implementation.
We cannot trust the user not to screw up the sparse array.
These arguments are valid even if the user is also the implementer!
Correct Solution
Use an Inner iterator!!!
59 / 81
Images/cinvestav-
Fixing the ADT
Poor Solution
Let the user step through the list.
The user should not need to know anything about implementation.
We cannot trust the user not to screw up the sparse array.
These arguments are valid even if the user is also the implementer!
Correct Solution
Use an Inner iterator!!!
59 / 81
Images/cinvestav-
Example
Code
public class SparseArrayIterator<Item> implements Iterator<Item> {
// any data you need to keep track of goes here
SparseArrayIterator() { ...You do need one... }
public boolean hasNext ( ) { ...you write this code... }
public Item next ( ) { ...you write this code... }
public Item remove ( ) { ...you write this code... }
}
60 / 81
Images/cinvestav-
So, we have that
Code for Max
S p a r s e A r r a y I t e r a t o r i t e r a t o r = new S p a r s e A r r a y I t e r a t o r ( a r r a y ) ;
Double max = ( Double ) a r r a y . get ( 0 ) ;
wh il e ( i t e r a t o r . hasNext ( ) ) {
temp = ( Double ) i t e r a t o r . next ( ) ;
i f ( temp . compareTo (max) > 0) {
max = temp ;
}
}
61 / 81
Images/cinvestav-
However
Problem
Our SparseArrayIterator is fine for stepping through the elements of
an array, but...
It does not tell us what index they were at.
For some problems, we may need this information
First Solution
Revise our iterator to tell us, not the value in each list cell, but the index
in each list cell
Problem
Somewhat more awkward to use, since we would need
array.get(iterator.next()) instead of just iterator.next().
But it’s worse than that, because next is defined to return an Item, so
we would have to wrap the index.
We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-
However
Problem
Our SparseArrayIterator is fine for stepping through the elements of
an array, but...
It does not tell us what index they were at.
For some problems, we may need this information
First Solution
Revise our iterator to tell us, not the value in each list cell, but the index
in each list cell
Problem
Somewhat more awkward to use, since we would need
array.get(iterator.next()) instead of just iterator.next().
But it’s worse than that, because next is defined to return an Item, so
we would have to wrap the index.
We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-
However
Problem
Our SparseArrayIterator is fine for stepping through the elements of
an array, but...
It does not tell us what index they were at.
For some problems, we may need this information
First Solution
Revise our iterator to tell us, not the value in each list cell, but the index
in each list cell
Problem
Somewhat more awkward to use, since we would need
array.get(iterator.next()) instead of just iterator.next().
But it’s worse than that, because next is defined to return an Item, so
we would have to wrap the index.
We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-
However
Problem
Our SparseArrayIterator is fine for stepping through the elements of
an array, but...
It does not tell us what index they were at.
For some problems, we may need this information
First Solution
Revise our iterator to tell us, not the value in each list cell, but the index
in each list cell
Problem
Somewhat more awkward to use, since we would need
array.get(iterator.next()) instead of just iterator.next().
But it’s worse than that, because next is defined to return an Item, so
we would have to wrap the index.
We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-
However
Problem
Our SparseArrayIterator is fine for stepping through the elements of
an array, but...
It does not tell us what index they were at.
For some problems, we may need this information
First Solution
Revise our iterator to tell us, not the value in each list cell, but the index
in each list cell
Problem
Somewhat more awkward to use, since we would need
array.get(iterator.next()) instead of just iterator.next().
But it’s worse than that, because next is defined to return an Item, so
we would have to wrap the index.
We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-
However
Problem
Our SparseArrayIterator is fine for stepping through the elements of
an array, but...
It does not tell us what index they were at.
For some problems, we may need this information
First Solution
Revise our iterator to tell us, not the value in each list cell, but the index
in each list cell
Problem
Somewhat more awkward to use, since we would need
array.get(iterator.next()) instead of just iterator.next().
But it’s worse than that, because next is defined to return an Item, so
we would have to wrap the index.
We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-
However
Problem
Our SparseArrayIterator is fine for stepping through the elements of
an array, but...
It does not tell us what index they were at.
For some problems, we may need this information
First Solution
Revise our iterator to tell us, not the value in each list cell, but the index
in each list cell
Problem
Somewhat more awkward to use, since we would need
array.get(iterator.next()) instead of just iterator.next().
But it’s worse than that, because next is defined to return an Item, so
we would have to wrap the index.
We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-
Instead of that use an IndexIterator Too
Code
p u b l i c c l a s s I n d e x I t e r a t o r
{
p r i v a t e ChainNode c u r r e n t ;
I n d e x I t e r a t o r () { // c o n s t r u c t o r
c u r r e n t = f i r s t N o d e ;
}
p u b l i c boolean hasNext ()
{ // j u s t l i k e b e f o r e }
p u b l i c i n t next () {
i n t index = c u r r e n t . index ;
c u r r e n t = c u r r e n t . next ;
r e t u r n index ;
}
}
63 / 81
Images/cinvestav-
Now, Sparse Matrices
First
Something Simple!!
64 / 81
Images/cinvestav-
Using Array Linear List for Matrices
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








Thus
list =
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 6 2 6
65 / 81
Images/cinvestav-
Using Array Linear List for Matrices
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








Thus
list =
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 6 2 6
65 / 81
Images/cinvestav-
Now, How do we represent this list using arrays?
Why?
We want compact representations...
Use your friend element from arrays
Thus you declare an array element with the Node information
Node element[] = new Node[1000];
66 / 81
Images/cinvestav-
Now, How do we represent this list using arrays?
Why?
We want compact representations...
Use your friend element from arrays
package d a t a S t r u c t u r e s ;
// Using Generic in Java
p u b l i c c l a s s Node<Item>{
// elements
p r i v a t e i n t row ;
p r i v a t e i n t column
p r i v a t e Item value ;
// c o n s t r u c t o r s come here
}
Thus you declare an array element with the Node information
Node element[] = new Node[1000];
66 / 81
Images/cinvestav-
Now, How do we represent this list using arrays?
Why?
We want compact representations...
Use your friend element from arrays
package d a t a S t r u c t u r e s ;
// Using Generic in Java
p u b l i c c l a s s Node<Item>{
// elements
p r i v a t e i n t row ;
p r i v a t e i n t column
p r i v a t e Item value ;
// c o n s t r u c t o r s come here
}
Thus you declare an array element with the Node information
Node element[] = new Node[1000];
66 / 81
Images/cinvestav-
What about other representations?
We can use chains too
We need to modify our node
To something like this
Graphically
67 / 81
Images/cinvestav-
What about other representations?
We can use chains too
We need to modify our node
To something like this
package d a t a S t r u c t u r e s ;
// Using Generic i n Java
p u b l i c c l a s s Node<Item >{
// elements
p r i v a t e i n t row ;
p r i v a t e i n t column ;
p r i v a t e Item v a l u e ;
p r i v a t e Node next ;
// c o n s t r u c t o r s come here
}
Graphically
67 / 81
Images/cinvestav-
What about other representations?
We can use chains too
We need to modify our node
To something like this
package d a t a S t r u c t u r e s ;
// Using Generic i n Java
p u b l i c c l a s s Node<Item >{
// elements
p r i v a t e i n t row ;
p r i v a t e i n t column ;
p r i v a t e Item v a l u e ;
p r i v a t e Node next ;
// c o n s t r u c t o r s come here
}
Graphically
67 / 81
Images/cinvestav-
We have then
Example
68 / 81
Images/cinvestav-
One Linear List Per Row
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








Thus
row1 = [(3,3) (5,4)]
row2 = [(3,5) (4,7)]
row3 = []
row4 = [(2,2) (3,6)]
Node Structure
69 / 81
Images/cinvestav-
One Linear List Per Row
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








Thus
row1 = [(3,3) (5,4)]
row2 = [(3,5) (4,7)]
row3 = []
row4 = [(2,2) (3,6)]
Node Structure
69 / 81
Images/cinvestav-
One Linear List Per Row
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








Thus
row1 = [(3,3) (5,4)]
row2 = [(3,5) (4,7)]
row3 = []
row4 = [(2,2) (3,6)]
Node Structure
69 / 81
Images/cinvestav-
Array Of Row Chains
Example
70 / 81
Images/cinvestav-
Compress the row
Use a sparse array!!!
null
null
null
null
null
null
0
1
2
3
4
5
6
7
8
9
0
2
5
8
71 / 81
Images/cinvestav-
We have a problem here
With 99% of the matrix as zeros of nulls
We have the problem that many row pointers are null...
What can you do?
Ideas
72 / 81
Images/cinvestav-
We have a problem here
With 99% of the matrix as zeros of nulls
We have the problem that many row pointers are null...
What can you do?
Ideas
72 / 81
Images/cinvestav-
However
Problems, problems Will Robinson!!!
Ideas?
Yes, access!!
We have a problem there!!
It is
Not so fast!!!! SOLUTIONS???
73 / 81
Images/cinvestav-
However
Problems, problems Will Robinson!!!
Ideas?
Yes, access!!
We have a problem there!!
It is
Not so fast!!!! SOLUTIONS???
73 / 81
Images/cinvestav-
However
Problems, problems Will Robinson!!!
Ideas?
Yes, access!!
We have a problem there!!
It is
Not so fast!!!! SOLUTIONS???
73 / 81
Images/cinvestav-
Orthogonal List Representation
More complexity
Both row and column lists.
Node Structure
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








74 / 81
Images/cinvestav-
Orthogonal List Representation
More complexity
Both row and column lists.
Node Structure
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








74 / 81
Images/cinvestav-
Orthogonal List Representation
More complexity
Both row and column lists.
Node Structure
Example








0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0








74 / 81
Images/cinvestav-
Row Lists
Row direction
75 / 81
Images/cinvestav-
Column Lists
Column Direction
76 / 81
Images/cinvestav-
Orthogonal Lists
All Together
77 / 81
Images/cinvestav-
Go One Step Further
Question
We have another problem the sparse arrays in the column and row
pointers!!!
Thus
How it will look like for this (You can try!!!)







0 1 0 0 2
0 0 0 0 0
0 0 2 0 0
0 0 0 0 0
0 0 1 0 1







(3)
78 / 81
Images/cinvestav-
Go One Step Further
Question
We have another problem the sparse arrays in the column and row
pointers!!!
Thus
How it will look like for this (You can try!!!)







0 1 0 0 2
0 0 0 0 0
0 0 2 0 0
0 0 0 0 0
0 0 1 0 1







(3)
78 / 81
Images/cinvestav-
Do not worry!!!
You have MANY VARIATIONS!!!
79 / 81
Images/cinvestav-
Approximate Memory Requirements
Example
500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytes
Single Array List 3 x 1994 x 4 = 23,928 bytes
One Chain Per Row 23928 + 500 x 4 = 25,928
What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-
Approximate Memory Requirements
Example
500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytes
Single Array List 3 x 1994 x 4 = 23,928 bytes
One Chain Per Row 23928 + 500 x 4 = 25,928
What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-
Approximate Memory Requirements
Example
500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytes
Single Array List 3 x 1994 x 4 = 23,928 bytes
One Chain Per Row 23928 + 500 x 4 = 25,928
What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-
Approximate Memory Requirements
Example
500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytes
Single Array List 3 x 1994 x 4 = 23,928 bytes
One Chain Per Row 23928 + 500 x 4 = 25,928
What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-
Approximate Memory Requirements
Example
500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytes
Single Array List 3 x 1994 x 4 = 23,928 bytes
One Chain Per Row 23928 + 500 x 4 = 25,928
What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-
Runtime Performance
Example Matrix Transpose
500 x 500 matrix with 1994 nonzero elements:
Method Time
2D Array 210 ms
Single Array List 6 ms
One Chain per Row 12 ms
Example Matrix Addition
500 x 500 matrix with 1994 nonzero elements:
Method Time
2D Array 880 ms
Single Array List 18 ms
One Chain per Row 29 ms
81 / 81
Images/cinvestav-
Runtime Performance
Example Matrix Transpose
500 x 500 matrix with 1994 nonzero elements:
Method Time
2D Array 210 ms
Single Array List 6 ms
One Chain per Row 12 ms
Example Matrix Addition
500 x 500 matrix with 1994 nonzero elements:
Method Time
2D Array 880 ms
Single Array List 18 ms
One Chain per Row 29 ms
81 / 81

Preparation Data Structures 06 arrays representation

  • 1.
    Data Structures Array Representation AndresMendez-Vazquez May 6, 2015 1 / 81
  • 2.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 2 / 81
  • 3.
    Images/cinvestav- Introduction Observation When a programmanipulates many variables that contain “similar” forms of data, organizational problems quickly arise. Example When a program manipulates many variables that contain “similar” forms of data, organizational problems quickly arise. We could use a name variable for each data double score0; double score1; double score2; double score3; double score4; double score5; 3 / 81
  • 4.
    Images/cinvestav- Introduction Observation When a programmanipulates many variables that contain “similar” forms of data, organizational problems quickly arise. Example When a program manipulates many variables that contain “similar” forms of data, organizational problems quickly arise. We could use a name variable for each data double score0; double score1; double score2; double score3; double score4; double score5; 3 / 81
  • 5.
    Images/cinvestav- Introduction Observation When a programmanipulates many variables that contain “similar” forms of data, organizational problems quickly arise. Example When a program manipulates many variables that contain “similar” forms of data, organizational problems quickly arise. We could use a name variable for each data double score0; double score1; double score2; double score3; double score4; double score5; 3 / 81
  • 6.
    Images/cinvestav- Making your lifemiserable Because you can ask Which is the highest score? Look at the code 4 / 81
  • 7.
    Images/cinvestav- Making your lifemiserable Because you can ask Which is the highest score? Look at the code double high_score = score0 ; i f ( score1 > high_score ) { high_score = score1 ; } i f ( score2 > high_score ) { high_score = score2 ; } i f ( score3 > high_score ) { high_score = score3 ; } i f ( score4 > high_score ) { high_score = score4 ; } i f ( score5 > high_score ) { high_score = score5 ; } System . out . p r i n t l n ( high_score ) ; 4 / 81
  • 8.
    Images/cinvestav- How do wesolve this? Thus Java, C and C++ use array variables to put together this collection of equal elements. Memory Layout 5 / 81
  • 9.
    Images/cinvestav- How do wesolve this? Thus Java, C and C++ use array variables to put together this collection of equal elements. Memory Layout a b c d START MEMORY LAYOUT 5 / 81
  • 10.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 6 / 81
  • 11.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 12.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 13.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 14.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 15.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 16.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 17.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 18.
    Images/cinvestav- 1D Array RepresentationIn Java, C, and C++ Notes For 1-dimensional array: The elements are mapped into contiguous memory locations They are accessed through the use of location (x [i]) = start + i How much memory is used for representing an array? 1 Space Overhead 1 Storing the start address: 4 bytes 2 Storing x.length: 4 bytes 2 Space for the elements, for example 4 bytes for n elements Total: 4 + 4 + 4n = 8 + 4n bytes 7 / 81
  • 19.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 8 / 81
  • 20.
    Images/cinvestav- Now, 2D Arrays Wedeclare 2-dimensional arrays as follow (Java, C) int[][] A = new int[3][4] It can be shown as a table A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] A[2][0] A[2][1] A[2][2] A[2][3] 9 / 81
  • 21.
    Images/cinvestav- Now, 2D Arrays Wedeclare 2-dimensional arrays as follow (Java, C) int[][] A = new int[3][4] It can be shown as a table A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] A[2][0] A[2][1] A[2][2] A[2][3] 9 / 81
  • 22.
    Images/cinvestav- Rows and Columnsin a 2-D Array Rows A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] A[2][0] A[2][1] A[2][2] A[2][3] Columns 10 / 81
  • 23.
    Images/cinvestav- Rows and Columnsin a 2-D Array Rows A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] A[2][0] A[2][1] A[2][2] A[2][3] Columns A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] A[2][0] A[2][1] A[2][2] A[2][3] 10 / 81
  • 24.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 11 / 81
  • 25.
    Images/cinvestav- 2D Array RepresentationIn Java, C, and C++ Given the following 2-dimensional array x x =        a b c d e f g h i j k l        This information is stored as 1D arrays with a reference to them 12 / 81
  • 26.
    Images/cinvestav- 2D Array RepresentationIn Java, C, and C++ Given the following 2-dimensional array x x =        a b c d e f g h i j k l        This information is stored as 1D arrays with a reference to them 12 / 81
  • 27.
    Images/cinvestav- Some Properties If wecall the lengths of each of them x.length ⇒ 3 x[0].length == x[1].length == x[2].length ⇒ 3 Space Overhead Remember, it is 8 bytes per 1D array 1 Thus, we have 4 × 8 = 32 bytes 2 Or we can see this as (number of rows + 1) × 8 bytes 13 / 81
  • 28.
    Images/cinvestav- Some Properties If wecall the lengths of each of them x.length ⇒ 3 x[0].length == x[1].length == x[2].length ⇒ 3 Space Overhead Remember, it is 8 bytes per 1D array 1 Thus, we have 4 × 8 = 32 bytes 2 Or we can see this as (number of rows + 1) × 8 bytes 13 / 81
  • 29.
    Images/cinvestav- Some Properties If wecall the lengths of each of them x.length ⇒ 3 x[0].length == x[1].length == x[2].length ⇒ 3 Space Overhead Remember, it is 8 bytes per 1D array 1 Thus, we have 4 × 8 = 32 bytes 2 Or we can see this as (number of rows + 1) × 8 bytes 13 / 81
  • 30.
    Images/cinvestav- Some Properties If wecall the lengths of each of them x.length ⇒ 3 x[0].length == x[1].length == x[2].length ⇒ 3 Space Overhead Remember, it is 8 bytes per 1D array 1 Thus, we have 4 × 8 = 32 bytes 2 Or we can see this as (number of rows + 1) × 8 bytes 13 / 81
  • 31.
    Images/cinvestav- Some Properties If wecall the lengths of each of them x.length ⇒ 3 x[0].length == x[1].length == x[2].length ⇒ 3 Space Overhead Remember, it is 8 bytes per 1D array 1 Thus, we have 4 × 8 = 32 bytes 2 Or we can see this as (number of rows + 1) × 8 bytes 13 / 81
  • 32.
    Images/cinvestav- More Properties First This representationis called the array-of-arrays representation. Second It requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4 bytes for the 4 1D-arrays. Third One memory block of size number of rows and number of rows blocks of size number of columns. 14 / 81
  • 33.
    Images/cinvestav- More Properties First This representationis called the array-of-arrays representation. Second It requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4 bytes for the 4 1D-arrays. Third One memory block of size number of rows and number of rows blocks of size number of columns. 14 / 81
  • 34.
    Images/cinvestav- More Properties First This representationis called the array-of-arrays representation. Second It requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4 bytes for the 4 1D-arrays. Third One memory block of size number of rows and number of rows blocks of size number of columns. 14 / 81
  • 35.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 15 / 81
  • 36.
    Images/cinvestav- Row-Major Mapping Again x =        ab c d e f g h i j k l        For this We will convert it into 1D-array y by collecting elements by rows. Thus Within a row elements are collected from left to right. Rows are collected from top to bottom. 16 / 81
  • 37.
    Images/cinvestav- Row-Major Mapping Again x =        ab c d e f g h i j k l        For this We will convert it into 1D-array y by collecting elements by rows. Thus Within a row elements are collected from left to right. Rows are collected from top to bottom. 16 / 81
  • 38.
    Images/cinvestav- Row-Major Mapping Again x =        ab c d e f g h i j k l        For this We will convert it into 1D-array y by collecting elements by rows. Thus Within a row elements are collected from left to right. Rows are collected from top to bottom. 16 / 81
  • 39.
    Images/cinvestav- We get An array y[]= {a, b, c, d, e, f, g, h, i, j, k, l} Memory Map 17 / 81
  • 40.
    Images/cinvestav- We get An array y[]= {a, b, c, d, e, f, g, h, i, j, k, l} Memory Map 17 / 81
  • 41.
    Images/cinvestav- How do welocate an element? Look At This Then Assume x has r rows and c columns. Each row has c elements. i rows to the left of row i. Thus We have ic elements to the left of x[i][0]. Then, x[i][j] is mapped to position ic + j of 1D array. 18 / 81
  • 42.
    Images/cinvestav- How do welocate an element? Look At This Then Assume x has r rows and c columns. Each row has c elements. i rows to the left of row i. Thus We have ic elements to the left of x[i][0]. Then, x[i][j] is mapped to position ic + j of 1D array. 18 / 81
  • 43.
    Images/cinvestav- How do welocate an element? Look At This Then Assume x has r rows and c columns. Each row has c elements. i rows to the left of row i. Thus We have ic elements to the left of x[i][0]. Then, x[i][j] is mapped to position ic + j of 1D array. 18 / 81
  • 44.
    Images/cinvestav- How do welocate an element? Look At This Then Assume x has r rows and c columns. Each row has c elements. i rows to the left of row i. Thus We have ic elements to the left of x[i][0]. Then, x[i][j] is mapped to position ic + j of 1D array. 18 / 81
  • 45.
    Images/cinvestav- How do welocate an element? Look At This Then Assume x has r rows and c columns. Each row has c elements. i rows to the left of row i. Thus We have ic elements to the left of x[i][0]. Then, x[i][j] is mapped to position ic + j of 1D array. 18 / 81
  • 46.
    Images/cinvestav- How do welocate an element? Look At This Then Assume x has r rows and c columns. Each row has c elements. i rows to the left of row i. Thus We have ic elements to the left of x[i][0]. Then, x[i][j] is mapped to position ic + j of 1D array. 18 / 81
  • 47.
    Images/cinvestav- Space Overhead We have 4bytes for start of 1D array. 4 bytes for length of 1D array. 4 bytes for c (number of columns) Total: 12 bytes Note: The number of rows = length/c Still Disadvantage: Need contiguous memory of size rc. 19 / 81
  • 48.
    Images/cinvestav- Space Overhead We have 4bytes for start of 1D array. 4 bytes for length of 1D array. 4 bytes for c (number of columns) Total: 12 bytes Note: The number of rows = length/c Still Disadvantage: Need contiguous memory of size rc. 19 / 81
  • 49.
    Images/cinvestav- Space Overhead We have 4bytes for start of 1D array. 4 bytes for length of 1D array. 4 bytes for c (number of columns) Total: 12 bytes Note: The number of rows = length/c Still Disadvantage: Need contiguous memory of size rc. 19 / 81
  • 50.
    Images/cinvestav- Space Overhead We have 4bytes for start of 1D array. 4 bytes for length of 1D array. 4 bytes for c (number of columns) Total: 12 bytes Note: The number of rows = length/c Still Disadvantage: Need contiguous memory of size rc. 19 / 81
  • 51.
    Images/cinvestav- Space Overhead We have 4bytes for start of 1D array. 4 bytes for length of 1D array. 4 bytes for c (number of columns) Total: 12 bytes Note: The number of rows = length/c Still Disadvantage: Need contiguous memory of size rc. 19 / 81
  • 52.
    Images/cinvestav- Space Overhead We have 4bytes for start of 1D array. 4 bytes for length of 1D array. 4 bytes for c (number of columns) Total: 12 bytes Note: The number of rows = length/c Still Disadvantage: Need contiguous memory of size rc. 19 / 81
  • 53.
    Images/cinvestav- Column-Major Mapping Similar Setup Convertinto 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. Thus x =        a b c d e f g h i j k l        We get y = {a, e, i, b, f, j, c, g, k, d, h, l} 20 / 81
  • 54.
    Images/cinvestav- Column-Major Mapping Similar Setup Convertinto 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. Thus x =        a b c d e f g h i j k l        We get y = {a, e, i, b, f, j, c, g, k, d, h, l} 20 / 81
  • 55.
    Images/cinvestav- Column-Major Mapping Similar Setup Convertinto 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. Thus x =        a b c d e f g h i j k l        We get y = {a, e, i, b, f, j, c, g, k, d, h, l} 20 / 81
  • 56.
    Images/cinvestav- Column-Major Mapping Similar Setup Convertinto 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. Thus x =        a b c d e f g h i j k l        We get y = {a, e, i, b, f, j, c, g, k, d, h, l} 20 / 81
  • 57.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 21 / 81
  • 58.
    Images/cinvestav- Matrix Something Notable Table ofvalues. It has rows and columns, but numbering begins at 1 rather than 0. We use the following notation Use notation x(i,j) rather than x[i][j]. Note It may use a 2D array to represent a matrix. 22 / 81
  • 59.
    Images/cinvestav- Matrix Something Notable Table ofvalues. It has rows and columns, but numbering begins at 1 rather than 0. We use the following notation Use notation x(i,j) rather than x[i][j]. Note It may use a 2D array to represent a matrix. 22 / 81
  • 60.
    Images/cinvestav- Matrix Something Notable Table ofvalues. It has rows and columns, but numbering begins at 1 rather than 0. We use the following notation Use notation x(i,j) rather than x[i][j]. Note It may use a 2D array to represent a matrix. 22 / 81
  • 61.
    Images/cinvestav- Drawbacks of usinga 2D Array First Indexes are off by 1. Second Java arrays do not support matrix operations such as add, transpose, multiply, and so on. However You can develop a class Matrix for object-oriented support of all matrix operations. 23 / 81
  • 62.
    Images/cinvestav- Drawbacks of usinga 2D Array First Indexes are off by 1. Second Java arrays do not support matrix operations such as add, transpose, multiply, and so on. However You can develop a class Matrix for object-oriented support of all matrix operations. 23 / 81
  • 63.
    Images/cinvestav- Drawbacks of usinga 2D Array First Indexes are off by 1. Second Java arrays do not support matrix operations such as add, transpose, multiply, and so on. However You can develop a class Matrix for object-oriented support of all matrix operations. 23 / 81
  • 64.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 24 / 81
  • 65.
    Images/cinvestav- Some Basic Matrices:Diagonal Matrix Diagonal Matrix An n × n matrix in which all nonzero terms are on the diagonal. Properties x(i,j) is on diagonal iff i = j Example x =           a 0 0 0 0 b 0 0 0 0 c 0 0 0 0 d           25 / 81
  • 66.
    Images/cinvestav- Some Basic Matrices:Diagonal Matrix Diagonal Matrix An n × n matrix in which all nonzero terms are on the diagonal. Properties x(i,j) is on diagonal iff i = j Example x =           a 0 0 0 0 b 0 0 0 0 c 0 0 0 0 d           25 / 81
  • 67.
    Images/cinvestav- Some Basic Matrices:Diagonal Matrix Diagonal Matrix An n × n matrix in which all nonzero terms are on the diagonal. Properties x(i,j) is on diagonal iff i = j Example x =           a 0 0 0 0 b 0 0 0 0 c 0 0 0 0 d           25 / 81
  • 68.
    Images/cinvestav- Some Basic Matrices:Diagonal Matrix Properties x(i,j) is on diagonal iff i = j DRAWBACK For a n × n you are required to have: For example using integers: 4n2 bytes... What to do? Store diagonal only vs n2 whole 26 / 81
  • 69.
    Images/cinvestav- Some Basic Matrices:Diagonal Matrix Properties x(i,j) is on diagonal iff i = j DRAWBACK For a n × n you are required to have: For example using integers: 4n2 bytes... What to do? Store diagonal only vs n2 whole 26 / 81
  • 70.
    Images/cinvestav- Some Basic Matrices:Diagonal Matrix Properties x(i,j) is on diagonal iff i = j DRAWBACK For a n × n you are required to have: For example using integers: 4n2 bytes... What to do? Store diagonal only vs n2 whole 26 / 81
  • 71.
    Images/cinvestav- Some Basic Matrices:Lower Triangular Matrix Definition An n × n matrix in which all nonzero terms are either on or below the diagonal. Example x =           1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10           27 / 81
  • 72.
    Images/cinvestav- Some Basic Matrices:Lower Triangular Matrix Definition An n × n matrix in which all nonzero terms are either on or below the diagonal. Example x =           1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10           27 / 81
  • 73.
    Images/cinvestav- Some Basic Matrices:Lower Triangular Matrix Properties x(i,j) is part of lower triangle iff i >= j. Number of elements in lower triangle is 1 + 2 + 3 + ... + n = n (n + 1) 2 (1) Thus You can store only the lower triangle 28 / 81
  • 74.
    Images/cinvestav- Some Basic Matrices:Lower Triangular Matrix Properties x(i,j) is part of lower triangle iff i >= j. Number of elements in lower triangle is 1 + 2 + 3 + ... + n = n (n + 1) 2 (1) Thus You can store only the lower triangle 28 / 81
  • 75.
    Images/cinvestav- Some Basic Matrices:Lower Triangular Matrix Properties x(i,j) is part of lower triangle iff i >= j. Number of elements in lower triangle is 1 + 2 + 3 + ... + n = n (n + 1) 2 (1) Thus You can store only the lower triangle 28 / 81
  • 76.
    Images/cinvestav- Array Of ArraysRepresentation We can use this Something Notable You can use an irregular 2-D array ... length of rows is not required to be the same. 29 / 81
  • 77.
    Images/cinvestav- Array Of ArraysRepresentation We can use this Something Notable You can use an irregular 2-D array ... length of rows is not required to be the same. 29 / 81
  • 78.
    Images/cinvestav- Code Code for irregulararray // d e c l a r e a two−dimensional a r r a y v a r i a b l e // and a l l o c a t e the d e s i r e d number of rows i n t [ ] [ ] i r r e g u l a r A r r a y = new i n t [ numberOfRows ] [ ] ; // now a l l o c a t e space f o r the elements i n each row f o r ( i n t i = 0; i < numberOfRows ; i++) i r r e g u l a r A r r a y [ i ] = new i n t [ s i z e [ i ] ] ; // use the a r r a y l i k e any r e g u l a r a r r a y i r r e g u l a r A r r a y [ 2 ] [ 3 ] = 5; i r r e g u l a r A r r a y [ 4 ] [ 6 ] = i r r e g u l a r A r r a y [ 2 ] [ 3 ] + 2; i r r e g u l a r A r r a y [ 1 ] [ 1 ] += 3; 30 / 81
  • 79.
    Images/cinvestav- However, You cando better Map Lower Triangular Array Into A 1D Array You can use a row-major order, but omit terms that are not part of the lower triangle. For example x =           1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10           We get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 31 / 81
  • 80.
    Images/cinvestav- However, You cando better Map Lower Triangular Array Into A 1D Array You can use a row-major order, but omit terms that are not part of the lower triangle. For example x =           1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10           We get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 31 / 81
  • 81.
    Images/cinvestav- However, You cando better Map Lower Triangular Array Into A 1D Array You can use a row-major order, but omit terms that are not part of the lower triangle. For example x =           1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10           We get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 31 / 81
  • 82.
    Images/cinvestav- The final mapping Wewant We have that 1 Order is: row 1, row 2, row 3, ... 2 Row i is preceded by rows 1, 2, ..., i-1 3 Size of row i is i. 32 / 81
  • 83.
    Images/cinvestav- The final mapping Wewant We have that 1 Order is: row 1, row 2, row 3, ... 2 Row i is preceded by rows 1, 2, ..., i-1 3 Size of row i is i. 32 / 81
  • 84.
    Images/cinvestav- The final mapping Wewant We have that 1 Order is: row 1, row 2, row 3, ... 2 Row i is preceded by rows 1, 2, ..., i-1 3 Size of row i is i. 32 / 81
  • 85.
    Images/cinvestav- The final mapping Wewant We have that 1 Order is: row 1, row 2, row 3, ... 2 Row i is preceded by rows 1, 2, ..., i-1 3 Size of row i is i. 32 / 81
  • 86.
    Images/cinvestav- More Find the totalnumber of elements before row i 1 + 2 + 3 + ... + i − 1 = i (i − 1) 2 (2) Thus So element (i,j) is at position i(i-1)/2 + j -1 of the 1D array. 33 / 81
  • 87.
    Images/cinvestav- More Find the totalnumber of elements before row i 1 + 2 + 3 + ... + i − 1 = i (i − 1) 2 (2) Thus So element (i,j) is at position i(i-1)/2 + j -1 of the 1D array. 33 / 81
  • 88.
    Images/cinvestav- Now the InterfaceMatrix You will need this for your homework p u b l i c i n t e r f a c e Matrix<Item >{ p u b l i c Item get ( i n t row , i n t column ) ; p u b l i c void add ( i n t row , i n t column , Item myobj ) ; p u b l i c Item remove ( i n t row , i n t column ) ; p u b l i c void output ( ) ; } 34 / 81
  • 89.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 35 / 81
  • 90.
    Images/cinvestav- Sparse Matrices Definition A sparsearray is simply an array most of whose entries are zero (or null, or some other default value). For Example Suppose you wanted a 2-dimensional array of course grades, whose rows are students and whose columns are courses. Properties There are about 90,173 students There are about 5000 courses This array would have about 450,865,000 entries 36 / 81
  • 91.
    Images/cinvestav- Sparse Matrices Definition A sparsearray is simply an array most of whose entries are zero (or null, or some other default value). For Example Suppose you wanted a 2-dimensional array of course grades, whose rows are students and whose columns are courses. Properties There are about 90,173 students There are about 5000 courses This array would have about 450,865,000 entries 36 / 81
  • 92.
    Images/cinvestav- Sparse Matrices Definition A sparsearray is simply an array most of whose entries are zero (or null, or some other default value). For Example Suppose you wanted a 2-dimensional array of course grades, whose rows are students and whose columns are courses. Properties There are about 90,173 students There are about 5000 courses This array would have about 450,865,000 entries 36 / 81
  • 93.
    Images/cinvestav- Sparse Matrices Definition A sparsearray is simply an array most of whose entries are zero (or null, or some other default value). For Example Suppose you wanted a 2-dimensional array of course grades, whose rows are students and whose columns are courses. Properties There are about 90,173 students There are about 5000 courses This array would have about 450,865,000 entries 36 / 81
  • 94.
    Images/cinvestav- Thus Something Notable Since moststudents take fewer than 5000 courses, there will be a lot of empty spaces in this array. Something Notable This is a big array, even by modern standards. 37 / 81
  • 95.
    Images/cinvestav- Thus Something Notable Since moststudents take fewer than 5000 courses, there will be a lot of empty spaces in this array. Something Notable This is a big array, even by modern standards. 37 / 81
  • 96.
    Images/cinvestav- Example: Airline Flights Example Airlineflight matrix. Properties Airports are numbered 1 through n flight(i,j) = list of nonstop flights from airport i to airport j Assume n = 1000 and you use an integer for representation n × n array of list references =⇒ 4 million bytes when all the airports are connected 38 / 81
  • 97.
    Images/cinvestav- Example: Airline Flights Example Airlineflight matrix. Properties Airports are numbered 1 through n flight(i,j) = list of nonstop flights from airport i to airport j Assume n = 1000 and you use an integer for representation n × n array of list references =⇒ 4 million bytes when all the airports are connected 38 / 81
  • 98.
    Images/cinvestav- Example: Airline Flights Example Airlineflight matrix. Properties Airports are numbered 1 through n flight(i,j) = list of nonstop flights from airport i to airport j Assume n = 1000 and you use an integer for representation n × n array of list references =⇒ 4 million bytes when all the airports are connected 38 / 81
  • 99.
    Images/cinvestav- Example: Airline Flights Example Airlineflight matrix. Properties Airports are numbered 1 through n flight(i,j) = list of nonstop flights from airport i to airport j Assume n = 1000 and you use an integer for representation n × n array of list references =⇒ 4 million bytes when all the airports are connected 38 / 81
  • 100.
    Images/cinvestav- Example: Airline Flights However Thetotal number of flights is way lesser =⇒ 20,000 Needed Storage We need at most 20,000 list references =⇒ Thus, we need at most 80,000 bytes 39 / 81
  • 101.
    Images/cinvestav- Example: Airline Flights However Thetotal number of flights is way lesser =⇒ 20,000 Needed Storage We need at most 20,000 list references =⇒ Thus, we need at most 80,000 bytes 39 / 81
  • 102.
    Images/cinvestav- Web Page Matrix Webpage 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. 40 / 81
  • 103.
    Images/cinvestav- Web Page Matrix Webpage 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. 40 / 81
  • 104.
    Images/cinvestav- Web Page Matrix Webpage 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. 40 / 81
  • 105.
    Images/cinvestav- Web Page Matrix Webpage 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. 40 / 81
  • 106.
    Images/cinvestav- Web Page Matrix SomethingNotable n= 2,000,000,000 (and growing by 1 million a day) If we used integers for representation n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB) Properties Each page links to 10 (say) other pages on average. On average there are 10 nonzero entries per row. Space needed for non-zero elements is approximately 20,000,000,000 x 4 bytes = 80,000,000,000 bytes (80 GB) 41 / 81
  • 107.
    Images/cinvestav- Web Page Matrix SomethingNotable n= 2,000,000,000 (and growing by 1 million a day) If we used integers for representation n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB) Properties Each page links to 10 (say) other pages on average. On average there are 10 nonzero entries per row. Space needed for non-zero elements is approximately 20,000,000,000 x 4 bytes = 80,000,000,000 bytes (80 GB) 41 / 81
  • 108.
    Images/cinvestav- Web Page Matrix SomethingNotable n= 2,000,000,000 (and growing by 1 million a day) If we used integers for representation n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB) Properties Each page links to 10 (say) other pages on average. On average there are 10 nonzero entries per row. Space needed for non-zero elements is approximately 20,000,000,000 x 4 bytes = 80,000,000,000 bytes (80 GB) 41 / 81
  • 109.
    Images/cinvestav- Web Page Matrix SomethingNotable n= 2,000,000,000 (and growing by 1 million a day) If we used integers for representation n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB) Properties Each page links to 10 (say) other pages on average. On average there are 10 nonzero entries per row. Space needed for non-zero elements is approximately 20,000,000,000 x 4 bytes = 80,000,000,000 bytes (80 GB) 41 / 81
  • 110.
    Images/cinvestav- Web Page Matrix SomethingNotable n= 2,000,000,000 (and growing by 1 million a day) If we used integers for representation n × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB) Properties Each page links to 10 (say) other pages on average. On average there are 10 nonzero entries per row. Space needed for non-zero elements is approximately 20,000,000,000 x 4 bytes = 80,000,000,000 bytes (80 GB) 41 / 81
  • 111.
    Images/cinvestav- What we need... Weneed... There are ways to represent sparse arrays efficiently 42 / 81
  • 112.
    Images/cinvestav- Sparse Matrices Definition Sparse ...many elements are zero Dense ... few elements are zero Examples of structured sparse matrices Diagonal Tridiagonal Lower triangular (Actually in the Middle of Sparse and Dense) Actually They may be mapped into a 1D array so that a mapping function can be used to locate an element. 43 / 81
  • 113.
    Images/cinvestav- Sparse Matrices Definition Sparse ...many elements are zero Dense ... few elements are zero Examples of structured sparse matrices Diagonal Tridiagonal Lower triangular (Actually in the Middle of Sparse and Dense) Actually They may be mapped into a 1D array so that a mapping function can be used to locate an element. 43 / 81
  • 114.
    Images/cinvestav- Sparse Matrices Definition Sparse ...many elements are zero Dense ... few elements are zero Examples of structured sparse matrices Diagonal Tridiagonal Lower triangular (Actually in the Middle of Sparse and Dense) Actually They may be mapped into a 1D array so that a mapping function can be used to locate an element. 43 / 81
  • 115.
    Images/cinvestav- Sparse Matrices Definition Sparse ...many elements are zero Dense ... few elements are zero Examples of structured sparse matrices Diagonal Tridiagonal Lower triangular (Actually in the Middle of Sparse and Dense) Actually They may be mapped into a 1D array so that a mapping function can be used to locate an element. 43 / 81
  • 116.
    Images/cinvestav- Sparse Matrices Definition Sparse ...many elements are zero Dense ... few elements are zero Examples of structured sparse matrices Diagonal Tridiagonal Lower triangular (Actually in the Middle of Sparse and Dense) Actually They may be mapped into a 1D array so that a mapping function can be used to locate an element. 43 / 81
  • 117.
    Images/cinvestav- Sparse Matrices Definition Sparse ...many elements are zero Dense ... few elements are zero Examples of structured sparse matrices Diagonal Tridiagonal Lower triangular (Actually in the Middle of Sparse and Dense) Actually They may be mapped into a 1D array so that a mapping function can be used to locate an element. 43 / 81
  • 118.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 44 / 81
  • 119.
    Images/cinvestav- Representation Of UnstructuredSparse Matrices We can use a Single linear list in row-major order. Thus We scan the non-zero 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). 45 / 81
  • 120.
    Images/cinvestav- Representation Of UnstructuredSparse Matrices We can use a Single linear list in row-major order. Thus We scan the non-zero 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). 45 / 81
  • 121.
    Images/cinvestav- Representation Of UnstructuredSparse Matrices We can use a Single linear list in row-major order. Thus We scan the non-zero 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). 45 / 81
  • 122.
    Images/cinvestav- Representation Of UnstructuredSparse Matrices We can use a Single linear list in row-major order. Thus We scan the non-zero 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). 45 / 81
  • 123.
    Images/cinvestav- Outline 1 Introduction 1D Arrays 2DArrays 2 Representation 2D Array Representation 3 Improving the representation Row-Major Mapping 4 Matrix Definition Types of Matrices 5 Sparse Matrices Sparse Matrices Representation Of Unstructured Sparse Matrices Sparse Arrays 46 / 81
  • 124.
    Images/cinvestav- Example with anSparse Array First We will start with sparse one-dimensional arrays, which are simpler Example 47 / 81
  • 125.
    Images/cinvestav- Example with anSparse Array First We will start with sparse one-dimensional arrays, which are simpler Example 47 / 81
  • 126.
    Images/cinvestav- Representation we can havethe following representation Where 1 The front element is the index. 2 The second element is the value at cell index. 3 A pointer to the next element 48 / 81
  • 127.
    Images/cinvestav- Representation we can havethe following representation Where 1 The front element is the index. 2 The second element is the value at cell index. 3 A pointer to the next element 48 / 81
  • 128.
    Images/cinvestav- Representation we can havethe following representation Where 1 The front element is the index. 2 The second element is the value at cell index. 3 A pointer to the next element 48 / 81
  • 129.
    Images/cinvestav- Representation we can havethe following representation Where 1 The front element is the index. 2 The second element is the value at cell index. 3 A pointer to the next element 48 / 81
  • 130.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 131.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 132.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 133.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 134.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 135.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 136.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 137.
    Images/cinvestav- Sparse Array ADT Fora one-dimensional array of Item A constructor: SparseArray(int length) A way to get values from the array: Object get(int index) A way to store values in the array: void add(int index, Item value) In addition, it is OK to ask for a value from an empty array position 1 For number, it should return ZERO. 2 For Item, it should return NULL. 49 / 81
  • 138.
    Images/cinvestav- Other Operations What about? 1int length(): return the size of the original array 2 int elementCount() : how many non-null values are in the array Question Do we forget something? 50 / 81
  • 139.
    Images/cinvestav- Other Operations What about? 1int length(): return the size of the original array 2 int elementCount() : how many non-null values are in the array Question Do we forget something? 50 / 81
  • 140.
    Images/cinvestav- Other Operations What about? 1int length(): return the size of the original array 2 int elementCount() : how many non-null values are in the array Question Do we forget something? 50 / 81
  • 141.
    Images/cinvestav- Other Operations What about? 1int length(): return the size of the original array 2 int elementCount() : how many non-null values are in the array Question Do we forget something? 50 / 81
  • 142.
    Images/cinvestav- Other Operations What about? 1int length(): return the size of the original array 2 int elementCount() : how many non-null values are in the array Question Do we forget something? 50 / 81
  • 143.
    Images/cinvestav- Example of Get Code pu b l i c Item get ( i n t index ) { ChainNode c u r r e n t = t h i s . f i r s t N o d e ; do { i f ( index == c u r r e n t . index ) { // found c o r r e c t l o c a t i o n r e t u r n c u r r e n t . value ; } c u r r e n t = c u r r e n t . next ; } w hi le ( index < c u r r e n t . index && next != n u l l ) ; r e t u r n n u l l ; } 51 / 81
  • 144.
    Images/cinvestav- Time Analysis First We mustsearch a linked list for a given index We can keep the elements in order by index Expected Time for both get and add If we have n elements, we have the following complexity O(n). For the other methods length, elementCount =⇒ O(1) 52 / 81
  • 145.
    Images/cinvestav- Time Analysis First We mustsearch a linked list for a given index We can keep the elements in order by index Expected Time for both get and add If we have n elements, we have the following complexity O(n). For the other methods length, elementCount =⇒ O(1) 52 / 81
  • 146.
    Images/cinvestav- Time Analysis First We mustsearch a linked list for a given index We can keep the elements in order by index Expected Time for both get and add If we have n elements, we have the following complexity O(n). For the other methods length, elementCount =⇒ O(1) 52 / 81
  • 147.
    Images/cinvestav- Problem with thisanalysis True fact In an ordinary array, indexing to find an element is the only operation we really need!!! True fact In a sparse array, we can do indexing reasonably quickly False Conclusion In a sparse array, indexing to find an element is the only operation we really need The problem is that in designing the ADT, we did not think enough about how it would be used !!! 53 / 81
  • 148.
    Images/cinvestav- Problem with thisanalysis True fact In an ordinary array, indexing to find an element is the only operation we really need!!! True fact In a sparse array, we can do indexing reasonably quickly False Conclusion In a sparse array, indexing to find an element is the only operation we really need The problem is that in designing the ADT, we did not think enough about how it would be used !!! 53 / 81
  • 149.
    Images/cinvestav- Problem with thisanalysis True fact In an ordinary array, indexing to find an element is the only operation we really need!!! True fact In a sparse array, we can do indexing reasonably quickly False Conclusion In a sparse array, indexing to find an element is the only operation we really need The problem is that in designing the ADT, we did not think enough about how it would be used !!! 53 / 81
  • 150.
    Images/cinvestav- Look at thiscode To find the maximum element in a normal array: double max = a r r a y [ 0 ] ; f o r ( i n t i = 0; i < a r r a y . length ; i++) { i f ( a r r a y [ i ] > max) max = a r r a y [ i ] ; } 54 / 81
  • 151.
    Images/cinvestav- Look at thiscode To find the maximum element in a sparse array: Double max = ( Double ) a r r a y . get ( 0 ) ; f o r ( i n t i = 0; i < a r r a y . length ( ) ; i++) { Double temp = ( Double ) a r r a y . get ( i ) ; i f ( temp . compareTo (max) > 0) { max = temp ; } } 55 / 81
  • 152.
    Images/cinvestav- What is theproblem? First A lot of wrapping and casting because using generics. Second More importantly, in a normal array, every element is relevant Third If a sparse array is 1% full, 99% of its elements will be zero. This is 100 times as many elements as we should need to examine 56 / 81
  • 153.
    Images/cinvestav- What is theproblem? First A lot of wrapping and casting because using generics. Second More importantly, in a normal array, every element is relevant Third If a sparse array is 1% full, 99% of its elements will be zero. This is 100 times as many elements as we should need to examine 56 / 81
  • 154.
    Images/cinvestav- What is theproblem? First A lot of wrapping and casting because using generics. Second More importantly, in a normal array, every element is relevant Third If a sparse array is 1% full, 99% of its elements will be zero. This is 100 times as many elements as we should need to examine 56 / 81
  • 155.
    Images/cinvestav- What is theproblem? Fourth Our search time is based on the size of the sparse array, not on the number of elements that are actually in it Question What to do? 57 / 81
  • 156.
    Images/cinvestav- What is theproblem? Fourth Our search time is based on the size of the sparse array, not on the number of elements that are actually in it Question What to do? 57 / 81
  • 157.
    Images/cinvestav- Fixing the ADT SomethingNotable Although “stepping through an array” is not a fundamental operation on an array, it is one we do frequently. However This is a very expensive thing to do with a sparse array This should not be so expensive: We have a list, and all we need to do is step through it 58 / 81
  • 158.
    Images/cinvestav- Fixing the ADT SomethingNotable Although “stepping through an array” is not a fundamental operation on an array, it is one we do frequently. However This is a very expensive thing to do with a sparse array This should not be so expensive: We have a list, and all we need to do is step through it 58 / 81
  • 159.
    Images/cinvestav- Fixing the ADT SomethingNotable Although “stepping through an array” is not a fundamental operation on an array, it is one we do frequently. However This is a very expensive thing to do with a sparse array This should not be so expensive: We have a list, and all we need to do is step through it 58 / 81
  • 160.
    Images/cinvestav- Fixing the ADT SomethingNotable Although “stepping through an array” is not a fundamental operation on an array, it is one we do frequently. However This is a very expensive thing to do with a sparse array This should not be so expensive: We have a list, and all we need to do is step through it 58 / 81
  • 161.
    Images/cinvestav- Fixing the ADT PoorSolution Let the user step through the list. The user should not need to know anything about implementation. We cannot trust the user not to screw up the sparse array. These arguments are valid even if the user is also the implementer! Correct Solution Use an Inner iterator!!! 59 / 81
  • 162.
    Images/cinvestav- Fixing the ADT PoorSolution Let the user step through the list. The user should not need to know anything about implementation. We cannot trust the user not to screw up the sparse array. These arguments are valid even if the user is also the implementer! Correct Solution Use an Inner iterator!!! 59 / 81
  • 163.
    Images/cinvestav- Fixing the ADT PoorSolution Let the user step through the list. The user should not need to know anything about implementation. We cannot trust the user not to screw up the sparse array. These arguments are valid even if the user is also the implementer! Correct Solution Use an Inner iterator!!! 59 / 81
  • 164.
    Images/cinvestav- Fixing the ADT PoorSolution Let the user step through the list. The user should not need to know anything about implementation. We cannot trust the user not to screw up the sparse array. These arguments are valid even if the user is also the implementer! Correct Solution Use an Inner iterator!!! 59 / 81
  • 165.
    Images/cinvestav- Fixing the ADT PoorSolution Let the user step through the list. The user should not need to know anything about implementation. We cannot trust the user not to screw up the sparse array. These arguments are valid even if the user is also the implementer! Correct Solution Use an Inner iterator!!! 59 / 81
  • 166.
    Images/cinvestav- Example Code public class SparseArrayIterator<Item>implements Iterator<Item> { // any data you need to keep track of goes here SparseArrayIterator() { ...You do need one... } public boolean hasNext ( ) { ...you write this code... } public Item next ( ) { ...you write this code... } public Item remove ( ) { ...you write this code... } } 60 / 81
  • 167.
    Images/cinvestav- So, we havethat Code for Max S p a r s e A r r a y I t e r a t o r i t e r a t o r = new S p a r s e A r r a y I t e r a t o r ( a r r a y ) ; Double max = ( Double ) a r r a y . get ( 0 ) ; wh il e ( i t e r a t o r . hasNext ( ) ) { temp = ( Double ) i t e r a t o r . next ( ) ; i f ( temp . compareTo (max) > 0) { max = temp ; } } 61 / 81
  • 168.
    Images/cinvestav- However Problem Our SparseArrayIterator isfine for stepping through the elements of an array, but... It does not tell us what index they were at. For some problems, we may need this information First Solution Revise our iterator to tell us, not the value in each list cell, but the index in each list cell Problem Somewhat more awkward to use, since we would need array.get(iterator.next()) instead of just iterator.next(). But it’s worse than that, because next is defined to return an Item, so we would have to wrap the index. We could deal with this by overloading get to take an Item argument. 62 / 81
  • 169.
    Images/cinvestav- However Problem Our SparseArrayIterator isfine for stepping through the elements of an array, but... It does not tell us what index they were at. For some problems, we may need this information First Solution Revise our iterator to tell us, not the value in each list cell, but the index in each list cell Problem Somewhat more awkward to use, since we would need array.get(iterator.next()) instead of just iterator.next(). But it’s worse than that, because next is defined to return an Item, so we would have to wrap the index. We could deal with this by overloading get to take an Item argument. 62 / 81
  • 170.
    Images/cinvestav- However Problem Our SparseArrayIterator isfine for stepping through the elements of an array, but... It does not tell us what index they were at. For some problems, we may need this information First Solution Revise our iterator to tell us, not the value in each list cell, but the index in each list cell Problem Somewhat more awkward to use, since we would need array.get(iterator.next()) instead of just iterator.next(). But it’s worse than that, because next is defined to return an Item, so we would have to wrap the index. We could deal with this by overloading get to take an Item argument. 62 / 81
  • 171.
    Images/cinvestav- However Problem Our SparseArrayIterator isfine for stepping through the elements of an array, but... It does not tell us what index they were at. For some problems, we may need this information First Solution Revise our iterator to tell us, not the value in each list cell, but the index in each list cell Problem Somewhat more awkward to use, since we would need array.get(iterator.next()) instead of just iterator.next(). But it’s worse than that, because next is defined to return an Item, so we would have to wrap the index. We could deal with this by overloading get to take an Item argument. 62 / 81
  • 172.
    Images/cinvestav- However Problem Our SparseArrayIterator isfine for stepping through the elements of an array, but... It does not tell us what index they were at. For some problems, we may need this information First Solution Revise our iterator to tell us, not the value in each list cell, but the index in each list cell Problem Somewhat more awkward to use, since we would need array.get(iterator.next()) instead of just iterator.next(). But it’s worse than that, because next is defined to return an Item, so we would have to wrap the index. We could deal with this by overloading get to take an Item argument. 62 / 81
  • 173.
    Images/cinvestav- However Problem Our SparseArrayIterator isfine for stepping through the elements of an array, but... It does not tell us what index they were at. For some problems, we may need this information First Solution Revise our iterator to tell us, not the value in each list cell, but the index in each list cell Problem Somewhat more awkward to use, since we would need array.get(iterator.next()) instead of just iterator.next(). But it’s worse than that, because next is defined to return an Item, so we would have to wrap the index. We could deal with this by overloading get to take an Item argument. 62 / 81
  • 174.
    Images/cinvestav- However Problem Our SparseArrayIterator isfine for stepping through the elements of an array, but... It does not tell us what index they were at. For some problems, we may need this information First Solution Revise our iterator to tell us, not the value in each list cell, but the index in each list cell Problem Somewhat more awkward to use, since we would need array.get(iterator.next()) instead of just iterator.next(). But it’s worse than that, because next is defined to return an Item, so we would have to wrap the index. We could deal with this by overloading get to take an Item argument. 62 / 81
  • 175.
    Images/cinvestav- Instead of thatuse an IndexIterator Too Code p u b l i c c l a s s I n d e x I t e r a t o r { p r i v a t e ChainNode c u r r e n t ; I n d e x I t e r a t o r () { // c o n s t r u c t o r c u r r e n t = f i r s t N o d e ; } p u b l i c boolean hasNext () { // j u s t l i k e b e f o r e } p u b l i c i n t next () { i n t index = c u r r e n t . index ; c u r r e n t = c u r r e n t . next ; r e t u r n index ; } } 63 / 81
  • 176.
  • 177.
    Images/cinvestav- Using Array LinearList for Matrices Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         Thus list = row 1 1 2 2 4 4 column 3 5 3 4 2 3 value 3 4 5 6 2 6 65 / 81
  • 178.
    Images/cinvestav- Using Array LinearList for Matrices Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         Thus list = row 1 1 2 2 4 4 column 3 5 3 4 2 3 value 3 4 5 6 2 6 65 / 81
  • 179.
    Images/cinvestav- Now, How dowe represent this list using arrays? Why? We want compact representations... Use your friend element from arrays Thus you declare an array element with the Node information Node element[] = new Node[1000]; 66 / 81
  • 180.
    Images/cinvestav- Now, How dowe represent this list using arrays? Why? We want compact representations... Use your friend element from arrays package d a t a S t r u c t u r e s ; // Using Generic in Java p u b l i c c l a s s Node<Item>{ // elements p r i v a t e i n t row ; p r i v a t e i n t column p r i v a t e Item value ; // c o n s t r u c t o r s come here } Thus you declare an array element with the Node information Node element[] = new Node[1000]; 66 / 81
  • 181.
    Images/cinvestav- Now, How dowe represent this list using arrays? Why? We want compact representations... Use your friend element from arrays package d a t a S t r u c t u r e s ; // Using Generic in Java p u b l i c c l a s s Node<Item>{ // elements p r i v a t e i n t row ; p r i v a t e i n t column p r i v a t e Item value ; // c o n s t r u c t o r s come here } Thus you declare an array element with the Node information Node element[] = new Node[1000]; 66 / 81
  • 182.
    Images/cinvestav- What about otherrepresentations? We can use chains too We need to modify our node To something like this Graphically 67 / 81
  • 183.
    Images/cinvestav- What about otherrepresentations? We can use chains too We need to modify our node To something like this package d a t a S t r u c t u r e s ; // Using Generic i n Java p u b l i c c l a s s Node<Item >{ // elements p r i v a t e i n t row ; p r i v a t e i n t column ; p r i v a t e Item v a l u e ; p r i v a t e Node next ; // c o n s t r u c t o r s come here } Graphically 67 / 81
  • 184.
    Images/cinvestav- What about otherrepresentations? We can use chains too We need to modify our node To something like this package d a t a S t r u c t u r e s ; // Using Generic i n Java p u b l i c c l a s s Node<Item >{ // elements p r i v a t e i n t row ; p r i v a t e i n t column ; p r i v a t e Item v a l u e ; p r i v a t e Node next ; // c o n s t r u c t o r s come here } Graphically 67 / 81
  • 185.
  • 186.
    Images/cinvestav- One Linear ListPer Row Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         Thus row1 = [(3,3) (5,4)] row2 = [(3,5) (4,7)] row3 = [] row4 = [(2,2) (3,6)] Node Structure 69 / 81
  • 187.
    Images/cinvestav- One Linear ListPer Row Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         Thus row1 = [(3,3) (5,4)] row2 = [(3,5) (4,7)] row3 = [] row4 = [(2,2) (3,6)] Node Structure 69 / 81
  • 188.
    Images/cinvestav- One Linear ListPer Row Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         Thus row1 = [(3,3) (5,4)] row2 = [(3,5) (4,7)] row3 = [] row4 = [(2,2) (3,6)] Node Structure 69 / 81
  • 189.
    Images/cinvestav- Array Of RowChains Example 70 / 81
  • 190.
    Images/cinvestav- Compress the row Usea sparse array!!! null null null null null null 0 1 2 3 4 5 6 7 8 9 0 2 5 8 71 / 81
  • 191.
    Images/cinvestav- We have aproblem here With 99% of the matrix as zeros of nulls We have the problem that many row pointers are null... What can you do? Ideas 72 / 81
  • 192.
    Images/cinvestav- We have aproblem here With 99% of the matrix as zeros of nulls We have the problem that many row pointers are null... What can you do? Ideas 72 / 81
  • 193.
    Images/cinvestav- However Problems, problems WillRobinson!!! Ideas? Yes, access!! We have a problem there!! It is Not so fast!!!! SOLUTIONS??? 73 / 81
  • 194.
    Images/cinvestav- However Problems, problems WillRobinson!!! Ideas? Yes, access!! We have a problem there!! It is Not so fast!!!! SOLUTIONS??? 73 / 81
  • 195.
    Images/cinvestav- However Problems, problems WillRobinson!!! Ideas? Yes, access!! We have a problem there!! It is Not so fast!!!! SOLUTIONS??? 73 / 81
  • 196.
    Images/cinvestav- Orthogonal List Representation Morecomplexity Both row and column lists. Node Structure Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         74 / 81
  • 197.
    Images/cinvestav- Orthogonal List Representation Morecomplexity Both row and column lists. Node Structure Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         74 / 81
  • 198.
    Images/cinvestav- Orthogonal List Representation Morecomplexity Both row and column lists. Node Structure Example         0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0         74 / 81
  • 199.
  • 200.
  • 201.
  • 202.
    Images/cinvestav- Go One StepFurther Question We have another problem the sparse arrays in the column and row pointers!!! Thus How it will look like for this (You can try!!!)        0 1 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 0 1        (3) 78 / 81
  • 203.
    Images/cinvestav- Go One StepFurther Question We have another problem the sparse arrays in the column and row pointers!!! Thus How it will look like for this (You can try!!!)        0 1 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 0 1        (3) 78 / 81
  • 204.
    Images/cinvestav- Do not worry!!! Youhave MANY VARIATIONS!!! 79 / 81
  • 205.
    Images/cinvestav- Approximate Memory Requirements Example 500x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1 million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row 23928 + 500 x 4 = 25,928 What about the sparse version even in row and column pointer? 80 / 81
  • 206.
    Images/cinvestav- Approximate Memory Requirements Example 500x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1 million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row 23928 + 500 x 4 = 25,928 What about the sparse version even in row and column pointer? 80 / 81
  • 207.
    Images/cinvestav- Approximate Memory Requirements Example 500x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1 million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row 23928 + 500 x 4 = 25,928 What about the sparse version even in row and column pointer? 80 / 81
  • 208.
    Images/cinvestav- Approximate Memory Requirements Example 500x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1 million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row 23928 + 500 x 4 = 25,928 What about the sparse version even in row and column pointer? 80 / 81
  • 209.
    Images/cinvestav- Approximate Memory Requirements Example 500x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1 million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row 23928 + 500 x 4 = 25,928 What about the sparse version even in row and column pointer? 80 / 81
  • 210.
    Images/cinvestav- Runtime Performance Example MatrixTranspose 500 x 500 matrix with 1994 nonzero elements: Method Time 2D Array 210 ms Single Array List 6 ms One Chain per Row 12 ms Example Matrix Addition 500 x 500 matrix with 1994 nonzero elements: Method Time 2D Array 880 ms Single Array List 18 ms One Chain per Row 29 ms 81 / 81
  • 211.
    Images/cinvestav- Runtime Performance Example MatrixTranspose 500 x 500 matrix with 1994 nonzero elements: Method Time 2D Array 210 ms Single Array List 6 ms One Chain per Row 12 ms Example Matrix Addition 500 x 500 matrix with 1994 nonzero elements: Method Time 2D Array 880 ms Single Array List 18 ms One Chain per Row 29 ms 81 / 81