1 / 17

COP 3530 : Discussion

COP 3530 : Discussion. --Ravi. Topics. HW 2 discussion Space calculation Step-count Asymptotic comparison : Big O. HW 2. Fibonacci Space complexity Step-count : Run-time complexity Time complexity. Space example: rSum (). T Rsum (T a[], int n)

zhen
Download Presentation

COP 3530 : Discussion

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. COP 3530 : Discussion --Ravi

  2. Topics • HW 2 discussion • Space calculation • Step-count • Asymptotic comparison : Big O

  3. HW 2 • Fibonacci • Space complexity • Step-count : Run-time complexity • Time complexity

  4. Space example: rSum() T Rsum(T a[], int n) {// Return sum of numbers a[0:n - 1]. if (n > 0) return Rsum(a, n-1) + a[n-1]; return 0; }

  5. Stack space for rsum() • Parameters • Return value • Depth of recursion

  6. Stack space for rsum() • Parameters • Return value • Depth of recursion 12*(n+1)

  7. Step-count example : Inef() T Sum(T a[], int n) {// Return sum of numbers a[0:n - 1]. T tsum = 0; for (int i = 0; i < n; i++) tsum += a[i]; return tsum; } voidInef(T a[], T b[], int n) {// Compute prefix sums. for (int j = 0; j < n; j++) b[j] = Sum(a, j + 1); }

  8. T Sum(T a[], int n) {// Return sum of numbers a[0:n - 1]. T tsum = 0; stepCount++; // 1 for (int i = 0; i < n; i++) { stepCount++; // n tsum += a[i]; stepCount++; // n } stepCount++; // 1 stepCount++; // 1 return tsum; }

  9. voidInef(T a[], T b[], int n) {// Compute prefix sums. for (int j = 0; j < n; j++) { stepCount++; // n b[j] = Sum(a, j + 1); stepCount += 1+ 2*(j+1)+3; // ?? } stepCount++; // 1 }

  10. Summation • ∑j = 0 to n-1 4 + 2 (j+1) = 4*n + ∑j = 0 to n-1 2 (j+1) = 4*n + 2 n*(n+1)/2 = 4*n + n*n + n = 5n + n2

  11. Asymptotic comparison • Def: t(m,n) is asymptotically bigger than u(m,n) iff (1) Lim n→∞ u(m,n)/t(m,n) = 0 AND Lim m→∞ u(m,n)/t(m,n) ≠ ∞ OR (2) Lim n→∞ u(m,n)/t(m,n) ≠ ∞ AND Lim m→∞ u(m,n)/t(m,n) = 0

  12. Example • 7m2n2 + 2m3n + mn + 5mn2

  13. Example • 7m2n2 + 2m3n + mn + 5mn2 Lim n→∞ mn/7m2n2 = 0 Lim m→∞ mn/7m2n2 ≠ ∞

  14. Problem • Given a sequence of numbers, find the maximum sum of a contiguous subsequence of those numbers. • Find Space and Step-count • Example: 0, 1, -2, 2, -1, 3, 0. Ans: 4 (2, -1, 3)

  15. Max sub subsequence : Naive solution intMaxSubSeq (int *a, int n) { int max = INT_MIN; for (inti=0; i<n; i++) { // n for(int j=i; j<n; j++) { // n2 intcurSum = 0; for(int k=i; k<=j ; k++) { curSum += a[k]; // n3 } if ( max < curSum) max = curSum; } } }

  16. Max sub subsequence : Naive solution intMaxSubSeq (int *a, int n) { int max = INT_MIN; for (inti=0; i<n; i++) { // n for(int j=i; j<n; j++) { // n2 intcurSum = 0; for(int k=i; k<=j ; k++) { curSum += a[k]; // n3 } if ( max < curSum) max = curSum; } } How to make it n2? }

  17. Best solution: O(1) extra space O(n) time

More Related