Docsity

Assignment with Solution - Data Structures and Algorithms | CSCI 2300, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
CSci 2300 — Data Structures and Algorithms
Week 4, Class 8 — SOLUTIONS
Exercises
1. (6 points) For each pair of functions, T(n) and f(n), determine which of
the following hold:
T(n) = O(f(n)) T(n) = θ(f(n))
Justify your answers. (Assume aand bare unspecified constants greater
than 1 and a > b.)
(a) T(n) = n2log n+ 5n,f(n) = n3
Solution: T(n) = O(f(n), but T(n)6=θ(f(n)). The reason is that
the limit of n2log n/n3is equal to the limit of log n/n, which is 0.
(b) T(n) = logan,f(n) = logbn
Solution: T(n) = O(f(n)) and T(n) = θ(f(n)). The reason is that
logan= logbn/logba, so the limit of T(n)/f (n) is 1/logba, which is
a constant.
2. (6 points) Give the best possible Oestimate for T(n),
(a) T(n) = (n3+ 10n2)·(n3log n+ 20n4)
Solution: The first factor (expression in parentheses) is O(n3), and
the second is O(n4). Multiplying these gives O(n7).
(b) T(n) = n3n+n10 + 1500n3log n.
Solution: As discussed in class, T(n) = O(n3n). You can’t eliminate
factors that involve n. The solution O(nn) is strictly correct, but not
as informative.
3. (5 points) Give the best possible “O” estimate for the computation time
required by the following code as a function of n, the length of the array.
// 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";
}
pf2

Partial preview of the text

Download Assignment with Solution - Data Structures and Algorithms | CSCI 2300 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CSci 2300 — Data Structures and Algorithms

Week 4, Class 8 — SOLUTIONS

Exercises

  1. (6 points) For each pair of functions, T (n) and f (n), determine which of the following hold:

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.

  1. (6 points) Give the best possible O estimate for T (n),

(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.

  1. (5 points) Give the best possible “O” estimate for the computation time required by the following code as a function of n, the length of the array.

// 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. Reading: The rest of Chapter 2. You should understand at the very least, the definition for the Max Subsequence Sum problem. (3 points) Find the maximum subsequence sum of

− 1 , 6 , − 3 , 2 , − 7 , 6 , 8 , − 3 , 4 , − 2 , 1

Solution: 15 This is the subsequence 6, 8, -3, 4.