

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


T (n) = O(f (n)) T (n) = θ(f (n))
Justify your answers. (Assume a and b are unspecified constants greater than 1 and a > b.)
(a) T (n) = n^2 log n + 5n, f (n) = n^3 Solution: T (n) = O(f (n), but T (n) 6 = θ(f (n)). The reason is that the limit of n^2 log n/n^3 is equal to the limit of log n/n, which is 0. (b) T (n) = loga n, f (n) = logb n Solution: T (n) = O(f (n)) and T (n) = θ(f (n)). The reason is that loga n = logb n/logba, so the limit of T (n)/f (n) is 1/logba, which is a constant.
(a) T (n) = (n^3 + 10n^2 ) · (n^3 log n + 20n^4 ) Solution: The first factor (expression in parentheses) is O(n^3 ), and the second is O(n^4 ). Multiplying these gives O(n^7 ). (b) T (n) = n 3 n^ + n^10 + 1500n^3 log n. Solution: As discussed in class, T (n) = O(n 3 n). You can’t eliminate factors that involve n. The solution O(nn) is strictly correct, but not as informative.
// assume arr is an array containing n integers int k = 5; for ( int i=0; i<=n-k; i++ ) { sum = 0; for ( int j=i; j<i+k; j++ ) { sum += arr[j]; } cout << "Sum of elements " << i << " through " << i+k-1 << " is " << sum << "\n"; }
Solution: The inner loop requires k iterations and each iteration requires constant time. But, since k = 5, this is O(1), i.e. constant time. Since the outer runs for n − k iterations and there is constant time per iteration, the overall result is O(n).
− 1 , 6 , − 3 , 2 , − 7 , 6 , 8 , − 3 , 4 , − 2 , 1
Solution: 15 This is the subsequence 6, 8, -3, 4.