1 / 16

Homework - Chapter 02

Homework - Chapter 02. Due: 2007/11/6. Problem 1. Consider the polynomial representation described in Exercise 9 on Page 94. Write algorithms to perform polynomial addition and multiplication using this representation, and analyze the performance of your approach. Solution 1-1.

cain-henry
Download Presentation

Homework - Chapter 02

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. Homework - Chapter 02 Due: 2007/11/6

  2. Problem 1 • Consider the polynomial representation described in Exercise 9 on Page 94. • Write algorithms to perform polynomial addition and multiplication using this representation, and analyze the performance of your approach.

  3. Solution 1-1 28 for ( ; ai <= finish; ai++) 29 { 30 termArray[free].exp = termArray[ai].exp; 31 termArray[free++].coef = termArray[ai].coef; 32 } 33 for ( ; bi <= B.finish; bi++) 34 { 35 termArray[free].exp = termArray[bi].exp; 36 termArray[free++].coef = termArray[bi].coef; 37 } 38 C.finish = free – 1; 40 return C; 41 } 00 Polynomial Add (Polynomial A, Polynomial B) 01 { 02 Suppose C is a Polynomial for storing addition result; 03 C.start = free; 04 C.finish = C.start – 1; 05 ai = start; 06 bi = B.start; 07 while (ai <= finish and bi <= B.finish) { 08 if (termArray[ai].exp and termArray[bi].exp are equal) { 09 Sum = termArray[ai].coef + termArray[bi].coef; 10 if (Sum is not zero) { 11 termArray[free].exp = termArray[ai].exp; 12 termArray[free++].coef = Sum; 14 } 15 ai++; bi++; 16 } 17 else if (termArray[ai].exp < termArray[bi].exp) { 18 termArray[free].exp = termArray[bi].exp; 19 termArray[free++].coef = termArray[bi].coef; 20 bi++; 21 } 22 else if (termArray[ai].exp < termArray[bi].exp) { 23 termArray[free].exp = termArray[ai].exp; 24 termArray[free++].coef = termArray[ai].coef; 25 ai++; 26 } 27 }

  4. Solution 1-1 • Suppose A and B are two polynomials. Let m and n be the number of nonzero terms in A and B. • Space complexity: • Except the static termArray, the function uses O(1) memory space to accomplish the task. When we consider the space complexity for the class to invoke this function, since it totally consumes at most m+n terms in termArray, it can be said that the space complexity is O(m+n). • Time complexity • Consider the increment of ai and bi in Line 7-27 and Line 28-37. The value of ai increases by 1 from start to finish, while the value of bi also increases by 1 from b.start to b.finish. • The overall time complexity of this function is therefore O(m+n).

  5. Solution 1-2 01 Polynomial Multiply(Polynomial A, Polynomial B) 02 { 03 Let C be the resulted polynomial; 04 C.Start = free; C.Finish = free – 1; 05 for (a=A.Start; a<= A.Finish; a++) { 06 for (b=B.Start; b<= B.Finish; b++) { 07 Exp = termArray[a].exp + termArray[b].exp; 08 Coef = termArray[a].coef * termArray[b].coef; 09 for (c = C.Finish; c >= C.Start; c--) { 10 if (termArray[c].exp < Exp) 11 Copy termArray[c] to termArray[c+1]; 12 else if (termArray[c].exp >= Exp) 13 break; 14 } 15 if (c != C.Start – 1 and termArray[c].exp == Exp) 16 termArray[c] += Coef; 17 else { 18 termArray[c+1].coef = Coef; 19 termArray[c+1].exp = Exp; 20 C.Finish++; 21 free++; 22 } 24 } 25 } 26 Return C; 27 }

  6. Solution 1-1 • Suppose A and B are two polynomials. Let m and n be the number of nonzero terms in A and B. • Space complexity: • Except the static termArray, the function uses O(1) memory space to accomplish the task. When we consider the space complexity for the class to invoke this function, since it totally consumes at most mxn terms in termArray, it can be said that the space complexity is O(mxn). • Time complexity • In the nested for loop from Line 5 to 20 executes, at most one new term is generated. • Line 9-14 is an inner loop that scans backward to determine the position where the new term should be inserted. • The loop at most executes t steps in each iteration where t is the number of terms in one iteration. Therefore, the overall steps to perform this examination is 1+2+…+mxn = O(m2n2)

  7. Problem 2 • By tracing Program 2.14, explicitly describe how to multiply two sparse matrix for computing the entry located at (1, 2) and (4, 3). • Analyze the performance of Program 2.14.

  8. Solution (Problem 2-1) • To compute (1, 2), suppose • sum = 0, • currRowA = 1, currColB = 2; • currRowBegin = 3; • currRowIndex = 3, currColIndex = 3. • ∵ smArray[currRowIndex].row = 1 = currRowA and, bXpose.smArray[currColIndex].row = 2 = currColB ∴Goto Line 40 • ∵ smArray[currRowIndex].col = 1 = bXpose.smArray[currColIndex].col ∴Execute Line 46-50; therefore, sum = sum + smArray[currRowIndex].value * bXpose.smArray[currColIndex].value = 0 + 11 * 3 = 33. currRowIndex = currRowIndex + 1 = 4, currColIndex = currColIndex + 1 = 4. Redo the loop. • ∵ smArray[currRowIndex].col = 2 < 5 = bXpose.smArray[currColIndex].col ∴Line 43: currRowIndex = currRowIndex + 1 = 4 + 1 = 5 Redo the loop. • Since currRowIndex = 5 and smArray[currRowIndex].row = 2 != currRowA, goto Line 24; store the value of sum at (1, 2). Line 25: assign currRowBegin to currRowIndex for computing the next entry at row 1. Line 28-29: Advance to the next column. The new value of currColIndex is 5. Line 30: Update currColB. The new value of currColB is 3.

  9. Solution (Problem 2-1) • To compute (4, 3), suppose • sum = 0, • currRowA = 4, currColB = 3; • currRowBegin = 6; currRowIndex = 6, currColIndex = 7. • ∵ smArray[currRowIndex].row = 4 = currRowA and, bXpose.smArray[currColIndex].row = 3 = currColB ∴Goto Line 40 • ∵ smArray[currRowIndex].col = 0 = bXpose.smArray[currColIndex].col ∴Execute Line 46-50; therefore, sum = sum + smArray[currRowIndex].value * bXpose.smArray[currColIndex].value = 0 + 91 * 22 = 2002. currRowIndex = currRowIndex + 1 = 7, currColIndex = currColIndex + 1 = 8. Redo the loop. • Since currRowIndex = 7 and smArray[currRowIndex].row = 5 != currRowA, goto Line 24; store the value of sum at (4, 5). Line 25: assign currRowBegin to currRowIndex for computing the next entry at row 1. Line 28-29: Advance to the next column. The new value of currColIndex is 8. Line 30: Update currColB. The new value of currColB is 6. Redo the loop. • ∵ smArray[currRowIndex].col = 0 > -1 = bXpose.smArray[currColIndex].col ∴Line 51: currColIndex = currColIndex + 1 = 6 + 1 = 7 > terms. Program execution will leave the loop. • Line 53-54: advance to the next row. The new value of currRowIndex is 7. Line 55: update currRowBegin. The new value of currRowBegin is 7. Line 56: currRowA = smArray[currRowIndex].row = 5.

  10. Solution (Problem 2-2) • Suppose A is a rAxcA matrix and B is a rBxcB matrix, and there are m and n nonzero entries in A and B. • Space complexity: • Line 4 invokes b.FastTranspose(). The space complexity is O(cB+n). • Since the product of two sparse matrices may not be sparse, at most rAxcB entries are produced. • The space complexity eventually is bounded in O(rAxcB).

  11. Solution (Problem 2-2) • Time complexity: • Line 3 invokes b.FastTranspose(). Its time complexity is O(cB+n). • Line 4-15 resizes smArray in A and B in O(m+n) computing time. • Line 16-57: the while loop totally executes at most rA times, where in each iteration • Over the while loop from Line 20 to 52: • The total increment of currColIndex is n. • Suppose tr is the number of row r of A. The total increment of currRowIndex is tr xcB. • Overall, the inner while loop executes in O(n+ tr xcB). • Line 53-54 advances the current row to next row; therefore, executes in O(tr) time. • The overall time for this loop is O(cB+n) + O(Σr(n+ tr xcB))=O(mxn+mxcB).

  12. n n a b Problem 3 • A general band matrix An,a,b is an n*n matrix where all the nonzero entries lie in a band shown as follows: (1) How many elements are there in the band of An, a, b?

  13. Problem 3 • Assume that the band of is stored sequentially in an array B by diagonals starting with the leftmost diagonal. • For example, A[0, 2]→B[0] A[1, 1]→B[1] A[2, 0]→B[2] A[0, 3]→B[3] A[1, 2]→B[4] A[2, 1]→B[5] A[3, 0]→B[6] A[1, 3]→B[7] A[2, 2]→B[8] …… B: 8 1 9 6 4 3 5 4 2 7 9 6

  14. Problem 3 (2) What is the location A[69, 33] in B for A100, 20, 30? (3) Let A[i, j] be the element of A located in B[299]. What are the values of i and j respectively?

  15. Solution (1) (2) Therefore, A[69, 33] will be in B[2073].

  16. Solution (3) ∵ 81 + 82 + 83 = 246 81 + 82 + 83 + 84 = 330 ∴ B[299] is at the 4-th nonzero diagonal. Since (299 + 1) – 246 = 54, B[299] is at A[54-1, 83-54+1 ] = A[53, 30].

More Related