270 likes | 414 Views
Data Structure (Part II). Chapter 2 – Arrays. n = 3. entry. m = 5. Matrix. A matrix with 5 rows and 3 columns can be represented by. We say this is a 5×3 matrix. A 5×3 matrix has 15 entries. Matrix. Suppose a m × n matrix A . If m = n , we call the matrix square .
E N D
Data Structure (Part II) Chapter 2 – Arrays
n = 3 entry m = 5 Matrix • A matrix with 5 rows and 3 columns can be represented by • We say this is a 5×3 matrix. • A 5×3 matrix has 15 entries.
Matrix • Suppose a m×n matrix A. If m = n, we call the matrix square. • We use two-tuple index to locate a matrix entry. • Example: • A(3, 2): entry locating at row 3, column 2. Col 0 Col 1 Col 2 Col 3 • The matrix is square. It has 4 rows and 4 columns. Row 0 Row 1 • A(3,2) = 0 Row 2 Row 3 In C++, it is natural to store a matrix in a 2D array, say a[m][n], and use a[i][j] to access the entry at (i, j).
Matrix Operations • Transposition • Suppose A is a m×n matrix. • AT is called the transpose of A where AT is n×m and AT(i, j) = A(j, i) for i = 0, …., m-1, and j = 0, …, n-1. • Example: A(0, 2) = AT(2, 0) = 5
Matrix Operations • Addition: • Suppose A is a RA×CA matrix and B a RB×CB matrix. • To sum up A and B, RA must equal to RB; CA must equal to CB. • If C = A + B, then C(i, j) = A(i, j) + B(i, j) for i=0, …, RA-1, j=0, …, CA-1. • Example: 3 + 0
Col 1 Row 2 Matrix Operations • Multiplication • Suppose A is a RA×CA matrix and B a RB×CB matrix. • To compute A×B, CA must equal to RB. If C =A×B, C is a RA×CB matrix, where C(i, j) = Σ(A(i, k).A(k, j)). • Example: C(2, 1)
Sparse Matrix • A sparse matrix is a matrix that has many zero entries. • This is a __×__ matrix. • There are _____ entries. • There are _____ nonzero entries. • There are _____ zero entries. Consider we use a 2D array to represent a n×n sparse matrix. How many entries are required? _____ entries. The space complexity is O( ).
2.4 Sparse Matrix • If n is large, say n = 5000, we need 25 million elements to store the matrix. • 25 million units of time for operation such as addition and transposition. • Using a representation that stores only the nonzero entries can reduce the space and time requirements considerably.
ADT SparseMatrix classSparseMatrix { public: //r rows, c columns, a capacity of t nonzero entries. SparseMatrix(intr, intc, intt); //interchange the row and column value of every tuple in *this SparseMatrix &Transpose(); //If the dimensions of *this and b are the same, sum up a*this and b. //Otherwise, throw exception. SparseMatrix &Add(SparseMatrix &b); //If the number of columns in *this equals to the number of rows in b, //compute and return the multiplication of *this and b. SparseMatrix &Multiply(SparseMatrix &b); };
2.4.2 Sparse Matrix Representation • The information we need to know • The number of rows • The number of columns • The number of nonzero entries • All the nonzero entries are stored in an array. Therefore, we also have to know • The capacity of the array • Each element contains a triple <row, col, value> to store. • The triples are ordered by rows and within rows by columns.
15 22 -15 11 3 -6 91 28 0 1 2 3 4 5 6 7 R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 2.4.2 Sparse Matrix Representation • Example 0 1 2 3 4 5 0 15 0 0 22 0 -15 1 0 11 3 0 0 0 2 0 0 0 -6 0 0 3 0 0 0 0 0 0 4 91 0 0 0 0 0 5 0 0 28 0 0 0 0 1 2 3 4 5 6 7
2.4.2 Sparse Matrix Representation classSparseMatrix; classMatrixEntry { friendclassSparseMatrix; private: introw, col, value; }; classSparseMatrix { private: introws, cols, terms, capacity; MatrixEntry *smArray; };
0 1 2 3 4 5 6 7 R: 0 C: 0 R: 4 C: 0 15 15 15 22 22 -15 -15 11 11 3 3 -6 -6 91 91 91 28 28 R: 0 C: 0 R: 3 C: 0 R: 5 C: 0 R: 1 C: 1 R: 2 C: 1 R: 3 C: 2 R: 0 C: 4 R: 2 C: 5 15 22 -15 11 3 -6 91 28 R: 0 C: 0 R: 0 C: 0 R: 0 C: 3 R: 0 C: 3 R: 0 C: 5 R: 0 C: 5 R: 1 C: 1 R: 1 C: 1 R: 1 C: 2 R: 1 C: 2 R: 2 C: 3 R: 2 C: 3 R: 4 C: 0 R: 4 C: 0 R: 5 C: 2 R: 5 C: 2 2.4.3 Transposing a Matrix • Sparse matrix and its transpose smArray smArray Consider column 0. For all elements in column 0 Store (i, 0, value) of the original matrix as (0, i, value) of the transpose
Algorithm of Program 2.10 SparseMatrix SparseMatrix::Transpose() { • Construct a SparseMatrix, b(cols, rows, terms), as output result; • currentB = 0; • If (terms > 0) { • For (c = 0; c < cols; c++) • For (i=0; i < terms; i++) • If (smArray[i].col == c) { • b.smArray[currentB].row = smArray[i].col; • b.smArray[currentB].col = smArray[i].row; • b.smArray[currentB++].value = smArray[i].value; • } • } • Return b; }
0 1 2 3 4 5 6 7 R: 3 C: 0 R: 2 C: 5 R: 3 C: 2 R: 5 C: 0 R: 2 C: 1 R: 1 C: 1 R: 0 C: R: 0 C: 4 R: 0 C: 0 R: 0 C: 0 R: 0 C: R: 0 C: 4 15 15 22 22 -15 -15 11 11 3 3 -6 -6 -15 22 11 3 91 91 15 -6 91 28 28 28 R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 15 22 -15 11 3 -6 91 28 R: 0 C: 0 R: 0 C: 0 R: 0 C: 3 R: 0 C: 3 R: 0 C: 5 R: 0 C: 5 R: 1 C: 1 R: 1 C: 1 R: 1 C: 2 R: 1 C: 2 R: 2 C: 3 R: 2 C: 3 R: 4 C: 0 R: 4 C: 0 R: 5 C: 2 R: 5 C: 2 currentB Algorithm of Program 2.10 Line 1: Construct a SparseMatrix, b(cols, rows, terms), as output result; Line 2: currentB= 0; Line 3: If (terms > 0) Line 4: For (c = 0; c < cols; c++) c = 1 c = 0 Line 5: For (i=0; i < terms; i++) Line 6: If (smArray[i].col == ) 0 c Line 9: b.smArray[currentB++].value = smArray[i].value; Line 8: b.smArray[currentB].col = smArray[i].row; Line 7: b.smArray[currentB].row = smArray[i].col; smArray b.smArray
Analysis of Transpose() • Space complexity: • We need an extra SparseMatrix to buffer the entries. • O(terms) • Time complexity: • Line 1 to 2: constant time. • Line 4 to 10: cols iterations. • For each iteration, every term is checked through Line 5 to 10. Line 6 is executed exactly terms times. • Line 12: constant time. • Overall, time complexity is O(cols.terms).
Considerations • In worst case, the matrix contains rows.cols entries. Therefore, the time complexity becomes O(cols.terms) = O(rows.cols2) • worse than O(rows.cols) time using the simple form! • Why do we need the for-loop at Line 5? • Because the transposed smArray has to be ordered by columns. • If using a little more space, we can transpose a matrix in O(terms + cols).
0 1 2 3 4 5 6 7 R: 5 C: 0 R: 2 C: 1 R: 2 C: 1 R: 5 C: 0 R: 1 C: 1 R: 0 C: 4 R: 3 C: 0 R: 0 C: 0 R: 2 C: 5 R: 3 C: 2 R: 3 C: 0 R: 2 C: 5 R: 3 C: 2 R: 1 C: 1 R: 0 C: 4 R: 0 C: 0 15 15 22 22 -15 -15 11 11 3 3 -6 -6 91 3 15 91 11 91 11 28 -6 -15 3 91 28 22 22 -6 15 -15 28 28 R: 0 C: 0 R: 0 C: 0 R: 0 C: 3 R: 0 C: 3 R: 0 C: 5 R: 0 C: 5 R: 1 C: 1 R: 1 C: 1 R: 1 C: 2 R: 1 C: 2 R: 2 C: 3 R: 2 C: 3 R: 4 C: 0 R: 4 C: 0 R: 5 C: 2 R: 5 C: 2 15 15 22 22 -15 -15 11 11 3 3 -6 -6 91 91 28 28 7 rowStart: 0 2 3 5 7 Col: 0 1 2 3 4 R: 0 C: 0 R: 0 C: 0 R: 0 C: 3 R: 0 C: 3 R: 0 C: 5 R: 0 C: 5 R: 1 C: 1 R: 1 C: 1 R: 1 C: 2 R: 1 C: 2 R: 2 C: 3 R: 2 C: 3 R: 4 C: 0 R: 4 C: 0 R: 5 C: 2 R: 5 C: 2 rowSize: 2 1 2 2 0 1 • How to locate the entry in transposed smArray? ? 5
Algorithm of Program 2.11 SparseMatrix SparseMatrix::FastTranspose() { • Construct a SparseMatrix, b(cols, rows, terms), as output result; • If (terms > 0) { • Let rowSize be an integer array of size cols. • Let rowStart be an integer array of size cols. • Initialize each element in rowSize to 0. • For (i=0; i<terms; i++) • rowSize [smArray[i].col]++; • rowStart [0] = 0; • For (i = 1; i < cols; i++) • rowStart [i] = rowSize [i-1] + rowStart [i-1]; • For (i=0; i < terms; i++) { • j = rowStart [ smArray [i].col ]; • Copy smArray[ i ] to smArray[ j ]; • Interchange row and col of smArray[ j ]; • rowStart [ smArray [i].col ]++; • } • } • Return b; }
i 0 1 2 3 4 5 6 7 R: 3 C: 0 R: 5 C: 0 R: 0 C: 4 R: 2 C: 5 R: 3 C: 2 R: 2 C: 1 R: 1 C: 1 R: 0 C: 0 15 15 22 22 -15 -15 11 11 3 3 -6 -6 91 28 -15 91 91 22 11 3 15 -6 28 28 R: 0 C: 0 R: 0 C: 0 R: 0 C: 3 R: 0 C: 3 R: 0 C: 5 R: 0 C: 5 R: 1 C: 1 R: 1 C: 1 R: 1 C: 2 R: 1 C: 2 R: 2 C: 3 R: 2 C: 3 R: 4 C: 0 R: 4 C: 0 R: 5 C: 2 R: 5 C: 2 15 15 22 22 -15 -15 11 11 3 3 -6 -6 91 91 28 28 7 8 7 7 7 rowStart: rowStart: rowStart: rowStart: rowStart: rowStart: rowStart: rowStart: rowStart: rowStart: 1 0 0 0 0 0 0 1 0 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 5 5 6 5 5 6 5 7 7 7 7 7 7 Col: 0 1 2 3 4 R: 0 C: 0 R: 0 C: 0 R: 0 C: 3 R: 0 C: 3 R: 0 C: 5 R: 0 C: 5 R: 1 C: 1 R: 1 C: 1 R: 1 C: 2 R: 1 C: 2 R: 2 C: 3 R: 2 C: 3 R: 4 C: 0 R: 4 C: 0 R: 5 C: 2 R: 5 C: 2 rowSize: rowSize: rowSize: rowSize: rowSize: rowSize: rowSize: rowSize: rowSize: 2 2 1 1 1 0 1 1 1 0 1 0 0 1 1 0 1 1 1 0 1 1 0 0 0 0 2 0 1 2 0 1 2 2 1 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 5
Analysis of FastTranspose() • Space complexity: • SparseMatrix b to buffer the entries: O(terms). • rowSize: cols elements; O(cols). • rowStart: cols elements; O(cols). • Overall, O(terms+cols). • Time complexity: • Line 5: O(cols) to initialize rowSize. • Line 6: O(terms) to scan all terms in smArray. • Line 9: O(cols) to compute rowStart. • Line 11 to 16: the loop executes terms times. In each iteration, Line 12 to 15 runs in constant time. Therefore, time complexity is O(terms). • Overall, time complexity of FastTranspose() is O(terms+cols).
Analysis of FastTranspose() • The time of O(terms+cols) becomes O(rows.cols) when terms equals to rows.cols. • The same complexity with the simple form. • However, the actual computation time of FastTranspose will be a bit larger. • When terms is sufficiently small, FastTranspose will be faster.
2.4.4 Matrix Multiplication • Definition: • Given two matrices, amxn and bnxp. The product matrix d has dimension m x p. Its (i, j) element is
2.4.4 Matrix Multiplication • The product of two sparse matrices may no longer be sparse. • We would like to multiply two sparse matrices represented as ordered lists.
2.4.4 Matrix Multiplication j i • We have to find all the elements in column j. • Scan all the elements in smArray? Exhausted! • We can compute the transpose of b. • This puts all column elements in consecutive order.
StoreSum() and ChangeSize() • ChangeSize1D(intnewSize) • Program 2.13 • To change the size of smArray to newSize. • StoreSum(intsum, intr, intc) • Program 2.12 • To store sum in the row of r and the column of c if sum is nonzero. • Invoke ChangeSize1D when smArray is full.
R: 0 C: 0 R: 0 C: 4 R: 1 C: 1 R: 2 C: 1 R: 5 C: 0 R: 2 C: 5 R: 3 C: 2 R: 3 C: 0 15 22 -15 11 3 -6 11 91 15 91 28 -15 3 -6 22 28 R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 R: 6 C: -1 currColB 15 22 -15 11 3 -6 91 28 currRowA currRowIndex currColIndex R: 0 C: 0 R: 0 C: 3 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 5 C: 2 currRowBegin Program 2.14 dummy Sum = 225 Sum = 0 d: 225