preparation data structures 06 arrays representation
TRANSCRIPT
Images/cinvestav-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
2 / 81
Images/cinvestav-1.jpg
Introduction
ObservationWhen a program manipulates many variables that contain “similar” formsof data, organizational problems quickly arise.
ExampleWhen a program manipulates many variables that contain “similar” formsof data, organizational problems quickly arise.
We could use a name variable for each datadouble score0; double score1; double score2;double score3; double score4; double score5;
3 / 81
Images/cinvestav-1.jpg
Introduction
ObservationWhen a program manipulates many variables that contain “similar” formsof data, organizational problems quickly arise.
ExampleWhen a program manipulates many variables that contain “similar” formsof data, organizational problems quickly arise.
We could use a name variable for each datadouble score0; double score1; double score2;double score3; double score4; double score5;
3 / 81
Images/cinvestav-1.jpg
Introduction
ObservationWhen a program manipulates many variables that contain “similar” formsof data, organizational problems quickly arise.
ExampleWhen a program manipulates many variables that contain “similar” formsof data, organizational problems quickly arise.
We could use a name variable for each datadouble score0; double score1; double score2;double score3; double score4; double score5;
3 / 81
Images/cinvestav-1.jpg
Making your life miserable
Because you can askWhich is the highest score?
Look at the code
4 / 81
Images/cinvestav-1.jpg
Making your life miserable
Because you can askWhich is the highest score?
Look at the code
doub l e h i gh_sco r e = s c o r e 0 ;i f ( s c o r e 1 > h igh_sco r e ) { h i gh_sco r e = s c o r e 1 ; }i f ( s c o r e 2 > h igh_sco r e ) { h i gh_sco r e = s c o r e 2 ; }i f ( s c o r e 3 > h igh_sco r e ) { h i gh_sco r e = s c o r e 3 ; }i f ( s c o r e 4 > h igh_sco r e ) { h i gh_sco r e = s c o r e 4 ; }i f ( s c o r e 5 > h igh_sco r e ) { h i gh_sco r e = s c o r e 5 ; }System . out . p r i n t l n ( h i gh_sco r e ) ;
4 / 81
Images/cinvestav-1.jpg
How do we solve this?
ThusJava, C and C++ use array variables to put together this collection ofequal elements.
Memory Layout
5 / 81
Images/cinvestav-1.jpg
How do we solve this?
ThusJava, C and C++ use array variables to put together this collection ofequal elements.
Memory Layout
a b c d
START
MEMORY LAYOUT
5 / 81
Images/cinvestav-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
6 / 81
Images/cinvestav-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
1D Array Representation In Java, C, and C++
NotesFor 1-dimensional array:
The elements are mapped into contiguous memory locationsThey 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 bytes2 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-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
8 / 81
Images/cinvestav-1.jpg
Now, 2D Arrays
We declare 2-dimensional arrays as follow (Java, C)int[][] A = new int[3][4]
It can be shown as a tableA[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-1.jpg
Now, 2D Arrays
We declare 2-dimensional arrays as follow (Java, C)int[][] A = new int[3][4]
It can be shown as a tableA[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-1.jpg
Rows and Columns in a 2-D Array
RowsA[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-1.jpg
Rows and Columns in a 2-D Array
RowsA[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-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
11 / 81
Images/cinvestav-1.jpg
2D Array Representation In Java, C, and C++
Given the following 2-dimensional array x
x =
a b c de f g hi j k l
This information is stored as 1D arrays with a reference to them
12 / 81
Images/cinvestav-1.jpg
2D Array Representation In Java, C, and C++Given the following 2-dimensional array x
x =
a b c de f g hi j k l
This information is stored as 1D arrays with a reference to them
12 / 81
Images/cinvestav-1.jpg
Some Properties
If we call the lengths of each of themx.length ⇒ 3x[0].length == x[1].length == x[2].length ⇒ 3
Space OverheadRemember, it is 8 bytes per 1D array
1 Thus, we have 4× 8 = 32 bytes2 Or we can see this as (number of rows + 1)× 8 bytes
13 / 81
Images/cinvestav-1.jpg
Some Properties
If we call the lengths of each of themx.length ⇒ 3x[0].length == x[1].length == x[2].length ⇒ 3
Space OverheadRemember, it is 8 bytes per 1D array
1 Thus, we have 4× 8 = 32 bytes2 Or we can see this as (number of rows + 1)× 8 bytes
13 / 81
Images/cinvestav-1.jpg
Some Properties
If we call the lengths of each of themx.length ⇒ 3x[0].length == x[1].length == x[2].length ⇒ 3
Space OverheadRemember, it is 8 bytes per 1D array
1 Thus, we have 4× 8 = 32 bytes2 Or we can see this as (number of rows + 1)× 8 bytes
13 / 81
Images/cinvestav-1.jpg
Some Properties
If we call the lengths of each of themx.length ⇒ 3x[0].length == x[1].length == x[2].length ⇒ 3
Space OverheadRemember, it is 8 bytes per 1D array
1 Thus, we have 4× 8 = 32 bytes2 Or we can see this as (number of rows + 1)× 8 bytes
13 / 81
Images/cinvestav-1.jpg
Some Properties
If we call the lengths of each of themx.length ⇒ 3x[0].length == x[1].length == x[2].length ⇒ 3
Space OverheadRemember, it is 8 bytes per 1D array
1 Thus, we have 4× 8 = 32 bytes2 Or we can see this as (number of rows + 1)× 8 bytes
13 / 81
Images/cinvestav-1.jpg
More Properties
FirstThis representation is called the array-of-arrays representation.
SecondIt requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4bytes for the 4 1D-arrays.
ThirdOne memory block of size number of rows and number of rows blocksof size number of columns.
14 / 81
Images/cinvestav-1.jpg
More Properties
FirstThis representation is called the array-of-arrays representation.
SecondIt requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4bytes for the 4 1D-arrays.
ThirdOne memory block of size number of rows and number of rows blocksof size number of columns.
14 / 81
Images/cinvestav-1.jpg
More Properties
FirstThis representation is called the array-of-arrays representation.
SecondIt requires contiguous memory of size 3 bytes, 4 bytes, 4 bytes, and 4bytes for the 4 1D-arrays.
ThirdOne memory block of size number of rows and number of rows blocksof size number of columns.
14 / 81
Images/cinvestav-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
15 / 81
Images/cinvestav-1.jpg
Row-Major Mapping
Again
x =
a b c de f g hi j k l
For thisWe will convert it into 1D-array y by collecting elements by rows.
ThusWithin a row elements are collected from left to right.Rows are collected from top to bottom.
16 / 81
Images/cinvestav-1.jpg
Row-Major Mapping
Again
x =
a b c de f g hi j k l
For thisWe will convert it into 1D-array y by collecting elements by rows.
ThusWithin a row elements are collected from left to right.Rows are collected from top to bottom.
16 / 81
Images/cinvestav-1.jpg
Row-Major Mapping
Again
x =
a b c de f g hi j k l
For thisWe will convert it into 1D-array y by collecting elements by rows.
ThusWithin a row elements are collected from left to right.Rows are collected from top to bottom.
16 / 81
Images/cinvestav-1.jpg
How do we locate an element?
Look At This
ThenAssume x has r rows and c columns.Each row has c elements.i rows to the left of row i.
ThusWe 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-1.jpg
How do we locate an element?
Look At This
ThenAssume x has r rows and c columns.Each row has c elements.i rows to the left of row i.
ThusWe 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-1.jpg
How do we locate an element?
Look At This
ThenAssume x has r rows and c columns.Each row has c elements.i rows to the left of row i.
ThusWe 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-1.jpg
How do we locate an element?
Look At This
ThenAssume x has r rows and c columns.Each row has c elements.i rows to the left of row i.
ThusWe 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-1.jpg
How do we locate an element?
Look At This
ThenAssume x has r rows and c columns.Each row has c elements.i rows to the left of row i.
ThusWe 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-1.jpg
How do we locate an element?
Look At This
ThenAssume x has r rows and c columns.Each row has c elements.i rows to the left of row i.
ThusWe 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-1.jpg
Space Overhead
We have4 bytes for start of 1D array.4 bytes for length of 1D array.4 bytes for c (number of columns)
Total: 12 bytesNote: The number of rows = length/c
StillDisadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-1.jpg
Space Overhead
We have4 bytes for start of 1D array.4 bytes for length of 1D array.4 bytes for c (number of columns)
Total: 12 bytesNote: The number of rows = length/c
StillDisadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-1.jpg
Space Overhead
We have4 bytes for start of 1D array.4 bytes for length of 1D array.4 bytes for c (number of columns)
Total: 12 bytesNote: The number of rows = length/c
StillDisadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-1.jpg
Space Overhead
We have4 bytes for start of 1D array.4 bytes for length of 1D array.4 bytes for c (number of columns)
Total: 12 bytesNote: The number of rows = length/c
StillDisadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-1.jpg
Space Overhead
We have4 bytes for start of 1D array.4 bytes for length of 1D array.4 bytes for c (number of columns)
Total: 12 bytesNote: The number of rows = length/c
StillDisadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-1.jpg
Space Overhead
We have4 bytes for start of 1D array.4 bytes for length of 1D array.4 bytes for c (number of columns)
Total: 12 bytesNote: The number of rows = length/c
StillDisadvantage: Need contiguous memory of size rc.
19 / 81
Images/cinvestav-1.jpg
Column-Major Mapping
Similar SetupConvert 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 de f g hi j k l
We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-1.jpg
Column-Major Mapping
Similar SetupConvert 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 de f g hi j k l
We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-1.jpg
Column-Major Mapping
Similar SetupConvert 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 de f g hi j k l
We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-1.jpg
Column-Major Mapping
Similar SetupConvert 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 de f g hi j k l
We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
20 / 81
Images/cinvestav-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
21 / 81
Images/cinvestav-1.jpg
Matrix
Something NotableTable of values.It has rows and columns, but numbering begins at 1 rather than 0.
We use the following notationUse notation x(i,j) rather than x[i][j].
NoteIt may use a 2D array to represent a matrix.
22 / 81
Images/cinvestav-1.jpg
Matrix
Something NotableTable of values.It has rows and columns, but numbering begins at 1 rather than 0.
We use the following notationUse notation x(i,j) rather than x[i][j].
NoteIt may use a 2D array to represent a matrix.
22 / 81
Images/cinvestav-1.jpg
Matrix
Something NotableTable of values.It has rows and columns, but numbering begins at 1 rather than 0.
We use the following notationUse notation x(i,j) rather than x[i][j].
NoteIt may use a 2D array to represent a matrix.
22 / 81
Images/cinvestav-1.jpg
Drawbacks of using a 2D Array
FirstIndexes are off by 1.
SecondJava arrays do not support matrix operations such as add, transpose,multiply, and so on.
HoweverYou can develop a class Matrix for object-oriented support of all matrixoperations.
23 / 81
Images/cinvestav-1.jpg
Drawbacks of using a 2D Array
FirstIndexes are off by 1.
SecondJava arrays do not support matrix operations such as add, transpose,multiply, and so on.
HoweverYou can develop a class Matrix for object-oriented support of all matrixoperations.
23 / 81
Images/cinvestav-1.jpg
Drawbacks of using a 2D Array
FirstIndexes are off by 1.
SecondJava arrays do not support matrix operations such as add, transpose,multiply, and so on.
HoweverYou can develop a class Matrix for object-oriented support of all matrixoperations.
23 / 81
Images/cinvestav-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
24 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Diagonal Matrix
Diagonal MatrixAn n × n matrix in which all nonzero terms are on the diagonal.
Propertiesx(i,j) is on diagonal iff i = j
Example
x =
a 0 0 00 b 0 00 0 c 00 0 0 d
25 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Diagonal Matrix
Diagonal MatrixAn n × n matrix in which all nonzero terms are on the diagonal.
Propertiesx(i,j) is on diagonal iff i = j
Example
x =
a 0 0 00 b 0 00 0 c 00 0 0 d
25 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Diagonal Matrix
Diagonal MatrixAn n × n matrix in which all nonzero terms are on the diagonal.
Propertiesx(i,j) is on diagonal iff i = j
Example
x =
a 0 0 00 b 0 00 0 c 00 0 0 d
25 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Diagonal Matrix
Propertiesx(i,j) is on diagonal iff i = j
DRAWBACKFor 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-1.jpg
Some Basic Matrices: Diagonal Matrix
Propertiesx(i,j) is on diagonal iff i = j
DRAWBACKFor 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-1.jpg
Some Basic Matrices: Diagonal Matrix
Propertiesx(i,j) is on diagonal iff i = j
DRAWBACKFor 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-1.jpg
Some Basic Matrices: Lower Triangular Matrix
DefinitionAn n × n matrix in which all nonzero terms are either on or below thediagonal.
Example
x =
1 0 0 02 3 0 04 5 6 07 8 9 10
27 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Lower Triangular Matrix
DefinitionAn n × n matrix in which all nonzero terms are either on or below thediagonal.
Example
x =
1 0 0 02 3 0 04 5 6 07 8 9 10
27 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Lower Triangular Matrix
Propertiesx(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)
ThusYou can store only the lower triangle
28 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Lower Triangular Matrix
Propertiesx(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)
ThusYou can store only the lower triangle
28 / 81
Images/cinvestav-1.jpg
Some Basic Matrices: Lower Triangular Matrix
Propertiesx(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)
ThusYou can store only the lower triangle
28 / 81
Images/cinvestav-1.jpg
Array Of Arrays Representation
We can use this
Something NotableYou can use an irregular 2-D array ... length of rows is not required to bethe same.
29 / 81
Images/cinvestav-1.jpg
Array Of Arrays Representation
We can use this
Something NotableYou can use an irregular 2-D array ... length of rows is not required to bethe same.
29 / 81
Images/cinvestav-1.jpg
Code
Code for irregular array
// d e c l a r e a two−d i m e n s i o n a l 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 o f rowsi 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 e l ement s i n each rowf 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 yi 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-1.jpg
However, You can do better
Map Lower Triangular Array Into A 1D ArrayYou can use a row-major order, but omit terms that are not part of thelower triangle.
For example
x =
1 0 0 02 3 0 04 5 6 07 8 9 10
We get1, 2, 3, 4, 5, 6, 7, 8, 9, 10
31 / 81
Images/cinvestav-1.jpg
However, You can do better
Map Lower Triangular Array Into A 1D ArrayYou can use a row-major order, but omit terms that are not part of thelower triangle.
For example
x =
1 0 0 02 3 0 04 5 6 07 8 9 10
We get1, 2, 3, 4, 5, 6, 7, 8, 9, 10
31 / 81
Images/cinvestav-1.jpg
However, You can do better
Map Lower Triangular Array Into A 1D ArrayYou can use a row-major order, but omit terms that are not part of thelower triangle.
For example
x =
1 0 0 02 3 0 04 5 6 07 8 9 10
We get1, 2, 3, 4, 5, 6, 7, 8, 9, 10
31 / 81
Images/cinvestav-1.jpg
The final mapping
We want
We have that1 Order is: row 1, row 2, row 3, ...2 Row i is preceded by rows 1, 2, ..., i-13 Size of row i is i.
32 / 81
Images/cinvestav-1.jpg
The final mapping
We want
We have that1 Order is: row 1, row 2, row 3, ...2 Row i is preceded by rows 1, 2, ..., i-13 Size of row i is i.
32 / 81
Images/cinvestav-1.jpg
The final mapping
We want
We have that1 Order is: row 1, row 2, row 3, ...2 Row i is preceded by rows 1, 2, ..., i-13 Size of row i is i.
32 / 81
Images/cinvestav-1.jpg
The final mapping
We want
We have that1 Order is: row 1, row 2, row 3, ...2 Row i is preceded by rows 1, 2, ..., i-13 Size of row i is i.
32 / 81
Images/cinvestav-1.jpg
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-1.jpg
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-1.jpg
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 Matr ix<Item >{p u b l i c I tem get ( i n t row , i n t column ) ;p u b l i c v o i d add ( i n t row , i n t column , Item myobj ) ;p u b l i c I tem remove ( i n t row , i n t column ) ;p u b l i c v o i d output ( ) ;
}
34 / 81
Images/cinvestav-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
35 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionA sparse array is simply an array most of whose entries are zero (or null, orsome other default value).
For ExampleSuppose you wanted a 2-dimensional array of course grades, whose rowsare students and whose columns are courses.
PropertiesThere are about 90,173 studentsThere are about 5000 coursesThis array would have about 450,865,000 entries
36 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionA sparse array is simply an array most of whose entries are zero (or null, orsome other default value).
For ExampleSuppose you wanted a 2-dimensional array of course grades, whose rowsare students and whose columns are courses.
PropertiesThere are about 90,173 studentsThere are about 5000 coursesThis array would have about 450,865,000 entries
36 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionA sparse array is simply an array most of whose entries are zero (or null, orsome other default value).
For ExampleSuppose you wanted a 2-dimensional array of course grades, whose rowsare students and whose columns are courses.
PropertiesThere are about 90,173 studentsThere are about 5000 coursesThis array would have about 450,865,000 entries
36 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionA sparse array is simply an array most of whose entries are zero (or null, orsome other default value).
For ExampleSuppose you wanted a 2-dimensional array of course grades, whose rowsare students and whose columns are courses.
PropertiesThere are about 90,173 studentsThere are about 5000 coursesThis array would have about 450,865,000 entries
36 / 81
Images/cinvestav-1.jpg
Thus
Something NotableSince most students take fewer than 5000 courses, there will be a lot ofempty spaces in this array.
Something NotableThis is a big array, even by modern standards.
37 / 81
Images/cinvestav-1.jpg
Thus
Something NotableSince most students take fewer than 5000 courses, there will be a lot ofempty spaces in this array.
Something NotableThis is a big array, even by modern standards.
37 / 81
Images/cinvestav-1.jpg
Example: Airline Flights
ExampleAirline flight matrix.
PropertiesAirports are numbered 1 through nflight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representationn × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-1.jpg
Example: Airline Flights
ExampleAirline flight matrix.
PropertiesAirports are numbered 1 through nflight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representationn × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-1.jpg
Example: Airline Flights
ExampleAirline flight matrix.
PropertiesAirports are numbered 1 through nflight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representationn × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-1.jpg
Example: Airline Flights
ExampleAirline flight matrix.
PropertiesAirports are numbered 1 through nflight(i,j) = list of nonstop flights from airport i to airport j
Assume n = 1000 and you use an integer for representationn × n array of list references =⇒ 4 million bytes when all the
airports are connected
38 / 81
Images/cinvestav-1.jpg
Example: Airline Flights
HoweverThe total number of flights is way lesser =⇒ 20,000
Needed StorageWe need at most 20,000 list references =⇒ Thus, we need at most 80,000bytes
39 / 81
Images/cinvestav-1.jpg
Example: Airline Flights
HoweverThe total number of flights is way lesser =⇒ 20,000
Needed StorageWe need at most 20,000 list references =⇒ Thus, we need at most 80,000bytes
39 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Web page matrixWeb pages are numbered 1 through n.web(i,j) = number of links from page i to page j.
Web AnalysisAuthority page ... page that has many links to it.Hub page ... links to many authority pages.
40 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Web page matrixWeb pages are numbered 1 through n.web(i,j) = number of links from page i to page j.
Web AnalysisAuthority page ... page that has many links to it.Hub page ... links to many authority pages.
40 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Web page matrixWeb pages are numbered 1 through n.web(i,j) = number of links from page i to page j.
Web AnalysisAuthority page ... page that has many links to it.Hub page ... links to many authority pages.
40 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Web page matrixWeb pages are numbered 1 through n.web(i,j) = number of links from page i to page j.
Web AnalysisAuthority page ... page that has many links to it.Hub page ... links to many authority pages.
40 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Something Notablen= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representationn × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
PropertiesEach 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,000x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Something Notablen= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representationn × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
PropertiesEach 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,000x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Something Notablen= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representationn × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
PropertiesEach 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,000x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Something Notablen= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representationn × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
PropertiesEach 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,000x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-1.jpg
Web Page Matrix
Something Notablen= 2,000,000,000 (and growing by 1 million a day)
If we used integers for representationn × n array of integers =⇒16 ∗ 1018 bytes (16 ∗ 109 GB)
PropertiesEach 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,000x 4 bytes = 80,000,000,000 bytes (80 GB)
41 / 81
Images/cinvestav-1.jpg
What we need...
We need...There are ways to represent sparse arrays efficiently
42 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionSparse ... many elements are zeroDense ... few elements are zero
Examples of structured sparse matricesDiagonalTridiagonalLower triangular (Actually in the Middle of Sparse and Dense)
ActuallyThey may be mapped into a 1D array so that a mapping function can beused to locate an element.
43 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionSparse ... many elements are zeroDense ... few elements are zero
Examples of structured sparse matricesDiagonalTridiagonalLower triangular (Actually in the Middle of Sparse and Dense)
ActuallyThey may be mapped into a 1D array so that a mapping function can beused to locate an element.
43 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionSparse ... many elements are zeroDense ... few elements are zero
Examples of structured sparse matricesDiagonalTridiagonalLower triangular (Actually in the Middle of Sparse and Dense)
ActuallyThey may be mapped into a 1D array so that a mapping function can beused to locate an element.
43 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionSparse ... many elements are zeroDense ... few elements are zero
Examples of structured sparse matricesDiagonalTridiagonalLower triangular (Actually in the Middle of Sparse and Dense)
ActuallyThey may be mapped into a 1D array so that a mapping function can beused to locate an element.
43 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionSparse ... many elements are zeroDense ... few elements are zero
Examples of structured sparse matricesDiagonalTridiagonalLower triangular (Actually in the Middle of Sparse and Dense)
ActuallyThey may be mapped into a 1D array so that a mapping function can beused to locate an element.
43 / 81
Images/cinvestav-1.jpg
Sparse Matrices
DefinitionSparse ... many elements are zeroDense ... few elements are zero
Examples of structured sparse matricesDiagonalTridiagonalLower triangular (Actually in the Middle of Sparse and Dense)
ActuallyThey may be mapped into a 1D array so that a mapping function can beused to locate an element.
43 / 81
Images/cinvestav-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
44 / 81
Images/cinvestav-1.jpg
Representation Of Unstructured Sparse Matrices
We can use aSingle linear list in row-major order.
ThusWe scan the non-zero elements of the sparse matrix in row-majororder.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-1.jpg
Representation Of Unstructured Sparse Matrices
We can use aSingle linear list in row-major order.
ThusWe scan the non-zero elements of the sparse matrix in row-majororder.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-1.jpg
Representation Of Unstructured Sparse Matrices
We can use aSingle linear list in row-major order.
ThusWe scan the non-zero elements of the sparse matrix in row-majororder.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-1.jpg
Representation Of Unstructured Sparse Matrices
We can use aSingle linear list in row-major order.
ThusWe scan the non-zero elements of the sparse matrix in row-majororder.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-1.jpg
Outline1 Introduction
1D Arrays2D Arrays
2 Representation2D Array Representation
3 Improving the representationRow-Major Mapping
4 MatrixDefinitionTypes of Matrices
5 Sparse MatricesSparse MatricesRepresentation Of Unstructured Sparse MatricesSparse Arrays
46 / 81
Images/cinvestav-1.jpg
Example with an Sparse Array
FirstWe will start with sparse one-dimensional arrays, which are simpler
Example
47 / 81
Images/cinvestav-1.jpg
Example with an Sparse Array
FirstWe will start with sparse one-dimensional arrays, which are simpler
Example
47 / 81
Images/cinvestav-1.jpg
Representation
we can have the following representation
Where1 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-1.jpg
Representation
we can have the following representation
Where1 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-1.jpg
Representation
we can have the following representation
Where1 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-1.jpg
Representation
we can have the following representation
Where1 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-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Sparse Array ADT
For a one-dimensional array of ItemA constructor:
I SparseArray(int length)
A way to get values from the array:I Object get(int index)
A way to store values in the array:I void add(int index, Item value)
In addition, it is OK to ask for a value from an empty array position1 For number, it should return ZERO.2 For Item, it should return NULL.
49 / 81
Images/cinvestav-1.jpg
Other Operations
What about?1 int length():
I return the size of the original array2 int elementCount() :
I how many non-null values are in the array
QuestionDo we forget something?
50 / 81
Images/cinvestav-1.jpg
Other Operations
What about?1 int length():
I return the size of the original array2 int elementCount() :
I how many non-null values are in the array
QuestionDo we forget something?
50 / 81
Images/cinvestav-1.jpg
Other Operations
What about?1 int length():
I return the size of the original array2 int elementCount() :
I how many non-null values are in the array
QuestionDo we forget something?
50 / 81
Images/cinvestav-1.jpg
Other Operations
What about?1 int length():
I return the size of the original array2 int elementCount() :
I how many non-null values are in the array
QuestionDo we forget something?
50 / 81
Images/cinvestav-1.jpg
Other Operations
What about?1 int length():
I return the size of the original array2 int elementCount() :
I how many non-null values are in the array
QuestionDo we forget something?
50 / 81
Images/cinvestav-1.jpg
Example of Get
Code
p u b l i c I tem get ( i n t i n d e x ) {ChainNode c u r r e n t = t h i s . f i r s t N o d e ;
do {i f ( i n d e x == c u r r e n t . i n d e x ) {
// found c o r r e c t l o c a t i o nr e t u r n c u r r e n t . v a l u e ;
}c u r r e n t = c u r r e n t . nex t ;
} w h i l e ( i n d e x < c u r r e n t . i n d e x && next != n u l l ) ;r e t u r n n u l l ;
}
51 / 81
Images/cinvestav-1.jpg
Time Analysis
FirstWe must search a linked list for a given indexWe can keep the elements in order by index
Expected Time for both get and addIf we have n elements, we have the following complexity O(n).
For the other methodslength, elementCount =⇒ O(1)
52 / 81
Images/cinvestav-1.jpg
Time Analysis
FirstWe must search a linked list for a given indexWe can keep the elements in order by index
Expected Time for both get and addIf we have n elements, we have the following complexity O(n).
For the other methodslength, elementCount =⇒ O(1)
52 / 81
Images/cinvestav-1.jpg
Time Analysis
FirstWe must search a linked list for a given indexWe can keep the elements in order by index
Expected Time for both get and addIf we have n elements, we have the following complexity O(n).
For the other methodslength, elementCount =⇒ O(1)
52 / 81
Images/cinvestav-1.jpg
Problem with this analysis
True factIn an ordinary array, indexing to find an element is the only operation wereally need!!!
True factIn a sparse array, we can do indexing reasonably quickly
False ConclusionIn a sparse array, indexing to find an element is the only operation wereally need
The problem is that in designing the ADT, we did not think enoughabout how it would be used !!!
53 / 81
Images/cinvestav-1.jpg
Problem with this analysis
True factIn an ordinary array, indexing to find an element is the only operation wereally need!!!
True factIn a sparse array, we can do indexing reasonably quickly
False ConclusionIn a sparse array, indexing to find an element is the only operation wereally need
The problem is that in designing the ADT, we did not think enoughabout how it would be used !!!
53 / 81
Images/cinvestav-1.jpg
Problem with this analysis
True factIn an ordinary array, indexing to find an element is the only operation wereally need!!!
True factIn a sparse array, we can do indexing reasonably quickly
False ConclusionIn a sparse array, indexing to find an element is the only operation wereally need
The problem is that in designing the ADT, we did not think enoughabout how it would be used !!!
53 / 81
Images/cinvestav-1.jpg
Look at this code
To find the maximum element in a normal array:
doub l e max = a r r a y [ 0 ] ;f o r ( i n t i = 0 ; i < a r r a y . l e n g t h ; i ++){
i f ( a r r a y [ i ] > max) max = a r r a y [ i ] ;}
54 / 81
Images/cinvestav-1.jpg
Look at this code
To find the maximum element in a sparse array:
Double max = ( Double ) a r r a y . ge t ( 0 ) ;f o r ( i n t i = 0 ; i < a r r a y . l e n g t h ( ) ; i ++){
Double temp = ( Double ) a r r a y . ge t ( i ) ;i f ( temp . compareTo (max) > 0) {
max = temp ;}
}
55 / 81
Images/cinvestav-1.jpg
What is the problem?
FirstA lot of wrapping and casting because using generics.
SecondMore importantly, in a normal array, every element is relevant
ThirdIf a sparse array is 1% full, 99% of its elements will be zero.
I This is 100 times as many elements as we should need to examine
56 / 81
Images/cinvestav-1.jpg
What is the problem?
FirstA lot of wrapping and casting because using generics.
SecondMore importantly, in a normal array, every element is relevant
ThirdIf a sparse array is 1% full, 99% of its elements will be zero.
I This is 100 times as many elements as we should need to examine
56 / 81
Images/cinvestav-1.jpg
What is the problem?
FirstA lot of wrapping and casting because using generics.
SecondMore importantly, in a normal array, every element is relevant
ThirdIf a sparse array is 1% full, 99% of its elements will be zero.
I This is 100 times as many elements as we should need to examine
56 / 81
Images/cinvestav-1.jpg
What is the problem?
FourthOur search time is based on the size of the sparse array, not on thenumber of elements that are actually in it
QuestionWhat to do?
57 / 81
Images/cinvestav-1.jpg
What is the problem?
FourthOur search time is based on the size of the sparse array, not on thenumber of elements that are actually in it
QuestionWhat to do?
57 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Something NotableAlthough “stepping through an array” is not a fundamental operation onan array, it is one we do frequently.
HoweverThis is a very expensive thing to do with a sparse arrayThis should not be so expensive:
I We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Something NotableAlthough “stepping through an array” is not a fundamental operation onan array, it is one we do frequently.
HoweverThis is a very expensive thing to do with a sparse arrayThis should not be so expensive:
I We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Something NotableAlthough “stepping through an array” is not a fundamental operation onan array, it is one we do frequently.
HoweverThis is a very expensive thing to do with a sparse arrayThis should not be so expensive:
I We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Something NotableAlthough “stepping through an array” is not a fundamental operation onan array, it is one we do frequently.
HoweverThis is a very expensive thing to do with a sparse arrayThis should not be so expensive:
I We have a list, and all we need to do is step through it
58 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Poor SolutionLet 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 SolutionUse an Inner iterator!!!
59 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Poor SolutionLet 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 SolutionUse an Inner iterator!!!
59 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Poor SolutionLet 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 SolutionUse an Inner iterator!!!
59 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Poor SolutionLet 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 SolutionUse an Inner iterator!!!
59 / 81
Images/cinvestav-1.jpg
Fixing the ADT
Poor SolutionLet 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 SolutionUse an Inner iterator!!!
59 / 81
Images/cinvestav-1.jpg
Example
Codepublic class SparseArrayIterator<Item> implements Iterator<Item> {
// any data you need to keep track of goes hereSparseArrayIterator() { ...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-1.jpg
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 . ge t ( 0 ) ;
w h i l e ( i t e r a t o r . hasNext ( ) ) {temp = ( Double ) i t e r a t o r . nex t ( ) ;i f ( temp . compareTo (max) > 0) {
max = temp ;}
}
61 / 81
Images/cinvestav-1.jpg
HoweverProblem
Our SparseArrayIterator is fine for stepping through the elements ofan array, but...
I It does not tell us what index they were at.I For some problems, we may need this information
First SolutionRevise our iterator to tell us, not the value in each list cell, but the indexin each list cell
ProblemSomewhat more awkward to use, since we would needarray.get(iterator.next()) instead of just iterator.next().But it’s worse than that, because next is defined to return an Item, sowe would have to wrap the index.We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-1.jpg
HoweverProblem
Our SparseArrayIterator is fine for stepping through the elements ofan array, but...
I It does not tell us what index they were at.I For some problems, we may need this information
First SolutionRevise our iterator to tell us, not the value in each list cell, but the indexin each list cell
ProblemSomewhat more awkward to use, since we would needarray.get(iterator.next()) instead of just iterator.next().But it’s worse than that, because next is defined to return an Item, sowe would have to wrap the index.We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-1.jpg
HoweverProblem
Our SparseArrayIterator is fine for stepping through the elements ofan array, but...
I It does not tell us what index they were at.I For some problems, we may need this information
First SolutionRevise our iterator to tell us, not the value in each list cell, but the indexin each list cell
ProblemSomewhat more awkward to use, since we would needarray.get(iterator.next()) instead of just iterator.next().But it’s worse than that, because next is defined to return an Item, sowe would have to wrap the index.We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-1.jpg
HoweverProblem
Our SparseArrayIterator is fine for stepping through the elements ofan array, but...
I It does not tell us what index they were at.I For some problems, we may need this information
First SolutionRevise our iterator to tell us, not the value in each list cell, but the indexin each list cell
ProblemSomewhat more awkward to use, since we would needarray.get(iterator.next()) instead of just iterator.next().But it’s worse than that, because next is defined to return an Item, sowe would have to wrap the index.We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-1.jpg
HoweverProblem
Our SparseArrayIterator is fine for stepping through the elements ofan array, but...
I It does not tell us what index they were at.I For some problems, we may need this information
First SolutionRevise our iterator to tell us, not the value in each list cell, but the indexin each list cell
ProblemSomewhat more awkward to use, since we would needarray.get(iterator.next()) instead of just iterator.next().But it’s worse than that, because next is defined to return an Item, sowe would have to wrap the index.We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-1.jpg
HoweverProblem
Our SparseArrayIterator is fine for stepping through the elements ofan array, but...
I It does not tell us what index they were at.I For some problems, we may need this information
First SolutionRevise our iterator to tell us, not the value in each list cell, but the indexin each list cell
ProblemSomewhat more awkward to use, since we would needarray.get(iterator.next()) instead of just iterator.next().But it’s worse than that, because next is defined to return an Item, sowe would have to wrap the index.We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-1.jpg
HoweverProblem
Our SparseArrayIterator is fine for stepping through the elements ofan array, but...
I It does not tell us what index they were at.I For some problems, we may need this information
First SolutionRevise our iterator to tell us, not the value in each list cell, but the indexin each list cell
ProblemSomewhat more awkward to use, since we would needarray.get(iterator.next()) instead of just iterator.next().But it’s worse than that, because next is defined to return an Item, sowe would have to wrap the index.We could deal with this by overloading get to take an Item argument.
62 / 81
Images/cinvestav-1.jpg
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 boo l ean hasNext ( ){ // j u s t l i k e b e f o r e }
p u b l i c i n t nex t ( ) {i n t i n d e x = c u r r e n t . i n d e x ;c u r r e n t = c u r r e n t . nex t ;r e t u r n i n d e x ;
}}
63 / 81
Images/cinvestav-1.jpg
Using Array Linear List for Matrices
Example 0 0 3 0 40 0 5 7 00 0 0 0 00 2 6 0 0
Thus
list =row 1 1 2 2 4 4
column 3 5 3 4 2 3value 3 4 5 6 2 6
65 / 81
Images/cinvestav-1.jpg
Using Array Linear List for Matrices
Example 0 0 3 0 40 0 5 7 00 0 0 0 00 2 6 0 0
Thus
list =row 1 1 2 2 4 4
column 3 5 3 4 2 3value 3 4 5 6 2 6
65 / 81
Images/cinvestav-1.jpg
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 informationNode element[] = new Node[1000];
66 / 81
Images/cinvestav-1.jpg
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 ;// Us ing Gene r i c i n Javap u b l i c c l a s s Node<Item>{
// e l ement sp r i v a t e i n t row ;p r i v a t e i n t columnp r i v a t e Item va l u e ;// c o n s t r u c t o r s come he r e
}
Thus you declare an array element with the Node informationNode element[] = new Node[1000];
66 / 81
Images/cinvestav-1.jpg
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 ;// Us ing Gene r i c i n Javap u b l i c c l a s s Node<Item>{
// e l ement sp r i v a t e i n t row ;p r i v a t e i n t columnp r i v a t e Item va l u e ;// c o n s t r u c t o r s come he r e
}
Thus you declare an array element with the Node informationNode element[] = new Node[1000];
66 / 81
Images/cinvestav-1.jpg
What about other representations?
We can use chains tooWe need to modify our node
To something like this
Graphically
67 / 81
Images/cinvestav-1.jpg
What about other representations?
We can use chains tooWe need to modify our node
To something like thispackage d a t a S t r u c t u r e s ;// Us ing G e n e r i c i n Javap u b l i c c l a s s Node<Item >{
// e l ement sp 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 he r e
}
Graphically
67 / 81
Images/cinvestav-1.jpg
What about other representations?We can use chains tooWe need to modify our node
To something like thispackage d a t a S t r u c t u r e s ;// Us ing G e n e r i c i n Javap u b l i c c l a s s Node<Item >{
// e l ement sp 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 he r e
}
Graphically
67 / 81
Images/cinvestav-1.jpg
One Linear List Per RowExample
0 0 3 0 40 0 5 7 00 0 0 0 00 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-1.jpg
One Linear List Per RowExample
0 0 3 0 40 0 5 7 00 0 0 0 00 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-1.jpg
One Linear List Per RowExample
0 0 3 0 40 0 5 7 00 0 0 0 00 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-1.jpg
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-1.jpg
We have a problem here
With 99% of the matrix as zeros of nullsWe have the problem that many row pointers are null...
What can you do?Ideas
72 / 81
Images/cinvestav-1.jpg
We have a problem here
With 99% of the matrix as zeros of nullsWe have the problem that many row pointers are null...
What can you do?Ideas
72 / 81
Images/cinvestav-1.jpg
However
Problems, problems Will Robinson!!!Ideas?
Yes, access!!We have a problem there!!
It isNot so fast!!!! SOLUTIONS???
73 / 81
Images/cinvestav-1.jpg
However
Problems, problems Will Robinson!!!Ideas?
Yes, access!!We have a problem there!!
It isNot so fast!!!! SOLUTIONS???
73 / 81
Images/cinvestav-1.jpg
However
Problems, problems Will Robinson!!!Ideas?
Yes, access!!We have a problem there!!
It isNot so fast!!!! SOLUTIONS???
73 / 81
Images/cinvestav-1.jpg
Orthogonal List Representation
More complexityBoth row and column lists.
Node Structure
Example 0 0 3 0 40 0 5 7 00 0 0 0 00 2 6 0 0
74 / 81
Images/cinvestav-1.jpg
Orthogonal List Representation
More complexityBoth row and column lists.
Node Structure
Example 0 0 3 0 40 0 5 7 00 0 0 0 00 2 6 0 0
74 / 81
Images/cinvestav-1.jpg
Orthogonal List Representation
More complexityBoth row and column lists.
Node Structure
Example 0 0 3 0 40 0 5 7 00 0 0 0 00 2 6 0 0
74 / 81
Images/cinvestav-1.jpg
Go One Step Further
QuestionWe have another problem the sparse arrays in the column and rowpointers!!!
ThusHow it will look like for this (You can try!!!)
0 1 0 0 20 0 0 0 00 0 2 0 00 0 0 0 00 0 1 0 1
(3)
78 / 81
Images/cinvestav-1.jpg
Go One Step Further
QuestionWe have another problem the sparse arrays in the column and rowpointers!!!
ThusHow it will look like for this (You can try!!!)
0 1 0 0 20 0 0 0 00 0 2 0 00 0 0 0 00 0 1 0 1
(3)
78 / 81
Images/cinvestav-1.jpg
Approximate Memory Requirements
Example500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytesSingle Array List 3 x 1994 x 4 = 23,928 bytesOne Chain Per Row 23928 + 500 x 4 = 25,928What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-1.jpg
Approximate Memory Requirements
Example500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytesSingle Array List 3 x 1994 x 4 = 23,928 bytesOne Chain Per Row 23928 + 500 x 4 = 25,928What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-1.jpg
Approximate Memory Requirements
Example500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytesSingle Array List 3 x 1994 x 4 = 23,928 bytesOne Chain Per Row 23928 + 500 x 4 = 25,928What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-1.jpg
Approximate Memory Requirements
Example500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytesSingle Array List 3 x 1994 x 4 = 23,928 bytesOne Chain Per Row 23928 + 500 x 4 = 25,928What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-1.jpg
Approximate Memory Requirements
Example500 x 500 matrix with 1994 nonzero elements
2D array 500 x 500 x 4 = 1 million bytesSingle Array List 3 x 1994 x 4 = 23,928 bytesOne Chain Per Row 23928 + 500 x 4 = 25,928What about the sparse version even in row and column pointer?
80 / 81
Images/cinvestav-1.jpg
Runtime PerformanceExample Matrix Transpose500 x 500 matrix with 1994 nonzero elements:
Method Time2D Array 210 ms
Single Array List 6 msOne Chain per Row 12 ms
Example Matrix Addition500 x 500 matrix with 1994 nonzero elements:
Method Time2D Array 880 ms
Single Array List 18 msOne Chain per Row 29 ms
81 / 81
Images/cinvestav-1.jpg
Runtime PerformanceExample Matrix Transpose500 x 500 matrix with 1994 nonzero elements:
Method Time2D Array 210 ms
Single Array List 6 msOne Chain per Row 12 ms
Example Matrix Addition500 x 500 matrix with 1994 nonzero elements:
Method Time2D Array 880 ms
Single Array List 18 msOne Chain per Row 29 ms
81 / 81