=========================================================================== CSC 263H Tutorial Outline for Week 2 Winter 2004 =========================================================================== This week's tutorial is a review of asymptotic notation and basic complexity analysis. It's all material that you should know from CSC148H, CSC165H, and CSC236H. Asymptotic notation: 1. Show that (n+1)^5 is O(n^5). 2. Show that 2^{n+1} is O(2^n). 3. Show that 2^{2n} is not O(2^n). 4. Show that n is o(n log n). 5. Show that n^2 is omega(n). 6. Show that O(f+g) = O(max{f,g}). 7. Show that for any k >= 0, 1^k + 2^k + ... + n^k is O(n^{k+1}). 8. Give an example of a positive function f(n) such that f(n) is not O(n) and f(n) is not Omega(n). Complexity analysis: 9. Find the worst-case complexity of the following code fragment: p := 1 for i := 1 to n: for j := 1 to i: p := p * j 10. Find the worst-case complexity of the following code fragment: p := 1 for i := 1 to k^2: for j := 1 to i: p := p * j 11. Find the worst-case complexity of the following code fragment: while s > 1: s := s / 2 // integer division, rounding down 12. Find the worst-case complexity of the following recursive algorithm // Sort L[b..e]: the portion of list L between indices b and e // (inclusive). Precondition: 1 <= b <= e <= length[L]. MergeSort(L, b, e): if b < e: m := (b + e) / 2 // integer division MergeSort(L, b, m) MergeSort(L, m+1, e) Merge(L, b, m, e) // else L[b..e] contains only 1 element and is already sorted Assume that a call to Merge(L, b, m, e) takes time Theta(e-b+1) (i.e., makes Theta(e-b+1) comparisons). 13. Consider computing the value of a polynomial p(x) = a_0 + a_1 x + a_2 x^2 + ... + a_n x^n for a given value of x. (a) Describe a simple Theta(n^2) algorithm for doing this. (b) What is the complexity (number of additions and multiplications) of the following method, known as "Horner's method": p(x) = a_0 + x(a_1 + x(a_2 + ... + x(a_{n-1} + x a_n)...))