Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix.

Similar presentations


Presentation on theme: "Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix."— Presentation transcript:

1 Data Structure (Part II) Chapter 2 – Arrays

2 Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix. A 5×3 matrix has 15 entries. entry

3 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. The matrix is square. It has 4 rows and 4 columns. A(3,2) =0 Col 0Col 1Col 2Col 3 Row 0 Row 1 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).

4 Matrix Operations Transposition –Suppose A is a m×n matrix. –A T is called the transpose of A where A T is n×m and A T (i, j) = A(j, i) for i = 0, …., m-1, and j = 0, …, n-1. –Example: A(0, 2) = A T (2, 0) = 5

5 Matrix Operations Addition: –Suppose A is a R A × C A matrix and B a R B × C B matrix. –To sum up A and B, R A must equal to R B ; C A must equal to C B. –If C = A + B, then C(i, j) = A(i, j) + B(i, j) for i=0, …, R A -1, j=0, …, C A -1. –Example: 3 + 0

6 Matrix Operations Multiplication –Suppose A is a R A ×C A matrix and B a R B ×C B matrix. –To compute A×B, C A must equal to R B. If C = A×B, C is a R A ×C B matrix, where C(i, j) = Σ(A(i, k) . A(k, j)). –Example: C(2, 1) Row 2 Col 1

7 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( ).

8 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.

9 ADT SparseMatrix class SparseMatrix { public: //r rows, c columns, a capacity of t nonzero entries. SparseMatrix(int r, int c, int t); //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); };

10 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 to store. –The triples are ordered by rows and within rows by columns.

11 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 01234567 15 91 113 28 22 -6 -15 2.4.2 Sparse Matrix Representation Example 15 0 0 0 91 0 0 11 0 0 0 0 0 3 0 0 0 28 22 0 -6 0 0 0 0 0 0 0 0 0 -15 0 0 0 0 0 0 1 2 3 4 5 012345 01234567

12 2.4.2 Sparse Matrix Representation class SparseMatrix; class MatrixEntry { friend class SparseMatrix; private: int row, col, value; }; class SparseMatrix { private: int rows, cols, terms, capacity; MatrixEntry *smArray; };

13 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 2.4.3 Transposing a Matrix Sparse matrix and its transpose 01234567 smArray R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 R: 0 C: 0 15 R: 3 C: 0 22 R: 5 C: 0 -15 R: 1 C: 1 11 R: 2 C: 1 3 R: 3 C: 2 -6 R: 0 C: 4 91 R: 2 C: 5 28 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 R: 0 C: 0 15 R: 4 C: 0 91

14 Algorithm of Program 2.10 SparseMatrix SparseMatrix::Transpose() { 1Construct a SparseMatrix, b(cols, rows, terms), as output result; 2currentB = 0; 3If (terms > 0) { 4 For (c = 0; c < cols; c++) 5 For (i=0; i < terms; i++) 6 If (smArray[i].col == c) { 7 b.smArray[currentB].row = smArray[i].col; 8 b.smArray[currentB].col = smArray[i].row; 9 b.smArray[currentB++].value = smArray[i].value; 10 } 11} 12Return b; }

15 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 Algorithm of Program 2.10 01234567 smArray R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: 5 -15 R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 b.smArray currentB 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 = 0 Line 5: For (i=0; i < terms; i++) Line 6: If (smArray[i].col == )c0 R: 0 C: R: 0 C: 0 R: 0 C: 0 15 R: 0 C: R: 0 C: 4 R: 0 C: 4 91 Line 7: b.smArray[currentB].row = smArray[i].col;Line 8: b.smArray[currentB].col = smArray[i].row;Line 9: b.smArray[currentB++].value = smArray[i].value; c = 1 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: 0 -15

16 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).

17 Considerations In worst case, the matrix contains rows . cols entries. Therefore, the time complexity becomes O(cols . terms) = O(rows . cols 2 ) –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).

18 How to locate the entry in transposed smArray? R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 01234567 R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: 5 -15 R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 R: 0 C: 0 15 R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: 0 -15 ? R: 0 C: 0 15 R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: 0 -15 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: 5 -15 R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 Col: 01234 02357 rowStart: 7 5 21220 rowSize: 1

19 Algorithm of Program 2.11 SparseMatrix SparseMatrix::FastTranspose() { 1.Construct a SparseMatrix, b(cols, rows, terms), as output result; 2.If (terms > 0) { 3. Let rowSize be an integer array of size cols. 4. Let rowStart be an integer array of size cols. 5. Initialize each element in rowSize to 0. 6. For (i=0; i<terms; i++) 7. rowSize [smArray[i].col]++; 8. rowStart [0] = 0; 9. For (i = 1; i < cols; i++) 10. rowStart [i] = rowSize [i-1] + rowStart [i-1]; 11. For (i=0; i < terms; i++) { 12. j = rowStart [ smArray [i].col ]; 13. Copy smArray[ i ] to smArray[ j ]; 14. Interchange row and col of smArray[ j ]; 15. rowStart [ smArray [i].col ]++; 16. } 17.} 18.Return b; }

20 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 01234567 R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: 5 -15 R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 R: 0 C: 0 15 R: 3 C: 0 22 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: 5 -15 R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 Col: 01234 5 00000 rowSize: 010000 010010 010010 111010 111110 111120 121120 121220 1 02357 rowStart: 7 0 02 023 0235 02357 02357 7 i 12357 7 12367 7 R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 2 -6 R: 5 C: 0 -15 12367 rowStart: 8

21 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).

22 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.

23 2.4.4 Matrix Multiplication Definition: –Given two matrices, a mxn and b nxp. The product matrix d has dimension m x p. Its (i, j) element is

24 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.

25 2.4.4 Matrix Multiplication 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. i j

26 StoreSum() and ChangeSize() ChangeSize1D(int newSize) –Program 2.13 –To change the size of smArray to newSize. StoreSum(int sum, int r, int c) –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.

27 Program 2.14 R: 0 C: 0 15 R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: 0 -15 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: 3 1522-15113-69128 R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: 5 -15 R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 currColIndexcurrRowIndex R: 6 C: -1 dummy currRowA currColB d:d: currRowBegin Sum = 225 225 Sum = 0


Download ppt "Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix."

Similar presentations


Ads by Google