SlideShare a Scribd company logo
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

More Related Content

PDF
Preparation Data Structures 10 trees
PDF
Preparation Data Structures 04 array linear_list
PDF
Preparation Data Structures 01 introduction
PDF
Preparation Data Structures 03 abstract data_types
PPT
Chapter 3 ds
PDF
Preparation Data Structures 05 chain linear_list
PPT
Chapter 5 ds
PDF
Preparation Data Structures 08 queues
Preparation Data Structures 10 trees
Preparation Data Structures 04 array linear_list
Preparation Data Structures 01 introduction
Preparation Data Structures 03 abstract data_types
Chapter 3 ds
Preparation Data Structures 05 chain linear_list
Chapter 5 ds
Preparation Data Structures 08 queues

What's hot (20)

PPTX
17. Trees and Tree Like Structures
PPT
Chapter 9 ds
PPT
Chapter 4 ds
PPT
Arrays and structures
PPTX
Bsc cs ii dfs u-1 introduction to data structure
PDF
Preparation Data Structures 11 graphs
PPTX
Data structures
PDF
Data Structures (BE)
PPTX
PPT
Lecture 2a arrays
PPTX
هياكلبيانات
PPTX
Row major and column major in 2 d
PPT
PPT
Review session2
PPTX
Data structures in c#
PPT
Data structures using C
PDF
Introduction to Data Structure
PPT
Array
PPTX
Set data structure
PPTX
Presentation on Elementary data structures
17. Trees and Tree Like Structures
Chapter 9 ds
Chapter 4 ds
Arrays and structures
Bsc cs ii dfs u-1 introduction to data structure
Preparation Data Structures 11 graphs
Data structures
Data Structures (BE)
Lecture 2a arrays
هياكلبيانات
Row major and column major in 2 d
Review session2
Data structures in c#
Data structures using C
Introduction to Data Structure
Array
Set data structure
Presentation on Elementary data structures
Ad

Similar to Preparation Data Structures 06 arrays representation (20)

PPTX
PPTX
PPTX
Programming data structure concept in array ppt
PDF
Array Data Structure for programing language
PPTX
UNIT-5_Array in c_part1.pptx
PPTX
unit 2.pptx
PDF
Arrays
PDF
arrays-130116232821-phpapp02.pdf
PDF
Chapter 4 (Part I) - Array and Strings.pdf
PPTX
NUMPY-2.pptx
PPTX
DOCX
PPTX
Various Operations Of Array(Data Structure Algorithm).pptx
PPTX
numpydocococ34554367827839271966666.pptx
PDF
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PDF
Numpy ndarrays.pdf
PPTX
arrays.pptx
PDF
Python programming : Arrays
PDF
CE344L-200365-Lab2.pdf
Programming data structure concept in array ppt
Array Data Structure for programing language
UNIT-5_Array in c_part1.pptx
unit 2.pptx
Arrays
arrays-130116232821-phpapp02.pdf
Chapter 4 (Part I) - Array and Strings.pdf
NUMPY-2.pptx
Various Operations Of Array(Data Structure Algorithm).pptx
numpydocococ34554367827839271966666.pptx
Unit4pptx__2024_11_ 11_10_16_09.pptx
Numpy ndarrays.pdf
arrays.pptx
Python programming : Arrays
CE344L-200365-Lab2.pdf
Ad

More from Andres Mendez-Vazquez (20)

PDF
2.03 bayesian estimation
PDF
05 linear transformations
PDF
01.04 orthonormal basis_eigen_vectors
PDF
01.03 squared matrices_and_other_issues
PDF
01.02 linear equations
PDF
01.01 vector spaces
PDF
06 recurrent neural_networks
PDF
05 backpropagation automatic_differentiation
PDF
Zetta global
PDF
01 Introduction to Neural Networks and Deep Learning
PDF
25 introduction reinforcement_learning
PDF
Neural Networks and Deep Learning Syllabus
PDF
Introduction to artificial_intelligence_syllabus
PDF
Ideas 09 22_2018
PDF
Ideas about a Bachelor in Machine Learning/Data Sciences
PDF
Analysis of Algorithms Syllabus
PDF
20 k-means, k-center, k-meoids and variations
PDF
18.1 combining models
PDF
17 vapnik chervonenkis dimension
PDF
A basic introduction to learning
2.03 bayesian estimation
05 linear transformations
01.04 orthonormal basis_eigen_vectors
01.03 squared matrices_and_other_issues
01.02 linear equations
01.01 vector spaces
06 recurrent neural_networks
05 backpropagation automatic_differentiation
Zetta global
01 Introduction to Neural Networks and Deep Learning
25 introduction reinforcement_learning
Neural Networks and Deep Learning Syllabus
Introduction to artificial_intelligence_syllabus
Ideas 09 22_2018
Ideas about a Bachelor in Machine Learning/Data Sciences
Analysis of Algorithms Syllabus
20 k-means, k-center, k-meoids and variations
18.1 combining models
17 vapnik chervonenkis dimension
A basic introduction to learning

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Computing-Curriculum for Schools in Ghana
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Presentation on HIE in infants and its manifestations
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
master seminar digital applications in india
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Final Presentation General Medicine 03-08-2024.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
A systematic review of self-coping strategies used by university students to ...
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Presentation on HIE in infants and its manifestations
O5-L3 Freight Transport Ops (International) V1.pdf
master seminar digital applications in india
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS

Preparation Data Structures 06 arrays representation

  • 1. Data Structures Array Representation Andres Mendez-Vazquez May 6, 2015 1 / 81
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. Images/cinvestav- Making your life miserable Because you can ask Which is the highest score? Look at the code 4 / 81
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 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 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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
  • 52. 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
  • 53. 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
  • 54. 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
  • 55. 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
  • 56. 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
  • 57. 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
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. 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
  • 62. 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
  • 63. 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
  • 64. 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
  • 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 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
  • 77. 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
  • 78. 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
  • 79. 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
  • 80. 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
  • 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
  • 82. 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
  • 83. 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
  • 84. 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
  • 85. 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
  • 86. 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
  • 87. 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
  • 88. 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
  • 89. 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
  • 90. 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
  • 91. 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
  • 92. 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
  • 93. 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
  • 94. 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
  • 95. 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
  • 96. 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
  • 97. 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
  • 98. 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
  • 99. 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
  • 100. 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
  • 101. 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
  • 102. 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
  • 103. 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
  • 104. 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
  • 105. 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
  • 106. 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
  • 107. 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
  • 108. 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
  • 109. 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
  • 110. 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
  • 111. Images/cinvestav- What we need... We need... 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 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
  • 119. 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
  • 120. 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
  • 121. 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
  • 122. 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
  • 123. 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
  • 124. Images/cinvestav- Example with an Sparse Array First We will start with sparse one-dimensional arrays, which are simpler Example 47 / 81
  • 125. Images/cinvestav- Example with an Sparse Array First We will start with sparse one-dimensional arrays, which are simpler Example 47 / 81
  • 126. 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
  • 127. 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
  • 128. 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
  • 129. 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
  • 130. 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
  • 131. 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
  • 132. 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
  • 133. 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
  • 134. 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
  • 135. 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
  • 136. 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
  • 137. 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
  • 138. 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
  • 139. 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
  • 140. 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
  • 141. 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
  • 142. 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
  • 143. 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
  • 144. 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
  • 145. 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
  • 146. 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
  • 147. 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
  • 148. 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
  • 149. 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
  • 150. 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
  • 151. 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
  • 152. 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
  • 153. 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
  • 154. 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
  • 155. 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
  • 156. 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
  • 157. 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
  • 158. 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
  • 159. 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
  • 160. 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
  • 161. 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
  • 162. 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
  • 163. 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
  • 164. 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
  • 165. 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
  • 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 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
  • 168. 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
  • 169. 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
  • 170. 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
  • 171. 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
  • 172. 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
  • 173. 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
  • 174. 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
  • 175. 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
  • 177. 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
  • 178. 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
  • 179. 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
  • 180. 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
  • 181. 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
  • 182. Images/cinvestav- What about other representations? We can use chains too We need to modify our node To something like this Graphically 67 / 81
  • 183. 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
  • 184. 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
  • 186. 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
  • 187. 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
  • 188. 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
  • 189. Images/cinvestav- Array Of Row Chains Example 70 / 81
  • 190. 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
  • 191. 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
  • 192. 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
  • 193. Images/cinvestav- However Problems, problems Will Robinson!!! Ideas? Yes, access!! We have a problem there!! It is Not so fast!!!! SOLUTIONS??? 73 / 81
  • 194. Images/cinvestav- However Problems, problems Will Robinson!!! Ideas? Yes, access!! We have a problem there!! It is Not so fast!!!! SOLUTIONS??? 73 / 81
  • 195. Images/cinvestav- However Problems, problems Will Robinson!!! Ideas? Yes, access!! We have a problem there!! It is Not so fast!!!! SOLUTIONS??? 73 / 81
  • 196. 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
  • 197. 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
  • 198. 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
  • 202. 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
  • 203. 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
  • 204. Images/cinvestav- Do not worry!!! You have MANY VARIATIONS!!! 79 / 81
  • 205. 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
  • 206. 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
  • 207. 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
  • 208. 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
  • 209. 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
  • 210. 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
  • 211. 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