Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures 4th Week

Similar presentations


Presentation on theme: "Data Structures 4th Week"— Presentation transcript:

1 Data Structures 4th Week
Sparse Matrix ADT Data Structures 4th Week

2 Matrix in General m rows n columns
store a two dimensional matrix in an array col 0 col 1 col 2 row 0 27 3 4 row 1 6 82 -2 row 2 109 -64 11 row 3 48 47

3 Sparse Matrix is …. col 0 col 1 col 2 col 3 col 4 col 5 row 0 15 22
22 -15 row 1 11 3 row 2 -6 row 3 row 4 91 row 5 28

4 Sparse Matrix ADT structure Sparse_Matrix is
objects: a set of triples, <row, column, value> functions: Sparse_Matrix Create(max_row, max_col) ::= … Sparse_Matrix Transpose(a) ::= … Sparse_Matrix Add(a, b) ::= … Sparse_Matrix Multiply(a, b) ::= …

5 Sparse Matrix ADT Create function #define MAX_TERMS 101
typedef struct { int col; int row; int value; } term; term a[MAX_TERMS];

6 Sparse Matrix Representation
a[0].row: number of rows of the matrix a[0].col: number of columns of the matrix a[0].value: number of nonzero entries The triples are ordered by row and within rows by columns. row col value a[0] 6 8 [1] 15 [2] 3 22 [3] 5 -15 [4] 1 11 [5] 2 [6] -6 [7] 4 91 [8] 28

7 Transposing Matrix Interchange the rows and columns
Each element a[i][j] in the original matrix becomes element b[j][i] in the transpose matrix col 0 col 1 col 2 col 3 col 4 col 5 row 0 15 91 row 1 11 row 2 3 28 row 3 22 -6 row 4 row 5 -15

8 Transposing a Matrix Bad approach: why?
for each row i take element <i, j, value> and store it as element <j, i, value> of the transpose Difficulty: where to put <j, i, value> (0, 0, 15) ====> (0, 0, 15) (0, 3, 22) ====> (3, 0, 22) (0, 5, -15) ====> (5, 0, -15) (1, 1, 11) ====> (1, 1, 11) Move elements down very often. row col value b[0] 6 8 [1] 15 [2] 3 22 [3] 5 -15 [4] 1 11 [5] 2 [6] -6 [7] 4 91 [8] 28

9 Transposing a Matrix Using column indices to determine placement of elements for all elements in column j place element <i, j, value> in element <j, i, value> Algorithms n = a[0].value; if (n > 0) { currentb = 1; for (i = 0; i < a[0].col; i++) /* for all columns */ for (j = 1; j <= n; j++) /* for all nonzero elements */ if (a[j].col == i) { /* in current column */ b[currentb].row = a[j].col; b[currentb].col = a[j].row; b[currentb].value = a[j].value; currentb++; }

10 Analysis of Transpose Algorithm
The total time for the nested for loops is columns*elements Asymptotic time complexity is O(columns*elements) When # of elements is order of columns*rows, O(columns*elements) becomes O(columns2*rows) A simple form algorithm has time complexity O(columns*rows) for (j = 0; j < columns; j++) for (i = 0; i < rows; i++) b[j][i] = a[i][j];

11 Fast Transpose First determine the number of elements in each column of original matrix Then, determine starting position of each row in transpose matrix num_cols = a[0].col; num_term = a[0].value; for (i = 0; i < num_cols; i++) row_terms[i] = 0; for (i = 1; i <= num_terms; i++) row_terms[a[i].col]++; starting_pos[0] = 1; for (i = 1; i < num_cols; i++) starting_pos[i] = starting_pos[i-1] + row_terms[i-1]; for (i = 1; i <= num_terms; i++) { j = starting_pos[a[i].col]++; b[j].row = a[i].col; b[j].col = a[i].row; b[j].value = a[i].value; }

12 Fast Transpose Example
row col value a[0] 6 8 [1] 15 [2] 3 22 [3] 5 -15 [4] 1 11 [5] 2 [6] -6 [7] 4 91 [8] 28 [0] [1] [2] [3] [4] [5] row_terms = 2 1 starting_pos = 3 4 6 8

13 Analysis of Fast Transpose
Four for loops: for (i = 0; i < num_cols; i++) num_cols times for (i = 1; i <= num_terms; i++) num_terms times for (i = 1; i < num_cols; i++) (num_cols – 1) times Time complexity is O(columns + elements) When the number of elements is of the order columns*rows, the time becomes O(columns*rows)

14 Matrix Multiplication
Definition Given A and B where A is mn and B is np, the product matrix D has dimension mp. Its <i, j> element is: for 0  i < m and 0  j < p. Example

15 Classic multiplication algorithm
for (i = 0; i < rows_a; i++) for (j = 0; j < cols_b; j++) { sum = 0; for (k = 0; k < cols_a; k++) sum += (a[i][k] * b[k][j]); d[i][j] = sum; } The time complexity is O(rows_a*cols_a*cols_b)

16 Multiply two sparse matrices
D = A×B Matrices are represented as ordered lists Pick a row of A and find all elements in column j of B for j = 0, 1, 2, …, cols_b – 1 Have to scan all of B to find all the elements in column j Compute the transpose of B first This put all column elements of B in consecutive order Then, just do a merge operation

17 Example row col value b[0] 6 8 [1] 15 [2] 4 91 [3] 1 11 [4] 2 3 [5] 5
15 [2] 4 91 [3] 1 11 [4] 2 3 [5] 5 28 [6] 22 [7] -6 [8] -15 col 0 col 1 col 2 col 3 col 4 col 5 row 0 15 22 -15 row 1 11 3 row 2 -6 row 3 row 4 91 row 5 28 Matrix B Transpose of B

18 mmult [1] int rows_a = a[0].row, cols_a = a[0].col, totala = a[0].value,… int row_begin = 1, row = a[1].row, sum = 0; fast_transpose(b, new_b); a[totala+1].row = rows_a; new_b[totalb+1].row = cols_b; new_b[totalb+1].col = 0; for (i = 1; i <= totala; ) { column = new_b[1].row; for (j = 1; j <= totalb+1; ) { if (a[i].row != row) { storesum(d, &totald, row, column, &sum); i = row_begin; for (; new_b[j].row == column; j++); column = new_b[j].row; }

19 mmult [2] else if (new_b[j].row != column) {
storesum(d, &totald, row, column, &sum); i = row_begin; column = new_b[j].row; } else switch (COMPARE(a[i].col, new_b[j],col)) { case -1: i++; break; // a[i].col < new_b[j],col, go to next term in a case 0: sum += (a[i++].value * new_b[j++].value); break; /* add terms, advance i and j*/ case 1: j++; /* advance to next term in b */ for (; a[i].row; == row; i++) ; row_begin = i; row = a[i].row; d[0].row = rows_a; d[0].col = cols_b; d[0].value = totald;

20 Interpretation 1 2 3 4 1 2 3 4 1 3 2 4 A B Transpose of B column row i
4 1 2 3 4 1 3 2 4 A B Transpose of B row col val [0] 3 4 [1] 1 [2] 2 [3] [4] [5] row col val [0] 3 2 4 [1] 1 [2] [3] [4] row col val [0] 2 3 4 [1] 1 [2] [3] [4] [5] i j row_begin a b new_b

21 Analysis of mmult transpose b: O(cols_b + totalb)
The outer for loop is executed totala times termsrow: the total number of terms in the current row of A Time for one iteration of the outer for loop is O(cols_b*termsrow + totalb) The overall time O((cols_b*termsrow + totalb)) = O(cols_b*total_a + rows_a*totalb) totala≦cols_a*rows_a and totalb≦cols_a*cols_b Time for mmult is at most O(rows_a*cols_a*cols_b)


Download ppt "Data Structures 4th Week"

Similar presentations


Ads by Google