Outline Chapter4 Arrays and Matrices
• Arrays • Matrices • Special Matrices – Diagonal Matrices – Tridiagonal Matrix – Triangular Matrices – Symmetric Matrices
• Sparse Matrices 1
2
For example
The Abstract Data Type of Arrays AbstractDataType Array{ instrances
• High={(0,82),(1,79),(2,85),(3,92),(4,88),(5, 89),(6,91)} • Store(3,83); • Retrieve(5)
set of (index, value)pairs, no two pairs have the same index
operations Create(): create an empty array Store(index, value): Retrieve(index)
} 3
4
1D Array Representation In C, and C++
Space Overhead
Memory a
b
c
Memory
d
a
start
b
c
d
start
• 1-dimensional array x = [a, b, c, d] • map into contiguous memory locations
space overhead = 4 bytes for start + 4 bytes for x.length = 8 bytes (excludes space needed for the elements of x)
• location(x[i]) = start + I 5
6
2D Arrays
Rows Of A 2D Array
The elements of a 2-dimensional array a declared as: a = new int[3][4]; may 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]
a[0][0] a[1][0] a[2][0]
a[0][1] a[1][1] a[2][1]
a[0][2] a[0][3] a[1][2] a[1][3] a[2][2] a[2][3]
row 0 row 1 row 2
7
2D Array Representation In C, and C++
Columns Of A 2D Array a[0][0] a[1][0] a[2][0]
a[0][1] a[1][1] a[2][1]
8
2-dimensional array x a, b, c, d e, f, g, h i, j, k, l
a[0][2] a[0][3] a[1][2] a[1][3] a[2][2] a[2][3]
column 0 column 1 column 2 column 3
9
view 2D array as a 1D array of rows x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l] and store as 4 1D arrays
2D Array Representation In C, and C++ x[]
10
Space Overhead x[]
a
b
c
d
a
b
c
d
e
f
g
h
e
f
g
h
i
j
k
l
i
j
k
l
space overhead = overhead for 4 1D arrays = 4 * 8 bytes = 32 bytes = (number of rows + 1) x 8 bytes
x.length = 3 x[0].length = x[1].length = x[2].length = 4 11
12
Array Representation In C++
Row-Major Mapping
x[] a
b
c
d
e
f
g
h
i
j
k
l
• Example 3 x 4 array:
• This representation is called the array-of-arrays representation. • Requires contiguous memory of size 3, 4, 4, and 4 for the 4 1D arrays. • 1 memory block of size number of rows and number of rows blocks of size number of columns
• • • •
abcd efgh i jkl Convert into 1D array y by collecting elements by rows. Within a row elements are collected from left to right. Rows are collected from top to bottom. We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l} row 0
row 1
row 2
…
row i
13
14
Locating Element x[i][j] row 0
row 1
• • • • •
row 2
…
Space Overhead
row i
row 0
assume x has r rows and c columns each row has c elements i rows to the left of row i so ic elements to the left of x[i][0] so x[i][j] is mapped to position ic + j of the 1D array
row 1
row 2
…
row i
4 bytes for start of 1D array + 4 bytes for length of 1D array + 4 bytes for c (number of columns) = 12 bytes (number of rows = length /c) 15
16
Column-Major Mapping
Disadvantage
• •
Need contiguous memory of size rc.
• • 17
abcd efgh i jkl 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. We get y = {a, e, i, b, f, j, c, g, k, d, h, l} 18
Class Array2D template class Array2D { friend ostream& operator n) throw OutOfBounds(); // (i,j) in lower triangle iff i >= j if (i >= j) t[i*(i-1)/2+j-1] = x; else if (x != 0) throw MustBeZero(); return *this; }
43
Retrieve method
44
Sparse Matrices
template T LowerMatrix::Retrieve(int i, int j) const {// Retrieve L(i,j). if ( i < 1 || j < 1 || i > n || j > n) throw OutOfBounds();
sparse … many elements are zero dense … few elements are zero
// (i,j) in lower triangle iff i >= j if (i >= j) return t[i*(i-1)/2+j-1]; else return 0; } 45
Example Of Sparse Matrices
46
Unstructured Sparse Matrices
diagonal tridiagonal lower triangular (?)
Airline flight matrix. airports are numbered 1 through n flight(i,j) = list of nonstop flights from airport i to airport j n = 1000 (say) n x n array of list references => 4 million bytes total number of flights = 20,000 (say) need at most 20,000 list references => at most 80,000 bytes
These are structured sparse matrices. May be mapped into a 1D array so that a mapping function can be used to locate an element. 47
48
Web Page Matrix
Unstructured Sparse Matrices
n = 2 billion (and growing by 1 million a day) n x n array of ints => 16 * 1018 bytes (16 * 109 GB) each page links to 10 (say) other pages on average on average there are 10 nonzero entries per row space needed for nonzero elements is approximately 20 billion x 4 bytes = 80 billion bytes (80 GB)
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
49
Representation Of Unstructured Sparse Matrices
50
Single Linear List Example
Single linear list in row-major order. scan the nonzero elements of the sparse matrix in rowmajor 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)
00304 00570 00000 02600
list = row 1 1 2 2 4 4 column 3 5 3 4 2 3 value 3 4 5 7 2 6
51
Array Linear List Representation
list =
Class Term
row 1 1 2 2 4 4 column 3 5 3 4 2 3 value 3 4 5 7 2 6
element row column value
0 1 3 3
1 1 5 4
2 2 3 5
3 2 4 7
4 4 2 2
52
template class Term { friend SparseMatrix; private: int row, col; T value; };
5 4 3 6 53
54
Single Chain
Chain Representation Node structure. list row value
row = column value
1 1 2 2 4 4 3 5 3 4 2 3 3 4 5 7 2 6
col next
1 3 3
1 5 4
2 3 5
2 4 7
4 2 2
4 3 6 null
firstNode 55
56
Array Of Row Chains
One Linear List Per Row
Node structure. row1 = [(3, 3), (5,4)] row2 = [(3,5), (4,7)] row3 = [] row4 = [(2,2), (3,6)]
00304 00570 00000 02600
next col value
57
Array Of Row Chains
Orthogonal List Representation Both row and column lists.
null
00304 00570 00000 02600
58
3
3
5
3
5
4
2
2
3
4
Node structure.
null 7
row down
col next
value
null
null 6
row[] 59
60
Row Lists
Column Lists
1 3 3
00304 00570 00000 02600
2 3 5
1 5 4 n 2 4 7 n
null
4 2 2
1 3 3
00304 00570 00000 02600
2 3 5
4 3 6 n
4 2 2 n
1 5 4 n 2 4 7
4 3 6 n
61
62
Orthogonal Lists
Variations 1 3 3
00304 00570 00000 02600
2 3 5
1 5 4 n n
May use circular lists instead of chains.
2 4 7 n
null
4 2 2 n row[]
4 3 6 n n
63
Approximate Memory Requirements 500 x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row 23928 + 500 x 4 = 25,928
65
64
Runtime Performance Matrix Transpose 500 x 500 matrix with 1994 nonzero elements 2D array Single Array List One Chain Per Row
210 ms 6 ms 12 ms 66
Performance
End of Chapter4
Matrix Addition. 500 x 500 matrices with 1994 and 999 nonzero elements 2D array Single Array List One Chain Per Row
• Homework: • Chapter4 11, 25, 37.
880 ms 18 ms 29 ms 67
E11
68
E25
69
E25
70
E37
71
72
E37
73