1 / 21

Sparse Matrix ADT

Sparse Matrix ADT. Data Structures 4 th Week. Matrix in General. m rows n columns store a two dimensional matrix in an array. Sparse Matrix is …. Sparse Matrix ADT. structure Sparse_Matrix is objects: a set of triples, < row, column, value > functions:

korene
Download Presentation

Sparse Matrix ADT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sparse Matrix ADT Data Structures 4th Week

  2. Matrix in General • m rows • n columns • store a two dimensional matrix in an array

  3. Sparse Matrix is ….

  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.

  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

  8. Transposing a Matrix • Bad approach: why? for each row i take element <i, j, value> and store it aselement <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.

  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 isO(columns*elements) • When # of elements is order ofcolumns*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

  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 for (i = 1; i <= num_terms; i++) num_terms 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 • 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 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 column row A B Transpose of B i j row_begin b a 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)

More Related