COMPSCI 220 β Tutorial 2 Richard Summary Analysing/ comparing algorithms Estimating running time Single loop (Number of elementary operations) Nested loops π(π) Conditional/ switch statement β¦ Big-O, Big-β¦, Big-Ξ Analysing (π Time complexity : : π(π) asymptotically. the input data size) Big-Ξ of running time Big-O vs. Actual Running Time Input data itself related: Average and Worse case Recurrence (for algorithms by Divide-and-Conquer ) Time complexity: When the Input Itself Matters ο½ Average case ο½ ο½ ο½ ο½ Specify the probability of different input (assumption) Difficult to compute Assumptions might not be realistic Worst case ο½ ο½ Upper bound Might only be the case for a few very specific input Algorithms based on Divide-and-Conquer ο½ Recurrence relation ο½ ο½ ο½ Implicit formula Explicit formula, closed form Two approaches ο½ ο½ Guess and prove Telescoping Exercise 1 Prove that T(n) = 3n3 + 2n4 is not O(n3) Suppose it is, then there must exist a c and n0 such that 3n3+2n4 <= cn3 for all n>n0. Rearrange the inequality we get c >= (3n3+2n4)/n3=3+2n. Such constant c does not exist. Exercise 2 ο½ Determine whether each statement is TRUE or FALSE Statement Rule of products Transitivity Limit rule TRUE π π + π = π(max{π, π}) π π. π = π π . π(π) π π + π = π π + π(π) Rule of sums Scaling TRUE or FALSE ππ π > 0, π ππ O(kf) For all π β 0, π is π ππ If β = π π , π = π π , π‘βππ β = π(π) If lim π(π) = 10 , then πββ π(π) π=π π If TRUE TRUE π(π) , then lim =0 πββ π(π) π=β¦ π If π = π π and π = π π^2 then π = π π Counter example for the last one: TRUE π = π π , and π = π π^2 Then π = π π^2 π = π, πππ π = π Exercise 3 Assume the running time of an algorithm is described by recurrence relation T(n) = 3T(n/3) + 1;T(1) = 1 Using telescoping find a closed form for T(n) and what is the time complexity of the algorithm ? n = 3m T(3m) = 3T(3m-1) + 1 T(3m-1) = 3T(3m-2) + 1 T(3m) = 9T(3m-2) + 1 + 3 T(3m) = 1 + 31 + 32 + β¦ + 3m T(3m) = (3m+1 β 1)/2 T(3m) = (3n β 1)/2 Therefore the solution is Ξ(n) Exercise 4 What is the time complexity of the recurrence T(n) = T(n/2) + n, T(1) = 0? n = 2m T(2m) = T(2m-1) + 2m T(2m-1) = T(2m-2) + 2m-1 T(2m) = T(2m-2) + 2m-1 + 2m T(2m) = T(20) + 21 + 22 + β¦ + 2m T(2m) = 2m+1 β 2 T(2m) = 2(n β 1) Therefore the solution is Ξ(n) Exercise 5 What is the time complexity of the recurrence T(n) = 2T(n/2) + n, T(1) = 1? n = 2m T(2m) = 2T(2m-1) + 2m T(2m-1) = 2T(2m-2) + 2m-1 T(2m) = 4T(2m-2) + 2m + 2m T(2m) = 2m + 2m + β¦ + 2m T(2m) = (m+1)(2m) T(3m) = n(1+lgn) Therefore the solution is Ξ(nlgn)
© Copyright 2024 Paperzz