170 likes | 247 Views
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)
E N D
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) {// Return sum of numbers a[0:n - 1]. if (n > 0) return Rsum(a, n-1) + a[n-1]; return 0; }
Stack space for rsum() • Parameters • Return value • Depth of recursion
Stack space for rsum() • Parameters • Return value • Depth of recursion 12*(n+1)
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); }
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; }
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 }
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
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
Example • 7m2n2 + 2m3n + mn + 5mn2
Example • 7m2n2 + 2m3n + mn + 5mn2 Lim n→∞ mn/7m2n2 = 0 Lim m→∞ mn/7m2n2 ≠ ∞
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)
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; } } }
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? }
Best solution: O(1) extra space O(n) time